# Installs the newest release of toolchain for the user running it, on # Windows: # # irm https://solidplanssoftware.com/install.ps1 | iex # # It reads the manifest the downloads host publishes, takes the # executable built for this machine, checks it against the sha256 the # manifest records, and puts it where `toolchain` itself keeps an # installed application, so `toolchain update` finds it afterwards: # # %LOCALAPPDATA%\hyperfoundry\toolchain\apps\toolchain\toolchain\\ the release # %LOCALAPPDATA%\hyperfoundry\toolchain\apps\toolchain\toolchain\current the version in use # %LOCALAPPDATA%\hyperfoundry\toolchain\bin\toolchain.cmd its launcher # # `bin` is added to the user's PATH. Nothing needs elevation. # # Everything runs inside one script block, so nothing it sets outlives it # in the session it was piped into, and a refusal is thrown rather than # closing that session. & { $ErrorActionPreference = 'Stop' $ProgressPreference = 'SilentlyContinue' $downloads = 'https://downloads.solidplanssoftware.com' $manifestPath = 'public/toolchain/toolchain/latest.json' $namespace = 'toolchain' $application = 'toolchain' # Windows PowerShell on an older machine offers nothing newer than # TLS 1.0 unless asked. [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 try { $manifest = Invoke-RestMethod -UseBasicParsing -Uri "$downloads/$manifestPath" } catch { throw "install: the manifest at $downloads/$manifestPath could not be read: $($_.Exception.Message)" } $supported = @() if ($manifest.platforms) { $supported = @($manifest.platforms.PSObject.Properties.Name) } $builtFor = if ($supported) { $supported -join ', ' } else { 'nothing yet' } # This machine as a release names it, `-`. if ($env:OS -ne 'Windows_NT') { throw "install: this script installs toolchain on Windows. On Linux or macOS, run https://solidplanssoftware.com/install.sh instead. toolchain is built for: $builtFor." } $cpu = if ($env:PROCESSOR_ARCHITEW6432) { $env:PROCESSOR_ARCHITEW6432 } else { $env:PROCESSOR_ARCHITECTURE } $cpu = switch ($cpu) { 'AMD64' { 'x64' } 'ARM64' { 'arm64' } default { "$cpu".ToLowerInvariant() } } $platform = "windows-$cpu" if ($supported -notcontains $platform) { throw "install: toolchain is not built for $platform. It is built for: $builtFor." } $entry = $manifest.platforms.$platform $version = [string]$manifest.version $path = [string]$entry.path $expected = ([string]$entry.sha256).ToLowerInvariant() if ($version -notmatch '^[0-9A-Za-z][0-9A-Za-z.+-]*$') { throw 'install: the manifest names no usable version.' } if ($path -notmatch '^public/' -or $path -match '\.\.|\\|/$') { throw "install: the manifest names no download for $platform." } if ($expected -notmatch '^[0-9a-f]{64}$') { throw "install: the manifest records no sha256 for $platform." } $name = $path.Substring($path.LastIndexOf('/') + 1) $local = if ($env:LOCALAPPDATA) { $env:LOCALAPPDATA } else { Join-Path $HOME 'AppData\Local' } $root = Join-Path (Join-Path $local 'hyperfoundry') 'toolchain' $appHome = Join-Path $root (Join-Path 'apps' (Join-Path $namespace $application)) $bin = Join-Path $root 'bin' $target = Join-Path $appHome $version $work = Join-Path ([IO.Path]::GetTempPath()) "toolchain-install-$PID" # Unpacked beside where it will live and moved into place whole, so a # run cut short leaves nothing that reads as an installed release. $staging = Join-Path $appHome "$version.$PID.partial" try { foreach ($directory in $work, $staging) { if (Test-Path -LiteralPath $directory) { Remove-Item -LiteralPath $directory -Recurse -Force } } foreach ($directory in $work, $appHome, $bin, $staging) { New-Item -ItemType Directory -Force -Path $directory | Out-Null } Write-Host "Downloading toolchain $version for $platform" $download = Join-Path $work $name try { Invoke-WebRequest -UseBasicParsing -Uri "$downloads/$path" -OutFile $download } catch { throw "install: $downloads/$path could not be downloaded: $($_.Exception.Message)" } $actual = (Get-FileHash -LiteralPath $download -Algorithm SHA256).Hash.ToLowerInvariant() if ($actual -ne $expected) { throw "install: the download is not the file the manifest records (sha256 $actual, expected $expected). Nothing was installed." } $executable = $null if ([IO.Path]::GetExtension($name) -eq '.zip') { try { Expand-Archive -LiteralPath $download -DestinationPath $staging -Force } catch { throw "install: $name could not be unpacked: $($_.Exception.Message)" } $stem = [IO.Path]::GetFileNameWithoutExtension($name) foreach ($suffix in '', '.exe', '.bin') { if (Test-Path -LiteralPath (Join-Path $staging "$stem$suffix") -PathType Leaf) { $executable = "$stem$suffix" break } } if (-not $executable) { throw "install: $name holds no executable named for it, so there is nothing to run." } } else { Copy-Item -LiteralPath $download -Destination (Join-Path $staging $name) $executable = $name } if (Test-Path -LiteralPath $target) { try { Remove-Item -LiteralPath $target -Recurse -Force } catch { throw "install: $target could not be replaced. Close toolchain and run this again." } } Move-Item -LiteralPath $staging -Destination $target } finally { foreach ($directory in $work, $staging) { if (Test-Path -LiteralPath $directory) { Remove-Item -LiteralPath $directory -Recurse -Force -ErrorAction SilentlyContinue } } } $runAs = $executable -replace '\.(exe|bin)$', '' $launcher = Join-Path $bin "$runAs.cmd" $plain = New-Object Text.UTF8Encoding $false [IO.File]::WriteAllText( $launcher, "@echo off`r`n`"$(Join-Path $target $executable)`" %*`r`nexit /b %ERRORLEVEL%`r`n", $plain) [IO.File]::WriteAllText((Join-Path $appHome 'current'), "$version`n", $plain) foreach ($other in Get-ChildItem -LiteralPath $appHome -Directory) { if ($other.FullName -eq $target) { continue } try { Remove-Item -LiteralPath $other.FullName -Recurse -Force } catch { Write-Host "An earlier version could not be removed: $($other.FullName)" } } # The user's PATH as the registry holds it, unexpanded, so a # `%NAME%` another entry is written with is kept as written. $environment = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey('Environment', $true) try { $held = [string]$environment.GetValue( 'Path', '', [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames) $entries = @($held -split ';' | Where-Object { $_ }) $named = $entries | Where-Object { $_.TrimEnd('\') -ieq $bin } if (-not $named) { $kind = if ($held -match '%') { [Microsoft.Win32.RegistryValueKind]::ExpandString } else { [Microsoft.Win32.RegistryValueKind]::String } $environment.SetValue('Path', (@($entries) + $bin) -join ';', $kind) # Writing the registry tells no running program. Setting and # clearing a variable through .NET sends the notice that makes # a terminal opened from now on read the new PATH. $notice = "toolchain-install-$PID" [Environment]::SetEnvironmentVariable($notice, '1', 'User') [Environment]::SetEnvironmentVariable($notice, [NullString]::Value, 'User') Write-Host "Added $bin to your PATH." } } finally { $environment.Close() } if (@($env:Path -split ';' | Where-Object { $_.TrimEnd('\') -ieq $bin }).Count -eq 0) { $env:Path = "$env:Path;$bin" } & $launcher --version | Out-Null if ($LASTEXITCODE -ne 0) { throw "install: toolchain $version was installed at $target and did not start." } Write-Host "Installed toolchain ${version}: $launcher" Write-Host "Run it as: $runAs" }