-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
197 lines (162 loc) · 6.48 KB
/
install.ps1
File metadata and controls
197 lines (162 loc) · 6.48 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
# DevTrail CLI installer for Windows — https://github.com/StrangeDaysTech/devtrail
#
# Usage:
# irm https://raw.githubusercontent.com/StrangeDaysTech/devtrail/main/install.ps1 | iex
#
# # Or with parameters:
# & ([scriptblock]::Create((irm https://raw.githubusercontent.com/StrangeDaysTech/devtrail/main/install.ps1))) -Tag v2.0.0
#
# Compatible with PowerShell 5.1+ and PowerShell Core (pwsh).
param(
[string]$Tag,
[string]$InstallDir
)
$ErrorActionPreference = "Stop"
$Repo = "StrangeDaysTech/devtrail"
$Binary = "devtrail.exe"
$Target = "x86_64-pc-windows-msvc"
function Write-Status($Message) {
Write-Host "devtrail-install: $Message"
}
function Write-Err($Message) {
Write-Host "devtrail-install: ERROR: $Message" -ForegroundColor Red
}
# ── Verify platform ─────────────────────────────────────────────────────
if ([System.Environment]::Is64BitOperatingSystem -eq $false) {
Write-Err "DevTrail requires a 64-bit Windows installation."
exit 1
}
if ($env:PROCESSOR_ARCHITECTURE -ne "AMD64" -and $env:PROCESSOR_ARCHITECTURE -ne "x86") {
# ARM64 Windows is not supported
Write-Err "Unsupported architecture: $env:PROCESSOR_ARCHITECTURE. Only x86_64 (AMD64) is supported."
exit 1
}
# ── Defaults ─────────────────────────────────────────────────────────────
if (-not $InstallDir) {
$InstallDir = Join-Path $env:LOCALAPPDATA "DevTrail\bin"
}
# ── GitHub API headers ───────────────────────────────────────────────────
function Get-GitHubHeaders {
$headers = @{
"Accept" = "application/vnd.github+json"
}
if ($env:GITHUB_TOKEN) {
$headers["Authorization"] = "token $env:GITHUB_TOKEN"
}
return $headers
}
# ── Get latest tag ───────────────────────────────────────────────────────
function Get-LatestTag {
$apiUrl = "https://api.github.com/repos/$Repo/releases/latest"
$headers = Get-GitHubHeaders
Write-Status "fetching latest release info..."
try {
$release = Invoke-RestMethod -Uri $apiUrl -Headers $headers -UseBasicParsing
}
catch {
Write-Err "Failed to fetch release info from GitHub API."
Write-Host ""
Write-Host " This may be due to rate limiting." -ForegroundColor Yellow
Write-Host " Set `$env:GITHUB_TOKEN to authenticate, or use -Tag to specify a version."
Write-Host ""
exit 1
}
$tagName = $release.tag_name
if (-not $tagName) {
Write-Err "Could not parse latest release tag from GitHub API response."
exit 1
}
Write-Status "latest version: $tagName"
return $tagName
}
# ── Main ─────────────────────────────────────────────────────────────────
$tempDir = $null
try {
# Resolve tag
if (-not $Tag) {
$Tag = Get-LatestTag
}
else {
Write-Status "using specified version: $Tag"
}
# Build asset name and URL
$versionNum = $Tag -replace "^v", ""
$asset = "devtrail-cli-v${versionNum}-${Target}.zip"
$url = "https://github.com/$Repo/releases/download/$Tag/$asset"
# Create temp directory
$tempDir = Join-Path $env:TEMP "devtrail-install-$(Get-Random)"
New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
$archivePath = Join-Path $tempDir $asset
# Download
Write-Status "downloading $asset..."
try {
# Use TLS 1.2+ (required for GitHub)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls13
$headers = Get-GitHubHeaders
Invoke-WebRequest -Uri $url -OutFile $archivePath -Headers $headers -UseBasicParsing
}
catch {
Write-Err "Download failed."
Write-Host ""
Write-Host " Possible causes:" -ForegroundColor Yellow
Write-Host " - Version $Tag does not exist"
Write-Host " - No binary available for $Target"
Write-Host " - Network connectivity issue"
Write-Host ""
exit 1
}
# Extract
Write-Status "extracting $Binary..."
$extractDir = Join-Path $tempDir "extract"
Expand-Archive -Path $archivePath -DestinationPath $extractDir -Force
$binaryPath = Join-Path $extractDir $Binary
if (-not (Test-Path $binaryPath)) {
Write-Err "Binary '$Binary' not found in archive."
exit 1
}
# Install
if (-not (Test-Path $InstallDir)) {
try {
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}
catch {
Write-Err "Failed to create install directory: $InstallDir"
Write-Host ""
Write-Host " Try specifying an alternative with -InstallDir" -ForegroundColor Yellow
Write-Host ""
exit 1
}
}
$destPath = Join-Path $InstallDir $Binary
Copy-Item -Path $binaryPath -Destination $destPath -Force
Write-Status "installed $Binary to $destPath"
# Add to PATH if not already present
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
$pathEntries = $userPath -split ";"
if ($pathEntries -notcontains $InstallDir) {
Write-Status "adding $InstallDir to user PATH..."
$newPath = "$userPath;$InstallDir"
[Environment]::SetEnvironmentVariable("Path", $newPath, "User")
# Also update current session PATH
$env:Path = "$env:Path;$InstallDir"
Write-Host ""
Write-Host " $InstallDir has been added to your user PATH." -ForegroundColor Green
Write-Host " Restart your terminal for the change to take effect in new sessions."
Write-Host ""
}
# Verify
try {
$version = & $destPath --version 2>&1
Write-Status "verified: $version"
}
catch {
Write-Status "warning: could not verify installation (binary may not run on this platform)"
}
Write-Status "done!"
}
finally {
# Cleanup temp directory
if ($tempDir -and (Test-Path $tempDir)) {
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
}
}