-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
250 lines (197 loc) · 6.31 KB
/
install.ps1
File metadata and controls
250 lines (197 loc) · 6.31 KB
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
# Apitally CLI installer
# https://github.com/apitally/cli
#
# Usage:
# irm https://apitally.io/cli/install.ps1 | iex
#
# Environment variables:
# APITALLY_VERSION - Install a specific version (e.g. "v0.1.0") instead of latest
# APITALLY_INSTALL_DIR - Override the install directory (default: ~/.local/bin)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$REPO = "apitally/cli"
$BINARY = "apitally"
$TMP_DIR = $null
# --- Helper functions --------------------------------------------------------
function Say {
param([string]$Message)
Write-Host $Message
}
function Err {
param([string]$Message)
throw "error: $Message"
}
function Normalize-Path {
param([string]$PathValue)
if ([string]::IsNullOrWhiteSpace($PathValue)) {
return ""
}
return $PathValue.Trim().TrimEnd("\")
}
function Path-Contains {
param(
[string]$PathValue,
[string]$Directory
)
$needle = Normalize-Path $Directory
foreach ($entry in ($PathValue -split ";")) {
if ((Normalize-Path $entry) -ieq $needle) {
return $true
}
}
return $false
}
function New-WebClient {
$client = New-Object Net.WebClient
$client.Headers["User-Agent"] = "apitally-installer"
return $client
}
# Download a URL to a file
function Download-File {
param(
[string]$Url,
[string]$Destination
)
$client = New-WebClient
try {
$client.DownloadFile($Url, $Destination)
} finally {
$client.Dispose()
}
}
# Fetch a URL and parse it as JSON
function Fetch-Json {
param([string]$Url)
$client = New-WebClient
try {
return $client.DownloadString($Url) | ConvertFrom-Json
} finally {
$client.Dispose()
}
}
function Get-Arch {
try {
$assembly = [System.Reflection.Assembly]::LoadWithPartialName("System.Runtime.InteropServices.RuntimeInformation")
if ($assembly) {
$runtimeInformationType = $assembly.GetType("System.Runtime.InteropServices.RuntimeInformation")
$osArchitectureProperty = $runtimeInformationType.GetProperty("OSArchitecture")
switch ($osArchitectureProperty.GetValue($null).ToString()) {
"X64" { return "x86_64" }
"Arm64" { return "aarch64" }
"X86" { Err "32-bit Windows is not supported" }
}
}
} catch {
}
if ([System.Environment]::Is64BitOperatingSystem) {
return "x86_64"
}
Err "unsupported architecture"
}
function Add-Path {
param([string]$Directory)
$registryPath = "registry::HKEY_CURRENT_USER\Environment"
$currentPath = (Get-Item -LiteralPath $registryPath).GetValue("Path", "", "DoNotExpandEnvironmentNames")
if (Path-Contains $currentPath $Directory) {
return $false
}
$entries = @()
foreach ($entry in ($currentPath -split ";")) {
if (-not [string]::IsNullOrWhiteSpace($entry)) {
$entries += $entry
}
}
$newPath = (@($Directory) + $entries) -join ";"
Set-ItemProperty -LiteralPath $registryPath -Name Path -Value $newPath -Type ExpandString
# Trigger a lightweight environment refresh for future shells.
$dummyName = "apitally-installer-" + [guid]::NewGuid().ToString()
[Environment]::SetEnvironmentVariable($dummyName, "1", "User")
[Environment]::SetEnvironmentVariable($dummyName, $null, "User")
return $true
}
function Add-Session-Path {
param([string]$Directory)
if (Path-Contains $env:Path $Directory) {
return $false
}
if ([string]::IsNullOrEmpty($env:Path)) {
$env:Path = $Directory
} else {
$env:Path = "$Directory;$env:Path"
}
return $true
}
function New-Temp-Dir {
$path = Join-Path ([System.IO.Path]::GetTempPath()) ([guid]::NewGuid().ToString())
New-Item -ItemType Directory -Path $path | Out-Null
return $path
}
# --- Main --------------------------------------------------------------------
function Main {
if ($PSVersionTable.PSVersion.Major -lt 5) {
Err "PowerShell 5 or later is required to install $BINARY"
}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$arch = Get-Arch
$target = "$arch-pc-windows-msvc"
$archive = "$BINARY-$target.zip"
if ($env:APITALLY_VERSION) {
$version = $env:APITALLY_VERSION
} else {
try {
$release = Fetch-Json "https://api.github.com/repos/$REPO/releases/latest"
} catch {
Err "could not fetch latest release from GitHub (check your internet connection)"
}
if (-not $release.tag_name) {
Err "could not determine latest version"
}
$version = $release.tag_name
}
if ($env:APITALLY_INSTALL_DIR) {
$installDir = $env:APITALLY_INSTALL_DIR
} else {
$installDir = Join-Path $HOME ".local\bin"
}
Say "downloading $BINARY $version..."
$script:TMP_DIR = New-Temp-Dir
$archivePath = Join-Path $TMP_DIR $archive
$binaryPath = Join-Path $TMP_DIR "$BINARY.exe"
$installPath = Join-Path $installDir "$BINARY.exe"
$url = "https://github.com/$REPO/releases/download/$version/$archive"
try {
Download-File $url $archivePath
} catch {
Err "failed to download $url"
}
Expand-Archive -LiteralPath $archivePath -DestinationPath $TMP_DIR -Force
if (-not (Test-Path -LiteralPath $binaryPath)) {
Err "downloaded archive did not contain $BINARY.exe"
}
New-Item -ItemType Directory -Path $installDir -Force | Out-Null
Copy-Item -LiteralPath $binaryPath -Destination $installPath -Force
Say "installed to $installPath"
$pathAdded = $false
try {
$pathAdded = Add-Path $installDir
} catch {
Say "warning: could not add $installDir to PATH automatically"
}
if ($pathAdded) {
$null = Add-Session-Path $installDir
Say "added $installDir to PATH"
}
if (-not (Path-Contains $env:Path $installDir)) {
Say "restart PowerShell or run: `$env:Path = `"$installDir;`$env:Path`""
}
}
try {
Main
} catch {
Write-Host $_.Exception.Message -ForegroundColor Red
exit 1
} finally {
if ($TMP_DIR -and (Test-Path -LiteralPath $TMP_DIR)) {
Remove-Item -LiteralPath $TMP_DIR -Recurse -Force -ErrorAction SilentlyContinue
}
}