# Installs tx0 on Windows: https://tx0.io/docs/install/
#
# irm https://tx0.io/install.ps1 | iex
#
# $env:TX0_VERSION a version to install, such as 0.1.0 (default: the latest release)
# $env:TX0_INSTALL_DIR where to put tx0.exe (default: ~\.local\bin)
# $env:TX0_DOWNLOAD_BASE a mirror to download from instead of https://tx0.io/download, laid
# out the same way: /latest holds the latest version, and the
# archives and their .sha256 files are in //
#
# The archive is checked against its published SHA-256 sum before anything is installed.
# Nothing is written outside the install directory, and no administrator rights are needed.
$ErrorActionPreference = 'Stop'
$ProgressPreference = 'SilentlyContinue'
& {
# Where the archives come from. tx0.io keeps them in // and names the latest
# version in /latest. The GitHub releases keep them in /v/ and name the
# latest one through the GitHub API; set $defaultBase to $githubBase to use them.
$siteBase = 'https://tx0.io/download'
$githubBase = 'https://github.com/tx0io/tx0/releases/download'
$defaultBase = $siteBase
$docs = 'https://tx0.io/docs/install/'
# The system's architecture, not the process's: an x64 PowerShell under emulation on
# Windows on ARM still installs the native build.
$arch = $env:PROCESSOR_ARCHITEW6432
if (-not $arch) { $arch = $env:PROCESSOR_ARCHITECTURE }
$target = if ($arch -eq 'ARM64') { 'aarch64-pc-windows-msvc' } else { 'x86_64-pc-windows-msvc' }
$base = if ($env:TX0_DOWNLOAD_BASE) { $env:TX0_DOWNLOAD_BASE.TrimEnd('/') } else { $defaultBase }
$github = $base -eq $githubBase
if ($env:TX0_VERSION) {
$version = $env:TX0_VERSION.TrimStart('v')
} elseif ($github) {
$latest = Invoke-RestMethod -Uri 'https://api.github.com/repos/tx0io/tx0/releases/latest' -Headers @{ 'User-Agent' = 'tx0-install' }
$version = "$($latest.tag_name)".TrimStart('v')
} else {
try {
$version = "$(Invoke-RestMethod -Uri "$base/latest" -UseBasicParsing)".Trim().TrimStart('v')
} catch {
throw "tx0: could not read the latest version from $base/latest"
}
}
if ($version -notmatch '^[0-9]') { throw "tx0: could not find the latest version at $base" }
$tag = "v$version"
$from = if ($github) { "$base/$tag" } else { "$base/$version" }
$name = "tx0-$tag-$target"
$dir = if ($env:TX0_INSTALL_DIR) { $env:TX0_INSTALL_DIR } else { Join-Path $HOME '.local\bin' }
$tmp = Join-Path ([System.IO.Path]::GetTempPath()) ("tx0-install-" + [System.Guid]::NewGuid())
New-Item -ItemType Directory -Path $tmp | Out-Null
try {
Write-Host "tx0: downloading tx0 $version for $target"
$zip = Join-Path $tmp "$name.zip"
$sum = Join-Path $tmp "$name.zip.sha256"
try {
Invoke-WebRequest -Uri "$from/$name.zip" -OutFile $zip -UseBasicParsing
} catch {
throw "tx0: could not download $name.zip from $from; tx0 $version may not be built for $target. Other ways to install: $docs"
}
try {
Invoke-WebRequest -Uri "$from/$name.zip.sha256" -OutFile $sum -UseBasicParsing
} catch {
throw "tx0: could not download the SHA-256 sum of $name.zip"
}
$want = ((Get-Content -Raw $sum).Trim() -split '\s+')[0].ToLowerInvariant()
$got = (Get-FileHash -Algorithm SHA256 -Path $zip).Hash.ToLowerInvariant()
if (-not $want -or $want -ne $got) {
throw "tx0: the SHA-256 sum of $name.zip does not match the published one; nothing was installed"
}
Expand-Archive -Path $zip -DestinationPath $tmp -Force
New-Item -ItemType Directory -Path $dir -Force | Out-Null
Copy-Item -Path (Join-Path $tmp "$name\tx0.exe") -Destination (Join-Path $dir 'tx0.exe') -Force
Write-Host "tx0: installed tx0 $version to $(Join-Path $dir 'tx0.exe')"
$path = [Environment]::GetEnvironmentVariable('Path', 'User')
if (-not (($path -split ';') -contains $dir)) {
Write-Host "tx0: $dir is not on your PATH. To add it for your user, run:"
Write-Host ""
Write-Host " [Environment]::SetEnvironmentVariable('Path', `"$dir;`" + [Environment]::GetEnvironmentVariable('Path', 'User'), 'User')"
Write-Host ""
Write-Host "and open a new terminal."
}
} finally {
Remove-Item -Recurse -Force $tmp -ErrorAction SilentlyContinue
}
}