-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
201 lines (170 loc) · 6.04 KB
/
install.ps1
File metadata and controls
201 lines (170 loc) · 6.04 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
param(
[string]$InstallDir,
[switch]$SkipConfig,
[string]$BaseUrl,
[switch]$Help
)
$ErrorActionPreference = "Stop"
$Repo = "skooch/codebase-memory-zig"
if ($Help) {
Write-Host "Usage: ./install.ps1 [-InstallDir <path>] [-SkipConfig] [-BaseUrl <url-or-path>]"
Write-Host ""
Write-Host "Downloads a packaged cbm release archive, verifies checksums and the release"
Write-Host "manifest when available,"
Write-Host "installs the binary, and optionally runs 'cbm install -y'."
exit 0
}
function Get-DefaultInstallDir {
if ($IsWindows) {
return Join-Path $env:LOCALAPPDATA "Programs\cbm"
}
return Join-Path $HOME ".local/bin"
}
function Get-ArchiveInfo {
$arch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString().ToLowerInvariant()
if ($IsWindows) {
return @{
Archive = "cbm-windows-amd64.zip"
Binary = "cbm.exe"
ArchiveType = "zip"
}
}
if ($IsMacOS) {
if ($arch -eq "arm64") {
return @{ Archive = "cbm-darwin-arm64.tar.gz"; Binary = "cbm"; ArchiveType = "tar.gz" }
}
return @{ Archive = "cbm-darwin-amd64.tar.gz"; Binary = "cbm"; ArchiveType = "tar.gz" }
}
if ($IsLinux) {
if ($arch -eq "arm64") {
return @{ Archive = "cbm-linux-arm64.tar.gz"; Binary = "cbm"; ArchiveType = "tar.gz" }
}
return @{ Archive = "cbm-linux-amd64.tar.gz"; Binary = "cbm"; ArchiveType = "tar.gz" }
}
throw "Unsupported platform"
}
function Get-Sha256 {
param([string]$Path)
return (Get-FileHash -Path $Path -Algorithm SHA256).Hash.ToLowerInvariant()
}
function Test-ReleaseManifest {
param(
[string]$ManifestPath,
[string]$ArchiveName,
[string]$ExpectedSha
)
$manifest = Get-Content -Raw -LiteralPath $ManifestPath | ConvertFrom-Json
if (-not $manifest.artifacts) {
throw "Release manifest missing artifacts list"
}
$matches = @($manifest.artifacts | Where-Object { $_.archive -eq $ArchiveName })
if ($matches.Count -ne 1) {
throw "Release manifest did not contain exactly one entry for $ArchiveName"
}
$artifact = $matches[0]
if (-not $artifact.sha256) {
throw "Release manifest entry for $ArchiveName is missing sha256"
}
if ($artifact.sha256.ToLowerInvariant() -ne $ExpectedSha.ToLowerInvariant()) {
throw "Release manifest checksum mismatch for $ArchiveName"
}
}
function Resolve-BaseUrl {
param([string]$Value)
if ($Value) {
return $Value.TrimEnd('/', '\')
}
return "https://github.com/$Repo/releases/latest/download"
}
function Copy-Or-Download {
param(
[string]$Base,
[string]$Name,
[string]$OutputPath
)
if ($Base.StartsWith("file://")) {
$uri = [System.Uri]::new(($Base.TrimEnd('/') + "/" + $Name))
Copy-Item -LiteralPath $uri.LocalPath -Destination $OutputPath -Force
return
}
if (Test-Path -LiteralPath $Base) {
Copy-Item -LiteralPath (Join-Path $Base $Name) -Destination $OutputPath -Force
return
}
Invoke-WebRequest -Uri ($Base.TrimEnd('/') + "/" + $Name) -OutFile $OutputPath -UseBasicParsing
}
if (-not $InstallDir) {
$InstallDir = Get-DefaultInstallDir
}
$BaseUrl = Resolve-BaseUrl $BaseUrl
$archiveInfo = Get-ArchiveInfo
$archiveName = $archiveInfo.Archive
$binaryName = $archiveInfo.Binary
$archiveType = $archiveInfo.ArchiveType
$tmpDir = Join-Path ([System.IO.Path]::GetTempPath()) ("cbm-install-" + [System.Guid]::NewGuid().ToString("N"))
New-Item -ItemType Directory -Path $tmpDir -Force | Out-Null
try {
$archivePath = Join-Path $tmpDir $archiveName
Write-Host "Downloading $archiveName..."
Copy-Or-Download -Base $BaseUrl -Name $archiveName -OutputPath $archivePath
$checksumsPath = Join-Path $tmpDir "checksums.txt"
try {
Copy-Or-Download -Base $BaseUrl -Name "checksums.txt" -OutputPath $checksumsPath
} catch {
$checksumsPath = $null
}
$manifestPath = Join-Path $tmpDir "release-manifest.json"
try {
Copy-Or-Download -Base $BaseUrl -Name "release-manifest.json" -OutputPath $manifestPath
} catch {
$manifestPath = $null
}
$actual = Get-Sha256 $archivePath
if ($checksumsPath -and (Test-Path $checksumsPath)) {
$line = Get-Content $checksumsPath | Where-Object { $_ -match (" " + [regex]::Escape($archiveName) + "$") }
if ($line) {
$expected = ($line -split '\s+')[0].ToLowerInvariant()
if ($expected -ne $actual) {
throw "Checksum mismatch for $archiveName"
}
Write-Host "Checksum verified."
}
}
if ($manifestPath -and (Test-Path $manifestPath)) {
Test-ReleaseManifest -ManifestPath $manifestPath -ArchiveName $archiveName -ExpectedSha $actual
Write-Host "Release manifest verified."
}
if ($archiveType -eq "zip") {
Expand-Archive -Path $archivePath -DestinationPath $tmpDir -Force
} else {
& tar -xzf $archivePath -C $tmpDir
if ($LASTEXITCODE -ne 0) {
throw "tar extraction failed"
}
}
$downloadedBinary = Join-Path $tmpDir $binaryName
if (-not (Test-Path $downloadedBinary)) {
throw "Archive did not contain $binaryName"
}
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
$dest = Join-Path $InstallDir $binaryName
Copy-Item -LiteralPath $downloadedBinary -Destination $dest -Force
$version = & $dest --version 2>&1
if ($LASTEXITCODE -ne 0) {
throw "Installed binary failed to run"
}
Write-Host "Installed: $version"
if ($SkipConfig) {
Write-Host "Skipping agent configuration (-SkipConfig)"
} else {
Write-Host "Configuring coding agents..."
try {
& $dest install -y
} catch {
Write-Warning "Agent configuration failed; run 'cbm install -y' manually"
}
}
Write-Host "Done."
} finally {
Remove-Item -Recurse -Force $tmpDir -ErrorAction SilentlyContinue
}