Repository navigation
Expand file tree
/
Copy pathinstall.ps1
More file actions
59 lines (54 loc) · 3.78 KB
/
Copy pathinstall.ps1
File metadata and controls
59 lines (54 loc) · 3.78 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
# hanfix installer for Windows:
# powershell -ExecutionPolicy Bypass -c "irm -useb https://github.com/NaNIsaac/hanfix/releases/latest/download/install.ps1 | iex"
# Puts hanfix.exe in $env:HANFIX_INSTALL_DIR (default %LOCALAPPDATA%\Programs\hanfix) and adds it to
# the user PATH. Pin a release with $env:HANFIX_VERSION = 'v0.2.3'. Run it again (or hanfix --update) to update.
# irm | iex runs this in your shell's scope; the script block keeps its settings and variables
# out of it. Only the PATH changes are meant to stay.
& {
$ErrorActionPreference = 'Stop'
[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
# tests/install-path.ps1 extracts this function by name, so keep it free of side effects.
# [string] turns the $null that GetEnvironmentVariable returns for an unset Path into ''.
function Merge-PathEntry([string]$Path, [string]$Dir) {
if (($Path -split ';') -contains $Dir) { return $Path }
($Path.TrimEnd(';') + ";$Dir").TrimStart(';')
}
$repo = 'NaNIsaac/hanfix'
$dir = if ($env:HANFIX_INSTALL_DIR) { $env:HANFIX_INSTALL_DIR } else { Join-Path $env:LOCALAPPDATA 'Programs\hanfix' }
$version = if ($env:HANFIX_VERSION) { $env:HANFIX_VERSION } else { 'latest' }
$arch = if ($env:PROCESSOR_ARCHITECTURE -eq 'ARM64' -or $env:PROCESSOR_ARCHITEW6432 -eq 'ARM64') { 'aarch64' } else { 'x86_64' }
$asset = "hanfix-$arch-pc-windows-msvc.exe"
$base = if ($version -eq 'latest') { "https://github.com/$repo/releases/latest/download" } else { "https://github.com/$repo/releases/download/$version" }
Write-Host "hanfix: downloading $asset ($version)"
New-Item -ItemType Directory -Force -Path $dir | Out-Null
$tmp = Join-Path ([IO.Path]::GetTempPath()) ([Guid]::NewGuid().ToString() + '.exe')
try {
Invoke-WebRequest -UseBasicParsing "$base/$asset" -OutFile $tmp
# GitHub serves the .sha256 as binary, so read it from a file rather than from .Content
Invoke-WebRequest -UseBasicParsing "$base/$asset.sha256" -OutFile "$tmp.sha256"
$want = ((Get-Content -Raw "$tmp.sha256").Trim() -split '\s+')[0]
# .NET instead of Get-FileHash, which is missing when Windows PowerShell inherits PowerShell 7's module path
$fs = [IO.File]::OpenRead($tmp)
try { $got = -join ([Security.Cryptography.SHA256]::Create().ComputeHash($fs) | ForEach-Object { $_.ToString('X2') }) } finally { $fs.Dispose() }
if ($want -ne $got) { throw "hanfix: checksum mismatch (expected $want, got $got)" }
# A running hanfix.exe (hanfix --update) cannot be overwritten, but it can be renamed.
$exe = Join-Path $dir 'hanfix.exe'
Remove-Item -Force "$exe.old" -ErrorAction SilentlyContinue
if (Test-Path $exe) { Move-Item -Force $exe "$exe.old" }
Move-Item -Force $tmp $exe
Remove-Item -Force "$exe.old" -ErrorAction SilentlyContinue # stays only while the old one runs
} finally {
Remove-Item -Force $tmp, "$tmp.sha256" -ErrorAction SilentlyContinue
}
$userPath = [Environment]::GetEnvironmentVariable('Path', 'User')
$merged = Merge-PathEntry $userPath $dir
if ($merged -ne $userPath) {
[Environment]::SetEnvironmentVariable('Path', $merged, 'User')
Write-Host "hanfix: added $dir to your user PATH"
}
if (($env:Path -split ';') -notcontains $dir) { $env:Path += ";$dir" }
Write-Host "hanfix: installed $(& (Join-Path $dir 'hanfix.exe') --version) to $dir"
if ($env:HANFIX_UPDATE) { return }
Write-Host 'hanfix: to fix the previous command with hf, run this once and open a new terminal:'
Write-Host " New-Item -ItemType Directory -Force (Split-Path `$PROFILE) | Out-Null; Add-Content `$PROFILE 'Invoke-Expression (hanfix --init powershell | Out-String)'"
}