Skip to content

Commit 047dce7

Browse files
authored
Merge pull request #67 from codecrafters-io/andy/support-windows
CC-2103: Add PowerShell installation script
2 parents 8fd4239 + b7b2628 commit 047dce7

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

.github/workflows/test.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,18 @@ jobs:
1515
go-version: "1.23"
1616
- run: make test
1717

18-
install-cli:
18+
install-cli-linux:
1919
runs-on: ubuntu-latest
2020
steps:
2121
- uses: actions/checkout@v4
2222
- run: dash -x install.sh
2323
- run: codecrafters --version
24+
25+
install-cli-windows:
26+
runs-on: windows-latest
27+
steps:
28+
- uses: actions/checkout@v4
29+
- run: |
30+
. .\install.ps1
31+
codecrafters --version
32+
shell: pwsh

install.ps1

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Set-StrictMode -Version Latest
2+
$ErrorActionPreference = "Stop"
3+
$ProgressPreference = 'SilentlyContinue' # Speeds up Invoke-WebRequest significantly
4+
5+
# Allow overriding the version
6+
$Version = if ($env:CODECRAFTERS_CLI_VERSION) { $env:CODECRAFTERS_CLI_VERSION } else { "v46" }
7+
8+
# Detect architecture
9+
$Arch = switch ($env:PROCESSOR_ARCHITECTURE) {
10+
"AMD64" { "amd64" }
11+
"ARM64" { "arm64" }
12+
default {
13+
Write-Error "Unsupported architecture: $env:PROCESSOR_ARCHITECTURE"
14+
exit 1
15+
}
16+
}
17+
18+
$InstallDir = if ($env:INSTALL_DIR) { $env:INSTALL_DIR } else { "$env:LOCALAPPDATA\Programs\codecrafters" }
19+
$InstallPath = if ($env:INSTALL_PATH) { $env:INSTALL_PATH } else { "$InstallDir\codecrafters.exe" }
20+
21+
$DownloadUrl = "https://github.com/codecrafters-io/cli/releases/download/$Version/${Version}_windows_$Arch.tar.gz"
22+
23+
Write-Host "This script will automatically install codecrafters ($Version) for you."
24+
Write-Host "Installation path: $InstallPath"
25+
26+
$TempDir = Join-Path $env:TEMP "codecrafters-install-$([System.Guid]::NewGuid().ToString('N'))"
27+
New-Item -ItemType Directory -Path $TempDir -Force | Out-Null
28+
29+
try {
30+
$TarGzPath = Join-Path $TempDir "codecrafters.tar.gz"
31+
Write-Host "Downloading CodeCrafters CLI..."
32+
33+
try {
34+
Invoke-WebRequest -Uri $DownloadUrl -OutFile $TarGzPath -UseBasicParsing
35+
} catch {
36+
Write-Error "Failed to download. Your platform and architecture (windows-$Arch) may be unsupported."
37+
exit 1
38+
}
39+
40+
tar -xzf $TarGzPath -C $TempDir
41+
42+
if (-not (Test-Path $InstallDir)) {
43+
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
44+
}
45+
46+
$ExtractedBinary = Join-Path $TempDir "codecrafters.exe"
47+
Move-Item -Path $ExtractedBinary -Destination $InstallPath -Force
48+
49+
$InstalledVersion = & $InstallPath --version
50+
Write-Host "Installed $InstalledVersion"
51+
52+
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User")
53+
if ($UserPath -notlike "*$InstallDir*") {
54+
$NewUserPath = if ($UserPath) { "$UserPath;$InstallDir" } else { $InstallDir }
55+
[Environment]::SetEnvironmentVariable("Path", $NewUserPath, "User")
56+
$env:Path += ";$InstallDir"
57+
}
58+
Write-Host "Done!"
59+
} finally {
60+
# Cleanup
61+
if (Test-Path $TempDir) {
62+
Remove-Item -Path $TempDir -Recurse -Force -ErrorAction SilentlyContinue
63+
}
64+
}

0 commit comments

Comments
 (0)