-
Notifications
You must be signed in to change notification settings - Fork 7
/
ODT_update.ps1
118 lines (92 loc) · 4.42 KB
/
ODT_update.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
$ScriptDirectory = $PSScriptRoot
Set-Location -Path $ScriptDirectory
[Environment]::CurrentDirectory = Get-Location
if (-Not (Test-Path "downloads\ODT_updates" -PathType Container)) {
New-Item -ItemType Directory -Path "downloads\ODT_updates"
}
if (-Not (Test-Path "backups" -PathType Container)) {
New-Item -ItemType Directory -Path "backups"
}
Add-Type -AssemblyName System.Windows.Forms
# GitHub API URL
$apiUrl = "https://api.github.com/repos/Schmurtzm/Onion-Desktop-Tools/releases/latest"
# Get the information of the latest release from GitHub API
$releaseInfo = Invoke-RestMethod -Uri $apiUrl
# Get the version number from the tag_name in the API response
if ($releaseInfo -and $releaseInfo.tag_name) {
$latestVersion = $releaseInfo.tag_name
}
else {
Write-Host "Failed to retrieve the latest version from GitHub API. No action required."
}
# Read the Menu.ps1 file and get the version number from the first line
$menuFilePath = "Menu.ps1"
$menuContent = Get-Content -Path $menuFilePath
if ($menuContent -and $menuContent[0]) {
$currentVersion = $menuContent[0] -replace "# Onion-Desktop-Tools-"
}
else {
Write-Host "Failed to retrieve the current version from Menu.ps1 file. No action required."
}
# Compare the versions
if ($latestVersion -gt $currentVersion) {
$messageBoxText = "A newer version of Onion Desktop Tools is available:`nCurrent version: $currentVersion.`nAvailable version: $latestVersion.`n`nDo you want to download and install the new version?"
$form = New-Object System.Windows.Forms.Form
$form.Text = $messageBoxCaption
$form.Width = 420
$form.Height = 200
$Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$Form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(20, 20)
$label.Size = New-Object System.Drawing.Size(350, 60)
$label.Text = $messageBoxText
$form.Controls.Add($label)
$buttonYes = New-Object System.Windows.Forms.Button
$buttonYes.Location = New-Object System.Drawing.Point(40, 100)
$buttonYes.Size = New-Object System.Drawing.Size(100, 30)
$buttonYes.Text = "Yes"
$buttonYes.DialogResult = [System.Windows.Forms.DialogResult]::Yes
$form.Controls.Add($buttonYes)
$buttonNo = New-Object System.Windows.Forms.Button
$buttonNo.Location = New-Object System.Drawing.Point(160, 100)
$buttonNo.Size = New-Object System.Drawing.Size(100, 30)
$buttonNo.Text = "No"
$buttonNo.DialogResult = [System.Windows.Forms.DialogResult]::No
$form.Controls.Add($buttonNo)
$buttonUrl = New-Object System.Windows.Forms.Button
$buttonUrl.Location = New-Object System.Drawing.Point(280, 100)
$buttonUrl.Size = New-Object System.Drawing.Size(100, 30)
$buttonUrl.Text = "Show URL"
$buttonUrl.Add_Click({ Start-Process $releaseInfo.html_url })
$form.Controls.Add($buttonUrl)
$form.ShowDialog() | Out-Null
if ($form.DialogResult -eq [System.Windows.Forms.DialogResult]::Yes) {
Write-Host "Downloading..."
# Download the ZIP file
$zipUrl = $releaseInfo.zipball_url
$zipFilePath = "downloads\ODT_updates\Onion-Desktop-Tools.zip"
Invoke-WebRequest -Uri $zipUrl -OutFile $zipFilePath
# Extract the contents of the ZIP file using 7-Zip
$extractDir = "downloads\ODT_updates\tempUpdateFolder"
if (-Not (Test-Path "downloads\ODT_updates\tempUpdateFolder" -PathType Container)) {
New-Item -ItemType Directory -Path "downloads\ODT_updates\tempUpdateFolder"
}
$7zPath = "tools\7z.exe"
$output = & $7zPath x -y $zipFilePath "-o$extractDir"
# Find the subfolder with a GUID inside the extracted contents
$guidSubfolder = Get-ChildItem -Path $extractDir | Where-Object { $_.PSIsContainer -and $_.Name -match 'schmurtzm-Onion-Desktop-Tools-.+' }
# Move the files from the GUID subfolder to the current folder
Move-Item -Path "$($guidSubfolder.FullName)\*" -Destination . -Force
# Clean up the temporary extraction folder
Remove-Item -Path $extractDir -Recurse -Force
Write-Host "Download and extraction completed."
}
else {
Write-Host "Operation canceled. No download performed."
}
}
else {
Write-Host "The version $latestVersion is the same or lower than $currentVersion. No action required."
}
. "$PSScriptRoot\Menu.ps1"