-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathinstall.ps1
More file actions
392 lines (335 loc) · 13.3 KB
/
install.ps1
File metadata and controls
392 lines (335 loc) · 13.3 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
param(
[string]$Repo = "microsoft/apm"
)
$ErrorActionPreference = "Stop"
$installRoot = Join-Path $env:LOCALAPPDATA "Programs\apm"
$binDir = Join-Path $installRoot "bin"
$releasesDir = Join-Path $installRoot "releases"
$assetName = "apm-windows-x86_64.zip"
# ---------------------------------------------------------------------------
# Helper functions
# ---------------------------------------------------------------------------
function Write-Info {
param([string]$Message)
Write-Host $Message -ForegroundColor Cyan
}
function Write-Success {
param([string]$Message)
Write-Host $Message -ForegroundColor Green
}
function Write-WarningText {
param([string]$Message)
Write-Host $Message -ForegroundColor Yellow
}
function Write-ErrorText {
param([string]$Message)
Write-Host $Message -ForegroundColor Red
}
function Get-AuthHeader {
if ($env:GITHUB_APM_PAT) {
return @{ Authorization = "token $($env:GITHUB_APM_PAT)" }
}
if ($env:GITHUB_TOKEN) {
return @{ Authorization = "token $($env:GITHUB_TOKEN)" }
}
return @{}
}
function Invoke-GitHubJson {
param(
[string]$Url,
[hashtable]$Headers
)
if ($Headers.Count -gt 0) {
return Invoke-RestMethod -Uri $Url -Headers $Headers
}
return Invoke-RestMethod -Uri $Url
}
function Add-ToUserPath {
param([string]$PathEntry)
$currentUserPath = [Environment]::GetEnvironmentVariable("Path", "User")
$userEntries = @()
if ($currentUserPath) {
$userEntries = $currentUserPath.Split(";", [System.StringSplitOptions]::RemoveEmptyEntries)
}
if ($userEntries -notcontains $PathEntry) {
$newUserPath = if ($currentUserPath) { "$PathEntry;$currentUserPath" } else { $PathEntry }
[Environment]::SetEnvironmentVariable("Path", $newUserPath, "User")
Write-Info "Added $PathEntry to your user PATH."
}
if (($env:Path -split ";") -notcontains $PathEntry) {
$env:Path = "$PathEntry;$env:Path"
}
}
function Test-PythonRequirement {
foreach ($cmd in @("python3", "python")) {
$exe = Get-Command $cmd -ErrorAction SilentlyContinue
if ($exe) {
try {
$verStr = & $cmd -c "import sys; print('.'.join(map(str, sys.version_info[:2])))" 2>$null
if ($verStr) {
$parts = $verStr.Split('.')
$major = [int]$parts[0]
$minor = [int]$parts[1]
if ($major -gt 3 -or ($major -eq 3 -and $minor -ge 9)) {
return $cmd
}
}
} catch {
# Ignore; try next candidate
}
}
}
return $null
}
function Install-ViaPip {
$pythonCmd = Test-PythonRequirement
if (-not $pythonCmd) {
Write-ErrorText "Python 3.9+ is not available — cannot fall back to pip."
return $false
}
Write-Info "Attempting installation via pip ($pythonCmd)..."
$pipCmd = $null
foreach ($candidate in @("pip3", "pip")) {
if (Get-Command $candidate -ErrorAction SilentlyContinue) {
$pipCmd = $candidate
break
}
}
if (-not $pipCmd) {
$pipCmd = "$pythonCmd -m pip"
}
try {
$pipArgs = "install --user apm-cli"
if ($pipCmd -like "* -m pip") {
$output = & $pythonCmd -m pip install --user apm-cli 2>&1
$pipExitCode = $LASTEXITCODE
$output | Write-Host
} else {
$output = & $pipCmd install --user apm-cli 2>&1
$pipExitCode = $LASTEXITCODE
$output | Write-Host
}
if ($pipExitCode -ne 0) {
Write-ErrorText "pip install failed (exit code $pipExitCode)."
return $false
}
} catch {
Write-ErrorText "pip install failed: $_"
return $false
}
# Verify apm is available after pip install
$apmExe = Get-Command apm -ErrorAction SilentlyContinue
if ($apmExe) {
$ver = & apm --version 2>$null
Write-Success "APM installed successfully via pip! Version: $ver"
Write-Info "Location: $($apmExe.Source)"
} else {
Write-WarningText "APM installed but not found in PATH."
Write-Host "You may need to add your Python user scripts directory to PATH."
}
return $true
}
function Write-ManualInstallHelp {
Write-Host ""
Write-Info "Manual installation options:"
Write-Host " 1. pip (recommended): pip install --user apm-cli"
Write-Host " 2. From source:"
Write-Host " git clone https://github.com/$Repo.git"
Write-Host " cd apm && uv sync && uv run pip install -e ."
Write-Host ""
Write-Host "Need help? Create an issue at: https://github.com/$Repo/issues"
}
# ---------------------------------------------------------------------------
# Banner
# ---------------------------------------------------------------------------
Write-Host ""
Write-Host "===========================================================" -ForegroundColor Blue
Write-Host " APM Installer " -ForegroundColor Blue
Write-Host " The NPM for AI-Native Development " -ForegroundColor Blue
Write-Host "===========================================================" -ForegroundColor Blue
Write-Host ""
# ---------------------------------------------------------------------------
# Stage 1 — Fetch release info (unauthenticated first, then authenticated)
# ---------------------------------------------------------------------------
Write-Info "Fetching latest release information..."
$release = $null
$headers = @{}
# Try unauthenticated first
try {
$release = Invoke-RestMethod -Uri "https://api.github.com/repos/$Repo/releases/latest"
} catch {
# Swallow — will try authenticated below
}
if (-not $release -or -not $release.tag_name) {
Write-Info "Unauthenticated request failed or returned no data. Retrying with authentication..."
$headers = Get-AuthHeader
if ($headers.Count -eq 0) {
Write-ErrorText "Repository may be private but no authentication token found."
Write-Host "Set GITHUB_APM_PAT or GITHUB_TOKEN and retry."
Write-ManualInstallHelp
exit 1
}
try {
$release = Invoke-GitHubJson -Url "https://api.github.com/repos/$Repo/releases/latest" -Headers $headers
} catch {
Write-ErrorText "Failed to fetch release information: $_"
Write-ManualInstallHelp
exit 1
}
}
if (-not $release.tag_name) {
Write-ErrorText "Could not determine the latest release tag."
Write-ManualInstallHelp
exit 1
}
$asset = $release.assets | Where-Object { $_.name -eq $assetName } | Select-Object -First 1
if (-not $asset) {
Write-ErrorText "Release $($release.tag_name) does not contain $assetName."
Write-ManualInstallHelp
exit 1
}
$tagName = $release.tag_name
Write-Success "Latest version: $tagName"
$releaseDir = Join-Path $releasesDir $tagName
$tempDir = Join-Path ([System.IO.Path]::GetTempPath()) ("apm-install-" + [System.Guid]::NewGuid().ToString("N"))
$zipPath = Join-Path $tempDir $assetName
New-Item -ItemType Directory -Force -Path $tempDir | Out-Null
New-Item -ItemType Directory -Force -Path $binDir | Out-Null
New-Item -ItemType Directory -Force -Path $releasesDir | Out-Null
try {
# ------------------------------------------------------------------
# Stage 2 — Download binary (3-stage fallback chain)
# ------------------------------------------------------------------
Write-Info "Downloading $assetName from $tagName..."
$downloadOk = $false
# 2a. Direct browser_download_url without auth
try {
Invoke-WebRequest -Uri $asset.browser_download_url -OutFile $zipPath -UseBasicParsing
$downloadOk = $true
Write-Success "Download successful"
} catch {
Write-WarningText "Unauthenticated download failed, retrying with authentication..."
}
# 2b. API asset URL with Accept: application/octet-stream (authenticated)
if (-not $downloadOk) {
if ($headers.Count -eq 0) { $headers = Get-AuthHeader }
if ($headers.Count -gt 0 -and $asset.url) {
try {
$apiHeaders = $headers.Clone()
$apiHeaders["Accept"] = "application/octet-stream"
Invoke-WebRequest -Uri $asset.url -Headers $apiHeaders -OutFile $zipPath -UseBasicParsing
$downloadOk = $true
Write-Success "Download successful via GitHub API"
} catch {
Write-WarningText "API download failed, trying direct URL with auth..."
}
}
}
# 2c. Direct browser_download_url with auth header
if (-not $downloadOk) {
if ($headers.Count -eq 0) { $headers = Get-AuthHeader }
if ($headers.Count -gt 0) {
try {
Invoke-WebRequest -Uri $asset.browser_download_url -Headers $headers -OutFile $zipPath -UseBasicParsing
$downloadOk = $true
Write-Success "Download successful with authentication"
} catch {
# Will fall through to pip fallback
}
}
}
if (-not $downloadOk) {
Write-ErrorText "All download attempts failed."
Write-Host "This might mean:"
Write-Host " - Network connectivity issues"
Write-Host " - Invalid GitHub token or insufficient permissions"
Write-Host " - Private repository requires authentication"
Write-Host ""
Write-Info "Attempting automatic fallback to pip..."
if (Install-ViaPip) { exit 0 }
Write-ManualInstallHelp
exit 1
}
# ------------------------------------------------------------------
# Verify checksum (if .sha256 asset is available)
# ------------------------------------------------------------------
$sha256AssetName = "$assetName.sha256"
$sha256Asset = $release.assets | Where-Object { $_.name -eq $sha256AssetName } | Select-Object -First 1
if ($sha256Asset) {
Write-Info "Verifying download checksum..."
$sha256Path = Join-Path $tempDir $sha256AssetName
try {
Invoke-WebRequest -Uri $sha256Asset.browser_download_url -OutFile $sha256Path -UseBasicParsing
$expectedHash = (Get-Content $sha256Path -Raw).Trim().Split(" ")[0]
$actualHash = (Get-FileHash -Path $zipPath -Algorithm SHA256).Hash.ToLower()
if ($actualHash -ne $expectedHash) {
Write-ErrorText "Checksum verification FAILED."
Write-Host " Expected: $expectedHash"
Write-Host " Actual: $actualHash"
Write-Info "Attempting automatic fallback to pip..."
if (Install-ViaPip) { exit 0 }
Write-ManualInstallHelp
exit 1
}
Write-Success "Checksum verified"
} catch {
Write-WarningText "Could not verify checksum (non-fatal): $_"
}
}
# ------------------------------------------------------------------
# Extract
# ------------------------------------------------------------------
Write-Info "Extracting package..."
Expand-Archive -Path $zipPath -DestinationPath $tempDir -Force
$packageDir = Join-Path $tempDir "apm-windows-x86_64"
$exePath = Join-Path $packageDir "apm.exe"
if (-not (Test-Path $exePath)) {
Write-ErrorText "Extracted package is missing apm.exe."
Write-Info "Attempting automatic fallback to pip..."
if (Install-ViaPip) { exit 0 }
Write-ManualInstallHelp
exit 1
}
# ------------------------------------------------------------------
# Stage 3 — Binary test before installation
# ------------------------------------------------------------------
Write-Info "Testing binary..."
try {
$testOutput = & $exePath --version 2>&1
if ($LASTEXITCODE -ne 0) { throw "exit code $LASTEXITCODE" }
Write-Success "Binary test successful: $testOutput"
} catch {
Write-ErrorText "Downloaded binary failed to run: $_"
Write-Host ""
Write-Info "Attempting automatic fallback to pip..."
if (Install-ViaPip) { exit 0 }
Write-ManualInstallHelp
exit 1
}
# ------------------------------------------------------------------
# Install
# ------------------------------------------------------------------
if (Test-Path $releaseDir) {
Remove-Item -Recurse -Force $releaseDir
}
Move-Item -Path $packageDir -Destination $releaseDir
$shimPath = Join-Path $binDir "apm.cmd"
$shimContent = "@echo off`r`n`"$releaseDir\apm.exe`" %*`r`n"
Set-Content -Path $shimPath -Value $shimContent -Encoding ASCII
Add-ToUserPath -PathEntry $binDir
Write-Host ""
Write-Success "APM $tagName installed successfully!"
Write-Info "Command shim: $shimPath"
Write-Host ""
Write-Info "Quick start:"
Write-Host " apm init my-app # Create a new APM project"
Write-Host " cd my-app && apm install # Install dependencies"
Write-Host " apm run # Run your first prompt"
Write-Host ""
Write-Host "Documentation: https://github.com/$Repo"
Write-Info "Run 'apm --version' in a new terminal to verify the installation."
} finally {
if (Test-Path $tempDir) {
Remove-Item -Recurse -Force $tempDir
}
}