-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
70 lines (56 loc) · 2.4 KB
/
install.ps1
File metadata and controls
70 lines (56 loc) · 2.4 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
Param(
[string]$Version = $env:STAC_VERSION
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
function Write-Log { param([string]$m) Write-Host "[stac_cli] $m" }
function Write-Err { param([string]$m) Write-Error "[stac_cli] $m" }
$Repo = if ($env:STAC_RELEASES_REPO) { $env:STAC_RELEASES_REPO } else { 'StacDev/cli-installer' }
if (-not $Version -or $Version -eq '') { $Version = 'latest' }
function Get-Arch {
if ($env:PROCESSOR_ARCHITECTURE -eq 'ARM64') { return 'arm64' }
return 'x64'
}
function Download-File([string]$Url, [string]$Dest) {
if (Get-Command curl.exe -ErrorAction SilentlyContinue) {
curl.exe -fsSL $Url -o $Dest | Out-Null
} elseif (Get-Command Invoke-WebRequest -ErrorAction SilentlyContinue) {
Invoke-WebRequest -Uri $Url -OutFile $Dest -UseBasicParsing | Out-Null
} else {
throw 'curl or Invoke-WebRequest required'
}
}
function Get-LatestTag {
$api = "https://api.github.com/repos/$Repo/releases/latest"
$res = Invoke-RestMethod -UseBasicParsing -Uri $api -Method GET
return $res.tag_name
}
try {
$arch = Get-Arch
$os = 'windows'
$tag = if ($Version -eq 'latest') { Get-LatestTag } else { "stac-cli-v$Version" }
if (-not $tag) { throw 'Failed to resolve latest tag' }
$resolved = $tag -replace '^stac-cli-v',''
$asset = "stac_cli_${resolved}_${os}_${arch}.zip"
$url = "https://github.com/$Repo/releases/download/$tag/$asset"
$tmp = New-Item -ItemType Directory -Path ([System.IO.Path]::GetTempPath() + [System.Guid]::NewGuid().ToString()) -Force
$zip = Join-Path $tmp.FullName $asset
Write-Log "Downloading $asset from $Repo ($tag)"
Download-File $url $zip
Write-Log 'Extracting'
Expand-Archive -Path $zip -DestinationPath $tmp.FullName -Force
$installDir = if ($env:STAC_INSTALL_DIR) { $env:STAC_INSTALL_DIR } else { "$env:USERPROFILE\\.stac\\bin" }
New-Item -ItemType Directory -Path $installDir -Force | Out-Null
Copy-Item (Join-Path $tmp.FullName 'stac.exe') (Join-Path $installDir 'stac.exe') -Force
$bin = $installDir
$path = [System.Environment]::GetEnvironmentVariable('Path', 'User')
if (-not $path.ToLower().Contains($bin.ToLower())) {
[System.Environment]::SetEnvironmentVariable('Path', "$bin;$path", 'User')
Write-Log "Added $bin to PATH for current user. Restart your terminal."
}
Write-Log "Installed to $bin\\stac.exe"
Write-Log 'Run: stac --help'
} catch {
Write-Err $_.Exception.Message
exit 1
}