Repository navigation
Expand file tree
/
Copy pathinstall-release.ps1
More file actions
229 lines (207 loc) · 8.81 KB
/
Copy pathinstall-release.ps1
File metadata and controls
229 lines (207 loc) · 8.81 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
[CmdletBinding()]
param(
[string]$Version,
[string]$BinDir,
[string]$Repository,
[string]$GitHubUrl,
[string]$GitHubApiUrl
)
$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"
Set-StrictMode -Version 2.0
function Get-Setting {
param(
[string]$Value,
[string]$EnvironmentName,
[string]$DefaultValue
)
if (-not [string]::IsNullOrWhiteSpace($Value)) {
return $Value
}
$environmentValue = [Environment]::GetEnvironmentVariable($EnvironmentName)
if (-not [string]::IsNullOrWhiteSpace($environmentValue)) {
return $environmentValue
}
return $DefaultValue
}
function Fail {
param([string]$Message)
throw "codexometer installer: $Message"
}
function Resolve-ReleaseArchitecture {
param(
[string]$OSArchitecture,
[string]$NativeArchitecture,
[string]$ProcessArchitecture
)
# Prefer the OS, not the shell architecture. WOW64 exposes the native
# architecture separately when a 32-bit shell runs on 64-bit Windows.
$architecture = $OSArchitecture
if ([string]::IsNullOrWhiteSpace($architecture)) {
$architecture = $NativeArchitecture
}
if ([string]::IsNullOrWhiteSpace($architecture)) {
$architecture = $ProcessArchitecture
}
switch ($architecture) {
{ $_ -in @("X64", "AMD64") } { return "amd64" }
"ARM64" { return "arm64" }
default { Fail "unsupported or undetermined architecture: $architecture" }
}
}
$Version = Get-Setting $Version "CODEXOMETER_VERSION" "latest"
$Repository = Get-Setting $Repository "CODEXOMETER_REPOSITORY" "merefield/codexometer"
$GitHubUrl = (Get-Setting $GitHubUrl "CODEXOMETER_GITHUB_URL" "https://github.com").TrimEnd("/")
$GitHubApiUrl = (Get-Setting $GitHubApiUrl "CODEXOMETER_GITHUB_API_URL" "https://api.github.com").TrimEnd("/")
$semanticTagPattern = '^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(?:-((?:0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*)(?:\.(?:0|[1-9][0-9]*|[0-9]*[A-Za-z-][0-9A-Za-z-]*))*))?(?:\+([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?$'
if ([string]::IsNullOrWhiteSpace($BinDir)) {
$BinDir = [Environment]::GetEnvironmentVariable("CODEXOMETER_BIN_DIR")
}
if ([string]::IsNullOrWhiteSpace($BinDir)) {
if (-not [string]::IsNullOrWhiteSpace($env:LOCALAPPDATA)) {
$BinDir = Join-Path $env:LOCALAPPDATA "Programs\codexometer\bin"
} else {
$BinDir = Join-Path $HOME ".local\bin"
}
}
if ($Repository -notmatch '^[A-Za-z0-9._-]+/[A-Za-z0-9._-]+$') {
Fail "CODEXOMETER_REPOSITORY must have the form owner/repository"
}
if ([string]::IsNullOrWhiteSpace($BinDir)) {
Fail "CODEXOMETER_BIN_DIR must not be empty"
}
if ((Test-Path -LiteralPath $BinDir) -and -not (Test-Path -LiteralPath $BinDir -PathType Container)) {
Fail "CODEXOMETER_BIN_DIR exists and is not a directory: $BinDir"
}
if ($Version -ne "latest" -and $Version -cnotmatch $semanticTagPattern) {
Fail "invalid semantic release tag: $Version"
}
$osArchitecture = ""
try {
$osArchitecture = [Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString()
} catch {
# Some Windows PowerShell 5.1/.NET Framework environments do not expose
# this property (or its type). Use Windows' architecture variables instead.
}
$releaseArch = Resolve-ReleaseArchitecture $osArchitecture `
([Environment]::GetEnvironmentVariable("PROCESSOR_ARCHITEW6432")) `
([Environment]::GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"))
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
$headers = @{ "User-Agent" = "codexometer-release-installer" }
$temporaryDirectory = Join-Path ([IO.Path]::GetTempPath()) ("codexometer-release-install-" + [Guid]::NewGuid().ToString("N"))
New-Item -ItemType Directory -Path $temporaryDirectory | Out-Null
try {
$releaseTag = $Version
if ($releaseTag -eq "latest") {
Write-Host "Resolving the latest Codexometer release..."
$latestUrl = "$GitHubApiUrl/repos/$Repository/releases/latest"
try {
$release = Invoke-RestMethod -Uri $latestUrl -Headers $headers
} catch {
Fail "could not resolve the latest release: $($_.Exception.Message)"
}
if ($release -is [string]) {
try {
$release = $release | ConvertFrom-Json
} catch {
Fail "could not parse the latest release response: $($_.Exception.Message)"
}
}
$releaseTag = [string]$release.tag_name
if ([string]::IsNullOrWhiteSpace($releaseTag)) {
Fail "could not determine the latest release tag"
}
}
if ($releaseTag -cnotmatch $semanticTagPattern) {
Fail "invalid semantic release tag: $releaseTag"
}
$releaseVersion = $releaseTag -replace '^v', ''
if ([string]::IsNullOrWhiteSpace($releaseVersion)) {
Fail "invalid release tag: $releaseTag"
}
$archiveName = "codexometer_${releaseVersion}_windows_${releaseArch}.zip"
$releaseUrl = "$GitHubUrl/$Repository/releases/download/$releaseTag"
$archivePath = Join-Path $temporaryDirectory $archiveName
$checksumsPath = Join-Path $temporaryDirectory "checksums.txt"
Write-Host "Downloading Codexometer $releaseTag for windows/$releaseArch..."
try {
Invoke-WebRequest -Uri "$releaseUrl/$archiveName" -Headers $headers -OutFile $archivePath -UseBasicParsing
Invoke-WebRequest -Uri "$releaseUrl/checksums.txt" -Headers $headers -OutFile $checksumsPath -UseBasicParsing
} catch {
Fail "release download failed: $($_.Exception.Message)"
}
$matchingChecksums = @(
Get-Content -LiteralPath $checksumsPath | ForEach-Object {
if ($_ -match '^([0-9A-Fa-f]{64})\s+\*?(.+)$' -and $Matches[2] -eq $archiveName) {
$Matches[1]
}
}
)
if ($matchingChecksums.Count -ne 1) {
Fail "checksums.txt does not contain exactly one valid checksum for $archiveName"
}
$expectedHash = $matchingChecksums[0].ToLowerInvariant()
$actualHash = (Get-FileHash -LiteralPath $archivePath -Algorithm SHA256).Hash.ToLowerInvariant()
if ($actualHash -ne $expectedHash) {
Fail "SHA-256 checksum verification failed for $archiveName"
}
Write-Host "Verified the release checksum."
Add-Type -AssemblyName System.IO.Compression.FileSystem
$archive = [IO.Compression.ZipFile]::OpenRead($archivePath)
try {
$binaryEntries = @($archive.Entries | Where-Object { $_.FullName -eq "codexometer.exe" })
if ($binaryEntries.Count -ne 1) {
Fail "release archive does not contain exactly one root-level codexometer.exe binary"
}
$candidate = Join-Path $temporaryDirectory "codexometer.exe"
$source = $binaryEntries[0].Open()
try {
$destination = [IO.File]::Create($candidate)
try {
$source.CopyTo($destination)
} finally {
$destination.Dispose()
}
} finally {
$source.Dispose()
}
} finally {
$archive.Dispose()
}
$versionOutput = (& $candidate --version 2>&1 | Out-String).Trim()
if ($LASTEXITCODE -ne 0) {
Fail "the downloaded codexometer binary failed its version check"
}
if ($versionOutput -ne "codexometer $releaseVersion") {
Fail "the downloaded binary reported an unexpected version: $versionOutput"
}
New-Item -ItemType Directory -Path $BinDir -Force | Out-Null
$target = Join-Path $BinDir "codexometer.exe"
if (Test-Path -LiteralPath $target -PathType Container) {
Fail "installation target exists and is a directory: $target"
}
$stagedTarget = $null
try {
$stagedTarget = Join-Path $BinDir (".codexometer-" + [Guid]::NewGuid().ToString("N") + ".exe")
Copy-Item -LiteralPath $candidate -Destination $stagedTarget
if (Test-Path -LiteralPath $target -PathType Container) {
Remove-Item -LiteralPath $stagedTarget -Force
Fail "installation target exists and is a directory: $target"
}
Move-Item -LiteralPath $stagedTarget -Destination $target -Force
} catch {
if (-not [string]::IsNullOrWhiteSpace($stagedTarget) -and (Test-Path -LiteralPath $stagedTarget -PathType Leaf)) {
Remove-Item -LiteralPath $stagedTarget -Force
}
Fail "could not install into ${BinDir}: $($_.Exception.Message); set CODEXOMETER_BIN_DIR to a writable directory"
}
Write-Host "Installed codexometer to $target ($versionOutput)."
$pathEntries = @($env:PATH -split ';')
if ($pathEntries -notcontains $BinDir) {
Write-Host "Add $BinDir to PATH before invoking codexometer."
}
} finally {
if (Test-Path -LiteralPath $temporaryDirectory) {
Remove-Item -LiteralPath $temporaryDirectory -Recurse -Force
}
}