-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
99 lines (88 loc) · 3.9 KB
/
Copy pathinstall.ps1
File metadata and controls
99 lines (88 loc) · 3.9 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
# Install the `deed` binary on Windows.
#
# irm https://raw.githubusercontent.com/deed-lang/deed/main/install.ps1 | iex
#
# The same three steps as install.sh: work out which release asset fits this
# machine, refuse it if its hash is not the one the release published, and put
# one file in your own profile. It never needs elevation, because it never
# writes outside your profile.
#
# What the hash does and does not buy: the checksums come from the same release
# as the binary, so this catches a truncated or corrupted download and does not
# catch a compromised release.
#
# `DEED_VERSION` pins a release, `DEED_INSTALL_DIR` says where the file goes,
# and `DEED_DOWNLOAD_BASE` points at a mirror of the release assets.
#
# `crates/deed-driver/tests/install.rs` holds the platform this knows against
# the ones `.github/workflows/release.yml` actually builds.
$ErrorActionPreference = 'Stop'
$repo = 'deed-lang/deed'
$installDir = if ($env:DEED_INSTALL_DIR) {
$env:DEED_INSTALL_DIR
} else {
Join-Path $env:LOCALAPPDATA 'Programs\deed'
}
# There is one Windows build, and it is x64. Saying which machine this is
# beats installing something that will not start.
$arch = $env:PROCESSOR_ARCHITECTURE
if ($arch -ne 'AMD64') {
throw "no release is built for Windows on $arch: ``cargo install deed-lang`` builds one"
}
$target = 'x86_64-pc-windows-msvc'
$version = if ($env:DEED_VERSION) { $env:DEED_VERSION } else { $null }
if (-not $version) {
$latest = Invoke-RestMethod "https://api.github.com/repos/$repo/releases/latest"
$version = $latest.tag_name
}
if (-not $version) { throw "could not work out the latest release of $repo" }
if (-not $version.StartsWith('v')) { $version = "v$version" }
$name = "deed-$version-$target"
$asset = "$name.zip"
$base = if ($env:DEED_DOWNLOAD_BASE) {
$env:DEED_DOWNLOAD_BASE
} else {
"https://github.com/$repo/releases/download/$version"
}
$tmp = Join-Path ([System.IO.Path]::GetTempPath()) ("deed-install-" + [guid]::NewGuid())
New-Item -ItemType Directory -Path $tmp | Out-Null
try {
Write-Host "downloading $asset"
$archive = Join-Path $tmp $asset
Invoke-WebRequest "$base/$asset" -OutFile $archive
$sums = Join-Path $tmp 'checksums.txt'
Invoke-WebRequest "$base/deed-$version-checksums.txt" -OutFile $sums
# The last field is the name and the first is the hash, read by field
# rather than by exact spacing.
$want = $null
foreach ($line in Get-Content $sums) {
$fields = $line.Trim() -split '\s+'
if ($fields.Length -lt 2) { continue }
$file = $fields[-1] -replace '^\*', '' -replace '^\./', ''
if ($file -eq $asset) { $want = $fields[0]; break }
}
if (-not $want) { throw "the checksum list does not mention $asset" }
$got = (Get-FileHash $archive -Algorithm SHA256).Hash.ToLower()
if ($want.ToLower() -ne $got) {
throw "$asset hashes to $got and the release says $want"
}
Write-Host 'sha256 ok'
Expand-Archive -Path $archive -DestinationPath $tmp -Force
$binary = Join-Path $tmp "$name\deed.exe"
if (-not (Test-Path $binary)) { throw "$asset does not contain $name\deed.exe" }
New-Item -ItemType Directory -Path $installDir -Force | Out-Null
Move-Item -Path $binary -Destination (Join-Path $installDir 'deed.exe') -Force
Write-Host "installed $installDir\deed.exe"
} finally {
Remove-Item -Recurse -Force $tmp -ErrorAction SilentlyContinue
}
# The user's own PATH, not the machine's: this wrote one file into one profile
# and changing the machine would be a larger claim than the install made.
$userPath = [Environment]::GetEnvironmentVariable('Path', 'User')
if (($userPath -split ';') -notcontains $installDir) {
[Environment]::SetEnvironmentVariable('Path', "$userPath;$installDir", 'User')
Write-Host ''
Write-Host "added $installDir to your PATH; open a new terminal to pick it up"
}
Write-Host ''
Write-Host 'next: deed new greeter; cd greeter; deed test .'