-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.ps1
More file actions
87 lines (72 loc) · 3.39 KB
/
Copy pathinstall.ps1
File metadata and controls
87 lines (72 loc) · 3.39 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
# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
# or more contributor license agreements. Licensed under the Apache License,
# Version 2.0 (the "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# Elastic Docs Utils installer shim for Windows (PowerShell).
# Usage:
# irm https://ela.st/docs-utils-ps | iex
$ErrorActionPreference = 'Stop'
$Repo = "elastic/docs-utils"
$BinaryName = "elastic-docs-utils"
$InstallDir = "$env:LOCALAPPDATA\Elastic\DocsUtils"
function Write-Info { Write-Host "ℹ $args" -ForegroundColor Cyan }
function Write-OK { Write-Host "✓ $args" -ForegroundColor Green }
function Write-Warn { Write-Host "⚠ $args" -ForegroundColor Yellow }
function Write-Err { Write-Host "✗ $args" -ForegroundColor Red }
# Fetch latest release version from GitHub.
function Get-LatestVersion {
$release = Invoke-RestMethod "https://api.github.com/repos/$Repo/releases/latest"
return $release.tag_name
}
$Version = Get-LatestVersion
Write-Info "Latest release: $Version"
$Archive = "${BinaryName}_windows_amd64.zip"
$DownloadUrl = "https://github.com/$Repo/releases/download/$Version/$Archive"
$ChecksumUrl = "https://github.com/$Repo/releases/download/$Version/checksums.txt"
$TempDir = New-TemporaryFile | ForEach-Object { Remove-Item $_; New-Item -ItemType Directory -Path $_ }
try {
$ArchivePath = Join-Path $TempDir $Archive
Write-Info "Downloading $Archive..."
Invoke-WebRequest $DownloadUrl -OutFile $ArchivePath
# Verify checksum.
Write-Info "Verifying checksum..."
$Checksums = (Invoke-WebRequest $ChecksumUrl).Content
$Expected = ($Checksums -split "`n" | Where-Object { $_ -match $Archive } | Select-Object -First 1) -split '\s+' | Select-Object -First 1
if ($Expected) {
$Actual = (Get-FileHash -Algorithm SHA256 $ArchivePath).Hash.ToLower()
$Expected = $Expected.ToLower()
if ($Actual -ne $Expected) {
Write-Err "Checksum mismatch! Expected $Expected, got $Actual"
exit 1
}
Write-OK "Checksum verified"
} else {
Write-Warn "Could not find checksum for $Archive — skipping verification"
}
# Extract and install.
Expand-Archive $ArchivePath -DestinationPath $TempDir
$BinaryPath = Join-Path $TempDir "$BinaryName.exe"
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
$InstallPath = Join-Path $InstallDir "$BinaryName.exe"
Copy-Item $BinaryPath $InstallPath -Force
Write-OK "Installed Elastic Docs Utils $Version → $InstallPath"
# Ensure install dir is on PATH.
$UserPath = [Environment]::GetEnvironmentVariable("PATH", "User")
if ($UserPath -notlike "*$InstallDir*") {
[Environment]::SetEnvironmentVariable("PATH", "$UserPath;$InstallDir", "User")
Write-Info "Added $InstallDir to user PATH (restart your shell to pick it up)"
}
Write-Host ""
& $InstallPath @args
} finally {
Remove-Item $TempDir -Recurse -Force -ErrorAction SilentlyContinue
}