-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathinstall.ps1
More file actions
314 lines (284 loc) · 11.2 KB
/
Copy pathinstall.ps1
File metadata and controls
314 lines (284 loc) · 11.2 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
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
307
308
309
310
311
312
313
314
<#
.SYNOPSIS
Install the firma CLI on Windows.
.DESCRIPTION
Usage:
iwr -useb https://<host>/install.ps1 | iex
iwr -useb https://<host>/install.ps1 | iex -- -NoInit
.\install.ps1 -Version v0.7.0 -InstallDir 'C:\Tools\firma'
#>
#Requires -Version 5
[CmdletBinding()]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSAvoidUsingWriteHost', '',
Justification = 'Installer chrome is user-facing; redirecting Write-Output would interleave with download progress and break piped invocations.'
)]
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSReviewUnusedParameter', 'Version',
Justification = 'Read inside helper functions via $script:Version after parameter binding.'
)]
param(
[string]$Version = $env:FIRMA_VERSION,
[string]$InstallDir = $env:FIRMA_INSTALL_DIR,
[switch]$NoModifyPath,
[switch]$NoInit,
[switch]$Force,
[switch]$DryRun,
[switch]$Help
)
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
$QuickstartUrl = 'https://firma-ai.github.io/openfirma/quickstart/'
$GitHubRepo = 'Firma-AI/openfirma'
# Auth header for downloads when GITHUB_TOKEN is set. Used by CI runs
# against a private repo; harmless when unset.
function Get-AuthHeader {
if ($env:GITHUB_TOKEN) {
return @{ Authorization = "Bearer $env:GITHUB_TOKEN" }
}
return @{}
}
function Write-Info ($msg) { Write-Host "[firma-installer] $msg" }
function Write-Warn ($msg) { Write-Warning "[firma-installer] $msg" }
function Stop-Install {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseShouldProcessForStateChangingFunctions', '',
Justification = 'Terminal error helper; ShouldProcess semantics do not apply.'
)]
param([string]$msg)
Write-Host "[firma-installer] error: $msg" -ForegroundColor Red
exit 1
}
function Show-Usage {
@'
Install the firma CLI on Windows.
Usage: install.ps1 [options]
Options:
-Version <v> Install a specific release tag (e.g. v0.7.0).
Default: latest. Env: FIRMA_VERSION.
-InstallDir <path> Install location.
Default: %LOCALAPPDATA%\Programs\firma\bin.
Env: FIRMA_INSTALL_DIR.
-NoModifyPath Do not edit the User PATH.
-NoInit Do not prompt to run 'firma config'.
-Force Overwrite an existing install without prompting.
-DryRun Print planned actions only.
-Help Show this message.
Quickstart: https://firma-ai.github.io/openfirma/quickstart/
'@ | Write-Host
}
if ($Help) { Show-Usage; exit 0 }
if (-not $NoModifyPath -and $env:FIRMA_NO_MODIFY_PATH -eq '1') { $NoModifyPath = $true }
if (-not $NoInit -and $env:FIRMA_NO_INIT -eq '1') { $NoInit = $true }
if (-not $Force -and $env:FIRMA_FORCE -eq '1') { $Force = $true }
if (-not $DryRun -and $env:FIRMA_DRY_RUN -eq '1') { $DryRun = $true }
if (-not $InstallDir -or $InstallDir.Length -eq 0) {
$InstallDir = Join-Path $env:LOCALAPPDATA 'Programs\firma\bin'
}
function Invoke-Step ([string]$Description, [scriptblock]$Action) {
if ($DryRun) {
Write-Host "[dry-run] $Description"
} else {
& $Action
}
}
function Get-Target {
$arch = [System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture
switch ($arch) {
'X64' { return 'x86_64-pc-windows-msvc' }
'Arm64' { return 'aarch64-pc-windows-msvc' }
default { Stop-Install "unsupported windows arch: $arch" }
}
}
function Resolve-FirmaVersion {
if ($script:Version) {
if ($script:Version -notmatch '^v') { $script:Version = "v$script:Version" }
Write-Info "version (pinned): $script:Version"
return
}
Write-Info 'resolving latest release ...'
$req = [System.Net.HttpWebRequest]::Create("https://github.com/$GitHubRepo/releases/latest")
$req.AllowAutoRedirect = $false
$req.Method = 'HEAD'
if ($env:GITHUB_TOKEN) { $req.Headers.Add('Authorization', "Bearer $env:GITHUB_TOKEN") }
$location = $null
try {
$resp = $req.GetResponse()
$location = $resp.Headers['Location']
$resp.Close()
} catch [System.Net.WebException] {
$resp = $_.Exception.Response
if ($resp) {
$location = $resp.Headers['Location']
$resp.Close()
}
}
if (-not $location) { Stop-Install 'could not resolve latest release URL' }
$candidate = ($location -split '/')[-1]
if ($candidate -notmatch '^v[0-9]') {
Stop-Install "unexpected latest-release URL: $location"
}
$script:Version = $candidate
Write-Info "version: $script:Version"
}
function Test-Existing {
$cmd = Get-Command firma -ErrorAction SilentlyContinue
if (-not $cmd) { return }
$verLine = (& firma --version 2>$null)
$current = if ($verLine) { ($verLine -split ' ')[1] } else { 'unknown' }
$targetVer = $script:Version.TrimStart('v')
if ($current -eq $targetVer -and -not $Force) {
Write-Info "firma $current already installed. Use -Force to reinstall."
exit 0
}
if ($Force) {
Write-Info "replacing firma $current with $targetVer (-Force)"
return
}
if ([Environment]::UserInteractive) {
$ans = Read-Host "[firma-installer] replace firma $current with $targetVer? [y/N]"
if ($ans -notmatch '^(y|Y|yes|YES)$') { Stop-Install 'aborted by user' }
} else {
Stop-Install "firma $current already on PATH. Re-run with -Force or -Version."
}
}
function New-TempDir {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
'PSUseShouldProcessForStateChangingFunctions', '',
Justification = 'Internal scratch-dir helper; the script-level -DryRun flag already gates real work.'
)]
param()
$dir = Join-Path ([System.IO.Path]::GetTempPath()) "firma-installer-$([Guid]::NewGuid())"
New-Item -ItemType Directory -Path $dir -Force | Out-Null
return $dir
}
function Get-FirmaArchive ($TmpDir) {
$script:ArchiveName = "firma-$script:Target.zip"
$script:ArchivePath = Join-Path $TmpDir $script:ArchiveName
$base = "https://github.com/$GitHubRepo/releases/download/$script:Version"
$script:Headers = Get-AuthHeader
Write-Info "downloading $script:ArchiveName ..."
try {
Invoke-Step "Invoke-WebRequest $base/$script:ArchiveName" {
Invoke-WebRequest -UseBasicParsing -Headers $script:Headers -Uri "$base/$script:ArchiveName" -OutFile $script:ArchivePath
}
} catch {
Remove-Item -Force -ErrorAction SilentlyContinue $script:ArchivePath
$versionBare = $script:Version.TrimStart('v')
$script:ArchiveName = "firma-$versionBare-$script:Target.zip"
$script:ArchivePath = Join-Path $TmpDir $script:ArchiveName
Write-Info "cargo-dist archive unavailable; trying legacy archive $script:ArchiveName ..."
Invoke-Step "Invoke-WebRequest $base/$script:ArchiveName" {
Invoke-WebRequest -UseBasicParsing -Headers $script:Headers -Uri "$base/$script:ArchiveName" -OutFile $script:ArchivePath
}
}
$script:ChecksumPath = "$script:ArchivePath.sha256"
Invoke-Step "Invoke-WebRequest $base/$script:ArchiveName.sha256" {
Invoke-WebRequest -UseBasicParsing -Headers $script:Headers -Uri "$base/$script:ArchiveName.sha256" -OutFile $script:ChecksumPath
}
}
function Test-FirmaChecksum {
Write-Info 'verifying checksum ...'
if ($DryRun) { Write-Info '(dry-run) skipping verification'; return }
$expected = (Get-Content $script:ChecksumPath -Raw).Trim().Split()[0]
if (-not $expected) { Stop-Install "empty checksum file: $script:ChecksumPath" }
$actual = (Get-FileHash -Algorithm SHA256 -Path $script:ArchivePath).Hash.ToLower()
if ($expected.ToLower() -ne $actual) {
Stop-Install "checksum mismatch: expected $expected, got $actual"
}
Write-Info 'checksum ok'
}
function Install-FirmaBinary ($TmpDir) {
$extractDir = Join-Path $TmpDir 'extract'
Invoke-Step "Expand-Archive $script:ArchivePath" {
Expand-Archive -Path $script:ArchivePath -DestinationPath $extractDir -Force
}
if ($DryRun) {
$binarySrc = Join-Path $extractDir 'firma.exe'
} else {
$binarySrc = (Get-ChildItem -Path $extractDir -Filter 'firma.exe' -Recurse | Select-Object -First 1).FullName
if (-not $binarySrc) { Stop-Install "could not find firma.exe inside $script:ArchiveName" }
}
Invoke-Step "New-Item -Path $InstallDir" {
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}
$dest = Join-Path $InstallDir 'firma.exe'
Invoke-Step "Move-Item $binarySrc -> $dest" {
Move-Item -Force -Path $binarySrc -Destination $dest
}
Write-Info "installed: $dest"
}
function Add-FirmaToPath {
$userPath = [Environment]::GetEnvironmentVariable('Path', 'User')
if (-not $userPath) { $userPath = '' }
$segments = $userPath -split ';' | Where-Object { $_ -ne '' }
if ($segments -contains $InstallDir) {
Write-Info "$InstallDir already on User PATH"
return
}
if ($NoModifyPath) {
Write-Warn "$InstallDir not on PATH. Add it manually via System Properties or:"
Write-Host " [Environment]::SetEnvironmentVariable('Path', `"$userPath;$InstallDir`", 'User')"
return
}
$newPath = if ($userPath) { "$userPath;$InstallDir" } else { $InstallDir }
Invoke-Step "set User PATH (+= $InstallDir)" {
[Environment]::SetEnvironmentVariable('Path', $newPath, 'User')
}
Write-Info 'open a new shell to pick up the updated PATH.'
}
function Get-InstalledFirmaExe {
$local = Join-Path $InstallDir 'firma.exe'
if (Test-Path -Path $local -PathType Leaf) { return $local }
$cmd = Get-Command firma -ErrorAction SilentlyContinue
if ($null -ne $cmd) { return $cmd.Source }
return $null
}
function Test-FirmaSupportsConfig {
$exe = Get-InstalledFirmaExe
if (-not $exe) { return $false }
& $exe help config 2>$null | Out-Null
return $LASTEXITCODE -eq 0
}
function Get-NextStepCmd {
if (Test-FirmaSupportsConfig) { return 'firma config' }
return 'firma --help'
}
function Invoke-PostInstall {
Write-Info 'firma installed.'
Write-Info "quickstart: $QuickstartUrl"
if ($NoInit -or -not [Environment]::UserInteractive) {
Write-Info "next step: $(Get-NextStepCmd)"
return
}
if (-not (Test-FirmaSupportsConfig)) {
Write-Info "next step: $(Get-NextStepCmd)"
return
}
$ans = Read-Host '[firma-installer] run `firma config` now? [Y/n]'
if ($ans -match '^(y|Y|yes|YES|)$') {
if ($DryRun) { Write-Info '(dry-run) would invoke: firma config'; return }
& (Get-InstalledFirmaExe) config
} else {
Write-Info "next step: $(Get-NextStepCmd)"
}
}
function Main {
Write-Info "install dir: $InstallDir"
if ($DryRun) { Write-Info 'dry-run mode: no files will be written' }
$script:Target = Get-Target
Write-Info "target: $script:Target"
Resolve-FirmaVersion
Test-Existing
$tmp = New-TempDir
try {
Get-FirmaArchive $tmp
Test-FirmaChecksum
Install-FirmaBinary $tmp
} finally {
if (Test-Path $tmp) { Remove-Item -Recurse -Force $tmp }
}
Add-FirmaToPath
Invoke-PostInstall
}
Main