|
| 1 | +# Vix.cpp installer (Windows PowerShell) |
| 2 | +# Usage: |
| 3 | +# irm https://vixcpp.com/install.ps1 | iex |
| 4 | +# Optional: |
| 5 | +# $env:VIX_VERSION="v1.20.1" |
| 6 | +# $env:VIX_INSTALL_DIR="$env:LOCALAPPDATA\Vix\bin" |
| 7 | +# $env:VIX_REPO="vixcpp/vix" |
| 8 | + |
| 9 | +$ErrorActionPreference = "Stop" |
| 10 | +$ProgressPreference = "SilentlyContinue" |
| 11 | + |
| 12 | +function Info($msg) { Write-Host "vix install: $msg" } |
| 13 | +function Die($msg) { throw "vix install: $msg" } |
| 14 | + |
| 15 | +$Repo = if ($env:VIX_REPO) { $env:VIX_REPO } else { "vixcpp/vix" } |
| 16 | +$Version = if ($env:VIX_VERSION) { $env:VIX_VERSION } else { "latest" } |
| 17 | +$InstallDir = if ($env:VIX_INSTALL_DIR) { $env:VIX_INSTALL_DIR } else { Join-Path $env:LOCALAPPDATA "Vix\bin" } |
| 18 | +$BinName = "vix.exe" |
| 19 | + |
| 20 | +function Resolve-LatestTag([string]$repo) { |
| 21 | + # Robust way: call GitHub API (no auth needed for low volume) |
| 22 | + $api = "https://api.github.com/repos/$repo/releases/latest" |
| 23 | + try { |
| 24 | + $resp = Invoke-RestMethod -Uri $api -Headers @{ "User-Agent" = "vix-installer" } |
| 25 | + if (-not $resp.tag_name) { Die "could not resolve latest tag. Set VIX_VERSION=vX.Y.Z" } |
| 26 | + return $resp.tag_name |
| 27 | + } catch { |
| 28 | + Die "could not resolve latest tag (GitHub API). Set VIX_VERSION=vX.Y.Z" |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +$Tag = if ($Version -eq "latest") { Resolve-LatestTag $Repo } else { $Version } |
| 33 | + |
| 34 | +# Detect arch (prefer OS bitness + ARM check) |
| 35 | +$archRaw = $env:PROCESSOR_ARCHITECTURE |
| 36 | +$Arch = switch -Regex ($archRaw) { |
| 37 | + "AMD64" { "x86_64"; break } |
| 38 | + "^ARM" { "aarch64"; break } |
| 39 | + default { Die "unsupported arch: $archRaw" } |
| 40 | +} |
| 41 | + |
| 42 | +$Asset = "vix-windows-$Arch.zip" |
| 43 | +$BaseUrl = "https://github.com/$Repo/releases/download/$Tag" |
| 44 | +$UrlBin = "$BaseUrl/$Asset" |
| 45 | +$UrlSha = "$UrlBin.sha256" |
| 46 | + |
| 47 | +Info "repo=$Repo version=$Tag arch=$Arch" |
| 48 | +Info "install_dir=$InstallDir" |
| 49 | + |
| 50 | +# Temp dir unique |
| 51 | +$TmpDir = Join-Path ([System.IO.Path]::GetTempPath()) ("vix-" + [System.Guid]::NewGuid().ToString("N")) |
| 52 | +New-Item -ItemType Directory -Force -Path $TmpDir | Out-Null |
| 53 | +try { |
| 54 | + $ZipPath = Join-Path $TmpDir $Asset |
| 55 | + $ShaPath = Join-Path $TmpDir ($Asset + ".sha256") |
| 56 | + |
| 57 | + Info "downloading: $UrlBin" |
| 58 | + Invoke-WebRequest -Uri $UrlBin -OutFile $ZipPath |
| 59 | + |
| 60 | + # SHA256 verification policy: |
| 61 | + # - If sha256 file exists -> MUST verify and match. |
| 62 | + # - If sha256 missing -> warn (optionally you can hard-fail; currently warn). |
| 63 | + Info "trying sha256 verification..." |
| 64 | + $shaOk = $false |
| 65 | + try { |
| 66 | + Invoke-WebRequest -Uri $UrlSha -OutFile $ShaPath |
| 67 | + |
| 68 | + $first = (Get-Content -LiteralPath $ShaPath -TotalCount 1).Trim() |
| 69 | + if (-not $first) { Die "invalid sha256 file" } |
| 70 | + |
| 71 | + # Accept: |
| 72 | + # 1) "<sha> file" |
| 73 | + # 2) "SHA256 (file) = <sha>" |
| 74 | + $expected = $null |
| 75 | + if ($first -match "^[0-9a-fA-F]{64}") { |
| 76 | + $expected = ($first -split "\s+")[0] |
| 77 | + } elseif ($first -match "=\s*([0-9a-fA-F]{64})\s*$") { |
| 78 | + $expected = $Matches[1] |
| 79 | + } |
| 80 | + if (-not $expected) { Die "invalid sha256 format" } |
| 81 | + |
| 82 | + $actual = (Get-FileHash -Algorithm SHA256 -LiteralPath $ZipPath).Hash |
| 83 | + if ($expected.ToLower() -ne $actual.ToLower()) { Die "sha256 mismatch" } |
| 84 | + |
| 85 | + $shaOk = $true |
| 86 | + Info "sha256 ok" |
| 87 | + } catch { |
| 88 | + Info "sha256 file not found (skipping)" |
| 89 | + } |
| 90 | + |
| 91 | + # Extract to temp first, then move only vix.exe (avoids zip path layout issues) |
| 92 | + $ExtractDir = Join-Path $TmpDir "extract" |
| 93 | + New-Item -ItemType Directory -Force -Path $ExtractDir | Out-Null |
| 94 | + Expand-Archive -LiteralPath $ZipPath -DestinationPath $ExtractDir -Force |
| 95 | + |
| 96 | + # Find vix.exe anywhere in archive |
| 97 | + $ExeCandidate = Get-ChildItem -LiteralPath $ExtractDir -Recurse -File -Filter $BinName | Select-Object -First 1 |
| 98 | + if (-not $ExeCandidate) { Die "archive does not contain $BinName" } |
| 99 | + |
| 100 | + New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null |
| 101 | + $Exe = Join-Path $InstallDir $BinName |
| 102 | + Copy-Item -LiteralPath $ExeCandidate.FullName -Destination $Exe -Force |
| 103 | + |
| 104 | + Info "installed to $Exe" |
| 105 | + |
| 106 | + # Add to user PATH (idempotent + exact segment check) |
| 107 | + $userPath = [Environment]::GetEnvironmentVariable("Path", "User") |
| 108 | + if (-not $userPath) { $userPath = "" } |
| 109 | + |
| 110 | + $segments = $userPath -split ";" | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne "" } |
| 111 | + $already = $false |
| 112 | + foreach ($s in $segments) { |
| 113 | + if ([string]::Equals($s.TrimEnd("\"), $InstallDir.TrimEnd("\"), [System.StringComparison]::OrdinalIgnoreCase)) { |
| 114 | + $already = $true |
| 115 | + break |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if (-not $already) { |
| 120 | + $newPath = ($segments + $InstallDir) -join ";" |
| 121 | + [Environment]::SetEnvironmentVariable("Path", $newPath, "User") |
| 122 | + Info "added to PATH (restart your terminal)" |
| 123 | + } else { |
| 124 | + Info "PATH already contains install_dir" |
| 125 | + } |
| 126 | + |
| 127 | + # Quick check |
| 128 | + try { |
| 129 | + $ver = & $Exe --version 2>$null |
| 130 | + if ($ver) { Info "version: $ver" } |
| 131 | + } catch { } |
| 132 | + |
| 133 | + Info "done" |
| 134 | +} |
| 135 | +finally { |
| 136 | + Remove-Item -LiteralPath $TmpDir -Recurse -Force -ErrorAction SilentlyContinue | Out-Null |
| 137 | +} |
0 commit comments