-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.ps1
More file actions
284 lines (247 loc) · 10.9 KB
/
install.ps1
File metadata and controls
284 lines (247 loc) · 10.9 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env pwsh
# install.ps1 — download and install the latest github-code-search binary on Windows.
#
# Usage:
# powershell -c "irm https://raw.githubusercontent.com/fulll/github-code-search/main/install.ps1 | iex"
#
# With options:
# & { $(irm https://raw.githubusercontent.com/fulll/github-code-search/main/install.ps1) } -Version v1.8.0
param(
[String]$Version = "latest",
[String]$InstallDir = "${Home}\.github-code-search\bin",
# Force a specific Windows variant (x64-modern, x64-baseline, arm64).
# When omitted, the script detects the architecture and probes the release
# for the best available binary with automatic fallback.
[String]$Target = "",
[Switch]$NoPathUpdate = $false,
[Switch]$DownloadWithoutCurl = $false
)
Set-StrictMode -Version Latest
$BinaryName = "github-code-search"
$Repo = "fulll/github-code-search"
# ── Helpers ──────────────────────────────────────────────────────────────────
function Publish-Env {
if (-not ("Win32.NativeMethods" -as [Type])) {
Add-Type -Namespace Win32 -Name NativeMethods -MemberDefinition @"
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessageTimeout(
IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam,
uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);
"@
}
$HWND_BROADCAST = [IntPtr] 0xffff
$WM_SETTINGCHANGE = 0x1a
$result = [UIntPtr]::Zero
[Win32.NativeMethods]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE, [UIntPtr]::Zero, "Environment", 2, 5000, [ref] $result) | Out-Null
}
function Get-Env {
param([String]$Key)
$RegisterKey = Get-Item -Path 'HKCU:'
$EnvRegisterKey = $RegisterKey.OpenSubKey('Environment')
$EnvRegisterKey.GetValue($Key, $null, [Microsoft.Win32.RegistryValueOptions]::DoNotExpandEnvironmentNames)
}
function Write-Env {
param([String]$Key, [String]$Value)
$RegisterKey = Get-Item -Path 'HKCU:'
$EnvRegisterKey = $RegisterKey.OpenSubKey('Environment', $true)
if ($null -eq $Value) {
$EnvRegisterKey.DeleteValue($Key)
} else {
$RegistryValueKind = if ($Value.Contains('%')) {
[Microsoft.Win32.RegistryValueKind]::ExpandString
} else {
[Microsoft.Win32.RegistryValueKind]::String
}
$EnvRegisterKey.SetValue($Key, $Value, $RegistryValueKind)
}
Publish-Env
}
# ── Architecture detection ───────────────────────────────────────────────────
$Arch = (Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment').PROCESSOR_ARCHITECTURE
# Only auto-detect when the caller has not forced a specific variant via -Target.
if ($Target -eq "") {
switch ($Arch) {
"AMD64" {
# Prefer the modern (AVX2) variant; fall back to the baseline build on
# older hardware. The probe loop below handles the automatic fallback.
$Target = "x64-modern"
}
"ARM64" { $Target = "arm64" }
default {
Write-Output "Install Failed:"
Write-Output " ${BinaryName} for Windows does not support architecture: $Arch"
exit 1
}
}
}
# ── Windows version guard ────────────────────────────────────────────────────
$MinBuild = 17763 # Windows 10 1809 / Windows Server 2019
$MinBuildName = "Windows 10 1809"
$WinVer = [System.Environment]::OSVersion.Version
if ($WinVer.Major -lt 10 -or ($WinVer.Major -eq 10 -and $WinVer.Build -lt $MinBuild)) {
Write-Warning "${BinaryName} requires ${MinBuildName} or newer (build ${MinBuild}+)."
Write-Warning "Your system: Windows $($WinVer.Major).$($WinVer.Minor) build $($WinVer.Build)"
exit 1
}
$ErrorActionPreference = "Stop"
# ── Resolve version ──────────────────────────────────────────────────────────
if ($Version -eq "latest") {
Write-Output "Detecting latest release..."
$ApiUrl = "https://api.github.com/repos/${Repo}/releases/latest"
try {
$Response = Invoke-RestMethod -Uri $ApiUrl -Headers @{ "Accept" = "application/vnd.github.v3+json" }
$Tag = $Response.tag_name
$ReleaseAssets = $Response.assets | ForEach-Object { $_.name }
} catch {
Write-Output "Install Failed:"
Write-Output " Could not determine the latest release from the GitHub API."
Write-Output " URL: $ApiUrl"
Write-Output " $_"
exit 1
}
} else {
$Tag = $Version
$ApiUrl = "https://api.github.com/repos/${Repo}/releases/tags/${Tag}"
try {
$Response = Invoke-RestMethod -Uri $ApiUrl -Headers @{ "Accept" = "application/vnd.github.v3+json" }
$ReleaseAssets = $Response.assets | ForEach-Object { $_.name }
} catch {
# GitHub API unreachable (blocked, rate-limited, …) — fall back to probing
# candidate artifacts directly with curl.exe HEAD requests. curl.exe ships
# with Windows 10 1803+ and is already used for the binary download below.
Write-Output " GitHub API unavailable ($ApiUrl); falling back to direct artifact probing..."
$ReleaseAssets = $null
}
}
# ── Resolve artifact name with automatic x64 fallback ────────────────────────
#
# Release layout (as of v1.9.0):
# github-code-search-windows-x64-modern.exe — AVX2 / SSE4.2 (most CPUs since ~2013)
# github-code-search-windows-x64-baseline.exe — compatible with any x86-64 CPU
# github-code-search-windows-x64.exe — legacy alias kept for back-compat
# github-code-search-windows-arm64.exe — ARM64
#
# Artifact availability is checked against the GitHub release asset list already
# fetched above — no extra network request needed, and no Invoke-WebRequest
# which triggers a security warning on Windows PowerShell 5.1.
# When the API was unreachable ($ReleaseAssets is $null), we fall back to
# probing candidate URLs with curl.exe HEAD requests instead.
$CandidateTargets = @($Target)
if ($Target -eq "x64-modern") {
$CandidateTargets += "x64-baseline"
$CandidateTargets += "x64"
} elseif ($Target -eq "x64-baseline") {
$CandidateTargets += "x64"
}
$Artifact = $null
foreach ($Candidate in $CandidateTargets) {
$CandidateArtifact = "${BinaryName}-windows-${Candidate}.exe"
$Found = $false
if ($null -ne $ReleaseAssets) {
$Found = $ReleaseAssets -contains $CandidateArtifact
} else {
# Fallback: probe via curl.exe HEAD (no Invoke-WebRequest, no PS5.1 warning).
$CheckUrl = "https://github.com/${Repo}/releases/download/${Tag}/${CandidateArtifact}"
$null = curl.exe -fsI $CheckUrl 2>$null
$Found = $LASTEXITCODE -eq 0
}
if ($Found) {
$Artifact = $CandidateArtifact
$Target = $Candidate
break
}
}
if ($null -eq $Artifact) {
$AvailableWindows = if ($null -ne $ReleaseAssets) { $ReleaseAssets | Where-Object { $_ -like "*windows*" } } else { @() }
Write-Output "Install Failed:"
Write-Output " No compatible Windows binary found for ${Tag}."
Write-Output " Tried: $($CandidateTargets -join ', ')"
if ($AvailableWindows) {
Write-Output " Available Windows assets: $($AvailableWindows -join ', ')"
}
exit 1
}
Write-Output "Installing ${BinaryName} ${Tag} (windows/${Target})..."
# ── Download ─────────────────────────────────────────────────────────────────
$DownloadUrl = "https://github.com/${Repo}/releases/download/${Tag}/${Artifact}"
$TmpDir = $env:TEMP
$TmpFile = Join-Path $TmpDir $Artifact
$DownloadFailed = $false
if (-not $DownloadWithoutCurl) {
try {
curl.exe "-#SfLo" "$TmpFile" "$DownloadUrl"
if ($LASTEXITCODE -ne 0) {
$DownloadFailed = $true
}
} catch {
$DownloadFailed = $true
}
}
if ($DownloadWithoutCurl -or $DownloadFailed) {
try {
Write-Output "Downloading with Invoke-RestMethod..."
Invoke-RestMethod -Uri $DownloadUrl -OutFile $TmpFile
} catch {
Write-Output "Install Failed:"
Write-Output " Could not download ${BinaryName} ${Tag}."
Write-Output " URL: $DownloadUrl"
Write-Output ""
Write-Output " $_"
Write-Output ""
Write-Output " If your antivirus is blocking the download, try adding an exclusion."
exit 1
}
}
if (-not (Test-Path $TmpFile)) {
Write-Output "Install Failed:"
Write-Output " Downloaded file not found at ${TmpFile}."
Write-Output " This may be caused by antivirus software (e.g. Windows Defender) quarantining the binary."
Write-Output " Try adding an exclusion for ${TmpFile} and retry."
exit 1
}
# ── Install ──────────────────────────────────────────────────────────────────
$null = New-Item -ItemType Directory -Path $InstallDir -Force
$DestPath = Join-Path $InstallDir "${BinaryName}.exe"
try {
Move-Item -Path $TmpFile -Destination $DestPath -Force
} catch {
Write-Output "Install Failed:"
Write-Output " Could not move binary to ${DestPath}."
Write-Output " $_"
Write-Output ""
Write-Output " If the file is in use, close any running instance and retry."
Write-Output " If your antivirus deleted the file, add an exclusion and retry."
exit 1
}
# ── PATH update ──────────────────────────────────────────────────────────────
if (-not $NoPathUpdate) {
$ExistingPath = Get-Env -Key "Path"
$Path = if ($null -ne $ExistingPath) { $ExistingPath -split ';' } else { @() }
if ($Path -notcontains $InstallDir) {
$Path += $InstallDir
Write-Env -Key 'Path' -Value ($Path -join ';')
$env:Path = "${InstallDir};${env:Path}"
Write-Output ""
Write-Output " Added ${InstallDir} to user PATH."
}
}
# ── Verification ─────────────────────────────────────────────────────────────
try {
$InstalledVersion = & $DestPath --version
Write-Output ""
Write-Output " ${BinaryName} ${Tag} installed successfully!"
Write-Output " Binary: ${DestPath}"
Write-Output " Version: ${InstalledVersion}"
Write-Output ""
Write-Output " Remember to set your GitHub token:"
Write-Output ' $env:GITHUB_TOKEN = "ghp_your_token_here"'
} catch {
Write-Output ""
Write-Output " ${BinaryName} was installed to ${DestPath},"
Write-Output " but the verification command (--version) failed:"
Write-Output " $_"
Write-Output ""
Write-Output " This may be caused by antivirus software blocking execution."
Write-Output " Try adding an exclusion for ${DestPath} and run:"
Write-Output " ${BinaryName} --version"
}