-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
146 lines (118 loc) · 4.34 KB
/
install.ps1
File metadata and controls
146 lines (118 loc) · 4.34 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
# ask installer for Windows
# https://github.com/verseles/ask
#
# Licensed under AGPL-3.0
$ErrorActionPreference = "Stop"
$REPO = "verseles/ask"
$BINARY_NAME = "ask"
$INSTALL_DIR = "$env:USERPROFILE\.local\bin"
function Write-Info {
param([string]$Message)
Write-Host "info: " -ForegroundColor Cyan -NoNewline
Write-Host $Message
}
function Write-Success {
param([string]$Message)
Write-Host "success: " -ForegroundColor Green -NoNewline
Write-Host $Message
}
function Write-Warn {
param([string]$Message)
Write-Host "warning: " -ForegroundColor Yellow -NoNewline
Write-Host $Message
}
function Write-Error {
param([string]$Message)
Write-Host "error: " -ForegroundColor Red -NoNewline
Write-Host $Message
exit 1
}
function Get-Architecture {
$arch = [System.Environment]::GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
switch ($arch) {
"AMD64" { return "x86_64" }
"ARM64" { return "aarch64" }
default { Write-Error "Unsupported architecture: $arch" }
}
}
function Get-LatestVersion {
try {
$response = Invoke-RestMethod -Uri "https://api.github.com/repos/$REPO/releases/latest"
return $response.tag_name
} catch {
Write-Error "Could not determine latest version: $_"
}
}
function Get-FileHash256 {
param([string]$Path)
return (Get-FileHash -Path $Path -Algorithm SHA256).Hash.ToLower()
}
function Main {
Write-Info "Installing $BINARY_NAME..."
$arch = Get-Architecture
Write-Info "Detected architecture: $arch"
# Map to artifact name
$target = switch ($arch) {
"x86_64" { "windows-x86_64" }
"aarch64" { "windows-aarch64" }
default { Write-Error "Unsupported architecture: $arch" }
}
$version = Get-LatestVersion
Write-Info "Latest version: $version"
# Create temp directory
$tempDir = New-Item -ItemType Directory -Path ([System.IO.Path]::GetTempPath()) -Name ([System.Guid]::NewGuid().ToString())
try {
$binaryUrl = "https://github.com/$REPO/releases/download/$version/$BINARY_NAME-$target.exe"
$checksumUrl = "$binaryUrl.sha256"
$binaryPath = Join-Path $tempDir "$BINARY_NAME.exe"
$checksumPath = Join-Path $tempDir "$BINARY_NAME.exe.sha256"
Write-Info "Downloading $BINARY_NAME..."
Invoke-WebRequest -Uri $binaryUrl -OutFile $binaryPath
Invoke-WebRequest -Uri $checksumUrl -OutFile $checksumPath
# Verify checksum
$expectedHash = (Get-Content $checksumPath).Split()[0].Trim()
$actualHash = Get-FileHash256 -Path $binaryPath
if ($actualHash -ne $expectedHash) {
Write-Error "Checksum verification failed"
}
Write-Success "Checksum verified"
# Create install directory
if (-not (Test-Path $INSTALL_DIR)) {
New-Item -ItemType Directory -Path $INSTALL_DIR -Force | Out-Null
}
# Install binary
$installPath = Join-Path $INSTALL_DIR "$BINARY_NAME.exe"
Move-Item -Path $binaryPath -Destination $installPath -Force
Write-Success "Installed $BINARY_NAME to $installPath"
# Check PATH
$userPath = [Environment]::GetEnvironmentVariable("PATH", "User")
if (-not $userPath.Contains($INSTALL_DIR)) {
Write-Warn "$INSTALL_DIR is not in your PATH"
Write-Host ""
$addToPath = Read-Host "Add to PATH? (Y/n)"
if ($addToPath -ne "n" -and $addToPath -ne "N") {
$newPath = "$INSTALL_DIR;$userPath"
[Environment]::SetEnvironmentVariable("PATH", $newPath, "User")
$env:PATH = "$INSTALL_DIR;$env:PATH"
Write-Success "Added to PATH"
}
}
Write-Host ""
Write-Success "Installation complete!"
Write-Host ""
Write-Host "Get started:"
Write-Host " $BINARY_NAME init # Configure API keys"
Write-Host " $BINARY_NAME --help # Show help"
Write-Host ""
# Prompt to run init
$runInit = Read-Host "Would you like to run '$BINARY_NAME init' now to configure your API keys? (Y/n)"
if ($runInit -ne "n" -and $runInit -ne "N") {
Write-Host ""
& $installPath init
}
} finally {
# Cleanup
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
Main