# SKC Coding Agent Installer for Windows # Usage: irm https://raw.githubusercontent.com/jaybeyond/Sayknow_CLI/main/scripts/install.ps1 | iex # # Or with options: # & ([scriptblock]::Create((irm https://raw.githubusercontent.com/jaybeyond/Sayknow_CLI/main/scripts/install.ps1))) -Source # & ([scriptblock]::Create((irm https://raw.githubusercontent.com/jaybeyond/Sayknow_CLI/main/scripts/install.ps1))) -Binary # & ([scriptblock]::Create((irm https://raw.githubusercontent.com/jaybeyond/Sayknow_CLI/main/scripts/install.ps1))) -Source -Ref v3.20.1 # & ([scriptblock]::Create((irm https://raw.githubusercontent.com/jaybeyond/Sayknow_CLI/main/scripts/install.ps1))) -Source -Ref main # & ([scriptblock]::Create((irm https://raw.githubusercontent.com/jaybeyond/Sayknow_CLI/main/scripts/install.ps1))) -Binary -Ref v3.20.1 param( [switch]$Source, [switch]$Binary, [string]$Ref ) $ErrorActionPreference = "Stop" # Windows PowerShell 5.1 runs on .NET Framework, which may still default to # TLS 1.0; GitHub and bun.sh both require TLS 1.2+, so opt in explicitly. try { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 } catch {} $Repo = "jaybeyond/Sayknow_CLI" $Package = "@sayknow-cli/coding-agent" $InstallDir = if ($env:SKC_INSTALL_DIR) { $env:SKC_INSTALL_DIR } else { "$env:LOCALAPPDATA\skc" } $BinaryName = "skc-windows-x64.exe" $MinimumBunVersion = "1.3.14" $BinaryManifestAsset = "sayknow-release-binaries-v1.json" $BinarySha256Asset = "sayknow-release-binaries.sha256" function Test-BunInstalled { try { $null = Get-Command bun -ErrorAction Stop return $true } catch { return $false } } function Get-BunVersion { try { $versionText = (bun --version 2>$null) if (-not $versionText) { return $null } $clean = $versionText.Trim().Split("-")[0] return [version]$clean } catch { return $null } } function Test-BunVersion { param([string]$MinimumVersion) $currentVersion = Get-BunVersion if (-not $currentVersion) { return $false } return $currentVersion -ge [version]$MinimumVersion } function Assert-BunVersion { param([string]$MinimumVersion) if (-not (Test-BunVersion $MinimumVersion)) { $current = Get-BunVersion $currentText = if ($current) { $current.ToString() } else { "unknown" } throw "Bun $MinimumVersion or newer is required. Current version: $currentText. Upgrade Bun at https://bun.sh/docs/installation" } } function Test-GitInstalled { try { $null = Get-Command git -ErrorAction Stop return $true } catch { return $false } } function Test-GitLfsInstalled { try { $null = Get-Command git-lfs -ErrorAction Stop return $true } catch { return $false } } function Find-BashShell { # Check Git Bash first (most common on Windows) $gitBash = "C:\Program Files\Git\bin\bash.exe" if (Test-Path $gitBash) { return $gitBash } # Check bash.exe on PATH (Cygwin, MSYS2, WSL) try { $bashCmd = Get-Command bash.exe -ErrorAction Stop return $bashCmd.Source } catch { return $null } } function Configure-BashShell { try { $settingsDir = Join-Path $env:USERPROFILE ".skc\agent" $settingsFile = Join-Path $settingsDir "settings.json" # Check if settings.json already has a shellPath configured if (Test-Path $settingsFile) { try { $existingSettings = Get-Content $settingsFile -Raw | ConvertFrom-Json if ($existingSettings.shellPath) { Write-Host "Bash shell already configured: $($existingSettings.shellPath)" -ForegroundColor Cyan return } } catch { # Invalid JSON, we'll overwrite it } } $bashPath = Find-BashShell if ($bashPath) { Write-Host "Found bash shell: $bashPath" -ForegroundColor Cyan # Create settings directory if needed if (-not (Test-Path $settingsDir)) { New-Item -ItemType Directory -Force -Path $settingsDir | Out-Null } # Read existing settings or create new. ConvertFrom-Json's AsHashtable # switch only exists on PowerShell 6+; the documented `irm | iex` # install path runs under Windows PowerShell 5.1, where that parameter # throws and the catch below used to wipe every existing settings key. $settings = @{} if (Test-Path $settingsFile) { try { $parsed = Get-Content $settingsFile -Raw | ConvertFrom-Json if ($parsed) { foreach ($prop in $parsed.PSObject.Properties) { $settings[$prop.Name] = $prop.Value } } } catch { $settings = @{} } } # Set shellPath $settings["shellPath"] = $bashPath # Write settings $settings | ConvertTo-Json -Depth 10 | Set-Content $settingsFile -Encoding UTF8 Write-Host "✓ Configured shell path in $settingsFile" -ForegroundColor Green } else { Write-Host "" Write-Host "⚠ No bash shell found!" -ForegroundColor Yellow Write-Host " SKC requires a bash shell on Windows. Options:" -ForegroundColor Yellow Write-Host " 1. Install Git for Windows: https://git-scm.com/download/win" -ForegroundColor Yellow Write-Host " 2. Use WSL, Cygwin, or MSYS2" -ForegroundColor Yellow Write-Host "" Write-Host " After installing, you can set a custom path in:" -ForegroundColor Yellow Write-Host " $settingsFile" -ForegroundColor Yellow Write-Host ' { "shellPath": "C:\\path\\to\\bash.exe" }' -ForegroundColor Yellow } } catch { Write-Host "⚠ Could not configure bash shell: $_" -ForegroundColor Yellow } } function Install-Bun { Write-Host "Installing bun..." irm bun.sh/install.ps1 | iex # Refresh PATH $env:Path = [System.Environment]::GetEnvironmentVariable("Path", "User") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "Machine") Assert-BunVersion $MinimumBunVersion } function Install-ViaBun { Write-Host "Installing via bun..." if ($Ref) { if (-not (Test-GitInstalled)) { throw "git is required for -Ref when installing from source" } $tmpRoot = Join-Path ([System.IO.Path]::GetTempPath()) ("skc-install-" + [System.Guid]::NewGuid().ToString("N")) New-Item -ItemType Directory -Force -Path $tmpRoot | Out-Null try { $repoUrl = "https://github.com/$Repo.git" $cloneOk = $false try { git clone --depth 1 --branch $Ref $repoUrl $tmpRoot | Out-Null $cloneOk = $true } catch { $cloneOk = $false } if (-not $cloneOk) { git clone $repoUrl $tmpRoot | Out-Null Push-Location $tmpRoot try { git checkout $Ref | Out-Null } finally { Pop-Location } } # Pull LFS files if (Test-GitLfsInstalled) { Push-Location $tmpRoot try { git lfs pull | Out-Null } finally { Pop-Location } } $packagePath = Join-Path $tmpRoot "packages\coding-agent" if (-not (Test-Path $packagePath)) { throw "Expected package at $packagePath" } bun install -g $packagePath if ($LASTEXITCODE -ne 0) { throw "Failed to install from $packagePath via bun" } } finally { Remove-Item -Recurse -Force $tmpRoot -ErrorAction SilentlyContinue } } else { bun install -g $Package if ($LASTEXITCODE -ne 0) { throw "Failed to install $Package via bun" } } Write-Host "" Write-Host "✓ Installed skc via bun" -ForegroundColor Green Configure-BashShell Write-Host "Run 'skc' to get started!" } function Get-WebStatus { param($ErrorRecord) try { return [int]$ErrorRecord.Exception.Response.StatusCode } catch { return 0 } } function Assert-BinaryChecksum { param([string]$Tag, [string]$AssetName, [string]$DownloadedPath) $sumsUrl = "https://github.com/$Repo/releases/download/$Tag/$BinarySha256Asset" $manifestUrl = "https://github.com/$Repo/releases/download/$Tag/$BinaryManifestAsset" $sumsTmp = Join-Path $InstallDir (".skc.sums." + [System.Guid]::NewGuid().ToString("N")) $manifestTmp = Join-Path $InstallDir (".skc.manifest." + [System.Guid]::NewGuid().ToString("N")) try { $expected = $null try { Invoke-WebRequest -Uri $sumsUrl -OutFile $sumsTmp $checksumMatches = @() foreach ($line in Get-Content $sumsTmp) { if ($line -match '^([0-9a-fA-F]{64}) \*?([^/\\]+)$' -and $Matches[2] -eq $AssetName) { $checksumMatches += $Matches[1].ToLowerInvariant() } } if ($checksumMatches.Count -ne 1) { throw "Release checksum file did not list exactly one SHA-256 for $AssetName" } $expected = $checksumMatches[0] } catch { if ($_.Exception.Message -match "Release checksum file") { throw } if ((Get-WebStatus $_) -ne 404) { throw "Integrity asset $BinarySha256Asset could not be fetched. Existing install was not changed." } try { Invoke-WebRequest -Uri $manifestUrl -OutFile $manifestTmp } catch { throw "Release $Tag has no Sayknow binary integrity manifest. Existing install was not changed." } $manifest = Get-Content $manifestTmp -Raw | ConvertFrom-Json $expectedVersion = $Tag -replace '^sayknow-v', '' -replace '^v', '' if ( $manifest.schema -ne "sayknow-release-binaries-v1" -or $manifest.schema_version -ne 1 -or $manifest.release_version -ne $expectedVersion -or $manifest.tag -ne $Tag ) { throw "Release binary manifest metadata does not match $Tag" } $matches = @($manifest.binaries | Where-Object { $_.name -eq $AssetName }) if ($matches.Count -ne 1 -or [string]$matches[0].sha256 -notmatch '^[0-9a-f]{64}$') { throw "Release binary manifest did not list one valid SHA-256 for $AssetName" } $expected = [string]$matches[0].sha256 } $actual = (Get-FileHash -Algorithm SHA256 -Path $DownloadedPath).Hash.ToLowerInvariant() if ($actual -ne $expected) { throw "SHA-256 mismatch for $AssetName. Existing install was not changed." } Write-Host "Verified SHA-256 for $AssetName" } finally { Remove-Item -Force $sumsTmp -ErrorAction SilentlyContinue Remove-Item -Force $manifestTmp -ErrorAction SilentlyContinue } } function Install-Binary { if ($Ref) { Write-Host "Fetching release $Ref..." try { $Release = Invoke-RestMethod -Uri "https://api.github.com/repos/$Repo/releases/tags/$Ref" } catch { throw "Release tag not found: $Ref`nFor branch/commit installs, use -Source with -Ref." } } else { Write-Host "Fetching latest release..." $Release = Invoke-RestMethod -Uri "https://api.github.com/repos/$Repo/releases/latest" } $Latest = $Release.tag_name if (-not $Latest) { throw "Failed to fetch release tag" } Write-Host "Using version: $Latest" New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null $LockDir = Join-Path $InstallDir ".skc-install.lock" try { New-Item -ItemType Directory -Path $LockDir -ErrorAction Stop | Out-Null } catch { throw "Another SKC installer is already running in $InstallDir (lock: $LockDir)." } try { # Download binary to a temp file first so a failed or partial download # never clobbers an existing working install at $InstallDir\skc.exe. $BinaryUrl = "https://github.com/$Repo/releases/download/$Latest/$BinaryName" Write-Host "Downloading $BinaryName..." $OutPath = Join-Path $InstallDir "skc.exe" $DownloadTmp = Join-Path $InstallDir (".skc.download." + [System.Guid]::NewGuid().ToString("N")) try { Invoke-WebRequest -Uri $BinaryUrl -OutFile $DownloadTmp } catch { Remove-Item -Force $DownloadTmp -ErrorAction SilentlyContinue throw } Assert-BinaryChecksum -Tag $Latest -AssetName $BinaryName -DownloadedPath $DownloadTmp Move-Item -Force $DownloadTmp $OutPath Write-Host "" Write-Host "✓ Installed skc to $OutPath" -ForegroundColor Green # Add to PATH if not already there $UserPath = [Environment]::GetEnvironmentVariable("Path", "User") $needsRestart = $UserPath -notlike "*$InstallDir*" if ($needsRestart) { Write-Host "Adding $InstallDir to PATH..." [Environment]::SetEnvironmentVariable("Path", "$UserPath;$InstallDir", "User") } Configure-BashShell if ($needsRestart) { Write-Host "Restart your terminal, then run 'skc' to get started!" } else { Write-Host "Run 'skc' to get started!" } } finally { if ($DownloadTmp) { Remove-Item -Force $DownloadTmp -ErrorAction SilentlyContinue } Remove-Item -Recurse -Force $LockDir -ErrorAction SilentlyContinue } } # Main logic if ($Ref -and -not $Source -and -not $Binary) { $Source = $true } if ($Source) { if (-not (Test-BunInstalled)) { Install-Bun } Assert-BunVersion $MinimumBunVersion Install-ViaBun } elseif ($Binary) { Install-Binary } else { # Default: use bun if available, otherwise binary if (Test-BunInstalled) { Assert-BunVersion $MinimumBunVersion Install-ViaBun } else { Install-Binary } }