|
| 1 | +#Requires -Version 5.1 |
| 2 | +<# |
| 3 | +.SYNOPSIS |
| 4 | + Install supply-guard on Windows. |
| 5 | +.DESCRIPTION |
| 6 | + Downloads the latest supply-guard release, verifies its checksum, |
| 7 | + and installs it to a directory in your PATH. |
| 8 | +.PARAMETER Version |
| 9 | + Specific version to install (e.g. "v0.3.1"). Defaults to latest. |
| 10 | +.PARAMETER InstallDir |
| 11 | + Installation directory. Defaults to "$env:LOCALAPPDATA\supply-guard". |
| 12 | +.EXAMPLE |
| 13 | + irm https://raw.githubusercontent.com/AlbertoMZCruz/supply-guard/main/install.ps1 | iex |
| 14 | +.EXAMPLE |
| 15 | + .\install.ps1 -Version v0.3.1 |
| 16 | +#> |
| 17 | +param( |
| 18 | + [string]$Version, |
| 19 | + [string]$InstallDir |
| 20 | +) |
| 21 | + |
| 22 | +$ErrorActionPreference = "Stop" |
| 23 | +$Repo = "AlbertoMZCruz/supply-guard" |
| 24 | +$Binary = "supply-guard.exe" |
| 25 | + |
| 26 | +function Write-Info { param($Msg) Write-Host " $Msg" -ForegroundColor Blue } |
| 27 | +function Write-Ok { param($Msg) Write-Host " $Msg" -ForegroundColor Green } |
| 28 | +function Write-Err { param($Msg) Write-Host " Error: $Msg" -ForegroundColor Red; exit 1 } |
| 29 | + |
| 30 | +function Get-Arch { |
| 31 | + switch ($env:PROCESSOR_ARCHITECTURE) { |
| 32 | + "AMD64" { return "amd64" } |
| 33 | + "ARM64" { return "arm64" } |
| 34 | + default { Write-Err "Unsupported architecture: $env:PROCESSOR_ARCHITECTURE" } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +function Get-LatestVersion { |
| 39 | + try { |
| 40 | + $release = Invoke-RestMethod -Uri "https://api.github.com/repos/$Repo/releases/latest" -UseBasicParsing |
| 41 | + return $release.tag_name |
| 42 | + } catch { |
| 43 | + Write-Err "Could not fetch latest version: $_" |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +$Arch = Get-Arch |
| 48 | + |
| 49 | +if (-not $InstallDir) { |
| 50 | + $InstallDir = Join-Path $env:LOCALAPPDATA "supply-guard" |
| 51 | +} |
| 52 | + |
| 53 | +if (-not $Version) { |
| 54 | + Write-Info "Fetching latest version..." |
| 55 | + $Version = Get-LatestVersion |
| 56 | +} |
| 57 | + |
| 58 | +if (-not $Version) { |
| 59 | + Write-Err "Could not determine latest version. Use -Version to specify." |
| 60 | +} |
| 61 | + |
| 62 | +$VersionNum = $Version.TrimStart("v") |
| 63 | +$Filename = "supply-guard_${VersionNum}_windows_${Arch}.zip" |
| 64 | +$DownloadUrl = "https://github.com/$Repo/releases/download/$Version/$Filename" |
| 65 | +$ChecksumUrl = "https://github.com/$Repo/releases/download/$Version/checksums.txt" |
| 66 | + |
| 67 | +Write-Info "Downloading supply-guard $Version for windows/$Arch..." |
| 68 | + |
| 69 | +$TmpDir = Join-Path ([System.IO.Path]::GetTempPath()) ("supply-guard-install-" + [System.Guid]::NewGuid().ToString("N").Substring(0, 8)) |
| 70 | +New-Item -ItemType Directory -Path $TmpDir -Force | Out-Null |
| 71 | + |
| 72 | +try { |
| 73 | + $ZipPath = Join-Path $TmpDir $Filename |
| 74 | + $ChecksumPath = Join-Path $TmpDir "checksums.txt" |
| 75 | + |
| 76 | + Invoke-WebRequest -Uri $DownloadUrl -OutFile $ZipPath -UseBasicParsing |
| 77 | + Invoke-WebRequest -Uri $ChecksumUrl -OutFile $ChecksumPath -UseBasicParsing |
| 78 | + |
| 79 | + Write-Info "Verifying checksum..." |
| 80 | + |
| 81 | + $ExpectedLine = Get-Content $ChecksumPath | Where-Object { $_ -match $Filename } |
| 82 | + if (-not $ExpectedLine) { |
| 83 | + Write-Err "Could not find checksum for $Filename in checksums.txt" |
| 84 | + } |
| 85 | + $ExpectedHash = ($ExpectedLine -split "\s+")[0] |
| 86 | + |
| 87 | + $ActualHash = (Get-FileHash -Path $ZipPath -Algorithm SHA256).Hash.ToLower() |
| 88 | + |
| 89 | + if ($ExpectedHash -ne $ActualHash) { |
| 90 | + Write-Err "Checksum mismatch! Expected: $ExpectedHash, Got: $ActualHash. The download may have been tampered with." |
| 91 | + } |
| 92 | + Write-Ok "Checksum verified" |
| 93 | + |
| 94 | + Write-Info "Extracting..." |
| 95 | + Expand-Archive -Path $ZipPath -DestinationPath $TmpDir -Force |
| 96 | + |
| 97 | + $BinarySrc = Join-Path $TmpDir "supply-guard.exe" |
| 98 | + if (-not (Test-Path $BinarySrc)) { |
| 99 | + Write-Err "Binary not found in archive. Contents: $(Get-ChildItem $TmpDir -Name)" |
| 100 | + } |
| 101 | + |
| 102 | + if (-not (Test-Path $InstallDir)) { |
| 103 | + New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null |
| 104 | + } |
| 105 | + |
| 106 | + $BinaryDest = Join-Path $InstallDir $Binary |
| 107 | + Move-Item -Path $BinarySrc -Destination $BinaryDest -Force |
| 108 | + |
| 109 | + $currentPath = [Environment]::GetEnvironmentVariable("Path", "User") |
| 110 | + if ($currentPath -notlike "*$InstallDir*") { |
| 111 | + Write-Info "Adding $InstallDir to user PATH..." |
| 112 | + [Environment]::SetEnvironmentVariable("Path", "$currentPath;$InstallDir", "User") |
| 113 | + $env:Path = "$env:Path;$InstallDir" |
| 114 | + } |
| 115 | + |
| 116 | + Write-Ok "supply-guard $Version installed to $BinaryDest" |
| 117 | + Write-Host "" |
| 118 | + & $BinaryDest version |
| 119 | + |
| 120 | + if ($currentPath -notlike "*$InstallDir*") { |
| 121 | + Write-Host "" |
| 122 | + Write-Info "Restart your terminal for PATH changes to take effect." |
| 123 | + } |
| 124 | +} finally { |
| 125 | + Remove-Item -Path $TmpDir -Recurse -Force -ErrorAction SilentlyContinue |
| 126 | +} |
0 commit comments