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