0a1b89f9f9946db7d22ad909b2c7be79d3e3ab0c
[webi-installers/.git] / _webi / webi.ps1
1 #!/usr/bin/env pwsh
2
3 # If a command returns an error, halt the script.
4 $ErrorActionPreference = 'Stop'
5
6 # Ignore progress events from cmdlets so Invoke-WebRequest is not painfully slow
7 $ProgressPreference = 'SilentlyContinue'
8
9 # This is the canonical CPU arch when the process is emulated
10 $my_arch = "$Env:PROCESSOR_ARCHITEW6432"
11 IF ($my_arch -eq $null -or $my_arch -eq "") {
12   # This is the canonical CPU arch when the process is native
13   $my_arch = "$Env:PROCESSOR_ARCHITECTURE"
14 }
15 # TODO API should know to prefer x86 for windows when arm binary is not available
16 $Env:WEBI_UA = "Windows/10 $my_arch"
17 $exename = $args[0]
18
19 # Switch to userprofile
20 pushd $Env:USERPROFILE
21
22 # Make paths if needed
23 if (!(Test-Path -Path .local\bin))
24 {
25     New-Item -Path .local\bin -ItemType Directory
26 }
27 if (!(Test-Path -Path .local\xbin))
28 {
29     New-Item -Path .local\xbin -ItemType Directory
30 }
31 Set-Content -Path .local\bin\webi.bat -Value "@echo off`r`npushd %USERPROFILE%`r`npowershell -ExecutionPolicy Bypass .local\bin\webi.ps1 %1`r`npopd"
32 if (!(Test-Path -Path .local\opt))
33 {
34     New-Item -Path .local\opt -ItemType Directory
35 }
36 # TODO windows version of mktemp -d
37 if (!(Test-Path -Path .local\tmp))
38 {
39     New-Item -Path .local\tmp -ItemType Directory
40 }
41
42 # TODO SetStrictMode
43 # TODO Test-Path variable:global:Env:WEBI_HOST ???
44 IF($Env:WEBI_HOST -eq $null -or $Env:WEBI_HOST -eq "")
45 {
46     $Env:WEBI_HOST = "https://webinstall.dev"
47 }
48
49 if (!(Test-Path -Path .local\bin\pathman.exe))
50 {
51     & curl.exe -fsSL -A "$Env:WEBI_UA" "$Env:WEBI_HOST/packages/pathman/install.ps1" -o .\.local\tmp\pathman-setup.ps1
52     powershell .\.local\tmp\pathman-setup.ps1
53     # TODO del .\.local\tmp\pathman-setup.bat
54 }
55
56 # Run pathman to set up the folder
57 # (using unix style path because... cmd vs powershell vs whatever)
58 & "$Env:USERPROFILE\.local\bin\pathman.exe" add ~/.local/bin
59
60 # {{ baseurl }}
61 # {{ version }}
62
63 # Fetch <whatever>.ps1
64 # TODO detect formats
65 $PKG_URL = "$Env:WEBI_HOST/api/installers/$exename.ps1?formats=zip,exe,tar"
66 echo "Downloading $PKG_URL"
67 # Invoke-WebRequest -UserAgent "Windows amd64" "$PKG_URL" -OutFile ".\.local\tmp\$exename.install.ps1"
68 & curl.exe -fsSL -A "$Env:WEBI_UA" "$PKG_URL" -o .\.local\tmp\$exename.install.ps1
69
70 # Run <whatever>.ps1
71 powershell .\.local\tmp\$exename.install.ps1
72
73 # Done
74 popd