param( [Parameter(Mandatory=$false)] [string]$Tool ) $ErrorActionPreference = "Stop" $ProgressPreference = "SilentlyContinue" $baseUrl = "https://cli.whalerider.org" # ------------------------------------------------ # Resolve tool # ------------------------------------------------ if (-not $Tool -and $env:WR_TOOL) { $Tool = $env:WR_TOOL } if (-not $Tool) { $Tool = "wr" } $Tool = $Tool.ToLower() $validTools = @("wr","wrctl") if ($validTools -notcontains $Tool) { throw "Invalid tool '$Tool'. Supported tools: wr, wrctl" } # ------------------------------------------------ # Header # ------------------------------------------------ Write-Host "" Write-Host "Installing WhaleRider CLI ($Tool)..." Write-Host "" Write-Host "Using source: $baseUrl" Write-Host "" # ------------------------------------------------ # Resolve latest version # ------------------------------------------------ Write-Host "Checking latest version..." $version = (Invoke-RestMethod "$baseUrl/latest").Trim() if ([string]::IsNullOrWhiteSpace($version)) { throw "Could not determine latest version" } Write-Host "Latest version: $version" Write-Host "" # ------------------------------------------------ # Detect architecture # ------------------------------------------------ $arch = $env:PROCESSOR_ARCHITECTURE switch ($arch) { "AMD64" { $arch = "x64" } "ARM64" { $arch = "arm64" } default { $arch = "x64" } } Write-Host "OS: windows" Write-Host "Architecture: $arch" Write-Host "" # ------------------------------------------------ # Resolve artifact # ------------------------------------------------ $artifactName = "$Tool.zip" $downloadUrl = "$baseUrl/releases/$version/$Tool/windows/$arch/$artifactName" Write-Host "Downloading $downloadUrl" Write-Host "" # ------------------------------------------------ # Paths # ------------------------------------------------ $installRoot = Join-Path $HOME ".wr" $tempRoot = Join-Path $env:TEMP "wr-install-$Tool-$version" $tempZip = Join-Path $env:TEMP "$Tool-$version.zip" # Clean temp if (Test-Path $tempRoot) { Remove-Item $tempRoot -Recurse -Force } New-Item -ItemType Directory -Force -Path $tempRoot | Out-Null New-Item -ItemType Directory -Force -Path $installRoot | Out-Null # ------------------------------------------------ # Download # ------------------------------------------------ Invoke-WebRequest ` -Uri $downloadUrl ` -OutFile $tempZip # ------------------------------------------------ # Extract to temp # ------------------------------------------------ Write-Host "Extracting..." Expand-Archive ` -Path $tempZip ` -DestinationPath $tempRoot ` -Force Remove-Item $tempZip -Force # ------------------------------------------------ # Locate executable (handles nested zips) # ------------------------------------------------ $exeName = "$Tool.exe" $exe = Get-ChildItem ` -Path $tempRoot ` -Recurse ` -Filter $exeName ` | Select-Object -First 1 if (-not $exe) { throw "Installation failed: $exeName not found in archive" } # ------------------------------------------------ # Install (overwrite existing safely) # ------------------------------------------------ $targetExe = Join-Path $installRoot $exeName Write-Host "Installing to $installRoot" Copy-Item ` -Path $exe.FullName ` -Destination $targetExe ` -Force # ------------------------------------------------ # Ensure logs directory exists # ------------------------------------------------ $logsDir = Join-Path $installRoot "logs" New-Item -ItemType Directory -Force -Path $logsDir | Out-Null # ------------------------------------------------ # Add to PATH # ------------------------------------------------ $currentPath = [Environment]::GetEnvironmentVariable( "PATH", "User" ) if ($currentPath -notlike "*$installRoot*") { Write-Host "Adding $installRoot to PATH" [Environment]::SetEnvironmentVariable( "PATH", "$currentPath;$installRoot", "User" ) } # ------------------------------------------------ # Cleanup temp # ------------------------------------------------ Remove-Item $tempRoot -Recurse -Force # ------------------------------------------------ # Verify installation # ------------------------------------------------ if (-not (Test-Path $targetExe)) { throw "Installation failed: executable not found after install" } # ------------------------------------------------ # Done # ------------------------------------------------ Write-Host "" Write-Host "WhaleRider CLI installed successfully!" Write-Host "" Write-Host "Run:" Write-Host " $Tool --help" Write-Host "" Write-Host "Installed location:" Write-Host " $installRoot" Write-Host "" Write-Host "Logs:" Write-Host " $(Join-Path $logsDir "$Tool.log")" Write-Host "" Write-Host "If the command is not found, restart the terminal." Write-Host ""