Repository navigation
Expand file tree
/
Copy pathinstall.ps1
More file actions
196 lines (179 loc) · 9.14 KB
/
Copy pathinstall.ps1
File metadata and controls
196 lines (179 loc) · 9.14 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
[CmdletBinding()]
param(
[string]$Version,
[string]$InstallDir,
[string]$BaseUrl
)
$ErrorActionPreference = "Stop"
$Target = "x86_64-pc-windows-msvc"
function Fail([string]$Message) {
throw "error: $Message"
}
function Resolve-Setting([string]$Value, [string]$EnvironmentName, [string]$Default) {
if ($Value) { return $Value }
$environmentValue = [Environment]::GetEnvironmentVariable($EnvironmentName)
if ($environmentValue) { return $environmentValue }
return $Default
}
# Windows PowerShell 5.1 on older .NET Framework releases negotiates only
# TLS versions the release endpoints no longer accept; allow TLS 1.2 as well,
# and TLS 1.3 where the .NET Framework exposes it (Tls13 is absent before
# .NET 4.8-era enum support, so resolve it by name rather than hard-fail).
if ($PSVersionTable.PSVersion.Major -lt 6) {
$protocols = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
if ([Enum]::GetNames([Net.SecurityProtocolType]) -contains "Tls13") {
$protocols = $protocols -bor [Enum]::Parse([Net.SecurityProtocolType], "Tls13")
}
[Net.ServicePointManager]::SecurityProtocol = $protocols
}
function Get-RemoteFile([string]$Uri, [string]$Destination) {
# -UseBasicParsing keeps Windows PowerShell 5.1 independent of the Internet
# Explorer COM engine; suppressing progress reporting avoids the slow
# per-read progress updates on large archives.
$ProgressPreference = "SilentlyContinue"
Invoke-WebRequest -Uri $Uri -OutFile $Destination -UseBasicParsing
}
function Get-Version([string]$RequestedVersion, [string]$ReleaseBaseUrl) {
if ($RequestedVersion) {
$resolved = $RequestedVersion.TrimStart("v")
} else {
$latestVersionUrl = "$($ReleaseBaseUrl.TrimEnd('/'))/latest/version"
$latestVersionPath = Join-Path ([IO.Path]::GetTempPath()) ("wright-latest-" + [Guid]::NewGuid().ToString("N"))
try {
Get-RemoteFile $latestVersionUrl $latestVersionPath
$content = Get-Content -LiteralPath $latestVersionPath -Raw
$resolved = $content.Trim().TrimStart("v")
if (-not $resolved) {
Fail "latest version response from $latestVersionUrl was empty; pin a version with -Version"
}
} catch {
if ($_.Exception.Message -like "error: latest version response*") { throw }
Fail "could not resolve the latest release from ${latestVersionUrl}: $($_.Exception.Message); pin a version with -Version"
} finally {
if (Test-Path -LiteralPath $latestVersionPath) {
Remove-Item -LiteralPath $latestVersionPath -Force -ErrorAction SilentlyContinue
}
}
}
if ($resolved -notmatch '^[0-9]+\.[0-9]+\.[0-9]+(?:[-+][0-9A-Za-z.-]+)?$') {
Fail "invalid version '$resolved' (expected semver like 0.1.0)"
}
return $resolved
}
function Get-Checksum([string]$ChecksumPath, [string]$ArchiveName) {
$line = Get-Content -LiteralPath $ChecksumPath | Where-Object { $_.Trim() } | Select-Object -First 1
if ($line -notmatch '^\s*([0-9a-fA-F]{64})\s+\*?([^\s]+)\s*$') {
Fail "invalid checksum file for $ArchiveName"
}
if ([IO.Path]::GetFileName($Matches[2]) -ne $ArchiveName) {
Fail "checksum file names '$($Matches[2])', not '$ArchiveName'"
}
return $Matches[1].ToUpperInvariant()
}
function Test-Version([string]$Executable, [string]$Version) {
try {
$output = (& $Executable --version 2>&1 | Out-String).Trim()
} catch {
Fail "post-install smoke check could not execute '$Executable'"
}
if ($LASTEXITCODE -ne 0 -or $output -notlike "*$Version*") {
Fail "post-install smoke check for '$Executable' did not report version $Version"
}
}
if ([Environment]::OSVersion.Platform -ne [PlatformID]::Win32NT) {
Fail "unsupported operating system; install.ps1 must run on Windows x86_64"
}
# RuntimeInformation.OSArchitecture returns nothing on some .NET Framework
# 4.x builds, so read the architecture the OS exports instead. A 32-bit or
# emulated process reports the real OS architecture in PROCESSOR_ARCHITEW6432.
$OsArchitecture = if ($env:PROCESSOR_ARCHITEW6432) { $env:PROCESSOR_ARCHITEW6432 } else { $env:PROCESSOR_ARCHITECTURE }
if ($OsArchitecture -ne "AMD64") {
Fail "unsupported CPU architecture; install.ps1 supports Windows x86_64 only"
}
$BaseUrl = Resolve-Setting $BaseUrl "WRIGHT_INSTALL_BASE_URL" "https://install.wrightkit.dev/wright"
$Version = Get-Version $Version $BaseUrl
$DefaultInstallDir = -not $InstallDir
if ($DefaultInstallDir) {
$InstallRoot = if ($env:LOCALAPPDATA) { $env:LOCALAPPDATA } else { $env:USERPROFILE }
if (-not $InstallRoot) { Fail "could not determine a user-writable install directory; pass -InstallDir" }
$InstallDir = Join-Path $InstallRoot "Programs\Wright\bin"
}
$ArchiveName = "wright-$Version-$Target.zip"
$ArchiveUrl = "$($BaseUrl.TrimEnd('/'))/releases/$Version/$ArchiveName"
$ChecksumUrl = "${ArchiveUrl}.sha256"
$TempRoot = Join-Path ([IO.Path]::GetTempPath()) ("wright-install-" + [Guid]::NewGuid().ToString("N"))
$ExtractDir = Join-Path $TempRoot "extract"
$StageDir = Join-Path $TempRoot "stage"
try {
New-Item -ItemType Directory -Path $TempRoot, $ExtractDir, $StageDir -Force | Out-Null
$ArchivePath = Join-Path $TempRoot $ArchiveName
$ChecksumPath = "$ArchivePath.sha256"
Write-Host "==> downloading $ArchiveUrl"
try {
Get-RemoteFile $ArchiveUrl $ArchivePath
Get-RemoteFile $ChecksumUrl $ChecksumPath
} catch {
Fail "failed to download the release archive or checksum for v$Version from ${BaseUrl}: $($_.Exception.Message)"
}
Write-Host "==> verifying SHA-256 checksum"
$ExpectedHash = Get-Checksum $ChecksumPath $ArchiveName
$ActualHash = (Get-FileHash -LiteralPath $ArchivePath -Algorithm SHA256).Hash.ToUpperInvariant()
if ($ActualHash -ne $ExpectedHash) {
Fail "checksum verification failed for $ArchiveName; the download may be corrupted or tampered with, so nothing was installed"
}
Write-Host "==> extracting release archive"
try {
Expand-Archive -LiteralPath $ArchivePath -DestinationPath $ExtractDir -Force
} catch {
Fail "failed to extract $ArchiveName"
}
$PayloadDir = Join-Path $ExtractDir "wright-$Version-$Target"
$Wright = Join-Path $PayloadDir "wright.exe"
$Lsp = Join-Path $PayloadDir "wright-lsp.exe"
if (-not (Test-Path -LiteralPath $PayloadDir -PathType Container) -or
-not (Test-Path -LiteralPath $Wright -PathType Leaf) -or
-not (Test-Path -LiteralPath $Lsp -PathType Leaf)) {
Fail "unexpected archive layout; expected $PayloadDir with wright.exe and wright-lsp.exe"
}
Copy-Item -LiteralPath $Wright -Destination (Join-Path $StageDir "wright.exe")
Copy-Item -LiteralPath $Lsp -Destination (Join-Path $StageDir "wright-lsp.exe")
Test-Version (Join-Path $StageDir "wright.exe") $Version
Test-Version (Join-Path $StageDir "wright-lsp.exe") $Version
Write-Host "==> installing into $InstallDir"
try {
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
$WriteProbe = Join-Path $InstallDir (".wright-write-test-" + [Guid]::NewGuid().ToString("N"))
New-Item -ItemType File -Path $WriteProbe -Force | Out-Null
Remove-Item -LiteralPath $WriteProbe -Force
} catch {
Fail "install directory '$InstallDir' is not writable; choose a writable location with -InstallDir"
}
Copy-Item -LiteralPath (Join-Path $StageDir "wright.exe") -Destination (Join-Path $InstallDir "wright.exe") -Force
Copy-Item -LiteralPath (Join-Path $StageDir "wright-lsp.exe") -Destination (Join-Path $InstallDir "wright-lsp.exe") -Force
Test-Version (Join-Path $InstallDir "wright.exe") $Version
Test-Version (Join-Path $InstallDir "wright-lsp.exe") $Version
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User")
$PathEntry = $InstallDir.TrimEnd("\")
$PathEntries = if ($UserPath) { $UserPath -split ";" } else { @() }
$PathContainsInstallDir = @($PathEntries | Where-Object {
$_.Trim().TrimEnd("\") -ieq $PathEntry
}).Count -gt 0
if (-not $PathContainsInstallDir) {
if ($DefaultInstallDir) {
$NewUserPath = (@($PathEntries | Where-Object { $_.Trim() }) + $PathEntry) -join ";"
[Environment]::SetEnvironmentVariable("Path", $NewUserPath, "User")
$env:Path = "$env:Path;$PathEntry"
Write-Host "==> added '$PathEntry' to your user PATH; terminals opened from now on can run wright"
} else {
$DisplayPath = $InstallDir.Replace("'", "''")
Write-Host "note: '$InstallDir' is not on your user PATH; add it and open a new terminal to use wright by name:"
Write-Host " `$userPath = [Environment]::GetEnvironmentVariable('Path', 'User')"
Write-Host " [Environment]::SetEnvironmentVariable('Path', (`$userPath.TrimEnd(';') + ';$DisplayPath'), 'User')"
}
}
Write-Host "==> done: wright and wright-lsp $Version installed in $InstallDir"
} finally {
if (Test-Path -LiteralPath $TempRoot) {
Remove-Item -LiteralPath $TempRoot -Recurse -Force -ErrorAction SilentlyContinue
}
}