-
Notifications
You must be signed in to change notification settings - Fork 26
/
install.ps1
89 lines (73 loc) · 2.35 KB
/
install.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
$command = "window-switcher"
$repo = "sigoden/$command"
$url = "https://github.com/$repo"
if ($env:OS -like "Windows*") {
$os = "windows"
} else {
Write-Error "Unsupported operating system. Only Windows is currently supported."
exit 1
}
if ($env:PROCESSOR_ARCHITECTURE -eq "x86") {
$arch = "32"
} elseif ($env:PROCESSOR_ARCHITECTURE -eq "AMD64") {
$arch = "64"
} elseif ($env:PROCESSOR_ARCHITECTURE -eq "ARM64") {
$arch = "arm64"
} else {
Write-Error "Unsupported architecture."
exit 1
}
$target = "$os-$arch"
$tag = Invoke-RestMethod -Uri "https://api.github.com/repos/$repo/releases/latest" | select -Expand tag_name
$dest = "C:\Users\$env:USERNAME\AppData\Local\Programs\$command"
$archive = "$url/releases/download/$tag/$command-$tag-$target.zip"
$outfile = "$dest\$command.exe"
Write-Host "Repository: $url"
Write-Host "Command: $command"
Write-Host "Tag: $tag"
Write-Host "Target: $target"
Write-Host "Archive: $archive"
Write-Host "Destination: $dest"
$temp = New-TemporaryFile
try {
Invoke-WebRequest -Uri $archive -OutFile $temp -UseBasicParsing -ErrorAction Stop | Out-Null
} catch {
Write-Error "Download failed. Please check your internet connection and try again."
exit 1
}
Move-Item $temp "$temp.zip"
Expand-Archive "$temp.zip" -DestinationPath $temp
if (-not (Test-Path $dest)) {
New-Item -ItemType Directory -Path $dest | Out-Null
}
if (Test-Path $outfile) {
$retry = $true
while ($retry) {
try {
Remove-Item -Force $outfile -ErrorAction Stop
$retry = $false
} catch {
$id = (Get-Process | Where-Object { $_.Path -eq $outfile }).Id
if ($id) {
Write-Error "$command.exe is currently running. Please close it before continuing."
Pause
} else {
Write-Error "Failed to remove old $command.exe. Please try again."
}
}
}
}
Move-Item "$temp\$command.exe" $outfile
Remove-Item -Force "$temp.zip"
Remove-Item -Force -Recurse "$temp"
Write-Host ""
Write-Host "Installation successful!"
if ($Host.UI.RawUI.KeyAvailable) {
$ans = Read-Host -Prompt "Run window-switcher.exe? (y/n)"
if ($ans -eq "y") {
& $outfile
Write-Host "Run window-switcher.exe successful!"
exit
}
}
Write-Host "Please double-click '$outfile' to run it."