feat(cli): install tht as a system command
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
$ErrorActionPreference = 'Stop'
|
||||
|
||||
$repositoryRoot = Split-Path -Parent $PSScriptRoot
|
||||
$installer = Join-Path $repositoryRoot 'scripts/install-tht.ps1'
|
||||
if (-not (Test-Path -LiteralPath $installer)) {
|
||||
throw "installer is missing: $installer"
|
||||
}
|
||||
|
||||
$isWindowsHost = $env:OS -eq 'Windows_NT'
|
||||
if (-not $isWindowsHost) {
|
||||
$installerText = Get-Content -LiteralPath $installer -Raw
|
||||
foreach ($requiredText in @('THT_INSTALL_DIRECTORY', 'LOCALAPPDATA', 'SetEnvironmentVariable', 'build-tht.sh', 'Move-Item')) {
|
||||
if (-not $installerText.Contains($requiredText)) {
|
||||
throw "Windows installer is missing required behavior: $requiredText"
|
||||
}
|
||||
}
|
||||
Write-Output 'PowerShell installer behavior test skipped: requires Windows.'
|
||||
exit 0
|
||||
}
|
||||
|
||||
$temporaryRoot = Join-Path ([System.IO.Path]::GetTempPath()) ("tht installer test {0}" -f [guid]::NewGuid())
|
||||
$installDirectory = Join-Path $temporaryRoot 'command directory'
|
||||
$artifact = Join-Path $temporaryRoot 'tht.exe'
|
||||
$userPathStore = Join-Path $temporaryRoot 'user-path.txt'
|
||||
New-Item -ItemType Directory -Path $temporaryRoot -Force | Out-Null
|
||||
try {
|
||||
$program = @'
|
||||
using System;
|
||||
public static class Program {
|
||||
public static void Main(string[] args) {
|
||||
if (args.Length == 1 && args[0] == "--help") { Console.WriteLine("Usage: tht"); return; }
|
||||
if (args.Length == 1 && args[0] == "version") { Console.WriteLine("tht test version"); return; }
|
||||
Environment.Exit(2);
|
||||
}
|
||||
}
|
||||
'@
|
||||
Add-Type -TypeDefinition $program -OutputAssembly $artifact -OutputType ConsoleApplication
|
||||
[System.IO.File]::WriteAllText($userPathStore, 'C:\Existing Bin')
|
||||
|
||||
$env:THT_INSTALL_DIRECTORY = $installDirectory
|
||||
$env:THT_BUILD_ARTIFACT = $artifact
|
||||
$env:THT_BUILD_ARTIFACT_SHA256 = (Get-FileHash -Algorithm SHA256 -LiteralPath $artifact).Hash.ToLowerInvariant()
|
||||
$env:THT_USER_PATH_FILE = $userPathStore
|
||||
& $installer
|
||||
|
||||
$installed = Join-Path $installDirectory 'tht.exe'
|
||||
if (-not (Test-Path -LiteralPath $installed)) { throw 'installer did not create tht.exe' }
|
||||
if ((& $installed version) -ne 'tht test version') { throw 'installed tht.exe did not return version' }
|
||||
$firstBytes = [System.IO.File]::ReadAllBytes($installed)
|
||||
|
||||
& $installer
|
||||
if ((& $installed version) -ne 'tht test version') { throw 'installer was not idempotent' }
|
||||
if (-not ([System.IO.File]::ReadAllBytes($installed).Length -eq $firstBytes.Length)) { throw 'replacement changed the unexpected binary' }
|
||||
|
||||
$storedPath = [System.IO.File]::ReadAllText($userPathStore)
|
||||
$segments = $storedPath -split ';' | Where-Object { $_ -ne '' }
|
||||
if ((@($segments | Where-Object { $_ -ieq $installDirectory }).Count) -ne 1) {
|
||||
throw 'installer did not preserve a single destination PATH entry'
|
||||
}
|
||||
if (-not ($segments | Where-Object { $_ -ieq 'C:\Existing Bin' })) { throw 'installer did not preserve existing user PATH entries' }
|
||||
Write-Output 'PowerShell tht installer tests passed.'
|
||||
}
|
||||
finally {
|
||||
Remove-Item Env:THT_INSTALL_DIRECTORY -ErrorAction SilentlyContinue
|
||||
Remove-Item Env:THT_BUILD_ARTIFACT -ErrorAction SilentlyContinue
|
||||
Remove-Item Env:THT_BUILD_ARTIFACT_SHA256 -ErrorAction SilentlyContinue
|
||||
Remove-Item Env:THT_USER_PATH_FILE -ErrorAction SilentlyContinue
|
||||
Remove-Item -LiteralPath $temporaryRoot -Recurse -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
Reference in New Issue
Block a user