|
| 1 | +# Installs the latest ritobin-tools release for Windows (user scope) |
| 2 | + |
| 3 | +param( |
| 4 | + [string]$Owner = "LeagueToolkit", |
| 5 | + [string]$Repo = "ritobin-tools", |
| 6 | + [string]$Channel = "windows-x64", |
| 7 | + [string]$InstallDir = "$env:LOCALAPPDATA\LeagueToolkit\ritobin-tools" |
| 8 | +) |
| 9 | + |
| 10 | +$ErrorActionPreference = 'Stop' |
| 11 | + |
| 12 | +Write-Host "Installing ritobin-tools..." -ForegroundColor Cyan |
| 13 | + |
| 14 | +if (!(Test-Path -LiteralPath $InstallDir)) { |
| 15 | + New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null |
| 16 | +} |
| 17 | + |
| 18 | +# Get latest release metadata |
| 19 | +$releaseApi = "https://api.github.com/repos/$Owner/$Repo/releases/latest" |
| 20 | +try { |
| 21 | + $release = Invoke-RestMethod -Uri $releaseApi -Headers @{ 'User-Agent' = 'ritobin-tools-installer' } |
| 22 | +} catch { |
| 23 | + throw "Failed to query GitHub releases: $($_.Exception.Message)" |
| 24 | +} |
| 25 | + |
| 26 | +$tag = $release.tag_name |
| 27 | +# Extract the first semantic version (handles tags like "v0.1.1" or "ritobin-tools-v0.1.1") |
| 28 | +$match = [regex]::Match($tag, '\d+\.\d+\.\d+([\-\+][A-Za-z0-9\.-]+)?') |
| 29 | +$version = if ($match.Success) { $match.Value } else { $tag.TrimStart('v') } |
| 30 | + |
| 31 | +$assetName = "ritobin-tools-$version-$Channel.zip" |
| 32 | +$asset = $release.assets | Where-Object { $_.name -eq $assetName } | Select-Object -First 1 |
| 33 | +if (-not $asset) { |
| 34 | + # Fallback: find any ritobin-tools asset matching the channel |
| 35 | + $pattern = "^ritobin-tools-.*-" + [regex]::Escape($Channel) + "\.zip$" |
| 36 | + $asset = $release.assets | Where-Object { $_.name -match $pattern } | Select-Object -First 1 |
| 37 | +} |
| 38 | +if (-not $asset) { |
| 39 | + throw "Could not find asset matching '$assetName' (channel $Channel) in the latest release." |
| 40 | +} |
| 41 | +if ($asset.name -ne $assetName) { $assetName = $asset.name } |
| 42 | + |
| 43 | +$zipPath = Join-Path $env:TEMP $assetName |
| 44 | +Write-Host "Downloading $assetName ($version)..." -ForegroundColor Yellow |
| 45 | +Invoke-WebRequest -Uri $asset.browser_download_url -OutFile $zipPath -UseBasicParsing |
| 46 | + |
| 47 | +Write-Host "Extracting to $InstallDir" -ForegroundColor Yellow |
| 48 | +Expand-Archive -Path $zipPath -DestinationPath $InstallDir -Force |
| 49 | + |
| 50 | +# Create a shim directory so PATH is simple and stable |
| 51 | +$binDir = Join-Path $InstallDir 'bin' |
| 52 | +if (!(Test-Path -LiteralPath $binDir)) { New-Item -ItemType Directory -Path $binDir | Out-Null } |
| 53 | + |
| 54 | +# Ensure the executable exists |
| 55 | +$exePath = Join-Path $InstallDir 'ritobin-tools.exe' |
| 56 | +if (!(Test-Path -LiteralPath $exePath)) { |
| 57 | + throw "ritobin-tools.exe not found after extraction: $exePath" |
| 58 | +} |
| 59 | + |
| 60 | +# Place a thin cmd shim in bin to avoid spaces in paths and simplify PATH updates |
| 61 | +$shimCmd = @" |
| 62 | +@echo off |
| 63 | +"$exePath" %* |
| 64 | +"@ |
| 65 | +Set-Content -LiteralPath (Join-Path $binDir 'ritobin-tools.cmd') -Value $shimCmd -Encoding Ascii -Force |
| 66 | + |
| 67 | +# Add to user PATH if missing |
| 68 | +$currentPath = [Environment]::GetEnvironmentVariable('Path', 'User') |
| 69 | +if (-not ($currentPath -split ';' | Where-Object { $_ -eq $binDir })) { |
| 70 | + $newPath = if ([string]::IsNullOrEmpty($currentPath)) { $binDir } else { "$currentPath;$binDir" } |
| 71 | + [Environment]::SetEnvironmentVariable('Path', $newPath, 'User') |
| 72 | + Write-Host "Added to PATH (User): $binDir" -ForegroundColor Green |
| 73 | +} else { |
| 74 | + Write-Host "PATH already contains: $binDir" -ForegroundColor Green |
| 75 | +} |
| 76 | + |
| 77 | +Write-Host "Installed ritobin-tools $version to $InstallDir" -ForegroundColor Green |
| 78 | +Write-Host "Open a new terminal and run: ritobin-tools --help" -ForegroundColor Cyan |
| 79 | + |
0 commit comments