|
| 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