forked from mammouth-ai/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
151 lines (123 loc) · 5.57 KB
/
install.ps1
File metadata and controls
151 lines (123 loc) · 5.57 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
# Mammouth Code Installer for Windows
# Usage: irm https://code.mammouth.ai/install.ps1 | iex
$ErrorActionPreference = "Stop"
# Ensure TLS 1.2 for GitHub API/downloads (PowerShell 5.1 defaults to TLS 1.0/1.1)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$Repo = "mammouth-ai/code"
$BinaryName = "mammouth"
$InstallDir = "$env:USERPROFILE\.mammouth\bin"
function Write-Info { param($Message) Write-Host "[INFO] $Message" -ForegroundColor Green }
function Write-Warn { param($Message) Write-Host "[WARN] $Message" -ForegroundColor Yellow }
function Write-Err { param($Message) Write-Host "[ERROR] $Message" -ForegroundColor Red; exit 1 }
# Detect architecture
function Get-Platform {
$arch = if ([Environment]::Is64BitOperatingSystem) {
switch ($env:PROCESSOR_ARCHITECTURE) {
"ARM64" { "arm64" }
"AMD64" { "x64" }
default { "x64" }
}
} else {
Write-Err "32-bit systems are not supported."
}
return "mammouth-windows-$arch"
}
# Get latest version from GitHub API
function Get-LatestVersion {
if ($env:VERSION) {
return $env:VERSION
}
try {
$release = Invoke-RestMethod -Uri "https://api.github.com/repos/$Repo/releases/latest" -Headers @{ "User-Agent" = "MammouthInstaller" }
$version = $release.tag_name -replace "^v", ""
if (-not $version) {
Write-Err "Failed to determine latest version."
}
return $version
} catch {
Write-Err "Failed to fetch latest release: $_"
}
}
function Install-Mammouth {
$platform = Get-Platform
$version = Get-LatestVersion
Write-Info "Installing Mammouth Code v$version for $platform..."
# Create install directory
if (-not (Test-Path $InstallDir)) {
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}
$downloadUrl = "https://github.com/$Repo/releases/download/v$version/$platform.zip"
$tempDir = Join-Path $env:TEMP "mammouth-install-$(Get-Random)"
New-Item -ItemType Directory -Path $tempDir -Force | Out-Null
$tempDir = (Resolve-Path $tempDir).Path
$archivePath = Join-Path $tempDir "$platform.zip"
try {
Write-Info "Downloading from $downloadUrl..."
try {
Invoke-WebRequest -Uri $downloadUrl -OutFile $archivePath -UseBasicParsing
} catch {
Write-Err "Failed to download. Check if the release exists for your platform: $_"
}
Write-Info "Extracting..."
Expand-Archive -Path $archivePath -DestinationPath $tempDir -Force
# Find the binary
$binaryPath = Get-ChildItem -Path $tempDir -Recurse -Filter "$BinaryName.exe" | Select-Object -First 1
if (-not $binaryPath) {
# Bun currently doesn't produce a binary with .exe extension, so for now we have this if statement that alwasy pases.
# I keep it here in case one day I decide to make bun produce a .exe binary for windows, then I can remove the if statement and just look for the .exe binary :)
$binaryPath = Get-ChildItem -Path $tempDir -Recurse -Filter $BinaryName | Where-Object { -not $_.PSIsContainer } | Select-Object -First 1
}
if (-not $binaryPath) {
Write-Err "Binary not found in archive."
}
$destPath = Join-Path $InstallDir "$BinaryName.exe"
# Handle binary being locked (e.g. mammouth is currently running)
if (Test-Path $destPath) {
try {
# Try renaming the old binary out of the way first
$backupPath = "$destPath.old"
if (Test-Path $backupPath) { Remove-Item $backupPath -Force -ErrorAction SilentlyContinue }
Rename-Item -Path $destPath -NewName "$BinaryName.exe.old" -Force
} catch {
Write-Err "Cannot update $destPath. You most likely than not have Mammouth Code running. Please close Mammouth Code and try again."
Write-Info "If you're looking to update Mammouth Code, you can do so by running 'mammouth upgrade' in your terminal."
Write-Info "If you continue to have issues, please delete or rename $destPath and try again."
}
}
Copy-Item -Path $binaryPath.FullName -Destination $destPath -Force
# Clean up old binary
$backupPath = "$destPath.old"
if (Test-Path $backupPath) { Remove-Item $backupPath -Force -ErrorAction SilentlyContinue }
} finally {
# Cleanup temp directory regardless of success or failure
try {
Remove-Item -Path $tempDir -Recurse -Force -ErrorAction SilentlyContinue
} catch {
Write-Warn "Failed to clean up temporary files. Please check $tempDir and delete it if it still exists."
}
}
Write-Info "Installed to $destPath"
# Add to PATH
Add-ToPath
}
function Add-ToPath {
$currentPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($currentPath -like "*\.mammouth\bin*") {
Write-Info "Mammouth Code is ready! Run 'mammouth' to get started."
return
}
$newPath = "$InstallDir;$currentPath"
[Environment]::SetEnvironmentVariable("Path", $newPath, "User")
# Also update current session
$env:Path = "$InstallDir;$env:Path"
Write-Info "Added to user PATH."
Write-Host ""
Write-Info "Installation complete!"
Write-Host ""
Write-Host "To start using Mammouth Code, either:"
Write-Host " 1. Open a new terminal, or"
Write-Host " 2. Run: `$env:Path = '$InstallDir;' + `$env:Path"
Write-Host ""
Write-Host "Then run: mammouth"
}
Install-Mammouth