-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathinstall.ps1
More file actions
201 lines (170 loc) · 5.95 KB
/
install.ps1
File metadata and controls
201 lines (170 loc) · 5.95 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
# CodeWalk installer (Windows)
# Usage: irm https://raw.githubusercontent.com/<owner>/<repo>/main/install.ps1 | iex
$Repo = if ($env:CODEWALK_REPO) { $env:CODEWALK_REPO } else { "verseles/codewalk" }
$InstallDir = Join-Path $env:LOCALAPPDATA "CodeWalk"
$BinaryPath = Join-Path $InstallDir "codewalk.exe"
$VersionFile = Join-Path $InstallDir ".installed-version"
$StartMenuShortcutPath = Join-Path $env:APPDATA "Microsoft\Windows\Start Menu\Programs\CodeWalk.lnk"
function Info([string]$Message) {
Write-Host ":: $Message" -ForegroundColor Cyan
}
function Fail([string]$Message) {
throw $Message
}
function Warn([string]$Message) {
Write-Host ":: $Message" -ForegroundColor Yellow
}
function Get-WindowsArchitecture {
$runtimeInfoType = [Type]::GetType("System.Runtime.InteropServices.RuntimeInformation", $false)
if ($runtimeInfoType) {
$osArchitectureProperty = $runtimeInfoType.GetProperty("OSArchitecture", [System.Reflection.BindingFlags]::Public -bor [System.Reflection.BindingFlags]::Static)
if ($osArchitectureProperty) {
try {
$osArchitecture = $osArchitectureProperty.GetValue($null, $null)
if ($osArchitecture) {
return $osArchitecture.ToString().ToUpperInvariant()
}
}
catch {
# Ignore and use compatibility fallbacks below.
}
}
}
$processArchitecture = ""
$wow64Architecture = ""
if ($env:PROCESSOR_ARCHITECTURE) {
$processArchitecture = $env:PROCESSOR_ARCHITECTURE.ToUpperInvariant()
}
if ($env:PROCESSOR_ARCHITEW6432) {
$wow64Architecture = $env:PROCESSOR_ARCHITEW6432.ToUpperInvariant()
}
if ($processArchitecture -eq "ARM64" -or $wow64Architecture -eq "ARM64") {
return "ARM64"
}
if ([Environment]::Is64BitOperatingSystem) {
return "X64"
}
return "X86"
}
function Get-WindowsAssetCandidates {
$arch = Get-WindowsArchitecture
switch ($arch) {
"X64" { return @("codewalk-windows-x64.zip") }
"ARM64" {
return @("codewalk-windows-arm64.zip", "codewalk-windows-x64.zip")
}
default { Fail "Unsupported architecture: $arch" }
}
}
function Add-ToUserPath([string]$PathEntry) {
$current = [Environment]::GetEnvironmentVariable("Path", "User")
$parts = @()
if ($current) {
$parts = $current.Split(";") | Where-Object { $_ -and $_.Trim() -ne "" }
}
if ($parts -contains $PathEntry) {
return
}
$updated = if ($parts.Count -eq 0) { $PathEntry } else { ($parts + $PathEntry) -join ";" }
[Environment]::SetEnvironmentVariable("Path", $updated, "User")
}
function New-StartMenuShortcut([string]$TargetExePath) {
$shortcutDir = Split-Path -Parent $StartMenuShortcutPath
New-Item -ItemType Directory -Force -Path $shortcutDir | Out-Null
$shell = New-Object -ComObject WScript.Shell
$shortcut = $shell.CreateShortcut($StartMenuShortcutPath)
$shortcut.TargetPath = $TargetExePath
$shortcut.WorkingDirectory = Split-Path -Parent $TargetExePath
$shortcut.IconLocation = "$TargetExePath,0"
$shortcut.Description = "CodeWalk"
$shortcut.Save()
}
function Get-InstalledVersion {
if (-not (Test-Path $VersionFile)) {
return ""
}
try {
return (Get-Content -Path $VersionFile -Raw).Trim()
}
catch {
return ""
}
}
$assetCandidates = Get-WindowsAssetCandidates
Info "Fetching latest release from $Repo"
$release = Invoke-RestMethod -Uri "https://api.github.com/repos/$Repo/releases/latest" -Headers @{ "User-Agent" = "codewalk-install" }
if (-not $release.tag_name) {
Fail "Could not determine latest release tag."
}
$installedVersion = Get-InstalledVersion
if ($installedVersion) {
if ($installedVersion -eq $release.tag_name) {
Info "Reinstalling CodeWalk $installedVersion"
}
else {
Info "Updating CodeWalk from $installedVersion to $($release.tag_name)"
}
}
elseif (Test-Path $InstallDir) {
Info "Existing installation detected. Installing latest release $($release.tag_name)"
}
else {
Info "Installing CodeWalk $($release.tag_name)"
}
$match = $null
$asset = $null
foreach ($candidate in $assetCandidates) {
$found = $release.assets | Where-Object { $_.name -eq $candidate } | Select-Object -First 1
if ($found) {
$match = $found
$asset = $candidate
break
}
}
if (-not $match) {
$available = ($release.assets | ForEach-Object { $_.name }) -join ", "
$requested = $assetCandidates -join ", "
Fail "None of the expected assets were found ($requested). Available: $available"
}
$tmpRoot = Join-Path $env:TEMP ("codewalk-install-" + [Guid]::NewGuid().ToString("N"))
$zipPath = Join-Path $tmpRoot $asset
try {
New-Item -ItemType Directory -Force -Path $tmpRoot | Out-Null
Info "Downloading $asset"
Invoke-WebRequest -Uri $match.browser_download_url -OutFile $zipPath
if (Test-Path $InstallDir) {
Remove-Item -Recurse -Force -Path $InstallDir
}
New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null
Info "Extracting package"
Expand-Archive -Path $zipPath -DestinationPath $InstallDir -Force
# Remove Zone.Identifier ADS so SmartScreen does not warn on first run.
Get-ChildItem -Path $InstallDir -Recurse | Unblock-File -ErrorAction SilentlyContinue
if (-not (Test-Path $BinaryPath)) {
$nested = Join-Path $InstallDir "bin\codewalk.exe"
if (Test-Path $nested) {
Copy-Item -Force $nested $BinaryPath
} else {
Fail "codewalk.exe not found in archive."
}
}
Add-ToUserPath -PathEntry $InstallDir
try {
New-StartMenuShortcut -TargetExePath $BinaryPath
Info "Start Menu shortcut created at $StartMenuShortcutPath"
}
catch {
Warn "Could not create Start Menu shortcut: $($_.Exception.Message)"
}
Set-Content -Path $VersionFile -Value $release.tag_name -NoNewline
Write-Host ""
Write-Host "CodeWalk installed successfully at $InstallDir ($($release.tag_name))" -ForegroundColor Green
Write-Host "Open a new terminal and run: codewalk"
}
finally {
if (Test-Path $tmpRoot) {
Remove-Item -Recurse -Force -Path $tmpRoot -ErrorAction SilentlyContinue
}
}