-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinstall.ps1
More file actions
256 lines (207 loc) · 9.23 KB
/
install.ps1
File metadata and controls
256 lines (207 loc) · 9.23 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
# vx installer script for Windows
#
# Usage:
# irm https://raw.githubusercontent.com/loonghao/vx/main/install.ps1 | iex
#
# With specific version:
# $env:VX_VERSION="0.8.4"; irm https://raw.githubusercontent.com/loonghao/vx/main/install.ps1 | iex
#
# With custom install directory:
# $env:VX_INSTALL_DIR="C:\tools\bin"; irm https://raw.githubusercontent.com/loonghao/vx/main/install.ps1 | iex
#
# CDN acceleration (disabled by default, opt-in for slow GitHub access):
# $env:VX_CDN="1"; irm https://raw.githubusercontent.com/loonghao/vx/main/install.ps1 | iex
#
# Alternative package managers:
# winget install loonghao.vx
# scoop install vx
#
# Environment variables:
# VX_VERSION - Version to install (default: latest stable)
# VX_INSTALL_DIR - Installation directory (default: $env:USERPROFILE\.local\bin)
# VX_RELEASE_BASE_URLS- Comma-separated release mirror URLs
# GITHUB_TOKEN - GitHub API token to avoid rate limits
# VX_CDN - Set to "1" to enable CDN acceleration (disabled by default)
param(
[string]$Version = $env:VX_VERSION,
[string]$InstallDir = $env:VX_INSTALL_DIR,
[string]$ReleaseBaseUrls = $env:VX_RELEASE_BASE_URLS
)
$ErrorActionPreference = "Stop"
$RepoOwner = "loonghao"
$RepoName = "vx"
$BaseUrl = "https://github.com/$RepoOwner/$RepoName/releases"
function Get-ReleaseBaseUrls {
if (-not $ReleaseBaseUrls) {
return @($BaseUrl)
}
$urls = $ReleaseBaseUrls -split '[,;]' |
ForEach-Object { $_.Trim() } |
Where-Object { $_ -ne "" }
if (-not $urls -or $urls.Count -eq 0) {
return @($BaseUrl)
}
return $urls
}
if (-not $InstallDir) {
$InstallDir = "$env:USERPROFILE\.local\bin"
}
# ── Logging ──────────────────────────────────────────────────────────────────
function Write-Step { param([string]$m) Write-Host " $RepoName " -NoNewline -ForegroundColor Cyan; Write-Host $m }
function Write-Ok { param([string]$m) Write-Host " $RepoName " -NoNewline -ForegroundColor Green; Write-Host $m }
function Write-Fail { param([string]$m) Write-Host " $RepoName " -NoNewline -ForegroundColor Red; Write-Host $m }
# ── Platform detection ────────────────────────────────────────────────────────
function Get-Platform {
$arch = switch ([System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture) {
"X64" { "x86_64" }
"Arm64" { "aarch64" }
default { if ([Environment]::Is64BitOperatingSystem) { "x86_64" } else { "i686" } }
}
return "$arch-pc-windows-msvc"
}
# ── Download with retry ───────────────────────────────────────────────────────
function Invoke-Download {
param([string]$Url, [string]$Dest)
$headers = @{ "User-Agent" = "vx-installer/1.0" }
if ($env:GITHUB_TOKEN) {
$headers["Authorization"] = "Bearer $env:GITHUB_TOKEN"
}
$maxRetries = 3
for ($i = 1; $i -le $maxRetries; $i++) {
try {
$wc = New-Object System.Net.WebClient
foreach ($k in $headers.Keys) { $wc.Headers.Add($k, $headers[$k]) }
$wc.DownloadFile($Url, $Dest)
if ((Test-Path $Dest) -and (Get-Item $Dest).Length -gt 1024) {
return $true
}
} catch {
if ($i -lt $maxRetries) { Start-Sleep -Seconds 2 }
}
Remove-Item $Dest -Force -ErrorAction SilentlyContinue
}
return $false
}
function Get-LatestVersion {
$headers = @{
"User-Agent" = "vx-installer/1.0"
"Accept" = "application/vnd.github+json"
}
if ($env:GITHUB_TOKEN) {
$headers["Authorization"] = "Bearer $env:GITHUB_TOKEN"
}
try {
$apiUrl = "https://api.github.com/repos/$RepoOwner/$RepoName/releases?per_page=20"
$releases = Invoke-RestMethod -Uri $apiUrl -Headers $headers -TimeoutSec 30
foreach ($release in $releases) {
if (-not $release.prerelease -and -not $release.draft -and $release.assets -and $release.assets.Count -gt 0) {
return ($release.tag_name -replace '^(vx-)?v', '')
}
}
} catch {
# fallback handled by latest/download candidate
}
return $null
}
# ── Main ──────────────────────────────────────────────────────────────────────
function Main {
$platform = Get-Platform
Write-Step "Installing vx for Windows..."
Write-Step "Detected: Windows -> $platform"
$releaseBaseUrls = Get-ReleaseBaseUrls
# Resolve download URL
if ($Version -and $Version -ne "latest") {
# Normalize version tag
$ver = $Version -replace '^(vx-)?v', ''
# Try v{ver} first (v0.7.0+), then vx-v{ver} (legacy)
$archiveCandidates = @(
@{ Tag = "v$ver"; Archive = "vx-$ver-$platform.zip" },
@{ Tag = "v$ver"; Archive = "vx-$platform.zip" },
@{ Tag = "vx-v$ver"; Archive = "vx-$ver-$platform.zip" },
@{ Tag = "vx-v$ver"; Archive = "vx-$platform.zip" }
)
} else {
# latest may not always expose unversioned assets; try robust fallbacks
$archiveCandidates = @(
@{ Tag = "latest"; Archive = "vx-$platform.zip" }
)
$latestVersion = Get-LatestVersion
if ($latestVersion) {
Write-Step "Resolved latest stable version with assets: $latestVersion"
$archiveCandidates += @(
@{ Tag = "v$latestVersion"; Archive = "vx-$latestVersion-$platform.zip" },
@{ Tag = "v$latestVersion"; Archive = "vx-$platform.zip" },
@{ Tag = "vx-v$latestVersion"; Archive = "vx-$latestVersion-$platform.zip" },
@{ Tag = "vx-v$latestVersion"; Archive = "vx-$platform.zip" }
)
}
}
# Create temp dir
$tempDir = New-TemporaryFile | ForEach-Object { Remove-Item $_; New-Item -ItemType Directory -Path $_ }
try {
$archivePath = $null
$usedTag = $null
foreach ($releaseBaseUrl in $releaseBaseUrls) {
foreach ($combo in $archiveCandidates) {
$tag = $combo.Tag
$archive = $combo.Archive
if ($tag -eq "latest") {
$url = "$releaseBaseUrl/latest/download/$archive"
} else {
$url = "$releaseBaseUrl/download/$tag/$archive"
}
Write-Step "Downloading from: $url"
$dest = Join-Path $tempDir $archive
if (Invoke-Download -Url $url -Dest $dest) {
$archivePath = $dest
$usedTag = $tag
break
}
}
if ($archivePath) { break }
}
if (-not $archivePath) {
$hintVer = if ($latestVersion) { $latestVersion } else { "0.8.4" }
Write-Fail "Download failed. Please check your internet connection or specify a version:"
Write-Host " `$env:VX_VERSION='$hintVer'; irm https://raw.githubusercontent.com/loonghao/vx/main/install.ps1 | iex"
exit 1
}
# Extract
Write-Step "Extracting..."
if (-not (Test-Path $InstallDir)) {
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}
Expand-Archive -Path $archivePath -DestinationPath $tempDir -Force
# Find binary
$binary = Get-ChildItem -Path $tempDir -Filter "vx.exe" -Recurse | Select-Object -First 1
if (-not $binary) {
Write-Fail "vx.exe not found in archive"
exit 1
}
$destBin = Join-Path $InstallDir "vx.exe"
Copy-Item -Path $binary.FullName -Destination $destBin -Force
# Detect installed version
$installedVersion = & $destBin --version 2>&1 | Select-String '\d+\.\d+\.\d+' | ForEach-Object { $_.Matches[0].Value } | Select-Object -First 1
if (-not $installedVersion) { $installedVersion = $usedTag }
Write-Ok "Installed: vx $installedVersion"
# Update PATH
$currentPath = [Environment]::GetEnvironmentVariable("PATH", "User")
if ($currentPath -notlike "*$InstallDir*") {
[Environment]::SetEnvironmentVariable("PATH", "$InstallDir;$currentPath", "User")
$env:PATH = "$InstallDir;$env:PATH"
Write-Ok "Added to user PATH"
}
Write-Host ""
Write-Ok "vx installed successfully!"
Write-Host ""
Write-Host " Run: vx --help" -ForegroundColor Gray
Write-Host " Docs: https://github.com/$RepoOwner/$RepoName" -ForegroundColor Gray
Write-Host ""
Write-Host " Restart your terminal or run:" -ForegroundColor Gray
Write-Host " `$env:PATH = `"$InstallDir;`$env:PATH`"" -ForegroundColor Gray
}
finally {
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
Main