-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.ps1
306 lines (266 loc) · 8.76 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env pwsh
<#
.SYNOPSIS
QZ Tray installer, for PowerShell
.DESCRIPTION
This script was downloaded from https://github.com/qzind/qz.sh and is part of the QZ Tray product
Contribute to this script by visting the above website and opening an issue or pull request
.PARAMETER param1
Specifies QZ Tray version to install
Can be "stable", "beta" or "v2.2.1"
.LINK
https://github.com/qzind/qz.sh
#>
param(
[String] $param1
)
$OWNER="qzind"
$REPO="tray"
$URL="https://api.github.com/repos/${OWNER}/${REPO}/releases?per_page=100"
$SCRIPT_NAME="pwsh.sh"
$RELEASE="auto" # e.g. "stable", "unstable"
$TAG="auto" # e.g. "2.2.1", "v2.1.6"
if($param1) {
Write-Host "Picked up argument: " -NoNewline
Write-Host "$param1" -ForegroundColor Blue
switch -Regex ($param1) {
".*help" {
$SCRIPT="irm $SCRIPT_NAME | iex"
# Determine if running from a pipe or not
if($MyInvocation.MyCommand.Definition -contains ".ps1") {
$SCRIPT=Split-Path $MyInvocation.MyCommand.Definition -leaf
}
Write-Host "`nUsage:`n $SCRIPT [`"" -NoNewline
Write-Host "stable" -ForegroundColor Green -NoNewline
Write-Host "|`"" -NoNewline
Write-Host "beta" -ForegroundColor Yellow -NoNewline
Write-Host "`"|<" -NoNewline
Write-Host "version" -ForegroundColor Blue -NoNewline
Write-Host ">|`"" -NoNewline
Write-host "help" -ForegroundColor Magenta -NoNewline
Write-Host "`"]"
Write-Host " stable " -ForegroundColor Green -NoNewline
Write-Host " Downloads and installs the latest stable release"
Write-Host " beta " -ForegroundColor Yellow -NoNewline
Write-Host " Downloads and installs the latest beta release"
Write-Host " version " -ForegroundColor Blue -NoNewline
Write-Host " Downloads and installs the exact version specified, e.g. `"2.2.1`""
Write-Host " help " -ForegroundColor Magenta -NoNewline
Write-Host " Displays this help and exits"
Write-Host "`n The default behavior is to download and install the " -NoNewline
Write-Host "stable" -ForegroundColor Green -NoNewline
Write-Host " version`n"
exit 0
}
"stable" {
$RELEASE="stable"
}
"unstable" {
$RELEASE="beta"
}
"beta" {
$RELEASE="beta"
}
}
# If a parameter was provided but we don't recognize it, treat it as a tag
if("$RELEASE" -eq "auto" ) {
$TAG="$param1"
# Append "v" to version if missing (e.g. 2.2.1 vs v2.2.1)
if($TAG.Substring(0, 1) -ne "v") {
$TAG="v${TAG}"
}
}
}
# Determine architecture
# valid values: "amd64", "arm64", "riscv"
# Windows
if($Env:OS -and "$Env:OS".Substring(0, 3) -like "Win*") {
if("${Env:ProgramFiles(Arm)}" -ne "") {
# PowerShell on Windows ARM64 is very common to be running under emulation
# trust "ProgramFiles(Arm)" first
$ARCH="arm64"
} else {
switch("$Env:PROCESSOR_ARCHITECTURE") {
"ARM64" {
$ARCH="arm64"
break
}
"x86" {
Write-Host "WARNING: 32-bit platforms are unsupported"
}
default {
$ARCH="amd64"
}
}
}
}
# Unix/Linux
else {
$ARCH=(uname -m)
switch -Regex ($ARCH) {
".*arm64.*" {
$ARCH="arm64"
break
}
".*aarch64.*" {
$ARCH="arm64"
break
}
".*riscv.*" {
$ARCH="riscv"
break
}
default {
$ARCH="amd64"
}
}
}
# Determine file extension
# valid values: ".run", ".pkg", ".exe"
$EXTENSION=".exe"
if($Env:OS -and "$Env:OS".Substring(0, 3) -like "Win*") {
# Do nothing
} else {
if($IsMacOS) {
$EXTENSION=".pkg"
} else {
$EXTENSION=".run"
}
}
if("$RELEASE" -eq "auto" ) {
$RELEASE="stable"
}
Write-Host "Parsing " -NoNewline
Write-Host "$URL" -ForegroundColor Blue -NoNewline
Write-Host "... "
$jsonData = Invoke-RestMethod -Uri "$URL"
# Gather stable and beta tagged releases by loop over JSON returned from GitHub API
if("$TAG" -eq "auto") {
$STABLE_TAGS=@()
$BETA_TAGS=@()
$tag_name=""
foreach($item in $jsonData) {
$BETA_TAGS += $item.tag_name
if(-Not $item.prerelease) {
$STABLE_TAGS += $item.tag_name
}
}
# Sort the results
$LATEST_STABLE = $STABLE_TAGS | Sort-Object -Descending | Select-Object -First 1
$LATEST_BETA = $BETA_TAGS | Sort-Object -Descending | Select-Object -First 1
switch($RELEASE) {
"stable" {
$TAG="$LATEST_STABLE"
break
}
"beta" {
$TAG="$LATEST_BETA"
break
}
}
if("$TAG" -eq "") {
Write-Host "Unable to locate a tag for this release" -ForegroundColor Red
exit 2
}
Write-Host "Latest " -NoNewline
Write-Host "$RELEASE" -ForegroundColor Green -NoNewline
Write-Host " version found: " -NoNewline
Write-Host "$TAG" -ForegroundColor Blue
}
# Get URL for latest release
Write-Host "Searching " -NoNewline
Write-Host "${EXTENSION}" -ForegroundColor Blue -NoNewline
Write-Host " downloads for " -NoNewline
Write-Host "${TAG}" -ForegroundColor Blue -NoNewline
Write-Host " matching " -NoNewline
Write-Host "${ARCH}" -ForegroundColor Blue -NoNewline
Write-Host "..."
$OS_URLS=@()
foreach($item in $jsonData) {
if($item.tag_name -eq $TAG) {
foreach($asset in $item.assets) {
if($asset.browser_download_url.EndsWith($EXTENSION)) {
$OS_URLS += $asset.browser_download_url
}
}
}
}
# Gather all URLs that match current architecture
$AMD64_URLS=@()
$ARM64_URLS=@()
$RISCV_URLS=@()
foreach($url in $OS_URLS) {
switch -Regex ($url) {
".*arm64.*" {
$ARM64_URLS += $url
break
}
".*riscv.*" {
$RISCV_URLS += $url
break
}
default {
$AMD64_URLS += $url
}
}
}
# Echo the proper download URL
$DOWNLOAD_URL=""
switch -Regex ($arch) {
".*arm64.*" {
$DOWNLOAD_URL = $ARM64_URLS | Select-Object -First 1
break;
}
".*riscv.*" {
$DOWNLOAD_URL = $RISCV_URLS | Select-Object -First 1
break;
}
default {
$DOWNLOAD_URL = $AMD64_URLS | Select-Object -First 1
}
}
if ("$DOWNLOAD_URL" -eq "") {
Write-Host "Unable to locate a download for this platform" -ForegroundColor Red
exit 2
}
Write-Host "Downloading " -NoNewline
Write-Host "${DOWNLOAD_URL}" -ForegroundColor Blue -NoNewline
Write-Host "..."
if($env:TEMP) {
# Windows
$TEMP_FILE = "$env:TEMP\${REPO}-${TAG}${EXTENSION}"
} else {
$TEMP_FILE="/tmp/${REPO}-${TAG}${EXTENSION}"
}
# Remove old copy if needed
if (Test-Path -Path "$TEMP_FILE") {
Remove-Item -fo "$TEMP_FILE"
}
# Download the file
# Suppress output per: https://stackoverflow.com/q/28682642/3196753, https://stackoverflow.com/q/75168064/3196753
$ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest "$DOWNLOAD_URL" -OutFile "$TEMP_FILE"
# Install using unattended techniques: https://github.com/qzind/tray/wiki/deployment
Write-Host "Download successful, beginning the install..."
switch($EXTENSION) {
".pkg" {
sudo installer -pkg "$TEMP_FILE" -target /
break
}
".run" {
if (Get-Command "sudo" -errorAction SilentlyContinue) {
sudo bash "$TEMP_FILE" --nox11 -- -y
} else {
# fallback to "su -c"
su root -c "bash '$TEMP_FILE' --nox11 -- -y"
}
break
}
default {
# assume .exe
Start-Process "$TEMP_FILE" -ArgumentList "/S" -Verb RunAs -Wait
}
}
# Clean up
Remove-Item -fo "$TEMP_FILE"
exit 0