-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall-ferret.ps1
More file actions
105 lines (94 loc) · 4.01 KB
/
install-ferret.ps1
File metadata and controls
105 lines (94 loc) · 4.01 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
param(
[string]$Version = "latest",
[string]$Repo = "Ferret-Language/Ferret",
[string]$InstallDir = "$env:LOCALAPPDATA\Ferret"
)
$ErrorActionPreference = "Stop"
function Get-ArchName {
switch ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture.ToString().ToLowerInvariant()) {
"x64" { return "amd64" }
"arm64" { return "arm64" }
default { throw "Unsupported architecture: $([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)" }
}
}
function Get-ReleaseApiUrl([string]$repoName, [string]$ver) {
if ($ver -eq "latest") {
return "https://api.github.com/repos/$repoName/releases/latest"
}
return "https://api.github.com/repos/$repoName/releases/tags/$ver"
}
function Ensure-UserPathContains([string]$pathEntry) {
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ([string]::IsNullOrWhiteSpace($userPath)) {
$userPath = $pathEntry
} else {
$parts = $userPath.Split(';', [System.StringSplitOptions]::RemoveEmptyEntries)
$exists = $false
foreach ($part in $parts) {
if ($part.TrimEnd('\') -ieq $pathEntry.TrimEnd('\')) {
$exists = $true
break
}
}
if (-not $exists) {
$userPath = "$userPath;$pathEntry"
}
}
[Environment]::SetEnvironmentVariable("Path", $userPath, "User")
}
$archName = Get-ArchName
$coreAssetName = "ferret-windows-$archName.zip"
$toolchainAssetName = "ferret-toolchain-windows-$archName.zip"
$apiUrl = Get-ReleaseApiUrl -repoName $Repo -ver $Version
Write-Host "Resolving release from $apiUrl"
$release = Invoke-RestMethod -Uri $apiUrl -Headers @{ "User-Agent" = "ferret-installer" }
$coreAsset = $release.assets | Where-Object { $_.name -eq $coreAssetName } | Select-Object -First 1
if (-not $coreAsset) {
throw "Could not find release asset '$coreAssetName' in $Repo ($Version)."
}
$toolchainAsset = $release.assets | Where-Object { $_.name -eq $toolchainAssetName } | Select-Object -First 1
if (-not $toolchainAsset) {
throw "Could not find release asset '$toolchainAssetName' in $Repo ($Version)."
}
$tmpDir = Join-Path $env:TEMP ("ferret-install-" + [guid]::NewGuid().ToString("N"))
New-Item -ItemType Directory -Path $tmpDir | Out-Null
try {
$coreZipPath = Join-Path $tmpDir $coreAssetName
$toolchainZipPath = Join-Path $tmpDir $toolchainAssetName
Write-Host "Downloading $($coreAsset.browser_download_url)"
Invoke-WebRequest -Uri $coreAsset.browser_download_url -OutFile $coreZipPath
Write-Host "Downloading $($toolchainAsset.browser_download_url)"
Invoke-WebRequest -Uri $toolchainAsset.browser_download_url -OutFile $toolchainZipPath
if (Test-Path $InstallDir) {
Remove-Item -Recurse -Force $InstallDir
}
New-Item -ItemType Directory -Path $InstallDir | Out-Null
Expand-Archive -Path $coreZipPath -DestinationPath $InstallDir -Force
$toolchainDir = Join-Path $InstallDir "toolchain"
New-Item -ItemType Directory -Path $toolchainDir -Force | Out-Null
Expand-Archive -Path $toolchainZipPath -DestinationPath $toolchainDir -Force
$binDir = Join-Path $InstallDir "bin"
$ferretExe = Join-Path $binDir "ferret.exe"
if (-not (Test-Path $ferretExe)) {
$rootExe = Join-Path $InstallDir "ferret.exe"
if (Test-Path $rootExe) {
New-Item -ItemType Directory -Path $binDir -Force | Out-Null
Move-Item -Force $rootExe $ferretExe
}
}
if (-not (Test-Path $ferretExe)) {
throw "ferret.exe not found after extraction at $ferretExe"
}
Ensure-UserPathContains -pathEntry $binDir
if (-not ($env:Path -split ';' | Where-Object { $_.TrimEnd('\') -ieq $binDir.TrimEnd('\') })) {
$env:Path = "$binDir;$env:Path"
}
Write-Host "Ferret installed to: $InstallDir"
Write-Host "Binary: $ferretExe"
Write-Host "User PATH updated. Open a new terminal and run: ferret --help"
}
finally {
if (Test-Path $tmpDir) {
Remove-Item -Recurse -Force $tmpDir
}
}