-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.ps1
More file actions
181 lines (153 loc) · 6.29 KB
/
Copy pathinstall.ps1
File metadata and controls
181 lines (153 loc) · 6.29 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
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
Add-Type -AssemblyName System.IO.Compression.FileSystem
if (-not (Get-Command % -ErrorAction SilentlyContinue)) {
Set-Alias -Name % -Value ForEach-Object -Scope Script
}
function Write-Step {
param([string]$Message)
Write-Host "[shaka-install] $Message"
}
function Fail {
param([string]$Message)
Write-Error "[shaka-install] $Message"
exit 1
}
function Get-Sha256 {
param([string]$Path)
if (Get-Command Get-FileHash -ErrorAction SilentlyContinue) {
return (Get-FileHash -Path $Path -Algorithm SHA256).Hash.Trim().ToLowerInvariant()
}
$sha = [System.Security.Cryptography.SHA256]::Create()
$stream = [System.IO.File]::OpenRead($Path)
try {
$hashBytes = $sha.ComputeHash($stream)
} finally {
$stream.Dispose()
$sha.Dispose()
}
return ([System.BitConverter]::ToString($hashBytes) -replace "-", "").ToLowerInvariant()
}
$Repo = "NazmusSayad/shaka"
$ApiUrl = "https://api.github.com/repos/$Repo/releases/latest"
$InstallDir = Join-Path $env:LOCALAPPDATA "shaka\bin"
$BinaryPath = Join-Path $InstallDir "shaka.exe"
Write-Step "Detecting platform and architecture"
$os = [System.Runtime.InteropServices.RuntimeInformation]::OSDescription
$arch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString().ToLowerInvariant()
$target = switch ($arch) {
"x64" { "x86_64-pc-windows-msvc" }
"arm64" { "aarch64-pc-windows-msvc" }
default { Fail "Unsupported Windows architecture: $arch" }
}
Write-Step "Platform: $os"
Write-Step "Target: $target"
Write-Step "Fetching latest release metadata"
try {
$release = Invoke-RestMethod -Uri $ApiUrl -Headers @{ "User-Agent" = "shaka-installer" }
} catch {
Fail "Failed to fetch latest release from GitHub: $($_.Exception.Message)"
}
if (-not $release.tag_name) {
Fail "Latest release response did not include tag_name"
}
$tag = [string]$release.tag_name
$version = $tag.TrimStart("v")
$assetName = "shaka-$tag-$target.zip"
Write-Step "Latest release tag: $tag"
Write-Step "Selecting asset: $assetName"
$asset = $release.assets | Where-Object { $_.name -eq $assetName } | Select-Object -First 1
if (-not $asset) {
Fail "Could not find release asset: $assetName"
}
$checksumAsset = $release.assets | Where-Object { $_.name -eq "sha256sums.txt" } | Select-Object -First 1
if (-not $checksumAsset) {
Fail "Could not find checksum file in release assets: sha256sums.txt"
}
if (Test-Path $BinaryPath) {
Write-Step "Existing installation found at $BinaryPath"
try {
$currentVersionOutput = & $BinaryPath --version 2>$null
$currentVersion = $null
if ($currentVersionOutput) {
$match = [regex]::Match(($currentVersionOutput | Out-String), "(\d+\.\d+\.\d+)")
if ($match.Success) {
$currentVersion = $match.Groups[1].Value
}
}
if ($currentVersion -and $currentVersion -eq $version) {
Write-Step "Installed version ($currentVersion) is already up to date"
exit 0
}
if ($currentVersion) {
Write-Step "Replacing installed version $currentVersion with $version"
} else {
Write-Step "Replacing existing shaka binary with version $version"
}
} catch {
Write-Step "Replacing existing shaka binary with version $version"
}
}
$tempRoot = Join-Path ([System.IO.Path]::GetTempPath()) ("shaka-install-" + [System.Guid]::NewGuid().ToString("N"))
$archivePath = Join-Path $tempRoot $assetName
$checksumPath = Join-Path $tempRoot "sha256sums.txt"
$extractDir = Join-Path $tempRoot "extract"
New-Item -ItemType Directory -Path $tempRoot -Force | Out-Null
New-Item -ItemType Directory -Path $extractDir -Force | Out-Null
try {
Write-Step "Downloading release archive"
Invoke-WebRequest -Uri $asset.browser_download_url -OutFile $archivePath -Headers @{ "User-Agent" = "shaka-installer" }
Write-Step "Downloading checksum file"
Invoke-WebRequest -Uri $checksumAsset.browser_download_url -OutFile $checksumPath -Headers @{ "User-Agent" = "shaka-installer" }
Write-Step "Verifying archive checksum"
$checksumLines = Get-Content -Path $checksumPath
$checksumLine = $checksumLines | Where-Object { $_ -match ("\s+" + [regex]::Escape($assetName) + "$") } | Select-Object -First 1
if (-not $checksumLine) {
Fail "Could not find checksum entry for $assetName"
}
$expectedHash = ($checksumLine -split "\s+")[0].Trim().ToLowerInvariant()
if (-not $expectedHash) {
Fail "Checksum entry for $assetName is invalid"
}
$actualHash = Get-Sha256 -Path $archivePath
if ($expectedHash -ne $actualHash) {
Fail "Checksum mismatch for $assetName (expected $expectedHash, got $actualHash)"
}
Write-Step "Extracting archive"
if (Test-Path $extractDir) {
Remove-Item -Path $extractDir -Recurse -Force -ErrorAction SilentlyContinue
}
New-Item -ItemType Directory -Path $extractDir -Force | Out-Null
[System.IO.Compression.ZipFile]::ExtractToDirectory($archivePath, $extractDir)
$newBinary = Get-ChildItem -Path $extractDir -Recurse -File | Where-Object { $_.Name -eq "shaka.exe" } | Select-Object -First 1
if (-not $newBinary) {
Fail "Extracted archive does not contain shaka.exe"
}
Write-Step "Installing binary to $BinaryPath"
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
Move-Item -Path $newBinary.FullName -Destination $BinaryPath -Force
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
$pathEntries = @()
if ($userPath) {
$pathEntries = $userPath -split ";"
}
$inPath = $pathEntries | Where-Object { $_.TrimEnd("\\") -ieq $InstallDir.TrimEnd("\\") }
if (-not $inPath) {
Write-Step "Adding install directory to user PATH"
$newUserPath = if ([string]::IsNullOrWhiteSpace($userPath)) { $InstallDir } else { "$userPath;$InstallDir" }
[Environment]::SetEnvironmentVariable("Path", $newUserPath, "User")
} else {
Write-Step "Install directory already exists in user PATH"
}
if (-not (($env:Path -split ";") | Where-Object { $_.TrimEnd("\\") -ieq $InstallDir.TrimEnd("\\") })) {
$env:Path = "$InstallDir;$env:Path"
Write-Step "Updated PATH for current session"
}
Write-Step "Installation complete"
Write-Host "[shaka-install] Installed: $BinaryPath"
Write-Host "[shaka-install] Verify with: shaka --version"
} finally {
if (Test-Path $tempRoot) {
Remove-Item -Path $tempRoot -Recurse -Force -ErrorAction SilentlyContinue
}
}