|
| 1 | +# install.ps1 — download and install gitkit on Windows |
| 2 | +# Usage: irm https://raw.githubusercontent.com/JheisonMB/gitkit/main/install.ps1 | iex |
| 3 | +# |
| 4 | +# Options (set as env vars before running): |
| 5 | +# $env:VERSION = "0.1.0" # pin a specific version |
| 6 | +# $env:INSTALL_DIR = "C:\my\bin" # custom install directory |
| 7 | + |
| 8 | +$ErrorActionPreference = "Stop" |
| 9 | + |
| 10 | +$Repo = "JheisonMB/gitkit" |
| 11 | +$Binary = "gitkit.exe" |
| 12 | +$Target = "x86_64-pc-windows-msvc" |
| 13 | +$InstallDir = if ($env:INSTALL_DIR) { $env:INSTALL_DIR } else { "$env:USERPROFILE\.local\bin" } |
| 14 | + |
| 15 | +function Info($label, $msg) { |
| 16 | + Write-Host " " -NoNewline |
| 17 | + Write-Host $label -ForegroundColor Blue -NoNewline |
| 18 | + Write-Host " $msg" |
| 19 | +} |
| 20 | + |
| 21 | +function Fail($msg) { |
| 22 | + Write-Host " error: $msg" -ForegroundColor Red |
| 23 | + exit 1 |
| 24 | +} |
| 25 | + |
| 26 | +# --- resolve version --- |
| 27 | +if ($env:VERSION) { |
| 28 | + $Tag = "v$($env:VERSION)" |
| 29 | + Info "version" "$Tag (pinned)" |
| 30 | +} else { |
| 31 | + $latest = Invoke-RestMethod "https://api.github.com/repos/$Repo/releases/latest" |
| 32 | + $Tag = $latest.tag_name |
| 33 | + if (-not $Tag) { Fail "Could not resolve latest release tag" } |
| 34 | + Info "version" "$Tag (latest)" |
| 35 | +} |
| 36 | + |
| 37 | +# --- download --- |
| 38 | +$Archive = "gitkit-$Tag-$Target.zip" |
| 39 | +$Url = "https://github.com/$Repo/releases/download/$Tag/$Archive" |
| 40 | +$Tmp = Join-Path $env:TEMP "gitkit-install" |
| 41 | +New-Item -ItemType Directory -Force -Path $Tmp | Out-Null |
| 42 | + |
| 43 | +Info "download" $Url |
| 44 | +try { |
| 45 | + Invoke-WebRequest -Uri $Url -OutFile "$Tmp\$Archive" -UseBasicParsing |
| 46 | +} catch { |
| 47 | + Fail "Download failed: $_`nURL: $Url" |
| 48 | +} |
| 49 | + |
| 50 | +# --- extract --- |
| 51 | +Expand-Archive -Path "$Tmp\$Archive" -DestinationPath $Tmp -Force |
| 52 | +$extracted = Join-Path $Tmp $Binary |
| 53 | +if (-not (Test-Path $extracted)) { Fail "Binary not found in archive" } |
| 54 | + |
| 55 | +# --- install --- |
| 56 | +New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null |
| 57 | +Copy-Item $extracted "$InstallDir\$Binary" -Force |
| 58 | +Info "installed" "$InstallDir\$Binary" |
| 59 | + |
| 60 | +# --- ensure PATH --- |
| 61 | +$userPath = [Environment]::GetEnvironmentVariable("PATH", "User") |
| 62 | +if ($userPath -notlike "*$InstallDir*") { |
| 63 | + [Environment]::SetEnvironmentVariable("PATH", "$InstallDir;$userPath", "User") |
| 64 | + $env:PATH = "$InstallDir;$env:PATH" |
| 65 | + Info "updated" "User PATH" |
| 66 | +} |
| 67 | + |
| 68 | +# --- cleanup --- |
| 69 | +Remove-Item $Tmp -Recurse -Force |
| 70 | + |
| 71 | +# --- verify --- |
| 72 | +$ver = & "$InstallDir\$Binary" --version 2>$null |
| 73 | +Info "done" $ver |
| 74 | +Write-Host "" |
| 75 | +Info "ready" "Run 'gitkit hooks init commit-msg conventional-commits' to get started!" |
0 commit comments