forked from JuliusBrussee/caveman-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
98 lines (79 loc) · 3.25 KB
/
Copy pathinstall.ps1
File metadata and controls
98 lines (79 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#Requires -Version 5.1
<#
.SYNOPSIS
Cave installer for Windows.
.DESCRIPTION
Downloads cave-windows-x64.zip, extracts to %LOCALAPPDATA%\cave\<version>\, writes
cave.cmd and caveman.cmd shims into %LOCALAPPDATA%\cave\bin\, and prepends that to
user PATH.
Env knobs:
CAVE_VERSION Tag to install (default: latest)
CAVE_PREFIX Install root (default: %LOCALAPPDATA%\cave)
CAVE_BASE_URL Override the download base (used by smoke tests)
#>
$ErrorActionPreference = 'Stop'
$Repo = 'JuliusBrussee/caveman-cli'
$KeepVersions = 2
$arch = $env:PROCESSOR_ARCHITECTURE
if ($arch -ne 'AMD64') {
throw "Unsupported architecture: $arch (only AMD64 builds are published)"
}
$Triple = 'windows-x64'
$Version = $env:CAVE_VERSION
if (-not $Version) {
$release = Invoke-RestMethod -UseBasicParsing -Uri "https://api.github.com/repos/$Repo/releases/latest"
$Version = $release.tag_name
if (-not $Version) { throw 'Could not resolve latest version from GitHub' }
}
$Prefix = $env:CAVE_PREFIX
if (-not $Prefix) { $Prefix = Join-Path $env:LOCALAPPDATA 'cave' }
$BaseUrl = $env:CAVE_BASE_URL
if (-not $BaseUrl) { $BaseUrl = "https://github.com/$Repo/releases/download/$Version" }
$Zip = "cave-$Triple.zip"
$Url = "$BaseUrl/$Zip"
$LibDir = Join-Path $Prefix 'lib'
$BinDir = Join-Path $Prefix 'bin'
$VerDir = Join-Path $LibDir $Version
Write-Host "Installing cave $Version ($Triple) into $Prefix"
New-Item -ItemType Directory -Force -Path $LibDir, $BinDir | Out-Null
$tmp = New-Item -ItemType Directory -Force -Path (Join-Path $env:TEMP "cave-install-$([guid]::NewGuid())")
try {
$tmpZip = Join-Path $tmp.FullName $Zip
Write-Host " downloading $Url"
Invoke-WebRequest -UseBasicParsing -Uri $Url -OutFile $tmpZip
if (Test-Path $VerDir) { Remove-Item -Recurse -Force $VerDir }
New-Item -ItemType Directory -Force -Path $VerDir | Out-Null
Write-Host ' extracting'
Expand-Archive -Path $tmpZip -DestinationPath $VerDir -Force
} finally {
Remove-Item -Recurse -Force $tmp.FullName -ErrorAction SilentlyContinue
}
# cave.cmd shim — invokes cave.exe directly so process.execPath resolves inside $VerDir
$exePath = Join-Path $VerDir 'cave.exe'
if (-not (Test-Path $exePath)) { throw "Extracted archive is missing cave.exe at $exePath" }
$shim = @"
@echo off
"%~dp0..\lib\$Version\cave.exe" %*
"@
Set-Content -Path (Join-Path $BinDir 'cave.cmd') -Value $shim -Encoding ASCII
Set-Content -Path (Join-Path $BinDir 'caveman.cmd') -Value $shim -Encoding ASCII
# Prune older versions
Get-ChildItem -Path $LibDir -Directory |
Sort-Object LastWriteTime -Descending |
Select-Object -Skip $KeepVersions |
ForEach-Object {
Write-Host " pruning old version: $($_.Name)"
Remove-Item -Recurse -Force $_.FullName
}
# User PATH update
$userPath = [Environment]::GetEnvironmentVariable('Path', 'User')
$parts = if ($userPath) { $userPath -split ';' } else { @() }
if (-not ($parts -contains $BinDir)) {
$newPath = if ($userPath) { "$BinDir;$userPath" } else { $BinDir }
[Environment]::SetEnvironmentVariable('Path', $newPath, 'User')
Write-Host ''
Write-Host "Added $BinDir to user PATH. Open a new shell for it to take effect."
}
Write-Host ''
Write-Host "Installed: $VerDir"
& (Join-Path $BinDir 'cave.cmd') --version