<# .SYNOPSIS Install prebuilt `senate.exe` and `senated.exe` binaries on Windows. .DESCRIPTION Resolves the latest (or specified) release through the Senate website, which proxies GitHub Releases through its own edge, downloads the Windows archive and SHA256SUMS.txt, verifies the archive's SHA-256 checksum (failing closed), and installs `senate.exe` and `senated.exe` into `$InstallDir` (default: `%LOCALAPPDATA%\Programs\senate\bin`). Environment variables: SENATE_VERSION Release tag to install (default: latest) SENATE_INSTALL_DIR Destination directory SENATE_BASE_URL Website origin (default: https://senate.elata.ai) SENATE_DOWNLOAD_BASE_URL Directory with the release archives + SHA256SUMS.txt SENATE_GITHUB_FALLBACK 1 (default) retry from GitHub on failure, 0 never SENATE_REPO GitHub repository (default: oops-rs/senate) .EXAMPLE irm https://senate.elata.ai/install.ps1 | iex .EXAMPLE & ([scriptblock]::Create((irm https://senate.elata.ai/install.ps1))) -Version v0.15.0 #> [CmdletBinding()] param( [string]$Version = $env:SENATE_VERSION, [string]$InstallDir = $(if ($env:SENATE_INSTALL_DIR) { $env:SENATE_INSTALL_DIR } else { Join-Path $env:LOCALAPPDATA "Programs\senate\bin" }), [string]$BaseUrl = $(if ($env:SENATE_BASE_URL) { $env:SENATE_BASE_URL } else { "https://senate.elata.ai" }), [string]$DownloadBaseUrl = $env:SENATE_DOWNLOAD_BASE_URL, [string]$Repo = $(if ($env:SENATE_REPO) { $env:SENATE_REPO } else { "oops-rs/senate" }), [switch]$GitHub, [switch]$NoFallback, [switch]$DryRun ) Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' # Invoke-WebRequest's progress bar slows downloads dramatically on Windows PowerShell 5.1. $ProgressPreference = 'SilentlyContinue' try { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 } catch { } $BaseUrl = $BaseUrl.TrimEnd('/') $Fallback = -not $NoFallback -and ($env:SENATE_GITHUB_FALLBACK -ne '0') $TagPattern = '^v\d+\.\d+\.\d+(-[0-9A-Za-z][0-9A-Za-z.-]*)?$' function Write-Info([string]$Message) { Write-Host "senate-install: $Message" } function Write-Warn([string]$Message) { Write-Warning "senate-install: $Message" } function Resolve-Target { $arch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString().ToLowerInvariant() switch ($arch) { 'x64' { return 'x86_64-pc-windows-msvc' } default { throw "Unsupported Windows architecture: $arch (currently only x86_64-pc-windows-msvc is published)" } } } function Resolve-LatestTagFromWebsite { try { $body = Invoke-RestMethod -Uri "$BaseUrl/releases/latest" -Headers @{ "User-Agent" = "senate-install" } -UseBasicParsing $tag = ([string]$body).Trim() if ($tag -match $TagPattern) { return $tag } } catch { } return $null } function Resolve-LatestTagFromGitHub([string]$Repository) { try { $req = [System.Net.HttpWebRequest]::Create("https://github.com/$Repository/releases/latest") $req.AllowAutoRedirect = $false $req.UserAgent = "senate-install" $resp = $null try { $resp = $req.GetResponse() } catch [System.Net.WebException] { $resp = $_.Exception.Response } if ($resp) { try { $loc = [string]$resp.Headers["Location"] if ($loc -match '/releases/tag/([^/?#]+)$') { $tag = $Matches[1] if ($tag -match $TagPattern) { return $tag } } } finally { $resp.Close() } } } catch { } try { $release = Invoke-RestMethod -Uri "https://api.github.com/repos/$Repository/releases/latest" -Headers @{ "User-Agent" = "senate-install" } -UseBasicParsing $tag = [string]$release.tag_name if ($tag -match $TagPattern) { return $tag } } catch { } return $null } function Get-Release([string]$Base, [string]$ArchivePath, [string]$SumsPath, [string]$ArchiveName) { Remove-Item -Force -ErrorAction SilentlyContinue $ArchivePath, $SumsPath Invoke-WebRequest -Uri "$Base/$ArchiveName" -OutFile $ArchivePath -UseBasicParsing -Headers @{ "User-Agent" = "senate-install" } Invoke-WebRequest -Uri "$Base/SHA256SUMS.txt" -OutFile $SumsPath -UseBasicParsing -Headers @{ "User-Agent" = "senate-install" } } $target = Resolve-Target if ([string]::IsNullOrWhiteSpace($Version)) { $Version = $null if (-not $GitHub) { $Version = Resolve-LatestTagFromWebsite if (-not $Version -and $Fallback) { Write-Warn "could not resolve the latest release from $BaseUrl; trying GitHub" } } if (-not $Version -and ($GitHub -or $Fallback)) { $Version = Resolve-LatestTagFromGitHub -Repository $Repo } if (-not $Version) { throw "Could not resolve the latest Senate release; pass -Version vX.Y.Z explicitly." } } else { if (-not $Version.StartsWith('v')) { $Version = "v$Version" } if ($Version -notmatch $TagPattern) { throw "Invalid version: $Version (expected vX.Y.Z)" } } $archiveName = "senate-$Version-$target.zip" $githubBase = "https://github.com/$Repo/releases/download/$Version" if ($DownloadBaseUrl) { $primaryBase = $DownloadBaseUrl.TrimEnd('/') } elseif ($GitHub) { $primaryBase = $githubBase } else { $primaryBase = "$BaseUrl/releases/download/$Version" } $canFallBack = $Fallback -and ($primaryBase -ne $githubBase) if ($DryRun) { Write-Info "dry run:" Write-Host " version: $Version" Write-Host " target: $target" Write-Host " archive: $primaryBase/$archiveName" Write-Host " checksums: $primaryBase/SHA256SUMS.txt" if ($canFallBack) { Write-Host " fallback: $githubBase/$archiveName" } else { Write-Host " fallback: (none)" } Write-Host " install dir: $InstallDir" return } $tmpDir = Join-Path ([System.IO.Path]::GetTempPath()) ("senate-install-" + [System.Guid]::NewGuid().ToString("N")) New-Item -ItemType Directory -Force -Path $tmpDir | Out-Null try { $archivePath = Join-Path $tmpDir $archiveName $sumsPath = Join-Path $tmpDir "SHA256SUMS.txt" Write-Info "downloading $archiveName ($Version)..." try { Get-Release -Base $primaryBase -ArchivePath $archivePath -SumsPath $sumsPath -ArchiveName $archiveName } catch { if (-not $canFallBack) { throw "Failed to download $primaryBase/${archiveName}: $($_.Exception.Message)" } Write-Warn "download from $primaryBase failed; retrying from GitHub Releases" Get-Release -Base $githubBase -ArchivePath $archivePath -SumsPath $sumsPath -ArchiveName $archiveName } # Fail closed: a missing entry or a mismatch aborts before anything is installed. $expected = $null foreach ($line in Get-Content -Path $sumsPath) { if ($line -match '^([0-9a-fA-F]{64})\s+\*?(.+)$') { if ($Matches[2].Trim() -eq $archiveName) { $expected = $Matches[1].ToLowerInvariant() break } } } if (-not $expected) { throw "Checksum entry for $archiveName not found in SHA256SUMS.txt" } $actual = (Get-FileHash -Path $archivePath -Algorithm SHA256).Hash.ToLowerInvariant() if ($actual -ne $expected) { throw "SHA-256 mismatch for ${archiveName}: expected $expected, got $actual (refusing to install)" } Write-Info "verified SHA-256 ($actual)" Expand-Archive -Path $archivePath -DestinationPath $tmpDir -Force $unpacked = Join-Path $tmpDir "senate-$Version-$target" $senateSrc = Join-Path $unpacked "senate.exe" $senatedSrc = Join-Path $unpacked "senated.exe" if (-not (Test-Path $senateSrc) -or -not (Test-Path $senatedSrc)) { throw "Archive $archiveName did not contain expected senate.exe and senated.exe binaries" } # Install atomically via sibling temp files. New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null foreach ($bin in @("senate.exe", "senated.exe")) { $src = Join-Path $unpacked $bin $dst = Join-Path $InstallDir $bin $tmp = Join-Path $InstallDir ".$bin.new.$PID" Copy-Item -Path $src -Destination $tmp -Force Move-Item -Path $tmp -Destination $dst -Force } $userPath = [Environment]::GetEnvironmentVariable("Path", "User") $parts = @() if ($userPath) { $parts = $userPath -split ';' | Where-Object { $_ } } if ($parts -notcontains $InstallDir) { $newPath = (@($parts) + $InstallDir) -join ';' [Environment]::SetEnvironmentVariable("Path", $newPath, "User") $env:Path = "$InstallDir;$env:Path" Write-Info "added $InstallDir to your User PATH (restart open terminals to pick it up)" } Write-Info "installed into ${InstallDir}:" & (Join-Path $InstallDir "senate.exe") --version & (Join-Path $InstallDir "senated.exe") --version Write-Host "" Write-Host "Next steps:" Write-Host " senate setup init --local --person " Write-Host " senate setup install --yes" Write-Host " senated --machine" } finally { Remove-Item -Recurse -Force $tmpDir -ErrorAction SilentlyContinue }