Open History to see the last commit
1# Installs the T3 Code CLI from a GitHub Release archive on Windows. Needs
2# only PowerShell 5.1+; no Node, npm, or compiler.
3#
4# irm https://t3.codes/install.ps1 | iex
5#
6# Environment:
7# T3CODE_CHANNEL release train to follow: stable, nightly, or preview
8# (default: stable; preview is a maintainers' test train)
9# T3CODE_VERSION exact version to install (overrides T3CODE_CHANNEL)
10# T3CODE_HOME T3 home directory (default: ~\.t3)
11# T3CODE_INSTALL_BIN_DIR where t3.exe is linked (default: ~\.local\bin)
12# T3CODE_RELEASE_BASE_URL mirror for releases/download (default: GitHub)
13#
14# The archive is unpacked into $T3CODE_HOME\runtime\versions\<version>, the
15# same layout `t3 service install` uses, so the service reuses this download.
16$ErrorActionPreference = "Stop"
17[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
18
19$repo = "pingdotgg/t3code"
20$baseUrl = if ($env:T3CODE_RELEASE_BASE_URL) { $env:T3CODE_RELEASE_BASE_URL.TrimEnd("/") } else { "https://github.com/$repo/releases/download" }
21$t3Home = if ($env:T3CODE_HOME) { $env:T3CODE_HOME } else { Join-Path $HOME ".t3" }
22$binDir = if ($env:T3CODE_INSTALL_BIN_DIR) { $env:T3CODE_INSTALL_BIN_DIR } else { Join-Path $HOME ".local\bin" }
23
24function Fail([string] $message) {
25 Write-Error "t3 install: $message"
26 exit 1
27}
28
29# PROCESSOR_ARCHITEW6432 reports the real machine when a 32-bit PowerShell
30# runs under WOW64; RuntimeInformation needs .NET 4.7.1+, which 5.1 hosts
31# may lack.
32$rawArch = if ($env:PROCESSOR_ARCHITEW6432) { $env:PROCESSOR_ARCHITEW6432 } else { $env:PROCESSOR_ARCHITECTURE }
33$arch = switch ($rawArch) {
34 "AMD64" { "x64" }
35 "ARM64" { "arm64" }
36 default { Fail "unsupported architecture $rawArch" }
37}
38
39$channel = if ($env:T3CODE_CHANNEL) { $env:T3CODE_CHANNEL } else { "stable" }
40$version = $env:T3CODE_VERSION
41if (-not $version) {
42 # Tags are v<semver>; the channel is the prerelease identifier, or none for
43 # stable. Only tags of the requested train are considered, so a stable
44 # install can never pick up a nightly or preview build by accident.
45 $tagPattern = switch ($channel) {
46 "stable" { '^v\d+\.\d+\.\d+$' }
47 "nightly" { '^v\d+\.\d+\.\d+-nightly\.\d+\.\d+$' }
48 "preview" { '^v\d+\.\d+\.\d+-preview\.\d+\.\d+$' }
49 default { Fail "T3CODE_CHANNEL must be stable, nightly, or preview" }
50 }
51 $releases = Invoke-RestMethod -Uri "https://api.github.com/repos/$repo/releases?per_page=100" -Headers @{ "User-Agent" = "t3-install" }
52 $tag = ($releases | Where-Object { -not $_.draft -and $_.tag_name -match $tagPattern } | Select-Object -First 1).tag_name
53 if (-not $tag) { Fail "could not find a $channel release; set T3CODE_VERSION" }
54 $version = $tag.Substring(1)
55}
56if ($version -match '-preview\.') {
57 Write-Warning "t3 $version is a preview build. Preview builds are cut by maintainers from unreleased branches to exercise the release pipeline. They can be broken, receive no fixes, and are never offered as updates. Set T3CODE_CHANNEL=stable (the default) for a supported build."
58 if ($channel -ne "preview" -and -not $env:T3CODE_VERSION) {
59 Fail "refusing a preview build that was not explicitly requested"
60 }
61}
62
63$stem = "t3-$version-win32-$arch"
64$archive = "$stem.zip"
65$versionsDir = Join-Path $t3Home "runtime\versions"
66$targetDir = Join-Path $versionsDir $version
67$marker = Join-Path $targetDir ".install-complete"
68
69if ((Test-Path $marker) -and ((Get-Content $marker -Raw).Trim() -eq $version)) {
70 Write-Host "t3 $version is already installed at $targetDir"
71} else {
72 New-Item -ItemType Directory -Force -Path $versionsDir | Out-Null
73 $staging = Join-Path $versionsDir (".staging-" + [System.IO.Path]::GetRandomFileName())
74 New-Item -ItemType Directory -Path $staging | Out-Null
75 try {
76 Write-Host "Downloading $archive..."
77 try {
78 Invoke-WebRequest -Uri "$baseUrl/v$version/SHA256SUMS" -OutFile (Join-Path $staging "SHA256SUMS") -UseBasicParsing
79 } catch {
80 $status = $_.Exception.Response.StatusCode.value__
81 if ($status -eq 404) {
82 Fail "t3 $version has no release archive for win32-$arch; releases before the self-contained CLI can only be installed with 'npm install -g t3@$version'"
83 }
84 throw
85 }
86 Invoke-WebRequest -Uri "$baseUrl/v$version/$archive" -OutFile (Join-Path $staging $archive) -UseBasicParsing
87
88 $expected = (Get-Content (Join-Path $staging "SHA256SUMS") | Where-Object { $_ -match "\s\*?$([regex]::Escape($archive))$" } | Select-Object -First 1)
89 if (-not $expected) { Fail "$archive is not listed in SHA256SUMS" }
90 $expected = ($expected -split "\s+")[0].ToLowerInvariant()
91 $actual = (Get-FileHash -Algorithm SHA256 (Join-Path $staging $archive)).Hash.ToLowerInvariant()
92 if ($actual -ne $expected) { Fail "checksum mismatch for $archive" }
93
94 Expand-Archive -Path (Join-Path $staging $archive) -DestinationPath $staging -Force
95 # The archive wraps everything in one directory named after its stem.
96 Get-ChildItem (Join-Path $staging $stem) | Move-Item -Destination $staging
97 Remove-Item (Join-Path $staging $stem), (Join-Path $staging $archive), (Join-Path $staging "SHA256SUMS") -Recurse -Force
98
99 & (Join-Path $staging "t3.exe") --version | Out-Null
100 if ($LASTEXITCODE -ne 0) { Fail "the downloaded executable does not run" }
101 Set-Content -Path (Join-Path $staging ".install-complete") -Value $version -NoNewline
102
103 if (Test-Path $targetDir) { Remove-Item $targetDir -Recurse -Force }
104 Move-Item $staging $targetDir
105 } catch {
106 if (Test-Path $staging) { Remove-Item $staging -Recurse -Force }
107 throw
108 }
109}
110
111New-Item -ItemType Directory -Force -Path $binDir | Out-Null
112$shim = Join-Path $binDir "t3.cmd"
113# UTF-8 without a BOM: cmd.exe reads the shim as-is, and ASCII would corrupt
114# non-ASCII characters in the user's home path.
115[System.IO.File]::WriteAllText($shim, "@echo off`r`n`"$(Join-Path $targetDir 't3.exe')`" %*", (New-Object System.Text.UTF8Encoding $false))
116Write-Host "Installed t3 $version"
117Write-Host " $shim -> $(Join-Path $targetDir 't3.exe')"
118if (($env:PATH -split ";") -notcontains $binDir) {
119 Write-Host "Add $binDir to your PATH to run t3."
120}