|
| 1 | +$ErrorActionPreference = 'Stop' |
| 2 | + |
| 3 | +$Repo = if ($env:FREE_CODE_REPO) { $env:FREE_CODE_REPO } else { 'paoloanzn/free-code' } |
| 4 | +$Version = if ($env:FREE_CODE_VERSION) { $env:FREE_CODE_VERSION } else { 'latest' } |
| 5 | +$InstallRoot = if ($env:FREE_CODE_HOME) { $env:FREE_CODE_HOME } else { Join-Path $HOME 'AppData\Local\free-code' } |
| 6 | +$BinDir = if ($env:FREE_CODE_BIN_DIR) { $env:FREE_CODE_BIN_DIR } else { Join-Path $InstallRoot 'bin' } |
| 7 | +$SourceDir = Join-Path $InstallRoot 'src' |
| 8 | +$BunMinVersion = '1.3.11' |
| 9 | + |
| 10 | +function Write-Info($Message) { Write-Host "[*] $Message" -ForegroundColor Cyan } |
| 11 | +function Write-Ok($Message) { Write-Host "[+] $Message" -ForegroundColor Green } |
| 12 | +function Write-Warn($Message) { Write-Host "[!] $Message" -ForegroundColor Yellow } |
| 13 | +function Fail($Message) { throw $Message } |
| 14 | + |
| 15 | +function Get-Arch { |
| 16 | + $arch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString().ToLowerInvariant() |
| 17 | + switch ($arch) { |
| 18 | + 'x64' { return 'x64' } |
| 19 | + 'arm64' { return 'arm64' } |
| 20 | + default { Fail "Unsupported Windows architecture: $arch" } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +function Get-AssetName { |
| 25 | + $arch = Get-Arch |
| 26 | + return "free-code-windows-$arch.zip" |
| 27 | +} |
| 28 | + |
| 29 | +function Get-ReleaseUrl { |
| 30 | + param([string]$AssetName) |
| 31 | + |
| 32 | + if ($Version -eq 'latest') { |
| 33 | + return "https://github.com/$Repo/releases/latest/download/$AssetName" |
| 34 | + } |
| 35 | + |
| 36 | + return "https://github.com/$Repo/releases/download/$Version/$AssetName" |
| 37 | +} |
| 38 | + |
| 39 | +function Ensure-UserPath { |
| 40 | + if (-not (Test-Path -LiteralPath $BinDir)) { |
| 41 | + New-Item -ItemType Directory -Path $BinDir -Force | Out-Null |
| 42 | + } |
| 43 | + |
| 44 | + $currentUserPath = [Environment]::GetEnvironmentVariable('Path', 'User') |
| 45 | + $pathParts = @() |
| 46 | + if ($currentUserPath) { |
| 47 | + $pathParts = $currentUserPath -split ';' | Where-Object { $_ } |
| 48 | + } |
| 49 | + |
| 50 | + if ($pathParts -contains $BinDir) { |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + $newUserPath = if ($currentUserPath) { "$currentUserPath;$BinDir" } else { $BinDir } |
| 55 | + [Environment]::SetEnvironmentVariable('Path', $newUserPath, 'User') |
| 56 | + Write-Warn "$BinDir was added to your user PATH. Open a new terminal after install." |
| 57 | +} |
| 58 | + |
| 59 | +function Install-FromRelease { |
| 60 | + $assetName = Get-AssetName |
| 61 | + $url = Get-ReleaseUrl -AssetName $assetName |
| 62 | + $tempRoot = Join-Path ([System.IO.Path]::GetTempPath()) ("free-code-install-" + [guid]::NewGuid().ToString('N')) |
| 63 | + $archivePath = Join-Path $tempRoot $assetName |
| 64 | + $extractPath = Join-Path $tempRoot 'extract' |
| 65 | + |
| 66 | + New-Item -ItemType Directory -Path $extractPath -Force | Out-Null |
| 67 | + |
| 68 | + try { |
| 69 | + Write-Info "Downloading prebuilt package..." |
| 70 | + Invoke-WebRequest -Uri $url -OutFile $archivePath |
| 71 | + |
| 72 | + Expand-Archive -LiteralPath $archivePath -DestinationPath $extractPath -Force |
| 73 | + |
| 74 | + New-Item -ItemType Directory -Path $BinDir -Force | Out-Null |
| 75 | + Copy-Item -LiteralPath (Join-Path $extractPath 'free-code\free-code.exe') -Destination (Join-Path $BinDir 'free-code.exe') -Force |
| 76 | + |
| 77 | + Set-Content -LiteralPath (Join-Path $BinDir 'free-code.cmd') -Value "@echo off`r`n""%~dp0\free-code.exe"" %*`r`n" -NoNewline |
| 78 | + Set-Content -LiteralPath (Join-Path $BinDir 'free-code.ps1') -Value '$exe = Join-Path $PSScriptRoot ''free-code.exe''; & $exe @args; exit $LASTEXITCODE' -NoNewline |
| 79 | + |
| 80 | + Write-Ok "Installed prebuilt binary to $BinDir\free-code.exe" |
| 81 | + return $true |
| 82 | + } catch { |
| 83 | + Write-Warn "Prebuilt download failed: $($_.Exception.Message)" |
| 84 | + return $false |
| 85 | + } finally { |
| 86 | + if (Test-Path -LiteralPath $tempRoot) { |
| 87 | + Remove-Item -LiteralPath $tempRoot -Recurse -Force |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +function Version-Gte { |
| 93 | + param([string]$A, [string]$B) |
| 94 | + $versionA = [version]($A -replace '[^0-9\.].*$', '') |
| 95 | + $versionB = [version]($B -replace '[^0-9\.].*$', '') |
| 96 | + return $versionA -ge $versionB |
| 97 | +} |
| 98 | + |
| 99 | +function Ensure-Bun { |
| 100 | + $bun = Get-Command bun -ErrorAction SilentlyContinue |
| 101 | + if ($bun) { |
| 102 | + $version = (& $bun.Source --version).Trim() |
| 103 | + if (Version-Gte -A $version -B $BunMinVersion) { |
| 104 | + Write-Ok "bun: v$version" |
| 105 | + return |
| 106 | + } |
| 107 | + |
| 108 | + Write-Warn "bun v$version is too old. Upgrading..." |
| 109 | + } else { |
| 110 | + Write-Info 'Bun not found. Installing for source fallback...' |
| 111 | + } |
| 112 | + |
| 113 | + irm https://bun.sh/install.ps1 | iex |
| 114 | + $env:PATH = "$HOME\.bun\bin;$env:PATH" |
| 115 | + |
| 116 | + if (-not (Get-Command bun -ErrorAction SilentlyContinue)) { |
| 117 | + Fail 'Bun installation completed but bun is not on PATH.' |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +function Install-FromSource { |
| 122 | + if (-not (Get-Command git -ErrorAction SilentlyContinue)) { |
| 123 | + Fail 'git is required for source fallback but was not found.' |
| 124 | + } |
| 125 | + |
| 126 | + Ensure-Bun |
| 127 | + |
| 128 | + if (Test-Path -LiteralPath (Join-Path $SourceDir '.git')) { |
| 129 | + Write-Info 'Updating existing source checkout...' |
| 130 | + try { |
| 131 | + git -C $SourceDir pull --ff-only origin main | Out-Host |
| 132 | + } catch { |
| 133 | + Write-Warn 'git pull failed, using existing source checkout.' |
| 134 | + } |
| 135 | + } else { |
| 136 | + New-Item -ItemType Directory -Path $InstallRoot -Force | Out-Null |
| 137 | + Write-Info 'Cloning source repository...' |
| 138 | + git clone --depth 1 "https://github.com/$Repo.git" $SourceDir | Out-Host |
| 139 | + } |
| 140 | + |
| 141 | + Push-Location $SourceDir |
| 142 | + try { |
| 143 | + Write-Info 'Building from source...' |
| 144 | + bun install --frozen-lockfile 2>$null |
| 145 | + if ($LASTEXITCODE -ne 0) { |
| 146 | + bun install | Out-Host |
| 147 | + } |
| 148 | + bun run build:dev:full | Out-Host |
| 149 | + } finally { |
| 150 | + Pop-Location |
| 151 | + } |
| 152 | + |
| 153 | + New-Item -ItemType Directory -Path $BinDir -Force | Out-Null |
| 154 | + Copy-Item -LiteralPath (Join-Path $SourceDir 'cli-dev.exe') -Destination (Join-Path $BinDir 'free-code.exe') -Force |
| 155 | + Set-Content -LiteralPath (Join-Path $BinDir 'free-code.cmd') -Value "@echo off`r`n""%~dp0\free-code.exe"" %*`r`n" -NoNewline |
| 156 | + Set-Content -LiteralPath (Join-Path $BinDir 'free-code.ps1') -Value '$exe = Join-Path $PSScriptRoot ''free-code.exe''; & $exe @args; exit $LASTEXITCODE' -NoNewline |
| 157 | + Write-Ok "Installed source-built binary to $BinDir\free-code.exe" |
| 158 | +} |
| 159 | + |
| 160 | +Write-Host '' |
| 161 | +Write-Host 'free-code Windows installer' -ForegroundColor Cyan |
| 162 | +Write-Host '' |
| 163 | + |
| 164 | +if (-not (Install-FromRelease)) { |
| 165 | + Write-Warn 'Falling back to source build.' |
| 166 | + Install-FromSource |
| 167 | +} |
| 168 | + |
| 169 | +Ensure-UserPath |
| 170 | + |
| 171 | +Write-Host '' |
| 172 | +Write-Host 'Installation complete.' -ForegroundColor Green |
| 173 | +Write-Host "Run: $BinDir\free-code.exe" |
| 174 | +Write-Host 'Then set ANTHROPIC_API_KEY and launch free-code.' |
0 commit comments