-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall.ps1
More file actions
189 lines (163 loc) · 7.04 KB
/
Copy pathinstall.ps1
File metadata and controls
189 lines (163 loc) · 7.04 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
#Requires -Version 5.1
<#
.SYNOPSIS
Install the Flagsmith CLI.
.DESCRIPTION
irm https://get.flagsmith.com/install.ps1 | iex
`iex` cannot pass arguments, so either set the environment variables below
first, or invoke a script block:
&([scriptblock]::Create((irm https://get.flagsmith.com/install.ps1))) -Version <tag>
.PARAMETER Version
Version to install. Defaults to $env:FLAGSMITH_CLI_VERSION, else the version
this script shipped with.
.PARAMETER BinDir
Where to install. Defaults to $env:FLAGSMITH_INSTALL_DIR, else ~\.local\bin.
.PARAMETER NoModifyPath
Leave the user PATH alone. Also $env:FLAGSMITH_NO_MODIFY_PATH.
.PARAMETER DryRun
Report what would be installed, then stop.
#>
param(
[string]$Version,
[string]$BinDir,
[switch]$NoModifyPath,
[switch]$DryRun
)
$ErrorActionPreference = 'Stop'
# Invoke-WebRequest spends most of its time drawing the progress bar.
$ProgressPreference = 'SilentlyContinue'
$DefaultVersion = 'v2.0.0' # x-release-please-version
$Repo = 'Flagsmith/flagsmith-cli'
$ExeName = 'flagsmith.exe'
$BaseUrl = if ($env:FLAGSMITH_CLI_BASE_URL) {
$env:FLAGSMITH_CLI_BASE_URL
} else {
"https://github.com/$Repo/releases/download"
}
function Get-TargetArch {
$arch = try {
[System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString()
} catch {
# PROCESSOR_ARCHITECTURE from the registry, not the environment: a 32-bit
# PowerShell under WOW64 reports x86 for its own process.
(Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment').PROCESSOR_ARCHITECTURE
}
switch -Regex ($arch) {
'^(X64|AMD64)$' { return 'amd64' }
'^ARM64$' { return 'arm64' }
default { throw "unsupported architecture '$arch'" }
}
}
function Get-Checksum {
param([string]$SumsFile, [string]$Name)
$pattern = '\s\*?' + [regex]::Escape($Name) + '$'
$lines = @(Get-Content -LiteralPath $SumsFile | Where-Object { $_ -match $pattern })
if ($lines.Count -ne 1) {
throw "expected exactly one checksum for $Name in checksums.txt, found $($lines.Count)"
}
return ($lines[0] -split '\s+')[0]
}
# Add-UserPath adds to the user PATH through the registry rather than
# [Environment]::SetEnvironmentVariable, which rewrites REG_EXPAND_SZ as REG_SZ
# and so breaks any %VAR% already in PATH.
function Add-UserPath {
param([string]$Dir)
$key = 'registry::HKEY_CURRENT_USER\Environment'
$current = (Get-Item -LiteralPath $key).GetValue('Path', '', 'DoNotExpandEnvironmentNames') -split ';' -ne ''
if ($Dir -in $current) { return $false }
Set-ItemProperty -LiteralPath $key -Name Path -Type ExpandString -Value ((, $Dir + $current) -join ';')
# Tell running shells and Explorer to reread the environment.
$dummy = 'flagsmith-' + [guid]::NewGuid().ToString()
[Environment]::SetEnvironmentVariable($dummy, 'x', 'User')
[Environment]::SetEnvironmentVariable($dummy, [NullString]::Value, 'User')
return $true
}
# Write-ConflictWarning reports other flagsmith commands on PATH that may shadow
# (or be shadowed by) the one just installed.
function Write-ConflictWarning {
param([string]$Target)
$resolvedTarget = [System.IO.Path]::GetFullPath($Target)
# Application catches flagsmith.exe and npm's flagsmith.cmd shim;
# ExternalScript catches its flagsmith.ps1 shim. Both expose .Path, and
# neither triggers module auto-loading the way an unfiltered -All can.
$others = @(Get-Command -Name 'flagsmith' -CommandType Application, ExternalScript -All -ErrorAction SilentlyContinue |
Where-Object { $_.Path -and ([System.IO.Path]::GetFullPath($_.Path) -ne $resolvedTarget) })
foreach ($other in $others) {
Write-Output "warning: another 'flagsmith' is on your PATH at $($other.Path)"
}
if ($others.Count -gt 0) {
Write-Output "It may shadow $Target - uninstall it first ('npm uninstall -g flagsmith-cli' removes the old npm CLI), then open a new terminal."
}
}
# Add-CiPath makes the CLI available to later steps of a GitHub Actions job.
function Add-CiPath {
param([string]$Dir)
if ($env:GITHUB_PATH) {
Write-Output $Dir | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
}
}
if (-not $Version) {
$Version = if ($env:FLAGSMITH_CLI_VERSION) { $env:FLAGSMITH_CLI_VERSION } else { $DefaultVersion }
}
if ($Version -notlike 'v*') { $Version = "v$Version" }
if (-not $BinDir) {
$BinDir = if ($env:FLAGSMITH_INSTALL_DIR) {
$env:FLAGSMITH_INSTALL_DIR
} else {
Join-Path $env:USERPROFILE '.local\bin'
}
}
if ($env:FLAGSMITH_NO_MODIFY_PATH -eq '1') { $NoModifyPath = $true }
$arch = Get-TargetArch
$archive = "flagsmith_$($Version.TrimStart('v'))_windows_$arch.zip"
$archiveUrl = "$BaseUrl/$Version/$archive"
$sumsUrl = "$BaseUrl/$Version/checksums.txt"
if ($DryRun) {
Write-Output "would install flagsmith $Version (windows/$arch) to $BinDir"
Write-Output " archive: $archiveUrl"
Write-Output " checksums: $sumsUrl"
return
}
# PowerShell 5.1 still defaults to TLS 1.0, which github.com refuses.
if ([Net.ServicePointManager]::SecurityProtocol -notmatch 'Tls12') {
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
}
$tmp = New-Item -ItemType Directory -Path (Join-Path ([System.IO.Path]::GetTempPath()) ([guid]::NewGuid()))
try {
Write-Output "downloading flagsmith $Version (windows/$arch)"
$zip = Join-Path $tmp $archive
$sums = Join-Path $tmp 'checksums.txt'
try {
Invoke-WebRequest -Uri $archiveUrl -OutFile $zip -UseBasicParsing
} catch {
throw "cannot download $archiveUrl`nIf $Version was released moments ago its archives may still be uploading - retry shortly, or choose a version with -Version."
}
Invoke-WebRequest -Uri $sumsUrl -OutFile $sums -UseBasicParsing
$expected = Get-Checksum -SumsFile $sums -Name $archive
$actual = (Get-FileHash -LiteralPath $zip -Algorithm SHA256).Hash
if ($actual -ne $expected.ToUpperInvariant()) {
throw "checksum mismatch for ${archive}: expected $expected, got $actual"
}
Expand-Archive -LiteralPath $zip -DestinationPath $tmp -Force
New-Item -ItemType Directory -Force -Path $BinDir | Out-Null
$verb, $prep = if (Test-Path -LiteralPath (Join-Path $BinDir $ExeName)) { 'updated', 'at' } else { 'installed', 'to' }
Move-Item -Force -LiteralPath (Join-Path $tmp $ExeName) -Destination (Join-Path $BinDir $ExeName)
} finally {
Remove-Item -Recurse -Force -LiteralPath $tmp
}
$exe = Join-Path $BinDir $ExeName
$installed = & $exe --version
if ($LASTEXITCODE -ne 0) { throw "$exe was installed but will not run" }
Write-Output "$verb $installed $prep $exe"
Write-ConflictWarning -Target $exe
$pathAdded = $false
if (-not $NoModifyPath) {
$pathAdded = Add-UserPath -Dir $BinDir
Add-CiPath -Dir $BinDir
}
Write-Output ''
if ($pathAdded) {
Write-Output "Open a new terminal, then run 'flagsmith init' to get started."
} else {
Write-Output "Run 'flagsmith init' to get started."
}