-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.ps1
More file actions
76 lines (64 loc) 路 3.08 KB
/
Copy pathinstall.ps1
File metadata and controls
76 lines (64 loc) 路 3.08 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
# Install the latest tinyanalyzer release on 64-bit Windows.
[CmdletBinding()]
param()
$ErrorActionPreference = "Stop"
$Repository = "tinyhumansai/tinyanalyzer"
$Binary = "tinyanalyzer.exe"
if (-not [Environment]::Is64BitOperatingSystem) {
throw "tinyanalyzer installer: 64-bit Windows is required"
}
$Version = $env:TINYANALYZER_VERSION
if ([string]::IsNullOrWhiteSpace($Version)) {
$Release = Invoke-RestMethod -Uri "https://api.github.com/repos/$Repository/releases/latest" -Headers @{ "User-Agent" = "tinyanalyzer-installer" }
$Version = $Release.tag_name
}
if (-not $Version.StartsWith("v")) {
$Version = "v$Version"
}
$Asset = "tinyanalyzer-$Version-windows-x86_64.zip"
$ReleaseUrl = "https://github.com/$Repository/releases/download/$Version"
$Temporary = Join-Path ([IO.Path]::GetTempPath()) ("tinyanalyzer-install-" + [Guid]::NewGuid())
try {
New-Item -ItemType Directory -Path $Temporary | Out-Null
$Archive = Join-Path $Temporary $Asset
$Checksums = Join-Path $Temporary "SHA256SUMS"
Write-Host "Downloading $Asset"
Invoke-WebRequest -Uri "$ReleaseUrl/$Asset" -OutFile $Archive
Invoke-WebRequest -Uri "$ReleaseUrl/SHA256SUMS" -OutFile $Checksums
$ChecksumLine = Get-Content $Checksums | Where-Object {
$_ -match "^[0-9a-fA-F]{64}\s+(\./)?$([Regex]::Escape($Asset))$"
} | Select-Object -First 1
if (-not $ChecksumLine) {
throw "tinyanalyzer installer: $Asset is absent from SHA256SUMS"
}
$Expected = ($ChecksumLine -split "\s+")[0].ToLowerInvariant()
$Actual = (Get-FileHash -Algorithm SHA256 -Path $Archive).Hash.ToLowerInvariant()
if ($Actual -ne $Expected) {
throw "tinyanalyzer installer: checksum verification failed"
}
Expand-Archive -Path $Archive -DestinationPath $Temporary
$SourceBinary = Join-Path $Temporary "tinyanalyzer-$Version-windows-x86_64\$Binary"
if (-not (Test-Path -LiteralPath $SourceBinary -PathType Leaf)) {
throw "tinyanalyzer installer: archive does not contain $Binary"
}
$InstallDirectory = $env:TINYANALYZER_INSTALL_DIR
if ([string]::IsNullOrWhiteSpace($InstallDirectory)) {
$InstallDirectory = Join-Path $env:LOCALAPPDATA "Programs\tinyanalyzer\bin"
}
New-Item -ItemType Directory -Force -Path $InstallDirectory | Out-Null
Copy-Item -Force -LiteralPath $SourceBinary -Destination (Join-Path $InstallDirectory $Binary)
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User")
$PathEntries = @($UserPath -split ";" | Where-Object { -not [string]::IsNullOrWhiteSpace($_) })
if ($InstallDirectory -notin $PathEntries) {
$UpdatedPath = (@($PathEntries) + $InstallDirectory) -join ";"
[Environment]::SetEnvironmentVariable("Path", $UpdatedPath, "User")
$env:Path = "$InstallDirectory;$env:Path"
Write-Host "Added $InstallDirectory to your user PATH; open a new terminal to use it."
}
Write-Host "Installed tinyanalyzer $Version to $(Join-Path $InstallDirectory $Binary)"
}
finally {
if (Test-Path -LiteralPath $Temporary) {
Remove-Item -LiteralPath $Temporary -Recurse -Force
}
}