|
| 1 | +#Requires -Version 5.1 |
| 2 | +[CmdletBinding()] |
| 3 | +param ( |
| 4 | + [string]$Version = "latest", |
| 5 | + [switch]$Uninstall |
| 6 | +) |
| 7 | + |
| 8 | +$ErrorActionPreference = "Stop" |
| 9 | +$ProgressPreference = "SilentlyContinue" |
| 10 | + |
| 11 | +# Configuration |
| 12 | +$owner = "1broseidon" |
| 13 | +$repo = "promptext" |
| 14 | +$binaryName = "promptext.exe" |
| 15 | +$defaultInstallDir = Join-Path $env:LOCALAPPDATA "promptext" |
| 16 | + |
| 17 | +function Write-Status { |
| 18 | + param([string]$Message) |
| 19 | + Write-Host "→ $Message" -ForegroundColor Blue |
| 20 | +} |
| 21 | + |
| 22 | +function Get-LatestRelease { |
| 23 | + $url = "https://api.github.com/repos/$owner/$repo/releases/latest" |
| 24 | + $release = Invoke-RestMethod -Uri $url -UseBasicParsing |
| 25 | + return $release |
| 26 | +} |
| 27 | + |
| 28 | +function Get-OSInfo { |
| 29 | + $arch = switch ($env:PROCESSOR_ARCHITECTURE) { |
| 30 | + "AMD64" { "x86_64" } |
| 31 | + "ARM64" { "arm64" } |
| 32 | + default { |
| 33 | + throw "Unsupported architecture: $env:PROCESSOR_ARCHITECTURE" |
| 34 | + } |
| 35 | + } |
| 36 | + return @{ |
| 37 | + OS = "Windows" |
| 38 | + Arch = $arch |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +function Get-AssetUrl { |
| 43 | + param($Release) |
| 44 | + $osInfo = Get-OSInfo |
| 45 | + $patterns = @( |
| 46 | + "promptext_$($osInfo.OS)_$($osInfo.Arch).zip", |
| 47 | + "promptext-$($osInfo.OS)-$($osInfo.Arch).zip" |
| 48 | + ) |
| 49 | + |
| 50 | + Write-Host "Looking for release assets:" -ForegroundColor Yellow |
| 51 | + foreach ($pattern in $patterns) { |
| 52 | + Write-Host "- $pattern" |
| 53 | + } |
| 54 | + Write-Host "`nAvailable assets:" -ForegroundColor Yellow |
| 55 | + $Release.assets | ForEach-Object { Write-Host "- $($_.name)" } |
| 56 | + |
| 57 | + foreach ($pattern in $patterns) { |
| 58 | + $assets = $Release.assets | Where-Object { $_.name -eq $pattern } |
| 59 | + if ($assets) { |
| 60 | + Write-Host "`nFound matching asset: $($assets[0].name)" -ForegroundColor Green |
| 61 | + return $assets[0].browser_download_url |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + throw "Could not find compatible Windows binary. Please report this issue." |
| 66 | +} |
| 67 | + |
| 68 | +function Uninstall-Promptext { |
| 69 | + Write-Status "Uninstalling promptext..." |
| 70 | + |
| 71 | + # Remove from PATH |
| 72 | + $currentPath = [Environment]::GetEnvironmentVariable("Path", "User") |
| 73 | + if ($currentPath -like "*$defaultInstallDir*") { |
| 74 | + $newPath = ($currentPath.Split(';') | Where-Object { $_ -ne $defaultInstallDir }) -join ';' |
| 75 | + [Environment]::SetEnvironmentVariable("Path", $newPath, "User") |
| 76 | + $env:Path = ($env:Path.Split(';') | Where-Object { $_ -ne $defaultInstallDir }) -join ';' |
| 77 | + } |
| 78 | + |
| 79 | + # Remove installation directory |
| 80 | + if (Test-Path $defaultInstallDir) { |
| 81 | + Remove-Item -Path $defaultInstallDir -Recurse -Force |
| 82 | + } |
| 83 | + |
| 84 | + # Remove alias from profile |
| 85 | + if (Test-Path $PROFILE.CurrentUserCurrentHost) { |
| 86 | + $content = Get-Content $PROFILE.CurrentUserCurrentHost | Where-Object { $_ -notmatch "Set-Alias.*prx.*promptext" } |
| 87 | + Set-Content -Path $PROFILE.CurrentUserCurrentHost -Value $content |
| 88 | + } |
| 89 | + |
| 90 | + Write-Host "Promptext has been uninstalled successfully." -ForegroundColor Green |
| 91 | + exit 0 |
| 92 | +} |
| 93 | + |
| 94 | +function Verify-Checksum { |
| 95 | + param( |
| 96 | + $Release, |
| 97 | + $FilePath, |
| 98 | + $AssetName |
| 99 | + ) |
| 100 | + |
| 101 | + $checksumAsset = $Release.assets | Where-Object { |
| 102 | + $_.name -eq "checksums.txt" -or |
| 103 | + $_.name -eq "SHA256SUMS" -or |
| 104 | + $_.name -eq "sha256sums.txt" |
| 105 | + } |
| 106 | + if (-not $checksumAsset) { |
| 107 | + Write-Warning "Skipping checksum verification: checksum file not found in release" |
| 108 | + return |
| 109 | + } |
| 110 | + |
| 111 | + Write-Status "Verifying checksum..." |
| 112 | + Write-Host "Asset name: $AssetName" |
| 113 | + $checksumUrl = $checksumAsset.browser_download_url |
| 114 | + $checksumContent = (Invoke-WebRequest -Uri $checksumUrl -UseBasicParsing).Content |
| 115 | + Write-Host "Checksum content:" |
| 116 | + Write-Host $checksumContent |
| 117 | + $expectedChecksum = ($checksumContent -split "`n" | Where-Object { $_ -like "*$AssetName*" }) -split '\s+' | Select-Object -First 1 |
| 118 | + |
| 119 | + if (-not $expectedChecksum) { |
| 120 | + Write-Warning "Skipping checksum verification: checksum not found for $AssetName" |
| 121 | + return |
| 122 | + } |
| 123 | + |
| 124 | + $actualChecksum = Get-FileHash -Path $FilePath -Algorithm SHA256 | Select-Object -ExpandProperty Hash |
| 125 | + if ($actualChecksum -ne $expectedChecksum) { |
| 126 | + throw "Checksum verification failed.`nExpected: $expectedChecksum`nGot: $actualChecksum" |
| 127 | + } |
| 128 | + |
| 129 | + Write-Status "Checksum verification successful" |
| 130 | +} |
| 131 | + |
| 132 | +try { |
| 133 | + # Handle execution policy |
| 134 | + $policy = Get-ExecutionPolicy |
| 135 | + if ($policy -eq "Restricted") { |
| 136 | + Write-Status "Setting execution policy to RemoteSigned for current process..." |
| 137 | + Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned -Force |
| 138 | + } |
| 139 | + |
| 140 | + if ($Uninstall) { |
| 141 | + Uninstall-Promptext |
| 142 | + exit 0 |
| 143 | + } |
| 144 | + |
| 145 | + $osInfo = Get-OSInfo |
| 146 | + Write-Status "Installing promptext..." |
| 147 | + Write-Status "OS: $($osInfo.OS)" |
| 148 | + Write-Status "Architecture: $($osInfo.Arch)" |
| 149 | + Write-Status "Installation directory: $defaultInstallDir" |
| 150 | + |
| 151 | + # Create install directory |
| 152 | + if (-not (Test-Path $defaultInstallDir)) { |
| 153 | + New-Item -ItemType Directory -Path $defaultInstallDir | Out-Null |
| 154 | + } |
| 155 | + |
| 156 | + # Get latest release info |
| 157 | + Write-Status "Fetching release information..." |
| 158 | + $release = Get-LatestRelease |
| 159 | + $downloadUrl = Get-AssetUrl $release |
| 160 | + |
| 161 | + # Download and verify |
| 162 | + Write-Status "Downloading binary..." |
| 163 | + $zipPath = Join-Path $env:TEMP "promptext.zip" |
| 164 | + $assetName = $downloadUrl.Split('/')[-1] |
| 165 | + Invoke-WebRequest -Uri $downloadUrl -OutFile $zipPath -UseBasicParsing |
| 166 | + Verify-Checksum -Release $release -FilePath $zipPath -AssetName $assetName |
| 167 | + |
| 168 | + Write-Status "Extracting files..." |
| 169 | + try { |
| 170 | + Add-Type -AssemblyName System.IO.Compression.FileSystem |
| 171 | + $zip = [System.IO.Compression.ZipFile]::OpenRead($zipPath) |
| 172 | + |
| 173 | + # Find the executable entry |
| 174 | + $exeEntry = $zip.Entries | Where-Object { |
| 175 | + $_.Name -eq "promptext.exe" -and |
| 176 | + -not $_.FullName.Contains("../") -and |
| 177 | + -not $_.FullName.StartsWith("/") |
| 178 | + } | Select-Object -First 1 |
| 179 | + |
| 180 | + if (-not $exeEntry) { |
| 181 | + throw "Could not find valid promptext.exe in the archive" |
| 182 | + } |
| 183 | + |
| 184 | + # Extract the executable |
| 185 | + $exePath = Join-Path $defaultInstallDir "promptext.exe" |
| 186 | + [System.IO.Compression.ZipFileExtensions]::ExtractToFile($exeEntry, $exePath, $true) |
| 187 | + |
| 188 | + } catch { |
| 189 | + throw "Failed to extract executable: $_" |
| 190 | + } finally { |
| 191 | + if ($zip) { |
| 192 | + $zip.Dispose() |
| 193 | + } |
| 194 | + Remove-Item $zipPath -ErrorAction SilentlyContinue |
| 195 | + } |
| 196 | + |
| 197 | + # Update PATH |
| 198 | + $currentPath = [Environment]::GetEnvironmentVariable("Path", "User") |
| 199 | + if (-not ($currentPath -like "*$defaultInstallDir*")) { |
| 200 | + $newPath = "$currentPath;$defaultInstallDir" |
| 201 | + [Environment]::SetEnvironmentVariable("Path", $newPath, "User") |
| 202 | + $env:Path = "$env:Path;$defaultInstallDir" |
| 203 | + Write-Status "Added $defaultInstallDir to PATH" |
| 204 | + } |
| 205 | + |
| 206 | + # Add alias to PowerShell profile |
| 207 | + $profileDir = Split-Path $PROFILE.CurrentUserCurrentHost -Parent |
| 208 | + if (-not (Test-Path $profileDir)) { |
| 209 | + New-Item -ItemType Directory -Path $profileDir -Force | Out-Null |
| 210 | + } |
| 211 | + if (-not (Test-Path $PROFILE.CurrentUserCurrentHost)) { |
| 212 | + New-Item -ItemType File -Path $PROFILE.CurrentUserCurrentHost -Force | Out-Null |
| 213 | + } |
| 214 | + |
| 215 | + $aliasLine = "Set-Alias prx '$defaultInstallDir\promptext.exe'" |
| 216 | + if (-not (Select-String -Path $PROFILE.CurrentUserCurrentHost -Pattern "Set-Alias.*prx.*promptext" -Quiet)) { |
| 217 | + Add-Content -Path $PROFILE.CurrentUserCurrentHost -Value $aliasLine |
| 218 | + Write-Status "Added 'prx' alias to PowerShell profile" |
| 219 | + } |
| 220 | + |
| 221 | + # Verify installation |
| 222 | + $promptextPath = Join-Path $defaultInstallDir "promptext.exe" |
| 223 | + if (-not (Test-Path $promptextPath)) { |
| 224 | + throw "Installation failed: promptext.exe not found at $promptextPath" |
| 225 | + } |
| 226 | + |
| 227 | + # Test the installation |
| 228 | + try { |
| 229 | + $version = & $promptextPath -v |
| 230 | + Write-Status "Installation verified: $version" |
| 231 | + } catch { |
| 232 | + Write-Warning "Installation completed but verification failed: $_" |
| 233 | + } |
| 234 | + |
| 235 | + Write-Host "`n✨ Installation complete!" -ForegroundColor Green |
| 236 | + Write-Host "You can use either 'promptext' or 'prx' command after restarting your terminal." -ForegroundColor Yellow |
| 237 | + Write-Host "To uninstall, run this script with -Uninstall flag" -ForegroundColor Yellow |
| 238 | + |
| 239 | +} catch { |
| 240 | + Write-Host "Error: $_" -ForegroundColor Red |
| 241 | + exit 1 |
| 242 | +} |
0 commit comments