-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
220 lines (189 loc) · 7.1 KB
/
install.ps1
File metadata and controls
220 lines (189 loc) · 7.1 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
# PowerShell installation script for shepai on Windows
param(
[string]$InstallDir = "$env:LOCALAPPDATA\Programs\shepai"
)
$ErrorActionPreference = "Stop"
# Configuration
$REPO = "arifszn/shepai"
$BINARY_NAME = "shepai.exe"
# Color output functions
function Write-ColorOutput {
param(
[string]$ForegroundColor,
[string]$Message
)
$fc = $host.UI.RawUI.ForegroundColor
$host.UI.RawUI.ForegroundColor = $ForegroundColor
Write-Host $Message
$host.UI.RawUI.ForegroundColor = $fc
}
function Write-Success {
param([string]$Message)
Write-ColorOutput -ForegroundColor "Green" -Message $Message
}
function Write-Error-Message {
param([string]$Message)
Write-ColorOutput -ForegroundColor "Red" -Message $Message
}
function Write-Warning-Message {
param([string]$Message)
Write-ColorOutput -ForegroundColor "Yellow" -Message $Message
}
# Detect architecture
function Get-Platform {
$arch = [System.Environment]::GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
switch ($arch) {
"AMD64" { return "windows-amd64" }
"ARM64" { return "windows-arm64" }
default {
Write-Error-Message "Error: Unsupported architecture: $arch"
exit 1
}
}
}
# Get latest version from GitHub
function Get-LatestVersion {
Write-Warning-Message "Fetching latest version..."
$version = $null
# Try getting version from redirect url (avoids API rate limits)
try {
$request = [System.Net.WebRequest]::Create("https://github.com/$REPO/releases/latest")
$request.AllowAutoRedirect = $false
try {
$response = $request.GetResponse()
$location = $response.Headers["Location"]
$response.Close()
}
catch [System.Net.WebException] {
# In some environments, 3xx might throw, catch and inspect
$response = $_.Exception.Response
if ($null -ne $response) {
$location = $response.Headers["Location"]
$response.Close()
}
}
if ($null -ne $location) {
$version = $location.Split('/')[-1]
}
}
catch {
# Ignore errors here and fall back to API
}
# Fallback to API if redirect failed or returned "latest" (which happens if no redirect occurred or logic failed)
if ([string]::IsNullOrEmpty($version) -or $version -eq "latest") {
try {
$response = Invoke-RestMethod -Uri "https://api.github.com/repos/$REPO/releases/latest"
$version = $response.tag_name
}
catch {
Write-Error-Message "Error: Failed to fetch latest version from GitHub"
Write-Error-Message $_.Exception.Message
exit 1
}
}
if ([string]::IsNullOrEmpty($version)) {
Write-Error-Message "Error: Could not fetch latest version"
exit 1
}
Write-Success "Latest version: $version"
return $version
}
# Download and install
function Install-Shepai {
$platform = Get-Platform
$version = Get-LatestVersion
# Try compressed formats first, then raw binary
$downloadUrlZip = "https://github.com/$REPO/releases/download/$version/shepai-$platform.zip"
$downloadUrlExe = "https://github.com/$REPO/releases/download/$version/shepai-$platform.exe"
$downloadUrlRaw = "https://github.com/$REPO/releases/download/$version/shepai-$platform"
Write-Warning-Message "Downloading shepai $version for $platform..."
# Create temp directory
$tempDir = Join-Path $env:TEMP "shepai-install-$(Get-Random)"
New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
try {
$downloadedFile = $null
$downloadedFormat = $null
# Try zip first
try {
$zipFile = Join-Path $tempDir "shepai.zip"
Invoke-WebRequest -Uri $downloadUrlZip -OutFile $zipFile -ErrorAction Stop
$downloadedFile = $zipFile
$downloadedFormat = "zip"
}
catch {
# Try .exe
try {
$exeFile = Join-Path $tempDir "shepai.exe"
Invoke-WebRequest -Uri $downloadUrlExe -OutFile $exeFile -ErrorAction Stop
$downloadedFile = $exeFile
$downloadedFormat = "exe"
}
catch {
# Try raw binary
try {
$rawFile = Join-Path $tempDir "shepai"
Invoke-WebRequest -Uri $downloadUrlRaw -OutFile $rawFile -ErrorAction Stop
$downloadedFile = $rawFile
$downloadedFormat = "raw"
}
catch {
Write-Error-Message "Error: Failed to download release asset"
Write-Warning-Message "Tried:"
Write-Warning-Message " - $downloadUrlZip"
Write-Warning-Message " - $downloadUrlExe"
Write-Warning-Message " - $downloadUrlRaw"
exit 1
}
}
}
# Extract or prepare binary
$binaryPath = $null
if ($downloadedFormat -eq "zip") {
$extractDir = Join-Path $tempDir "extract"
Expand-Archive -Path $downloadedFile -DestinationPath $extractDir -Force
# Look for the binary in extracted files
$possibleBinary = Get-ChildItem -Path $extractDir -Recurse -Include "shepai-$platform", "shepai-$platform.exe", "shepai.exe", "shepai" | Select-Object -First 1
if ($possibleBinary) {
$binaryPath = $possibleBinary.FullName
}
else {
Write-Error-Message "Error: Could not find binary in extracted archive"
exit 1
}
}
else {
$binaryPath = $downloadedFile
}
# Create install directory
if (-not (Test-Path $InstallDir)) {
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}
# Copy binary to install directory
$targetPath = Join-Path $InstallDir $BINARY_NAME
Copy-Item -Path $binaryPath -Destination $targetPath -Force
Write-Success "[OK] Successfully installed shepai to $targetPath"
# Add to PATH if not already present
$userPath = [System.Environment]::GetEnvironmentVariable("Path", "User")
if ($userPath -notlike "*$InstallDir*") {
Write-Warning-Message "Adding $InstallDir to user PATH..."
[System.Environment]::SetEnvironmentVariable(
"Path",
"$userPath;$InstallDir",
"User"
)
Write-Success "[OK] Added to PATH. Please restart your terminal for changes to take effect."
}
# Verify installation
Write-Success "[OK] Installation complete!"
Write-Success "Run 'shepai --help' to get started (restart your terminal first)"
}
finally {
# Cleanup
if (Test-Path $tempDir) {
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
}
}
}
# Main
Write-Success "Installing shepai..."
Install-Shepai