1# install.ps1 - installs a released 012 binary on Windows. Served at the2# docs site's root with the release archives under /releases/; see3# docs/getting-started/install.md.4#5# irm https://012.dev.site/install.ps1 | iex6#7# It picks the archive for this architecture, checks its SHA256 against8# the release's SHA256SUMS, puts 012.exe in9# %LOCALAPPDATA%\Programs\012\bin and adds that folder to the user's10# PATH. It never asks for administrator rights.11#12# $env:O12_VERSION a release other than the latest, like v1.2.313# $env:O12_INSTALL_DIR another folder for 012.exe14# $env:O12_BASE_URL the site serving /releases/ (https://012.dev.site)1516$ErrorActionPreference = 'Stop'17$ProgressPreference = 'SilentlyContinue'1819function Install-012 {20 $base = if ($env:O12_BASE_URL) { $env:O12_BASE_URL } else { 'https://012.dev.site' }21 $version = if ($env:O12_VERSION) { $env:O12_VERSION } else { 'latest' }22 if ($version -match '^[0-9]') { $version = "v$version" }23 $dir = if ($env:O12_INSTALL_DIR) { $env:O12_INSTALL_DIR } else { Join-Path $env:LOCALAPPDATA 'Programs\012\bin' }2425 $arch = if ($env:PROCESSOR_ARCHITEW6432) { $env:PROCESSOR_ARCHITEW6432 } else { $env:PROCESSOR_ARCHITECTURE }26 switch ($arch) {27 'AMD64' { $arch = 'amd64' }28 'ARM64' { $arch = 'arm64' }29 default { throw "012 install: no release for $arch; build from source: go install src.rest/cat/012/cmd/012@latest" }30 }3132 $tmp = Join-Path ([IO.Path]::GetTempPath()) ("012-install-" + [Guid]::NewGuid())33 New-Item -ItemType Directory -Path $tmp | Out-Null34 try {35 $url = "$base/releases/$version"36 $sums = Join-Path $tmp 'SHA256SUMS'37 Invoke-WebRequest -UseBasicParsing -Uri "$url/SHA256SUMS" -OutFile $sums3839 # A line of SHA256SUMS: the digest, two spaces, 012_1.2.3_windows_amd64.zip.40 $line = Get-Content $sums | Where-Object { $_ -match "^([0-9a-f]{64}) (012_\S+_windows_$arch\.zip)$" } | Select-Object -First 141 if (-not $line) { throw "012 install: release $version has no archive for windows/$arch" }42 $null = $line -match "^([0-9a-f]{64}) (012_\S+_windows_$arch\.zip)$"43 $want = $Matches[1]44 $file = $Matches[2]45 $name = $file -replace '\.zip$', ''46 $got_version = 'v' + ($name -replace '^012_', '' -replace "_windows_$arch$", '')4748 Write-Host "012 install: downloading $url/$file"49 $zip = Join-Path $tmp $file50 Invoke-WebRequest -UseBasicParsing -Uri "$url/$file" -OutFile $zip51 $got = (Get-FileHash -Algorithm SHA256 -Path $zip).Hash.ToLowerInvariant()52 if ($got -ne $want) { throw "012 install: $file has SHA256 $got, SHA256SUMS says $want; nothing installed" }53 Write-Host "012 install: SHA256 checked against SHA256SUMS: $want"5455 Expand-Archive -Path $zip -DestinationPath $tmp -Force56 $exe = Join-Path $tmp "$name\012.exe"57 if (-not (Test-Path $exe)) { throw "012 install: $file holds no 012.exe" }58 New-Item -ItemType Directory -Force -Path $dir | Out-Null59 Copy-Item -Force $exe (Join-Path $dir '012.exe')60 Write-Host "012 install: installed 012 $got_version (windows/$arch) as $(Join-Path $dir '012.exe')"6162 $userPath = [Environment]::GetEnvironmentVariable('Path', 'User')63 $parts = @()64 if ($userPath) { $parts = $userPath -split ';' | Where-Object { $_ } }65 if ($parts -notcontains $dir) {66 [Environment]::SetEnvironmentVariable('Path', (($parts + $dir) -join ';'), 'User')67 $env:Path = "$env:Path;$dir"68 Write-Host "012 install: added $dir to your user PATH; new terminals find 012"69 } else {70 Write-Host "012 install: run 012 to start"71 }72 } finally {73 Remove-Item -Recurse -Force $tmp -ErrorAction SilentlyContinue74 }75}7677Install-012