-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
192 lines (159 loc) · 7.53 KB
/
Copy pathinstall.ps1
File metadata and controls
192 lines (159 loc) · 7.53 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
#Requires -Version 5.1
<#
.SYNOPSIS
Download and install the latest lvpm release for Windows.
.DESCRIPTION
Resolves the latest release of lvpm on GitHub, downloads the Windows x64
zip over HTTPS, verifies its SHA-256, and installs lvpm.exe.
The download is verified against every reference available before
anything is extracted:
- the SHA-256 digest GitHub records for the release asset,
- a SHA256SUMS file, when the release ships one,
- a hash you pass with -Sha256, from a source other than GitHub.
If none of those is available the script stops. It never runs anything
it downloaded; lvpm.exe is only invoked with --version at the very end,
after verification.
No sign-in is needed.
Files are unblocked after extraction (Windows' mark-of-the-web is
removed), so SmartScreen and LabVIEW do not treat them as untrusted.
.PARAMETER Version
Release tag to install, e.g. v0.2.0. Default: the newest release.
.PARAMETER Stable
Skip pre-releases when picking the newest release. By default
pre-releases count, because lvpm is still in alpha.
.PARAMETER InstallDir
Where lvpm.exe goes. Default: %LOCALAPPDATA%\lvpm\bin
.PARAMETER Sha256
Expected SHA-256 of the zip, obtained out of band (release notes, the
maintainer). When given, the download must match it.
.PARAMETER NoPath
Do not add InstallDir to the user PATH.
.PARAMETER Repo
GitHub repository as owner/name. Default: Flydroid/lvpm
.EXAMPLE
.\install.ps1
.EXAMPLE
.\install.ps1 -Version v0.2.0 -InstallDir C:\tools\lvpm -NoPath
.EXAMPLE
.\install.ps1 -Sha256 1a86ce51553b705566cc1071a8e1c7cd8acc2df664d2e14b6ea46beba3799931
#>
[CmdletBinding()]
param(
[string]$Version,
[switch]$Stable,
[string]$InstallDir = (Join-Path $env:LOCALAPPDATA 'lvpm\bin'),
[ValidatePattern('^$|^[0-9a-fA-F]{64}$')]
[string]$Sha256,
[switch]$NoPath,
[ValidatePattern('^[\w.-]+/[\w.-]+$')]
[string]$Repo = 'Flydroid/lvpm'
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
# Windows PowerShell 5.1 negotiates TLS 1.0 by default; insist on 1.2 or better.
try {
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
} catch { }
$assetPattern = 'lvpm-*-windows-x64.zip'
$api = "https://api.github.com/repos/$Repo"
$headers = @{
'Accept' = 'application/vnd.github+json'
'X-GitHub-Api-Version' = '2022-11-28'
'User-Agent' = 'lvpm-install.ps1'
}
function Invoke-GitHubApi([string]$Path) {
Invoke-RestMethod -Uri "$api/$Path" -Headers $headers -MaximumRedirection 5
}
# ------------------------------------------------------ resolve release ----
if ($Version) {
$release = Invoke-GitHubApi "releases/tags/$Version"
} else {
# Invoke-RestMethod hands a JSON array back as one object rather than as a
# stream of them, so piping the call straight into Where-Object tests the
# whole array instead of each release and matches nothing. Bind it to a
# variable first; piping a variable enumerates it.
$releases = Invoke-GitHubApi 'releases?per_page=30'
$release = @($releases) |
Where-Object { -not $_.draft -and (-not $Stable -or -not $_.prerelease) } |
Select-Object -First 1
if (-not $release) {
throw "no release found in $Repo" + $(if ($Stable) { ' (every release is a pre-release; drop -Stable)' })
}
}
$asset = $release.assets | Where-Object { $_.name -like $assetPattern } | Select-Object -First 1
if (-not $asset) {
throw "release $($release.tag_name) has no asset matching $assetPattern"
}
Write-Host "release: $($release.tag_name)$(if ($release.prerelease) { ' (pre-release)' })"
Write-Host "asset: $($asset.name) ($([math]::Round($asset.size / 1MB, 1)) MB)"
# ------------------------------------------------------------ download ----
$work = Join-Path ([IO.Path]::GetTempPath()) ("lvpm-install-" + [guid]::NewGuid().ToString('n'))
New-Item -ItemType Directory -Path $work | Out-Null
try {
$zip = Join-Path $work $asset.name
# With Accept: octet-stream the asset API URL redirects to a short-lived
# signed download URL.
$dl = $headers.Clone()
$dl['Accept'] = 'application/octet-stream'
Invoke-WebRequest -Uri $asset.url -Headers $dl -OutFile $zip -MaximumRedirection 5
# -------------------------------------------------------- verify ----
$actual = (Get-FileHash -Algorithm SHA256 -Path $zip).Hash.ToLowerInvariant()
$expected = @()
if ($Sha256) {
$expected += [pscustomobject]@{ Source = '-Sha256 parameter'; Hash = $Sha256.ToLowerInvariant() }
}
if (($asset.PSObject.Properties.Name -contains 'digest') -and $asset.digest -like 'sha256:*') {
$expected += [pscustomobject]@{ Source = 'GitHub asset digest'; Hash = $asset.digest.Substring(7).ToLowerInvariant() }
}
$sums = $release.assets |
Where-Object { $_.name -in @('SHA256SUMS', 'SHA256SUMS.txt', "$($asset.name).sha256") } |
Select-Object -First 1
if ($sums) {
$sumsFile = Join-Path $work $sums.name
Invoke-WebRequest -Uri $sums.url -Headers $dl -OutFile $sumsFile -MaximumRedirection 5
$line = Get-Content $sumsFile | Where-Object { $_ -match '^\s*([0-9a-fA-F]{64})\s+\*?(.+?)\s*$' -and $Matches[2] -eq $asset.name }
if ($line -and $line -match '^\s*([0-9a-fA-F]{64})') {
$expected += [pscustomobject]@{ Source = $sums.name; Hash = $Matches[1].ToLowerInvariant() }
} elseif ($sums.name -like '*.sha256' -and (Get-Content $sumsFile -Raw) -match '([0-9a-fA-F]{64})') {
$expected += [pscustomobject]@{ Source = $sums.name; Hash = $Matches[1].ToLowerInvariant() }
}
}
if ($expected.Count -eq 0) {
throw "nothing to verify $($asset.name) against: the release has no digest and no SHA256SUMS. " +
"Pass -Sha256 <hash> from a source you trust, or stop here. Downloaded file: $zip"
}
foreach ($e in $expected) {
if ($e.Hash -ne $actual) {
throw "SHA-256 mismatch against $($e.Source): expected $($e.Hash), got $actual. Nothing was installed."
}
Write-Host "sha256: ok ($($e.Source))"
}
# ------------------------------------------------------- install ----
Unblock-File -Path $zip
$stage = Join-Path $work 'unpacked'
Expand-Archive -Path $zip -DestinationPath $stage -Force
$exe = Get-ChildItem -Path $stage -Recurse -File -Filter 'lvpm.exe' | Select-Object -First 1
if (-not $exe) { throw "$($asset.name) contains no lvpm.exe" }
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
Copy-Item -Path (Join-Path $exe.DirectoryName '*') -Destination $InstallDir -Recurse -Force
Get-ChildItem -Path $InstallDir -Recurse -File | Unblock-File
Write-Host "install: $InstallDir"
if (-not $NoPath) {
$userPath = [Environment]::GetEnvironmentVariable('Path', 'User')
$parts = @($userPath -split ';' | Where-Object { $_ })
if ($parts -notcontains $InstallDir) {
[Environment]::SetEnvironmentVariable('Path', (($parts + $InstallDir) -join ';'), 'User')
$env:Path = "$env:Path;$InstallDir"
Write-Host "path: added to the user PATH (new terminals will see it)"
} else {
Write-Host "path: already on the user PATH"
}
}
Write-Host ''
$installed = (& (Join-Path $InstallDir 'lvpm.exe') --version) -join ' '
Write-Host "$installed installed"
}
finally {
Remove-Item -Path $work -Recurse -Force -ErrorAction SilentlyContinue
}