#Requires -Version 5.1 <# .SYNOPSIS Installs OpenCode (open-source AI coding agent) on Windows. .DESCRIPTION Auto-detects the latest release from https://github.com/anomalyco/opencode/releases, downloads the zip file matching your Windows architecture, extracts opencode.exe to $env:USERPROFILE\.local\bin, and adds the directory to your User PATH. Supported architectures: - x64 (AMD64 / Intel 64-bit) - ARM64 (Qualcomm Snapdragon, ARM 64-bit) .PARAMETER InstallDir Directory to install opencode.exe. Default: $env:USERPROFILE\.local\bin .PARAMETER AddToPath Whether to permanently add InstallDir to user PATH. Default: $true .EXAMPLE .\install-opencode.ps1 # One-liner install from remote (works on Windows 10+): powershell -ExecutionPolicy Bypass -c "[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; $t=\"$env:TEMP\install-opencode.ps1\"; iwr -useb 'https://gl.isitsoft.kr/publicproj/install-sh/-/raw/main/install-opencode.ps1' -OutFile $t; & $t" .NOTES If execution policy error occurs: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser #> [CmdletBinding()] param( [string] $InstallDir = "$env:USERPROFILE\.local\bin", [bool] $AddToPath = $true ) Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' # Enforce TLS 1.2+ for HTTPS downloads (required on Windows 10 / PowerShell 5.1) [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 # ──────────────────────────────────────────── # Constants # ──────────────────────────────────────────── $REPO = "anomalyco/opencode" $API_URL = "https://api.github.com/repos/$REPO/releases/latest" $FALLBACK_TAG = "v1.18.35" # ──────────────────────────────────────────── # Utility functions (English only to prevent Windows console encoding issues) # ──────────────────────────────────────────── function Info { Write-Host "[INFO] $args" -ForegroundColor Cyan } function Warn { Write-Host "[WARN] $args" -ForegroundColor Yellow } function Success { Write-Host "[OK] $args" -ForegroundColor Green } function ErrExit { Write-Host "[ERROR] $args" -ForegroundColor Red; exit 1 } # ──────────────────────────────────────────── # Platform detection # ──────────────────────────────────────────── function Get-Platform { $arch = $env:PROCESSOR_ARCHITEW6432 if (-not $arch) { $arch = $env:PROCESSOR_ARCHITECTURE } switch -Regex ($arch) { '^(AMD64|x64)$' { return 'windows-x64' } '^(ARM64)$' { return 'windows-arm64' } default { ErrExit "Unsupported CPU architecture: '$arch'. Supported: x64, ARM64." } } } # ──────────────────────────────────────────── # Fetch latest release tag and asset URL # ──────────────────────────────────────────── function Get-ReleaseInfo { param([string]$Platform) Info "Fetching latest OpenCode release from GitHub..." $tag = $FALLBACK_TAG $assetUrl = $null $targetPattern = "opencode-${Platform}.zip" try { $headers = @{ 'User-Agent' = 'install-opencode-ps1' } $release = Invoke-RestMethod -Uri $API_URL -Headers $headers -TimeoutSec 10 -UseBasicParsing if ($release -and $release.tag_name) { $tag = $release.tag_name $asset = $release.assets | Where-Object { $_.name -like "*${Platform}.zip" } | Select-Object -First 1 if ($asset -and $asset.browser_download_url) { $assetUrl = $asset.browser_download_url } } } catch { Warn "GitHub API lookup failed: $_" Warn "Using fallback release tag: $FALLBACK_TAG" } if (-not $assetUrl) { $cleanTag = $tag.TrimStart('v') $assetUrl = "https://github.com/$REPO/releases/download/$tag/opencode-$Platform.zip" } return @{ Tag = $tag Url = $assetUrl } } # ──────────────────────────────────────────── # Download and install binary # ──────────────────────────────────────────── function Install-OpenCode { param( [string]$Url, [string]$TargetDir ) if (-not (Test-Path $TargetDir)) { New-Item -ItemType Directory -Path $TargetDir -Force | Out-Null } $tmpDir = Join-Path $env:TEMP "opencode_install_$(Get-Random)" New-Item -ItemType Directory -Path $tmpDir -Force | Out-Null $zipPath = Join-Path $tmpDir "opencode.zip" try { Info "Downloading OpenCode archive..." Info "URL: $Url" $ProgressPreference = 'SilentlyContinue' Invoke-WebRequest -Uri $Url -OutFile $zipPath -UseBasicParsing $ProgressPreference = 'Continue' if (-not (Test-Path $zipPath)) { ErrExit "Failed to download OpenCode archive from $Url" } Info "Extracting opencode.exe to $TargetDir..." Add-Type -AssemblyName System.IO.Compression.FileSystem $archive = [System.IO.Compression.ZipFile]::OpenRead($zipPath) $extracted = $false foreach ($entry in $archive.Entries) { if ($entry.Name -eq "opencode.exe") { $destFile = Join-Path $TargetDir "opencode.exe" [System.IO.Compression.ZipFileExtensions]::ExtractToFile($entry, $destFile, $true) $extracted = $true break } } $archive.Dispose() if (-not $extracted) { ErrExit "opencode.exe was not found inside the downloaded archive." } Success "opencode.exe installed successfully into $TargetDir" } finally { if (Test-Path $tmpDir) { Remove-Item -Path $tmpDir -Recurse -Force -ErrorAction SilentlyContinue } } } # ──────────────────────────────────────────── # Register PATH environment variable # ──────────────────────────────────────────── function Update-PathEnvironment { param([string]$Dir) $normalizedDir = $Dir.TrimEnd('\') # Current session PATH if ($env:Path -notlike "*$normalizedDir*") { $env:Path = "$normalizedDir;$env:Path" Info "Added $normalizedDir to current session PATH." } # Permanent User PATH in Windows Registry $userPath = [System.Environment]::GetEnvironmentVariable('Path', 'User') if (-not $userPath) { $userPath = '' } $pathParts = $userPath -split ';' | Where-Object { $_ -ne '' } $alreadyInPath = $false foreach ($p in $pathParts) { if ($p.Trim().TrimEnd('\') -eq $normalizedDir) { $alreadyInPath = $true break } } if (-not $alreadyInPath) { $newUserPath = if ($userPath) { "$normalizedDir;$userPath" } else { $normalizedDir } [System.Environment]::SetEnvironmentVariable('Path', $newUserPath, 'User') Success "Added $normalizedDir to User PATH permanently." Warn "Please open a new terminal window for PATH changes to take full effect." } else { Info "$normalizedDir is already present in User PATH." } } # ──────────────────────────────────────────── # Main Entry Point # ──────────────────────────────────────────── function Main { Info "===== OpenCode install script (Windows PowerShell) =====" Info "Target install directory: $InstallDir" $platform = Get-Platform Info "Detected platform: $platform" $releaseInfo = Get-ReleaseInfo -Platform $platform Info "Release version: $($releaseInfo.Tag)" Install-OpenCode -Url $releaseInfo.Url -TargetDir $InstallDir if ($AddToPath) { Update-PathEnvironment -Dir $InstallDir } else { Warn "Skipping PATH environment update as requested (-AddToPath `$false)." } Success "OpenCode installation completed successfully!" Info "" Info "How to run:" Info " opencode - Launch interactive TUI coding session" Info " opencode auth login - Configure AI model provider accounts" Info " opencode --help - Show help and available commands" Info "" } Main