# Installs the T3 Code CLI from a GitHub Release archive on Windows. Needs
# only PowerShell 5.1+; no Node, npm, or compiler.
#
# irm https://t3.codes/install.ps1 | iex
#
# Environment:
# T3CODE_CHANNEL release train to follow: stable, nightly, or preview
# (default: stable; preview is a maintainers' test train)
# T3CODE_VERSION exact version to install (overrides T3CODE_CHANNEL)
# T3CODE_HOME T3 home directory (default: ~\.t3)
# T3CODE_INSTALL_BIN_DIR where t3.exe is linked (default: ~\.local\bin)
# T3CODE_RELEASE_BASE_URL mirror for releases/download (default: GitHub)
#
# The archive is unpacked into $T3CODE_HOME\runtime\versions\<version>, the
# same layout `t3 service install` uses, so the service reuses this download.
$ErrorActionPreference = "Stop"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$repo = "pingdotgg/t3code"
$baseUrl = if ($env:T3CODE_RELEASE_BASE_URL) { $env:T3CODE_RELEASE_BASE_URL.TrimEnd("/") } else { "https://github.com/$repo/releases/download" }
$t3Home = if ($env:T3CODE_HOME) { $env:T3CODE_HOME } else { Join-Path $HOME ".t3" }
$binDir = if ($env:T3CODE_INSTALL_BIN_DIR) { $env:T3CODE_INSTALL_BIN_DIR } else { Join-Path $HOME ".local\bin" }
function Fail([string] $message) {
Write-Error "t3 install: $message"
exit 1
}
$terminal = -not [Console]::IsErrorRedirected -and $env:TERM -ne "dumb"
$interactive = $terminal -and $Host.UI.SupportsVirtualTerminal
if ($interactive) {
# Legacy code pages may support ANSI escapes but not the logo or bar glyphs.
$glyphs = -join [char[]](0x2588, 0x2580, 0x2584, 0x25A0, 0x00B7)
$encoding = [Console]::Error.Encoding
$interactive = $encoding.GetString($encoding.GetBytes($glyphs)) -eq $glyphs
}
$esc = [char]27
$reset = $bold = $muted = $accent = $green = ""
if ($interactive -and -not $env:NO_COLOR) {
$reset = "$esc[0m"; $bold = "$esc[1m"; $muted = "$esc[2m"
$accent = "$esc[94m"; $green = "$esc[32m"
}
function Step([string] $message) {
if ($interactive) { [Console]::Error.Write("`r$esc[2K $muted$message$reset") }
else { [Console]::Error.WriteLine(" $message") }
}
function Draw-Download([long] $bytes, [long] $total) {
if (-not $interactive) { return }
$columns = [Console]::WindowWidth
if ($columns -le 0) { $columns = 80 }
if ($total -gt 0 -and $columns -ge 9) {
$percent = [Math]::Min(100, [Math]::Floor($bytes * 100.0 / $total))
$width = [Math]::Min(32, $columns - 8)
$filled = [int][Math]::Floor($percent * $width / 100)
$bar = ([string][char]0x25A0) * $filled
$rest = ([string][char]0x00B7) * ($width - $filled)
$sizes = (" {0:F1} / {1:F1} MB" -f ($bytes / 1MB), ($total / 1MB))
if ($width + 7 + $sizes.Length -ge $columns) { $sizes = "" }
[Console]::Error.Write(("`r$esc[2K $accent$bar$reset$muted$rest$reset {0,3}%" -f $percent) + "$muted$sizes$reset")
} else {
$line = if ($columns -ge 32) { " Downloading {0:F1} MB" -f ($bytes / 1MB) } else { " {0:F1} MB" -f ($bytes / 1MB) }
[Console]::Error.Write("`r$esc[2K" + $line.Substring(0, [Math]::Min($line.Length, $columns - 1)))
}
}
function Fetch([string] $uri, [string] $destination, [switch] $progress) {
if (-not $progress -or -not $interactive) {
$ProgressPreference = if ($progress -and $terminal) { "Continue" } else { "SilentlyContinue" }
Invoke-WebRequest -Uri $uri -OutFile $destination -UseBasicParsing
return
}
Add-Type -AssemblyName System.Net.Http
$client = New-Object System.Net.Http.HttpClient
$response = $source = $file = $null
try {
$response = $client.GetAsync($uri, [System.Net.Http.HttpCompletionOption]::ResponseHeadersRead).GetAwaiter().GetResult()
$response.EnsureSuccessStatusCode() | Out-Null
$source = $response.Content.ReadAsStreamAsync().GetAwaiter().GetResult()
$file = [IO.File]::Create($destination)
$buffer = New-Object byte[] 65536
$bytes = 0L
$clock = [Diagnostics.Stopwatch]::StartNew()
Draw-Download 0 $response.Content.Headers.ContentLength
while (($count = $source.Read($buffer, 0, $buffer.Length)) -gt 0) {
$file.Write($buffer, 0, $count)
$bytes += $count
if ($clock.ElapsedMilliseconds -ge 100) {
Draw-Download $bytes $response.Content.Headers.ContentLength
$clock.Restart()
}
}
Draw-Download $bytes $bytes
[Console]::Error.WriteLine()
} finally {
if ($file) { $file.Dispose() }
if ($source) { $source.Dispose() }
if ($response) { $response.Dispose() }
$client.Dispose()
}
}
if ($interactive) {
# Block characters are constructed so this file also loads correctly in PowerShell 5.1.
$mark = @(
"########## ######## ",
" ### _##^ ",
" ### ####_ ",
" ### _ ###",
" ### #######^ "
)
[Console]::Error.WriteLine()
for ($i = 0; $i -lt $mark.Length; $i++) {
$row = $mark[$i].Replace('#', [char]0x2588).Replace('^', [char]0x2580).Replace('_', [char]0x2584)
$label = if ($i -eq 1) { " ${bold}T3 Code$reset" } elseif ($i -eq 2) { " ${muted}CLI installer$reset" } else { "" }
[Console]::Error.WriteLine(" $bold$row$reset$label")
}
[Console]::Error.WriteLine()
}
Step "Finding your release..."
# PROCESSOR_ARCHITEW6432 bypasses WOW64 emulation; 5.1 hosts can lack RuntimeInformation's required .NET version.
$rawArch = if ($env:PROCESSOR_ARCHITEW6432) { $env:PROCESSOR_ARCHITEW6432 } else { $env:PROCESSOR_ARCHITECTURE }
$arch = switch ($rawArch) {
"AMD64" { "x64" }
"ARM64" { "arm64" }
default { Fail "unsupported architecture $rawArch" }
}
$channel = if ($env:T3CODE_CHANNEL) { $env:T3CODE_CHANNEL } else { "stable" }
$version = $env:T3CODE_VERSION
if (-not $version) {
$tagPattern = switch ($channel) {
"stable" { '^v\d+\.\d+\.\d+