# Installs Woolshed on Windows, or updates it: # # powershell -c "irm https://woolshed.dev/releases/app/install.ps1 | iex" # # The app into %LOCALAPPDATA%\Programs\woolshed (added to your PATH for # `woolshed`), Woolshed in the Start menu. The core (Woolshed Core) comes # inside the app: when Woolshed opens, it installs the core from there, with # no second download, and keeps it current with each app update. Running this # again updates the app (an open one keeps running until it quits). No admin # needed. Settings > Apps lists it; its Uninstall runs uninstall.ps1 in the app # folder. WOOLSHED_RELEASES_URL points the download elsewhere (tests). # Its own scope: under `irm | iex` it would otherwise set the caller's variables. & { $ErrorActionPreference = "Stop" $ProgressPreference = "SilentlyContinue" # throw, not exit: under `irm | iex`, exit would close the person's window. function Fail($why) { throw "woolshed: $why" } $releases = if ($env:WOOLSHED_RELEASES_URL) { $env:WOOLSHED_RELEASES_URL.TrimEnd("/") } else { "https://woolshed.dev/releases" } $dir = Join-Path $env:LOCALAPPDATA "Programs\woolshed" $arch = if ($env:PROCESSOR_ARCHITEW6432) { $env:PROCESSOR_ARCHITEW6432 } else { $env:PROCESSOR_ARCHITECTURE } $target = switch ($arch) { "AMD64" { "windows-x86_64" } "ARM64" { "windows-aarch64" } default { Fail "no build for $arch Windows" } } # Windows' own tar: Git Bash's, often first on PATH, takes C:\ for a remote host. $tar = Join-Path $env:SystemRoot "System32\tar.exe" if (-not (Get-Command curl.exe -ErrorAction SilentlyContinue) -or -not (Test-Path $tar)) { Fail "needs curl.exe and tar.exe (Windows 10 1803 or newer)" } $tmp = Join-Path ([IO.Path]::GetTempPath()) "woolshed-install-$PID" New-Item -ItemType Directory -Force $tmp | Out-Null try { # WOOLSHED_CHANNEL=beta: the newest beta (stable's while there is none). $got = $false if ($env:WOOLSHED_CHANNEL -eq "beta") { curl.exe -fsSL "$releases/app/beta.json" -o "$tmp\latest.json" 2>$null $got = -not $LASTEXITCODE } elseif ($env:WOOLSHED_CHANNEL -and $env:WOOLSHED_CHANNEL -ne "stable") { Fail "WOOLSHED_CHANNEL is stable or beta" } if (-not $got) { curl.exe -fsSL "$releases/app/latest.json" -o "$tmp\latest.json" if ($LASTEXITCODE) { Fail "cannot reach $releases" } } $m = Get-Content -Raw "$tmp\latest.json" | ConvertFrom-Json $t = $m.targets.$target # Releases before Windows on Arm builds: the x64 one runs there too, emulated. if (-not $t.url -and $target -eq "windows-aarch64") { $target = "windows-x86_64"; $t = $m.targets.$target } if (-not $t.url -or -not $t.sha256) { Fail "no build for $target yet" } # Tells the release server's download counter a new install from a # reinstall; the file is the same either way. Only plain URLs on the # release server in use are marked: one with a query or fragment (a # mirror's signed URL) is fetched exactly as the manifest gives it. $src = if (Test-Path $dir) { "reinstall" } else { "install" } $plain = $t.url.StartsWith("$releases/", [StringComparison]::Ordinal) -and $t.url.IndexOfAny([char[]]"?#") -lt 0 $download = if ($plain) { "$($t.url)?src=$src" } else { $t.url } Write-Host "Installing Woolshed $($m.version) for $target..." curl.exe -fsSL $download -o "$tmp\app.tar.gz" if ($LASTEXITCODE) { Fail "download failed: $($t.url)" } # .NET, not Get-FileHash: that one fails when PowerShell 7 starts 5.1. $stream = [IO.File]::OpenRead("$tmp\app.tar.gz") try { $got = -join ([Security.Cryptography.SHA256]::Create().ComputeHash($stream) | ForEach-Object { $_.ToString("x2") }) } finally { $stream.Close() } if ($got -ne $t.sha256) { Fail "the download is damaged (checksum mismatch); try again" } & $tar -xzf "$tmp\app.tar.gz" -C $tmp if ($LASTEXITCODE -or -not (Test-Path "$tmp\woolshed\woolshed.exe")) { Fail "the download holds no complete app" } # A running app's folder can be renamed, not deleted: the old one goes # now if it can, else on the next install. New-Item -ItemType Directory -Force (Split-Path $dir) | Out-Null Remove-Item -Recurse -Force "$dir.old" -ErrorAction SilentlyContinue if (Test-Path $dir) { Rename-Item $dir "$dir.old" } Move-Item "$tmp\woolshed" $dir Remove-Item -Recurse -Force "$dir.old" -ErrorAction SilentlyContinue $exe = Join-Path $dir "woolshed.exe" $menu = Join-Path ([Environment]::GetFolderPath("Programs")) "Woolshed.lnk" $link = (New-Object -ComObject WScript.Shell).CreateShortcut($menu) $link.TargetPath = $exe $link.WorkingDirectory = $env:USERPROFILE $link.Description = "Terminals and coding agents that keep running" $link.Save() # Settings > Apps lists it, with an Uninstall (uninstall.ps1, in the package). $uninstall = Join-Path $dir "uninstall.ps1" if (Test-Path $uninstall) { & (Join-Path $env:SystemRoot "System32\WindowsPowerShell\v1.0\powershell.exe") -NoProfile -ExecutionPolicy Bypass -File $uninstall -Register -Version $m.version if ($LASTEXITCODE) { Write-Warning "Woolshed is installed, but not listed in Settings > Apps." } } # The raw user PATH, so %VARIABLES% in it stay unexpanded and keep their type. $key = [Microsoft.Win32.Registry]::CurrentUser.OpenSubKey("Environment", $true) $raw = [string]$key.GetValue("Path", "", [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames) $listed = $raw -split ";" | Where-Object { $_.Trim().TrimEnd("\") -ieq $dir.TrimEnd("\") } if (-not $listed) { $kind = if ($key.GetValueNames() -contains "Path") { $key.GetValueKind("Path") } else { [Microsoft.Win32.RegistryValueKind]::ExpandString } $key.SetValue("Path", $(if ($raw) { "$($raw.TrimEnd(';'));$dir" } else { $dir }), $kind) # Setting any user variable tells running programs the environment changed. [Environment]::SetEnvironmentVariable("WOOLSHED_INSTALLING", "1", "User") [Environment]::SetEnvironmentVariable("WOOLSHED_INSTALLING", $null, "User") } $key.Close() if (($env:Path -split ";") -notcontains $dir) { $env:Path += ";$dir" } Write-Host "Woolshed is installed. Open it from the Start menu, or run: woolshed" } finally { Remove-Item -Recurse -Force $tmp -ErrorAction SilentlyContinue } }