-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh-cli-updater.ps1
216 lines (196 loc) · 6.95 KB
/
gh-cli-updater.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<#
.SYNOPSIS
CLI tool for managing scrcpy: installation, update, and removal.
.DESCRIPTION
The script checks the version of scrcpy, downloads the latest version from GitHub, and installs it to the user-specified location.
#>
$global:currentScriptVersion = "v2025.1.31"
# --- Functions ---
function Show-Menu {
do {
$choice = Read-Host "Please enter your choice (0-6)"
} while ($choice -notmatch '^([0-6])$')
return $choice
}
$global:currentVersion = ""
function Get-InstalledGhVersion {
if (Get-Command gh -ErrorAction SilentlyContinue) {
$versionOutput = gh version
$global:currentVersion = $versionOutput.Split("`n")[0] -replace "gh version ", "" -replace "\s+\(.*\)", ""
Write-Output "Current installed GitHub CLI version: $global:currentVersion"
}
else {
$global:currentVersion = $null
}
}
function SignInToGh {
if (Get-Command gh -ErrorAction SilentlyContinue) {
Start-Process -FilePath "gh" -ArgumentList "auth login" -NoNewWindow -Wait -PassThru | Select-Object -ExpandProperty StandardOutput
}
else {
Write-Error "GitHub CLI is not installed. Please install it first."
}
}
function SignOutFromGh {
if (Get-Command gh -ErrorAction SilentlyContinue) {
Start-Process -FilePath "gh" -ArgumentList "auth logout" -NoNewWindow -Wait -PassThru | Select-Object -ExpandProperty StandardOutput
}
else {
Write-Error "GitHub CLI is not installed. Please install it first."
}
}
function Install-Or-Update-GhCli {
param (
[switch]$install,
[switch]$update
)
$repo = "https://api.github.com/repos/cli/cli/releases/latest"
$ghCliTargetPath = "C:\Program Files\GitHub CLI"
# Determine platform
if ($IsWindows) {
if ($env:PROCESSOR_ARCHITECTURE -eq "AMD64") {
$platform = "windows_amd64"
}
elseif ($env:PROCESSOR_ARCHITECTURE -eq "x86") {
$platform = "windows_386"
}
else {
Write-Error "Unsupported platform: $env:PROCESSOR_ARCHITECTURE. Exiting..."
return
}
}
elseif ($IsMacOS) {
$platform = "macOS"
$ghCliTargetPath = "/usr/local/bin/gh"
}
elseif ($IsLinux) {
$platform = "linux"
$ghCliTargetPath = "/usr/local/bin/gh"
}
else {
Write-Error "Unsupported operating system. Exiting..."
return
}
# Fetch latest release from GitHub
Write-Output "Fetching latest release from GitHub..."
$response = Invoke-RestMethod -Uri $repo -Headers @{ 'User-Agent' = 'PowerShell' } -Verbose
$latestVersion = $response.tag_name -replace "v", ""
if ($update) {
# Check if GitHub CLI is already installed and update if necessary
Get-InstalledGhVersion
if ($global:currentVersion -eq $latestVersion) {
Write-Output "You already have the latest GitHub CLI version ($latestVersion). No update needed."
return
}
Write-Output "New version available: $latestVersion (Installed: $($global:currentVersion)). Proceeding with update..."
}
elseif ($install) {
# Check if GitHub CLI is already installed
Get-InstalledGhVersion
if ($global:currentVersion) {
Write-Output "GitHub CLI is already installed (Version: $($global:currentVersion)). Use the update option to update to the latest version."
return
}
Write-Output "Proceeding with installation of GitHub CLI version $latestVersion..."
}
else {
Write-Error "Please specify either -install or -update. Exiting..."
return
}
# Download the latest version
$downloadAsset = $response.assets | Where-Object { $_.name -like "*$platform*.msi" } | Select-Object -First 1
if (-not $downloadAsset) {
Write-Error "No compatible GitHub CLI release found for $platform. Exiting..."
return
}
$downloadUrl = $downloadAsset.browser_download_url
$msiFileName = $downloadAsset.name
# Download and install
$tempPath = Join-Path (Get-Location) "temp_update"
New-Item -ItemType Directory -Force -Path $tempPath | Out-Null
$msiFilePath = Join-Path $tempPath $msiFileName
Write-Output "Downloading GitHub CLI from $downloadUrl..."
Invoke-WebRequest -Uri $downloadUrl -OutFile $msiFilePath -Headers @{ 'User-Agent' = 'PowerShell' } -Verbose
Write-Output "Installing GitHub CLI..."
Start-Process msiexec.exe -ArgumentList "/i `"$msiFilePath`" /quiet /norestart" -Wait -NoNewWindow
# Clean up
Write-Output "Cleaning up temporary files..."
Remove-Item -Path $tempPath -Recurse -Force -Verbose
Write-Output "GitHub CLI installed/updated successfully to $ghCliTargetPath. Version: $latestVersion"
}
function Uninstall-GhCli {
Write-Output "Uninstalling GitHub CLI..."
$ghCliPath = "C:\Program Files\GitHub CLI\gh.exe"
if (Test-Path $ghCliPath) {
$msiExecPath = "C:\Windows\System32\msiexec.exe"
$uninstallString = (Get-WmiObject -Query "SELECT * FROM Win32_Product WHERE Name = 'GitHub CLI'").IdentifyingNumber
if ($uninstallString) {
Start-Process $msiExecPath -ArgumentList "/x $uninstallString /quiet /norestart" -Wait -NoNewWindow
Write-Output "GitHub CLI uninstalled successfully."
}
else {
Write-Warning "GitHub CLI uninstall information not found."
}
}
else {
Write-Warning "GitHub CLI is not installed."
}
}
# --- Main Program ---
$global:exit = $false
# Käyttövalikko
Do {
Clear-Host
Write-Output "==============================="
Write-Output " GitHub CLI Tool Menu "
Write-Output "==============================="
Write-Output "1. 📥 Asenna GitHub CLI"
Write-Output "2. 🔄 Päivitä GitHub CLI"
Write-Output "3. ❌ Poista GitHub CLI"
Write-Output "4. 🚀 Tarkista asennettu versio"
Write-Output "5. 🔑 Kirjaudu GitHubiin"
Write-Output "6. 🔒 Kirjaudu ulos GitHubista"
Write-Output "0. 🚪 Poistu"
Write-Output "==============================="
Write-Output ""
$choice = Read-Host "Syötä valinta"
switch ($choice) {
"1" {
Clear-Host
Install-Or-Update-GhCli -install
}
"2" {
Clear-Host
Install-Or-Update-GhCli -update
}
"3" {
Clear-Host
Uninstall-GhCli
}
"4" {
Clear-Host
Get-InstalledGhVersion
}
"5" {
Clear-Host
SignInToGh
}
"6" {
Clear-Host
SignOutFromGh
}
"0" {
Clear-Host
Write-Output "Poistutaan. Heippa!"
$exit = $true
}
default {
Clear-Host
Write-Warning "Virheellinen valinta. Valitse vaihtoehto (0-5)."
}
}
if (-not $exit) {
Write-Output "`nPaina Enter palataksesi valikkoon..."
Read-Host
}
} While (-not $exit)