-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
167 lines (138 loc) · 4.91 KB
/
install.ps1
File metadata and controls
167 lines (138 loc) · 4.91 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
# Belt installer for Windows
# Usage: irm https://raw.githubusercontent.com/kys0213/belt/main/install.ps1 | iex
#
# Environment variables:
# BELT_VERSION - Version to install (default: latest)
# BELT_INSTALL_DIR - Installation directory (default: $HOME\.belt\bin)
$ErrorActionPreference = "Stop"
$Repo = "kys0213/belt"
$GitHubApi = "https://api.github.com"
$GitHubRelease = "https://github.com/$Repo/releases"
function Write-Installer {
param([string]$Message)
Write-Host "belt-installer: $Message"
}
function Write-InstallerError {
param([string]$Message)
Write-Host "belt-installer: ERROR: $Message" -ForegroundColor Red
exit 1
}
# --- Detect architecture ---
function Get-MachineArch {
$arch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture
switch ($arch) {
"X64" { return "x86_64" }
default { Write-InstallerError "unsupported architecture: $arch (only x86_64 is supported on Windows)" }
}
}
# --- Resolve version ---
function Resolve-Version {
$version = $env:BELT_VERSION
if ($version) {
return $version
}
Write-Installer "fetching latest release version..."
$url = "$GitHubApi/repos/$Repo/releases/latest"
try {
$response = Invoke-RestMethod -Uri $url -Headers @{ "User-Agent" = "belt-installer" }
$tag = $response.tag_name
if (-not $tag) {
Write-InstallerError "could not determine latest version from GitHub API"
}
return $tag
}
catch {
Write-InstallerError "failed to fetch latest release info: $_"
}
}
# --- Main ---
function Install-Belt {
$arch = Get-MachineArch
Write-Installer "detected platform: windows $arch"
$version = Resolve-Version
# Normalize version prefix
if (-not $version.StartsWith("v")) {
$version = "v$version"
}
Write-Installer "installing belt $version"
$installDir = $env:BELT_INSTALL_DIR
if (-not $installDir) {
$installDir = Join-Path $HOME ".belt\bin"
}
$target = "${arch}-pc-windows-msvc"
$asset = "belt-$target.zip"
$url = "$GitHubRelease/download/$version/$asset"
# Create install directory
if (-not (Test-Path $installDir)) {
try {
New-Item -ItemType Directory -Path $installDir -Force | Out-Null
}
catch {
Write-InstallerError "failed to create install directory: $installDir ($_)"
}
}
try {
# Download to temp directory
$tmpDir = Join-Path ([System.IO.Path]::GetTempPath()) "belt-install-$([System.Guid]::NewGuid().ToString('N'))"
New-Item -ItemType Directory -Path $tmpDir -Force | Out-Null
$tmpFile = Join-Path $tmpDir $asset
Write-Installer "downloading $url..."
try {
Invoke-WebRequest -Uri $url -OutFile $tmpFile -UseBasicParsing -Headers @{ "User-Agent" = "belt-installer" }
}
catch {
Write-InstallerError "failed to download $url ($_)"
}
# Extract
Write-Installer "extracting to $installDir..."
try {
Expand-Archive -Path $tmpFile -DestinationPath $tmpDir -Force
}
catch {
Write-InstallerError "failed to extract archive ($_)"
}
# Find and install belt.exe
$beltExe = $null
$candidates = @(
(Join-Path $tmpDir "belt.exe"),
(Join-Path $tmpDir "belt-$target\belt.exe")
)
foreach ($candidate in $candidates) {
if (Test-Path $candidate) {
$beltExe = $candidate
break
}
}
if (-not $beltExe) {
# Search recursively
$found = Get-ChildItem -Path $tmpDir -Filter "belt.exe" -Recurse -File | Select-Object -First 1
if ($found) {
$beltExe = $found.FullName
}
else {
Write-InstallerError "could not find 'belt.exe' in downloaded archive"
}
}
Copy-Item -Path $beltExe -Destination (Join-Path $installDir "belt.exe") -Force
}
finally {
# Cleanup temp directory
Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction SilentlyContinue
}
Write-Installer "belt $version installed to $(Join-Path $installDir 'belt.exe')"
# PATH guidance
$currentPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($currentPath -and $currentPath.Split(";") -contains $installDir) {
Write-Installer "belt is already in your PATH"
}
else {
Write-Host ""
Write-Installer "add belt to your PATH by running:"
Write-Host ""
Write-Host " [Environment]::SetEnvironmentVariable('Path', '$installDir;' + [Environment]::GetEnvironmentVariable('Path', 'User'), 'User'); `$env:Path = '$installDir;' + `$env:Path"
Write-Host ""
}
Write-Host ""
Write-Installer "run 'belt --version' to verify the installation"
}
Install-Belt