-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.ps1
More file actions
167 lines (151 loc) · 5.81 KB
/
Copy pathinstall.ps1
File metadata and controls
167 lines (151 loc) · 5.81 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
$ErrorActionPreference = "Stop"
$Repo = if ($env:LING_REPO) { $env:LING_REPO } else { "LISTENAI/ling" }
$Bin = "ling"
$InstallDir = if ($env:LING_INSTALL_DIR) { $env:LING_INSTALL_DIR } else { Join-Path $HOME "bin" }
$Token = if ($env:GH_TOKEN) { $env:GH_TOKEN } else { $env:GITHUB_TOKEN }
$ApiUrl = "https://api.github.com/repos/$Repo"
$Headers = @{
"X-GitHub-Api-Version" = "2022-11-28"
}
if ($Token) {
$Headers["Authorization"] = "Bearer $Token"
}
function Invoke-GitHubJson {
param([string] $Uri)
return Invoke-RestMethod -Uri $Uri -Headers $Headers
}
function Resolve-Target {
$arch = $null
try {
$runtimeArch = [System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture
if ($runtimeArch) {
$arch = $runtimeArch.ToString().ToLowerInvariant()
}
} catch {
# Windows PowerShell 5.1 on older .NET can miss RuntimeInformation.
}
if (-not $arch) {
$envArch = if ($env:PROCESSOR_ARCHITEW6432) { $env:PROCESSOR_ARCHITEW6432 } else { $env:PROCESSOR_ARCHITECTURE }
if ($envArch) {
$arch = $envArch.ToLowerInvariant()
}
}
switch ($arch) {
"amd64" { return "x86_64-pc-windows-msvc" }
"x64" { return "x86_64-pc-windows-msvc" }
"x86_64" { return "x86_64-pc-windows-msvc" }
"arm64" { return "aarch64-pc-windows-msvc" }
"aarch64" { return "aarch64-pc-windows-msvc" }
default { throw "Unsupported CPU architecture: $arch" }
}
}
function Get-Release {
param([string] $Version)
if ($Version) {
return Invoke-GitHubJson "$ApiUrl/releases/tags/$Version"
}
return Invoke-GitHubJson "$ApiUrl/releases/latest"
}
function Save-ReleaseAsset {
param(
[object] $Release,
[string] $Name,
[string] $OutFile
)
if ($Token) {
$asset = $Release.assets | Where-Object { $_.name -eq $Name } | Select-Object -First 1
if (-not $asset) {
throw "Release asset not found: $Name"
}
$downloadHeaders = $Headers.Clone()
$downloadHeaders["Accept"] = "application/octet-stream"
Invoke-WebRequest -Uri $asset.url -Headers $downloadHeaders -OutFile $OutFile
} else {
Invoke-WebRequest -Uri "https://github.com/$Repo/releases/download/$($Release.tag_name)/$Name" -OutFile $OutFile
}
}
function Test-CommandOnPath {
param([string] $Directory)
$full = [System.IO.Path]::GetFullPath($Directory).TrimEnd('\')
$currentPath = if ($env:Path) { $env:Path } else { "" }
foreach ($part in ($currentPath -split ';')) {
if (-not $part) { continue }
try {
if ([System.IO.Path]::GetFullPath($part).TrimEnd('\') -ieq $full) {
return $true
}
} catch {
continue
}
}
return $false
}
$RequestedVersion = if ($env:LING_VERSION) { $env:LING_VERSION } else { $null }
$Release = Get-Release $RequestedVersion
$Version = $Release.tag_name
if (-not $Version) {
throw "Failed to resolve release tag for $Repo"
}
$Target = Resolve-Target
$Asset = "$Bin-$Version-$Target.zip"
$TempDir = Join-Path ([System.IO.Path]::GetTempPath()) ("ling-install-" + [System.Guid]::NewGuid().ToString("N"))
try {
New-Item -ItemType Directory -Force -Path $TempDir | Out-Null
$Archive = Join-Path $TempDir $Asset
$Checksums = Join-Path $TempDir "SHA256SUMS"
Write-Host "Installing $Bin $Version for $Target"
Write-Host "Downloading $Asset"
Save-ReleaseAsset $Release $Asset $Archive
try {
Save-ReleaseAsset $Release "SHA256SUMS" $Checksums
$line = Get-Content $Checksums | Where-Object { $_ -match "\s$([regex]::Escape($Asset))$" } | Select-Object -First 1
if ($line) {
$expected = (($line -split '\s+')[0]).ToLowerInvariant()
$actual = (Get-FileHash -Algorithm SHA256 -Path $Archive).Hash.ToLowerInvariant()
if ($expected -ne $actual) {
throw "Checksum mismatch for $Asset"
}
Write-Host "Checksum verified"
}
} catch {
Write-Warning "Skipping checksum verification: $($_.Exception.Message)"
}
Expand-Archive -Force -Path $Archive -DestinationPath $TempDir
$Source = Join-Path $TempDir "$Bin-$Version-$Target\$Bin.exe"
if (-not (Test-Path $Source)) {
throw "Binary not found in archive: $Source"
}
New-Item -ItemType Directory -Force -Path $InstallDir | Out-Null
$Destination = Join-Path $InstallDir "$Bin.exe"
Copy-Item -Force $Source $Destination
Write-Host "Installed to $Destination"
& $Destination --help *> $null
if (-not (Test-CommandOnPath $InstallDir)) {
if ($env:LING_NO_PATH -eq "1") {
Write-Host "Add $InstallDir to PATH if 'ling' is not found."
} else {
$fullInstallDir = [System.IO.Path]::GetFullPath($InstallDir).TrimEnd('\')
$userPath = [Environment]::GetEnvironmentVariable("Path", "User")
if ([string]::IsNullOrWhiteSpace($userPath)) {
$newUserPath = $fullInstallDir
} elseif (($userPath -split ';') -notcontains $fullInstallDir) {
$newUserPath = $userPath.TrimEnd(';') + ";" + $fullInstallDir
} else {
$newUserPath = $userPath
}
[Environment]::SetEnvironmentVariable("Path", $newUserPath, "User")
$currentPath = if ($env:Path) { $env:Path.TrimEnd(';') } else { "" }
if ($currentPath) {
$env:Path = $currentPath + ";" + $fullInstallDir
} else {
$env:Path = $fullInstallDir
}
Write-Host "Added $fullInstallDir to your user PATH. Restart the terminal if 'ling' is not found."
}
}
Write-Host "Done. Try: ling --help"
} finally {
if (Test-Path $TempDir) {
Remove-Item -Recurse -Force $TempDir
}
}