-
-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathinstall.ps1
More file actions
212 lines (175 loc) · 9.03 KB
/
install.ps1
File metadata and controls
212 lines (175 loc) · 9.03 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
# fastforge Windows installer
# Usage:
# iwr https://fastforge.dev/install.ps1 | iex
# $env:FASTFORGE_VERSION="0.7.0"; iwr https://fastforge.dev/install.ps1 | iex
# $env:GITHUB_TOKEN="ghp_xxx"; iwr https://fastforge.dev/install.ps1 | iex
$ErrorActionPreference = 'Stop'
$Repo = "fastforgedev/fastforge"
$BinaryName = "fastforge"
$InstallDir = if ($env:FASTFORGE_INSTALL_DIR) { $env:FASTFORGE_INSTALL_DIR } else { "$env:LOCALAPPDATA\fastforge\bin" }
# ── Helpers ───────────────────────────────────────────────────────────────────
function Write-Info { param($msg) Write-Host "info $msg" -ForegroundColor Cyan }
function Write-Success { param($msg) Write-Host "✓ $msg" -ForegroundColor Green }
function Write-Warn { param($msg) Write-Host "warn $msg" -ForegroundColor Yellow }
function Write-Fail { param($msg) Write-Host "error $msg" -ForegroundColor Red; exit 1 }
# ── Detect architecture ───────────────────────────────────────────────────────
function Get-Arch {
$arch = $env:PROCESSOR_ARCHITECTURE
switch ($arch) {
"AMD64" { return "x86_64" }
"ARM64" { return "aarch64" }
default { Write-Fail "Unsupported architecture: $arch" }
}
}
# ── Map to Rust target triple ─────────────────────────────────────────────────
function Get-Target {
param($arch)
switch ($arch) {
"x86_64" { return "x86_64-pc-windows-msvc" }
"aarch64" { return "aarch64-pc-windows-msvc" }
default { Write-Fail "No prebuilt binary available for architecture: $arch" }
}
}
# ── Build common API request headers ─────────────────────────────────────────
function Get-ApiHeaders {
$headers = @{ "User-Agent" = "fastforge-installer" }
if ($env:GITHUB_TOKEN) {
$headers["Authorization"] = "Bearer $($env:GITHUB_TOKEN)"
}
return $headers
}
# ── Resolve version + download URL ───────────────────────────────────────────
function Resolve-Release {
param($target)
# ── Manual version: construct URL directly ────────────────────────────────
if ($env:FASTFORGE_VERSION) {
$version = $env:FASTFORGE_VERSION
$archiveName = "$BinaryName-$version-$target.zip"
$downloadUrl = "https://github.com/$Repo/releases/download/v$version/$archiveName"
Write-Info "Using specified version: $version"
return @{ Version = $version; ArchiveName = $archiveName; DownloadUrl = $downloadUrl }
}
# ── Auto-detect: fetch releases list ──────────────────────────────────────
Write-Info "Fetching latest release version..."
try {
$headers = Get-ApiHeaders
$releases = Invoke-RestMethod `
-Uri "https://api.github.com/repos/$Repo/releases" `
-Headers $headers
} catch {
Write-Fail "Failed to fetch releases from GitHub: $_`nSet `$env:FASTFORGE_VERSION to specify a version manually."
}
$latest = $releases | Select-Object -First 1
if (-not $latest) { Write-Fail "No releases found in the repository." }
$version = $latest.tag_name -replace '^v', ''
if (-not $version) { Write-Fail "Failed to parse version from GitHub API response." }
Write-Info "Latest version: $version"
# ── Resolve download URL from assets ──────────────────────────────────────
$archiveName = "$BinaryName-$version-$target.zip"
$asset = $latest.assets | Where-Object { $_.name -eq $archiveName } | Select-Object -First 1
if (-not $asset) {
Write-Fail "No download asset found for target '$target' in the latest release.`nThe release may not have finished uploading assets yet."
}
$downloadUrl = $asset.browser_download_url
return @{ Version = $version; ArchiveName = $archiveName; DownloadUrl = $downloadUrl }
}
# ── Check if binary is already on PATH ───────────────────────────────────────
function Get-InstalledVersion {
try {
$v = & fastforge --version 2>$null
return $v
} catch {
return $null
}
}
# ── Download ──────────────────────────────────────────────────────────────────
function Download-Archive {
param($archiveName, $downloadUrl)
$tmpDir = Join-Path $env:TEMP "fastforge-install-$([System.IO.Path]::GetRandomFileName())"
$archivePath = Join-Path $tmpDir $archiveName
New-Item -ItemType Directory -Path $tmpDir -Force | Out-Null
Write-Info "Downloading $archiveName..."
Write-Info " from $downloadUrl"
try {
$prev = $global:ProgressPreference
$global:ProgressPreference = 'SilentlyContinue'
Invoke-WebRequest -Uri $downloadUrl -OutFile $archivePath -UseBasicParsing
$global:ProgressPreference = $prev
} catch {
Remove-Item -Recurse -Force $tmpDir -ErrorAction SilentlyContinue
Write-Fail "Download failed. Check your network or verify that the release assets exist.`n$_"
}
Write-Success "Downloaded $archiveName"
return @{ TmpDir = $tmpDir; ArchivePath = $archivePath }
}
# ── Install ───────────────────────────────────────────────────────────────────
function Install-Binary {
param($tmpDir, $archivePath, $version, $target)
Write-Info "Extracting archive..."
Expand-Archive -Path $archivePath -DestinationPath $tmpDir -Force
$binaryPath = Join-Path $tmpDir "$BinaryName-$version-$target\$BinaryName.exe"
if (-not (Test-Path $binaryPath)) {
Remove-Item -Recurse -Force $tmpDir -ErrorAction SilentlyContinue
Write-Fail "Binary not found in archive at expected path: $binaryPath"
}
if (-not (Test-Path $InstallDir)) {
Write-Info "Creating install directory: $InstallDir"
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}
$dest = Join-Path $InstallDir "$BinaryName.exe"
Move-Item -Path $binaryPath -Destination $dest -Force
Remove-Item -Recurse -Force $tmpDir -ErrorAction SilentlyContinue
Write-Success "Installed $BinaryName to $dest"
}
# ── Update PATH ───────────────────────────────────────────────────────────────
function Update-UserPath {
$currentPath = [System.Environment]::GetEnvironmentVariable("PATH", "User")
if ($currentPath -notlike "*$InstallDir*") {
Write-Info "Adding $InstallDir to your user PATH..."
$newPath = "$InstallDir;$currentPath"
[System.Environment]::SetEnvironmentVariable("PATH", $newPath, "User")
$env:PATH = "$InstallDir;$env:PATH"
Write-Success "PATH updated. Restart your terminal to apply changes."
} else {
Write-Info "$InstallDir is already in your PATH."
}
}
# ── Verify ────────────────────────────────────────────────────────────────────
function Verify-Install {
$exePath = Join-Path $InstallDir "$BinaryName.exe"
if (Test-Path $exePath) {
try {
$v = & $exePath --version 2>$null
Write-Success "Installation verified: $v"
} catch {
Write-Success "Binary installed at $exePath"
}
} else {
Write-Warn "Could not verify installation. Binary not found at $exePath"
}
}
# ── Main ──────────────────────────────────────────────────────────────────────
function Main {
Write-Host ""
Write-Host "fastforge installer" -ForegroundColor White -BackgroundColor DarkGreen
Write-Host ""
$arch = Get-Arch
$target = Get-Target -arch $arch
$release = Resolve-Release -target $target
Write-Info "Platform : windows-$arch"
Write-Info "Target : $target"
Write-Info "Version : $($release.Version)"
Write-Host ""
$dl = Download-Archive -archiveName $release.ArchiveName -downloadUrl $release.DownloadUrl
Install-Binary -tmpDir $dl.TmpDir -archivePath $dl.ArchivePath -version $release.Version -target $target
Update-UserPath
Verify-Install
Write-Host ""
Write-Host "fastforge $($release.Version) installed successfully!" -ForegroundColor Green
Write-Host ""
Write-Host "Run " -NoNewline
Write-Host "fastforge --help" -ForegroundColor Cyan -NoNewline
Write-Host " to get started."
Write-Host ""
}
Main