$ErrorActionPreference = "Stop" $Mode = "boat" $ApiUrl = "https://boat.dev" $InstallDir = Join-Path $HOME ".ascii\bin" if (-not [Environment]::Is64BitOperatingSystem) { throw "Only Windows x64 is supported for the initial Boat CLI release." } New-Item -ItemType Directory -Force $InstallDir | Out-Null $DownloadUrl = "$ApiUrl/api/boat/cli/download?platform=windows-x64&channel=prod" $ManifestUrl = "$DownloadUrl&integrity=sha256" $Asset = "boat-windows-x64.exe" $Target = Join-Path $InstallDir "$Mode.exe" $TempTarget = Join-Path $InstallDir "$Mode.exe.download" $TempSums = Join-Path $InstallDir "$Mode.exe.sha256sums.download" $DownloadHelp = @" The download failed or Windows Defender quarantined a file. Do not add antivirus or Defender exclusions. If this continues, contact support at support@ascii.dev. "@ try { try { Invoke-WebRequest -Uri $ManifestUrl -OutFile $TempSums Invoke-WebRequest -Uri $DownloadUrl -OutFile $TempTarget } catch { Write-Host $DownloadHelp throw "Install aborted: $_" } if (-not (Test-Path $TempTarget) -or -not (Test-Path $TempSums)) { Write-Host $DownloadHelp throw "Install aborted: a downloaded file was removed after download." } $ChecksumEntries = @( Get-Content -LiteralPath $TempSums | ForEach-Object { if ($_ -match '^\s*([0-9A-Fa-f]{64})[ \t]+\*?([^\s]+)[ \t]*$') { if ($Matches[2] -ceq $Asset) { $Matches[1].ToLowerInvariant() } } } ) if ($ChecksumEntries.Count -ne 1) { throw "Install aborted: expected exactly one SHA-256 checksum for $Asset in SHA256SUMS." } $Expected = $ChecksumEntries[0].ToLowerInvariant() try { $Actual = (Get-FileHash -Algorithm SHA256 -LiteralPath $TempTarget).Hash.ToLowerInvariant() } catch { Write-Host $DownloadHelp throw "Install aborted: could not hash the downloaded file. $_" } if ($Actual -ne $Expected) { throw "Install aborted: SHA-256 checksum mismatch for $Asset." } $Running = @(Get-Process -Name $Mode -ErrorAction SilentlyContinue) if ($Running.Count -gt 0) { Write-Host "Stopping running $Mode process before upgrade..." $Running | Stop-Process -Force Start-Sleep -Milliseconds 500 } $Installed = $false for ($i = 0; $i -lt 20; $i++) { try { Move-Item -Force $TempTarget $Target $Installed = $true break } catch { Start-Sleep -Milliseconds 250 } } if (-not $Installed) { throw "Could not replace $Target. Close any running $Mode windows or terminals and run the installer again." } } finally { Remove-Item -Force $TempTarget -ErrorAction SilentlyContinue Remove-Item -Force $TempSums -ErrorAction SilentlyContinue } $ConfigDir = Join-Path ([Environment]::GetFolderPath("ApplicationData")) "ascii\$Mode" New-Item -ItemType Directory -Force $ConfigDir | Out-Null @{ api_url = $ApiUrl channel = "prod" } | ConvertTo-Json | ForEach-Object { [System.IO.File]::WriteAllText((Join-Path $ConfigDir "config.json"), $_, [System.Text.UTF8Encoding]::new($false)) } $CurrentEnvName = if ($Mode -eq "boat-dev") { "BOAT_DEV_CURRENT_ID" } elseif ($Mode -eq "boat-staging") { "BOAT_STAGING_CURRENT_ID" } else { "BOAT_CURRENT_ID" } $CurrentConvoEnvName = if ($Mode -eq "boat-dev") { "BOAT_DEV_CURRENT_CONVO" } elseif ($Mode -eq "boat-staging") { "BOAT_STAGING_CURRENT_CONVO" } else { "BOAT_CURRENT_CONVO" } $ProfilePath = $PROFILE.CurrentUserAllHosts $ProfileDir = Split-Path -Parent $ProfilePath if ($ProfileDir) { New-Item -ItemType Directory -Force $ProfileDir | Out-Null } $FunctionBlock = @" # ASCII BOAT SHELL INTEGRATION START $Mode function $Mode { `$currentFile = [System.IO.Path]::GetTempFileName() `$convoFile = [System.IO.Path]::GetTempFileName() `$oldCurrentFile = [Environment]::GetEnvironmentVariable("BOAT_CURRENT_ID_FILE", "Process") `$oldConvoFile = [Environment]::GetEnvironmentVariable("BOAT_CURRENT_CONVO_FILE", "Process") [Environment]::SetEnvironmentVariable("BOAT_CURRENT_ID_FILE", `$currentFile, "Process") [Environment]::SetEnvironmentVariable("BOAT_CURRENT_CONVO_FILE", `$convoFile, "Process") try { & "$Target" @args `$status = `$LASTEXITCODE if (`$status -eq 0 -and `$args.Count -gt 0 -and @("new", "start", "fork") -contains `$args[0] -and (Test-Path `$currentFile)) { `$currentId = (Get-Content -Raw `$currentFile -ErrorAction SilentlyContinue).Trim() if (`$currentId) { [Environment]::SetEnvironmentVariable("$CurrentEnvName", `$currentId, "Process") } } if (`$status -eq 0 -and `$args.Count -gt 0 -and `$args[0] -eq "prompt" -and (Test-Path `$convoFile)) { `$currentConvo = (Get-Content -Raw `$convoFile -ErrorAction SilentlyContinue).Trim() if (`$currentConvo) { [Environment]::SetEnvironmentVariable("$CurrentConvoEnvName", `$currentConvo, "Process") } } } finally { if (`$oldCurrentFile) { [Environment]::SetEnvironmentVariable("BOAT_CURRENT_ID_FILE", `$oldCurrentFile, "Process") } else { [Environment]::SetEnvironmentVariable("BOAT_CURRENT_ID_FILE", `$null, "Process") } if (`$oldConvoFile) { [Environment]::SetEnvironmentVariable("BOAT_CURRENT_CONVO_FILE", `$oldConvoFile, "Process") } else { [Environment]::SetEnvironmentVariable("BOAT_CURRENT_CONVO_FILE", `$null, "Process") } Remove-Item -Force `$currentFile -ErrorAction SilentlyContinue Remove-Item -Force `$convoFile -ErrorAction SilentlyContinue } `$global:LASTEXITCODE = `$status return } # ASCII BOAT SHELL INTEGRATION END $Mode "@ $ExistingProfile = if (Test-Path $ProfilePath) { Get-Content -Raw $ProfilePath -ErrorAction SilentlyContinue } else { "" } $Pattern = "(?s)\r?\n?# ASCII BOAT SHELL INTEGRATION START $([regex]::Escape($Mode)).*?# ASCII BOAT SHELL INTEGRATION END $([regex]::Escape($Mode))\r?\n?" $NextProfile = [regex]::Replace($ExistingProfile, $Pattern, "`n") [System.IO.File]::WriteAllText($ProfilePath, $NextProfile.TrimEnd() + $FunctionBlock + "`n") Invoke-Expression $FunctionBlock Write-Host "Updated $Mode shell integration in $ProfilePath." Write-Host "Installed $Mode to $Target" $UserPath = [Environment]::GetEnvironmentVariable("Path", "User") if (($UserPath -split ";") -notcontains $InstallDir) { # An empty user PATH would otherwise gain a leading empty entry. $NextPath = if ($UserPath) { "$UserPath;$InstallDir" } else { $InstallDir } [Environment]::SetEnvironmentVariable("Path", $NextPath, "User") Write-Host "Added $InstallDir to your user PATH." } # A window opened before an earlier install persisted the PATH still lacks it in-process. if (($env:Path -split ";") -notcontains $InstallDir) { $env:Path = "$env:Path;$InstallDir" } Write-Host "$Mode is ready in this window and in new ones." Write-Host "" if (-not (Test-Path $Target)) { # Defender can also quarantine the binary after it lands on disk. Write-Host $DownloadHelp throw "Install aborted: $Target was removed after install." } & $Target onboard