-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathinstall.ps1
More file actions
72 lines (58 loc) · 2.58 KB
/
install.ps1
File metadata and controls
72 lines (58 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
$ProgressPreference = 'SilentlyContinue' # Speeds up Invoke-WebRequest significantly
# Allow overriding the version
$Version = if ($env:CODECRAFTERS_CLI_VERSION) { $env:CODECRAFTERS_CLI_VERSION } else { "v53" }
# Detect architecture
$Arch = switch ($env:PROCESSOR_ARCHITECTURE) {
"AMD64" { "amd64" }
"ARM64" { "arm64" }
default {
Write-Error "Unsupported architecture: $env:PROCESSOR_ARCHITECTURE"
exit 1
}
}
$InstallDir = if ($env:INSTALL_DIR) { $env:INSTALL_DIR } else { "$env:LOCALAPPDATA\Programs\codecrafters" }
$InstallPath = if ($env:INSTALL_PATH) { $env:INSTALL_PATH } else { "$InstallDir\codecrafters.exe" }
$DownloadUrl = "https://github.com/codecrafters-io/cli/releases/download/$Version/${Version}_windows_$Arch.tar.gz"
Write-Host "Downloading " -NoNewline
Write-Host "CodeCrafters CLI " -ForegroundColor Green -NoNewline
Write-Host "($Version)" -ForegroundColor DarkGray -NoNewline
Write-Host "..."
$TempDir = Join-Path $env:TEMP "codecrafters-install-$([System.Guid]::NewGuid().ToString('N'))"
New-Item -ItemType Directory -Path $TempDir -Force | Out-Null
try {
$TarGzPath = Join-Path $TempDir "codecrafters.tar.gz"
try {
Invoke-WebRequest -Uri $DownloadUrl -OutFile $TarGzPath -UseBasicParsing
} catch {
Write-Host "error: your platform and architecture (windows-$Arch) is unsupported."
exit 1
}
tar -xzf $TarGzPath -C $TempDir
if (-not (Test-Path -LiteralPath $InstallDir)) {
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}
$ExtractedBinary = Join-Path $TempDir "codecrafters.exe"
Move-Item -Path $ExtractedBinary -Destination $InstallPath -Force
$InstalledVersion = & $InstallPath --version
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($UserPath -notlike "*$InstallDir*") {
$NewUserPath = if ($UserPath) { "$UserPath;$InstallDir" } else { $InstallDir }
[Environment]::SetEnvironmentVariable("Path", $NewUserPath, "User")
$env:Path += ";$InstallDir"
}
Write-Host ([char]0x2713) -ForegroundColor Green -NoNewline
Write-Host " CodeCrafters CLI installed! " -NoNewline
Write-Host "Version: $InstalledVersion" -ForegroundColor DarkGray
Write-Host ""
} finally {
# Cleanup
if (Test-Path -LiteralPath $TempDir) {
try {
Remove-Item -LiteralPath $TempDir -Recurse -Force -ErrorAction Stop
} catch {
Write-Host "Warning: couldn't clean up temporary folder: $TempDir" -ForegroundColor Yellow
}
}
}