-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
173 lines (146 loc) · 4.85 KB
/
Copy pathinstall.ps1
File metadata and controls
173 lines (146 loc) · 4.85 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
#Requires -Version 5.1
<#
.SYNOPSIS
Install lo CLI on Windows.
.DESCRIPTION
Downloads a pre-built lo binary from GitHub Releases and installs it.
Falls back to building from source (go install) when no binary is available.
.EXAMPLE
irm https://raw.githubusercontent.com/notliad/liftoff/main/install.ps1 | iex
.EXAMPLE
.\install.ps1 -FromBinary
.EXAMPLE
.\install.ps1 -FromLocal
.EXAMPLE
.\install.ps1 -FromModule github.com/notliad/liftoff/cmd/lo@latest
.EXAMPLE
.\install.ps1 -Uninstall
#>
[CmdletBinding()]
param(
[switch]$FromBinary,
[switch]$FromLocal,
[string]$FromModule,
[switch]$Uninstall,
[string]$InstallDir = "$env:LOCALAPPDATA\Programs\lo"
)
$ErrorActionPreference = "Stop"
$GitHubRepo = "notliad/liftoff"
$BinName = "lo.exe"
$BinPath = Join-Path $InstallDir $BinName
$GoModuleDefault = "github.com/notliad/liftoff/cmd/lo@latest"
function Write-Info($msg) { Write-Host "[info] $msg" }
function Write-Warn($msg) { Write-Host "[warn] $msg" -ForegroundColor Yellow }
function Write-Done($msg) { Write-Host $msg -ForegroundColor Green }
function Write-Err($msg) { Write-Host "[error] $msg" -ForegroundColor Red }
function Get-Platform {
$arch = switch ($env:PROCESSOR_ARCHITECTURE) {
"AMD64" { "amd64" }
"ARM64" { "arm64" }
default {
Write-Warn "Unknown arch '$env:PROCESSOR_ARCHITECTURE', assuming amd64."
"amd64"
}
}
return "windows-$arch"
}
function Install-FromBinary {
$platform = Get-Platform
$assetName = "lo-$platform.exe"
$apiUrl = "https://api.github.com/repos/$GitHubRepo/releases/latest"
try {
$release = Invoke-RestMethod -Uri $apiUrl -UseBasicParsing
} catch {
Write-Warn "Could not reach GitHub API: $_"
return $false
}
$asset = $release.assets | Where-Object { $_.name -eq $assetName } | Select-Object -First 1
if (-not $asset) {
Write-Warn "No pre-built binary found for $platform in latest release."
return $false
}
New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null
Write-Info "Downloading $assetName..."
Invoke-WebRequest -Uri $asset.browser_download_url -OutFile $BinPath -UseBasicParsing
Write-Done "Installed lo ($platform) to $BinPath"
return $true
}
function Require-Go {
if (-not (Get-Command go -ErrorAction SilentlyContinue)) {
Write-Err "Go 1.22+ is required. Download from: https://go.dev/dl/"
exit 1
}
}
function Add-ToUserPath {
$current = [Environment]::GetEnvironmentVariable("PATH", "User")
if ($current -notlike "*$InstallDir*") {
[Environment]::SetEnvironmentVariable("PATH", "$InstallDir;$current", "User")
Write-Info "Added $InstallDir to your user PATH."
Write-Info "Restart your terminal for the change to take effect."
}
}
function Install-FromLocal {
if (-not (Test-Path ".\go.mod") -or -not (Test-Path ".\cmd\lo")) {
return $false
}
Require-Go
New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null
& go build -o $BinPath .\cmd\lo
if ($LASTEXITCODE -ne 0) { throw "go build failed" }
Write-Done "Installed local build to $BinPath"
return $true
}
function Install-FromGoModule {
param([string]$Module)
Require-Go
New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null
$env:GOBIN = $InstallDir
& go install $Module
if ($LASTEXITCODE -ne 0) { throw "go install $Module failed" }
Write-Done "Installed $Module to $BinPath"
}
function Uninstall-Lo {
if (Test-Path $BinPath) {
Remove-Item $BinPath -Force
Write-Host "Removed $BinPath"
} else {
Write-Info "$BinPath not found."
}
$configDir = Join-Path $env:APPDATA "lo"
Write-Info "To remove config, delete: $configDir"
}
# --- Main ---
if ($Uninstall) {
Uninstall-Lo
exit 0
}
if ($FromBinary) {
if (-not (Install-FromBinary)) {
Write-Err "Binary download failed. Re-run with -FromLocal or -FromModule to install from source."
exit 1
}
} elseif ($FromLocal) {
if (-not (Install-FromLocal)) {
Write-Err "Could not build from local source (expected .\go.mod and .\cmd\lo)."
exit 1
}
} elseif ($FromModule) {
Install-FromGoModule -Module $FromModule
} else {
# Auto: try binary first, then local build, then go install.
if (-not (Install-FromBinary)) {
if (-not (Install-FromLocal)) {
Install-FromGoModule -Module $GoModuleDefault
}
}
}
Add-ToUserPath
# Refresh PATH in the current session so we can check immediately.
$env:PATH = "$InstallDir;$env:PATH"
if (Get-Command lo -ErrorAction SilentlyContinue) {
$loPath = (Get-Command lo).Source
Write-Done "`nlo is available: $loPath"
} else {
Write-Done "`nInstall completed at $BinPath"
}
Write-Host "Run 'lo --help' to get started."