-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.ps1
More file actions
142 lines (108 loc) · 3.96 KB
/
Copy pathinstall.ps1
File metadata and controls
142 lines (108 loc) · 3.96 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
# OPAL installer for Windows — downloads the latest release binary.
# Usage: irm https://raw.githubusercontent.com/amorphous-engineering/OPAL/master/install.ps1 | iex
$ErrorActionPreference = "Stop"
$Repo = "amorphous-engineering/OPAL"
$ApiUrl = "https://api.github.com/repos/$Repo/releases/latest"
$InstallDir = Join-Path $env:LOCALAPPDATA "OPAL"
$InstallPath = Join-Path $InstallDir "opal.exe"
# --- Output helpers ---
function Write-Info {
param([string]$Message)
Write-Host "info: " -ForegroundColor Green -NoNewline
Write-Host $Message
}
function Write-Warn {
param([string]$Message)
Write-Host "warn: " -ForegroundColor Yellow -NoNewline
Write-Host $Message
}
function Write-Err {
param([string]$Message)
Write-Host "error: " -ForegroundColor Red -NoNewline
Write-Host $Message
exit 1
}
# --- Platform detection ---
function Get-Platform {
$Arch = $env:PROCESSOR_ARCHITECTURE
switch ($Arch) {
"AMD64" { return "x86_64" }
"x86" { Write-Err "32-bit Windows is not supported" }
"ARM64" { Write-Err "ARM64 Windows is not currently supported" }
default { Write-Err "Unsupported architecture: $Arch" }
}
}
# --- Release lookup ---
function Find-DownloadUrl {
param([string]$AssetPattern)
Write-Info "Fetching latest release from GitHub..."
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
try {
$Release = Invoke-RestMethod -Uri $ApiUrl -Headers @{ Accept = "application/vnd.github.v3+json" }
} catch {
Write-Err "Failed to fetch release info from GitHub: $_"
}
$script:Tag = $Release.tag_name
if (-not $Tag) {
Write-Err "Could not determine latest release tag"
}
Write-Info "Latest release: $Tag"
$Asset = $Release.assets | Where-Object { $_.name -like "*$AssetPattern*" } | Select-Object -First 1
if (-not $Asset) {
Write-Err "No release asset found matching '$AssetPattern'"
}
return $Asset.browser_download_url
}
# --- Install ---
function Install-Binary {
param([string]$Url, [string]$AssetName)
Write-Info "Downloading ${AssetName}..."
if (-not (Test-Path $InstallDir)) {
New-Item -ItemType Directory -Path $InstallDir -Force | Out-Null
}
$TmpFile = Join-Path $env:TEMP "opal-install-$([System.IO.Path]::GetRandomFileName()).exe"
try {
Invoke-WebRequest -Uri $Url -OutFile $TmpFile -UseBasicParsing
} catch {
if (Test-Path $TmpFile) { Remove-Item $TmpFile -Force }
Write-Err "Download failed: $_"
}
# Sanity check: file should be at least 1 KB
$Size = (Get-Item $TmpFile).Length
if ($Size -lt 1024) {
Remove-Item $TmpFile -Force
Write-Err "Downloaded file is too small ($Size bytes) -- something went wrong"
}
Move-Item -Path $TmpFile -Destination $InstallPath -Force
Write-Info "Installed to $InstallPath"
}
# --- PATH check ---
function Update-Path {
$UserPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ($UserPath -and $UserPath.Split(";") -contains $InstallDir) {
return
}
[Environment]::SetEnvironmentVariable("Path", "$InstallDir;$UserPath", "User")
$env:Path = "$InstallDir;$env:Path"
Write-Info "Added $InstallDir to your PATH"
Write-Warn "Restart your terminal for the PATH change to take effect in new sessions"
}
# --- Main ---
function Install-Opal {
Write-Host ""
Write-Host "OPAL Installer" -ForegroundColor White
Write-Host ""
$Arch = Get-Platform
Write-Info "Detected platform: windows $Arch"
$AssetName = "opal-windows-${Arch}.exe"
$DownloadUrl = Find-DownloadUrl -AssetPattern $AssetName
Install-Binary -Url $DownloadUrl -AssetName $AssetName
Update-Path
Write-Host ""
Write-Host "OPAL $Tag installed successfully." -ForegroundColor White
Write-Host " Run " -NoNewline
Write-Host "opal" -ForegroundColor Green -NoNewline
Write-Host " to start."
Write-Host ""
}
Install-Opal