|
| 1 | +param( |
| 2 | + [string]$Repo = $env:NYTGAMES_CLI_REPO, |
| 3 | + [string]$Version = $env:NYTGAMES_CLI_VERSION, |
| 4 | + [string]$InstallDir = $env:NYTGAMES_CLI_INSTALL_DIR |
| 5 | +) |
| 6 | + |
| 7 | +$ErrorActionPreference = "Stop" |
| 8 | + |
| 9 | +if ([string]::IsNullOrWhiteSpace($Repo)) { |
| 10 | + $Repo = "ph8n/nytgames-cli" |
| 11 | +} |
| 12 | + |
| 13 | +$headers = @{ "User-Agent" = "nytgames-cli-installer" } |
| 14 | + |
| 15 | +if ([string]::IsNullOrWhiteSpace($InstallDir)) { |
| 16 | + $base = $env:LOCALAPPDATA |
| 17 | + if ([string]::IsNullOrWhiteSpace($base)) { |
| 18 | + $base = Join-Path $HOME "AppData\\Local" |
| 19 | + } |
| 20 | + $InstallDir = Join-Path $base "nytgames-cli\\bin" |
| 21 | +} |
| 22 | + |
| 23 | +$arch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture |
| 24 | +switch ($arch) { |
| 25 | + "X64" { $arch = "amd64" } |
| 26 | + "Arm64" { throw "unsupported architecture: arm64 (Windows builds are amd64 only)" } |
| 27 | + default { throw "unsupported architecture: $arch" } |
| 28 | +} |
| 29 | + |
| 30 | +if ([string]::IsNullOrWhiteSpace($Version)) { |
| 31 | + $release = Invoke-RestMethod -Headers $headers "https://api.github.com/repos/$Repo/releases/latest" |
| 32 | + $tag = $release.tag_name |
| 33 | + if (-not $tag) { |
| 34 | + throw "failed to resolve latest version from GitHub for $Repo" |
| 35 | + } |
| 36 | + $Version = $tag.TrimStart("v") |
| 37 | +} else { |
| 38 | + $Version = $Version.TrimStart("v") |
| 39 | +} |
| 40 | + |
| 41 | +$asset = "nytgames-cli_${Version}_windows_${arch}.zip" |
| 42 | +$baseUrl = "https://github.com/$Repo/releases/download/v$Version" |
| 43 | +$url = "$baseUrl/$asset" |
| 44 | +$checksumsUrl = "$baseUrl/checksums.txt" |
| 45 | + |
| 46 | +$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ("nytgames-cli-" + [Guid]::NewGuid().ToString("N")) |
| 47 | +New-Item -ItemType Directory -Path $tempDir | Out-Null |
| 48 | + |
| 49 | +try { |
| 50 | + $assetPath = Join-Path $tempDir $asset |
| 51 | + Write-Host "Downloading $url" |
| 52 | + Invoke-WebRequest -Uri $url -OutFile $assetPath -UseBasicParsing -Headers $headers |
| 53 | + |
| 54 | + $checksumsPath = Join-Path $tempDir "checksums.txt" |
| 55 | + $expected = $null |
| 56 | + try { |
| 57 | + Invoke-WebRequest -Uri $checksumsUrl -OutFile $checksumsPath -UseBasicParsing -Headers $headers |
| 58 | + $expectedLine = Get-Content $checksumsPath | |
| 59 | + Where-Object { $_ -match ("\s+" + [regex]::Escape($asset) + "$") } | |
| 60 | + Select-Object -First 1 |
| 61 | + if ($expectedLine) { |
| 62 | + $expected = ($expectedLine -split "\s+")[0].ToLower() |
| 63 | + } |
| 64 | + } catch { |
| 65 | + $expected = $null |
| 66 | + } |
| 67 | + if ($expected) { |
| 68 | + $actual = (Get-FileHash -Algorithm SHA256 $assetPath).Hash.ToLower() |
| 69 | + if ($expected -ne $actual) { |
| 70 | + throw "checksum mismatch for $asset`nexpected: $expected`nactual: $actual" |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + Expand-Archive -Path $assetPath -DestinationPath $tempDir -Force |
| 75 | + |
| 76 | + $binPath = Join-Path $tempDir "nytgames.exe" |
| 77 | + if (-not (Test-Path $binPath)) { |
| 78 | + $bin = Get-ChildItem -Path $tempDir -Recurse -Filter "nytgames.exe" | Select-Object -First 1 |
| 79 | + if ($bin) { |
| 80 | + $binPath = $bin.FullName |
| 81 | + } else { |
| 82 | + throw "failed to find nytgames.exe in archive" |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null |
| 87 | + Copy-Item -Force $binPath (Join-Path $InstallDir "nytgames.exe") |
| 88 | + |
| 89 | + Write-Host "Installed nytgames.exe to $InstallDir" |
| 90 | + $userPath = [System.Environment]::GetEnvironmentVariable("PATH", "User") |
| 91 | + if ($userPath -and $userPath -notlike "*$InstallDir*") { |
| 92 | + Write-Warning "$InstallDir is not on your PATH. Add it to run 'nytgames' from any shell." |
| 93 | + } |
| 94 | +} finally { |
| 95 | + if (Test-Path $tempDir) { |
| 96 | + Remove-Item -Recurse -Force $tempDir |
| 97 | + } |
| 98 | +} |
0 commit comments