-
Notifications
You must be signed in to change notification settings - Fork 14
/
install.ps1
executable file
·377 lines (305 loc) · 11.4 KB
/
install.ps1
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# MetaCall Install Script by Parra Studios
# Cross-platform set of scripts to install MetaCall infrastructure.
#
# Copyright (C) 2016 - 2024 Vicente Eduardo Ferrer Garcia <[email protected]>
#
# 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.
<#
.SYNOPSIS
Installs MetaCall CLI
.DESCRIPTION
MetaCall is a extensible, embeddable and interoperable cross-platform polyglot runtime. It supports NodeJS, Vanilla JavaScript, TypeScript, Python, Ruby, C#, Java, WASM, Go, C, C++, Rust, D, Cobol and more.
.PARAMETER Version
Default: latest
Version of the tarball to be downloaded. Versions are available here: https://github.com/metacall/distributable-windows/releases.
Possible values are:
- latest - most latest build
- 3-part version in a format A.B.C - represents specific version of build
examples: 0.2.0, 0.1.0, 0.0.22
.PARAMETER InstallDir
Default: %LocalAppData%\MetaCall
Path to where to install MetaCall. Note that binaries will be placed directly in a given directory.
.PARAMETER FromPath
Default: $null
Path to the tarball to be installed. If specified, this parameter will override the Version parameter.
.PARAMETER Uninstall
Removes MetaCall from the specified installation directory and clears its entry from the PATH environment variable.
If this parameter is specified, the script will perform an uninstallation rather than an installation.
#>
[cmdletbinding()]
param(
[string]$InstallDir="<auto>",
[string]$Version="latest",
[string]$FromPath=$null,
[switch]$Uninstall=$false
)
Set-StrictMode -Version Latest
$ErrorActionPreference="Stop"
$ProgressPreference="SilentlyContinue"
function Print-With-Fallback([string]$Message) {
try {
Write-Host "$Message"
} catch {
Write-Output "$Message"
}
}
function Print-Title([string]$Message) {
Print-With-Fallback "$Message`n"
}
function Print-Info([string]$Message) {
Print-With-Fallback "$([char]0x25B7) $Message"
}
function Print-Success([string]$Message) {
Print-With-Fallback "$([char]0x2714) $Message"
}
function Print-Warning([string]$Message) {
Print-With-Fallback "$([char]0x26A0) $Message"
}
function Print-Error([string]$Message) {
Print-With-Fallback "$([char]0x2718) $Message"
}
function Print-Debug([string]$Message) {
if ($env:METACALL_INSTALL_DEBUG) {
Print-With-Fallback "$([char]0x2699) $Message"
}
}
function Get-Machine-Architecture() {
# On PS x86, PROCESSOR_ARCHITECTURE reports x86 even on x64 systems.
# To get the correct architecture, we need to use PROCESSOR_ARCHITEW6432.
# PS x64 doesn't define this, so we fall back to PROCESSOR_ARCHITECTURE.
# Possible values: amd64, x64, x86, arm64, arm
if( $env:PROCESSOR_ARCHITEW6432 -ne $null )
{
return $env:PROCESSOR_ARCHITEW6432
}
return $env:PROCESSOR_ARCHITECTURE
}
function Get-CLI-Architecture() {
$Architecture = $(Get-Machine-Architecture)
switch ($Architecture.ToLowerInvariant()) {
{ ($_ -eq "amd64") -or ($_ -eq "x64") } { return "x64" }
# TODO:
# { $_ -eq "x86" } { return "x86" }
# { $_ -eq "arm" } { return "arm" }
# { $_ -eq "arm64" } { return "arm64" }
default { throw "Architecture '$Architecture' not supported. If you are interested in this platform feel free to contribute to https://github.com/metacall/distributable-windows" }
}
}
function Get-User-Share-Path() {
$InstallRoot = $env:METACALL_INSTALL_DIR
if (!$InstallRoot) {
$InstallRoot = "$env:LocalAppData\MetaCall"
}
return $InstallRoot
}
function Resolve-Installation-Path([string]$InstallDir) {
if ($InstallDir -eq "<auto>") {
return Get-User-Share-Path
}
return $InstallDir
}
function Get-RedirectedUri {
<#
.SYNOPSIS
Gets the real download URL from the redirection.
.DESCRIPTION
Used to get the real URL for downloading a file, this will not work if downloading the file directly.
.EXAMPLE
Get-RedirectedURL -URL "https://download.mozilla.org/?product=firefox-latest&os=win&lang=en-US"
.PARAMETER URL
URL for the redirected URL to be un-obfuscated
.NOTES
Code from: Redone per issue #2896 in core https://github.com/PowerShell/PowerShell/issues/2896
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[string]$Uri
)
process {
do {
try {
$Request = Invoke-WebRequest -UseBasicParsing -Method Head -Uri $Uri
if ($Request.BaseResponse.ResponseUri -ne $null) {
# This is for Powershell 5
$RedirectUri = $Request.BaseResponse.ResponseUri
}
elseif ($Request.BaseResponse.RequestMessage.RequestUri -ne $null) {
# This is for Powershell core
$RedirectUri = $Request.BaseResponse.RequestMessage.RequestUri
}
$Retry = $false
}
catch {
if (($_.Exception.GetType() -match "HttpResponseException") -and ($_.Exception -match "302")) {
$Uri = $_.Exception.Response.Headers.Location.AbsoluteUri
$Retry = $true
}
else {
throw $_
}
}
} while ($Retry)
$RedirectUri
}
}
function Resolve-Version([string]$Version) {
if ($Version.ToLowerInvariant() -eq "latest") {
$LatestTag = $(Get-RedirectedUri "https://github.com/metacall/distributable-windows/releases/latest")
return $LatestTag.Segments[$LatestTag.Segments.Count - 1]
} else {
return "v$Version"
}
}
function Post-Install([string]$InstallRoot) {
# Reinstall Python Pip to the latest version (needed in order to patch the python.exe location)
$InstallLocation = Join-Path -Path $InstallRoot -ChildPath "metacall"
$InstallPythonScript = @"
setlocal
set "PYTHONHOME=$($InstallLocation)\runtimes\python"
set "PIP_TARGET=$($InstallLocation)\runtimes\python\Pip"
set "PATH=$($InstallLocation)\runtimes\python;$($InstallLocation)\runtimes\python\Scripts"
start "" "$($InstallLocation)\runtimes\python\python.exe" -m pip install --upgrade --force-reinstall pip
endlocal
"@
# PIP_TARGET here might be incorrect here, for more info check https://github.com/metacall/distributable-windows/pull/20
$InstallPythonScriptOneLine = $($InstallPythonScript.Trim()).replace("`n", " && ")
cmd /V /C "$InstallPythonScriptOneLine"
# Install Additional Packages
Install-Additional-Packages -InstallRoot $InstallRoot -Component "deploy"
Install-Additional-Packages -InstallRoot $InstallRoot -Component "faas"
# TODO: Replace in the files D:/ and D:\
}
function Path-Install([string]$InstallRoot) {
# Add safely MetaCall command to the PATH (and persist it)
# To add folder containing metacall.bat to PATH
$PersistedPaths = [Environment]::GetEnvironmentVariable('PATH', [EnvironmentVariableTarget]::User) -split ';'
if ($PersistedPaths -notcontains $InstallRoot) {
[Environment]::SetEnvironmentVariable('PATH', $env:PATH+";"+$InstallRoot, [EnvironmentVariableTarget]::User)
}
# To verify if PATH isn't already added
$EnvPaths = $env:PATH -split ';'
if ($EnvPaths -notcontains $InstallRoot) {
$EnvPaths = $EnvPaths + $InstallRoot | where { $_ }
$env:Path = $EnvPaths -join ';'
}
# Support for GitHub actions environment
if ($env:GITHUB_ENV -ne $null) {
echo "PATH=$env:PATH" >> $env:GITHUB_ENV
}
}
function Path-Uninstall([string]$Path) {
$PersistedPaths = [Environment]::GetEnvironmentVariable('PATH', [EnvironmentVariableTarget]::User) -split ';'
if ($PersistedPaths -contains $Path) {
$PersistedPaths = $PersistedPaths | where { $_ -and $_ -ne $Path }
[Environment]::SetEnvironmentVariable('PATH', $PersistedPaths -join ';', [EnvironmentVariableTarget]::User)
}
$EnvPaths = $env:PATH -split ';'
if ($EnvPaths -contains $Path) {
$EnvPaths = $EnvPaths | where { $_ -and $_ -ne $Path }
$env:Path = $EnvPaths -join ';'
}
}
function Install-Tarball([string]$InstallDir, [string]$Version) {
Print-Title "MetaCall Binary Installation."
$InstallRoot = Resolve-Installation-Path $InstallDir
$InstallOutput = Join-Path -Path $InstallRoot -ChildPath "metacall-tarball-win.zip"
# Delete directory contents if any
if (Test-Path $InstallRoot) {
Remove-Item -Recurse -Force $InstallRoot | Out-Null
}
Print-Debug "Install MetaCall in folder: $InstallRoot"
# Create directory if it does not exist
New-Item -ItemType Directory -Force -Path $InstallRoot | Out-Null
if (!$FromPath) {
Print-Info "Downloading tarball."
$InstallVersion = Resolve-Version $Version
$InstallArchitecture = Get-CLI-Architecture
$DownloadUri = "https://github.com/metacall/distributable-windows/releases/download/$InstallVersion/metacall-tarball-win-$InstallArchitecture.zip"
# Download the tarball
Invoke-WebRequest -Uri $DownloadUri -OutFile $InstallOutput
Print-Success "Tarball downloaded."
} else {
# Copy the tarball from the path
Copy-Item -Path $FromPath -Destination $InstallOutput
}
Print-Info "Uncompressing tarball."
# Unzip the tarball
Expand-Archive -Path $InstallOutput -DestinationPath $InstallRoot -Force
Print-Success "Tarball extracted correctly."
# Delete the tarball
Remove-Item -Force $InstallOutput | Out-Null
Print-Info "Running post-install scripts."
# Run post install scripts
Post-Install $InstallRoot
Print-Info "Adding MetaCall to PATH."
# Add MetaCall CLI to PATH
Path-Install $InstallRoot
Print-Success "MetaCall installed successfully."
}
function Set-NodePath {
param (
[string]$NodePath,
[string]$FilePath
)
if (-not (Test-Path $FilePath)) {
Print-Error "Failed to set up an additional package, the file $FilePath does not exist."
return
}
$Content = Get-Content -Path $FilePath
Print-Debug "Replace $FilePath content:`n$Content"
$Content = $Content -replace '%dp0%\\node.exe', $NodePath
$Content = $Content -replace '""', '"'
Print-Debug "With new content:`n$Content"
Set-Content -Path $FilePath -Value $Content
}
function Install-Additional-Packages {
param (
[string]$InstallRoot,
[string]$Component
)
$ComponentDir = Join-Path -Path $InstallRoot -ChildPath "deps\$Component"
if (-not (Test-Path $ComponentDir)) {
New-Item -ItemType Directory -Force -Path $ComponentDir | Out-Null
}
Print-Info "Installing '$Component' additional package."
$NodePath = Join-Path -Path $InstallRoot -ChildPath "metacall\runtimes\nodejs\node.exe"
Invoke-Expression "npm install --global --prefix=`"$ComponentDir`" @metacall/$Component"
Set-NodePath -NodePath $NodePath -FilePath "$ComponentDir\metacall-$Component.cmd"
Print-Success "Package '$Component' has been installed."
}
function Uninstall([string]$InstallDir) {
Print-Title "MetaCall Uninstallation"
$InstallRoot = Resolve-Installation-Path $InstallDir
if (-not (Test-Path $InstallRoot)) {
Print-Error "Failed to uninstall MetaCall, the folder $InstallRoot does not exist."
return
}
Print-Info "Removing MetaCall files."
# Delete MetaCall files from the install directory
Remove-Item -Recurse -Force $InstallRoot
if (-not (Test-Path $InstallRoot)) {
Print-Debug "MetaCall files removed from: $InstallRoot successfully"
}
Print-Info "Removing MetaCall from PATH."
# Call the Path-Uninstall function to remove from PATH
Path-Uninstall $InstallRoot
Print-Success "MetaCall uninstallation completed."
}
if ($Uninstall) {
# Uninstall the metacall and remove path
Uninstall $InstallDir
} else {
# Install the tarball and post scripts
Install-Tarball $InstallDir $Version
}