f9aa66da0fb9777a26ae249863b8d572dec11192
[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 | out-null
29 # TODO replace all xbin with opt\bin\
30 New-Item -Path .local\xbin -ItemType Directory -Force | out-null
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 # Backwards-compat bugfix: remove old webi-pwsh.ps1 location
35 Remove-Item -Path .local\bin\webi.ps1 -Recurse -ErrorAction Ignore
36 if (!(Test-Path -Path .local\opt))
37 {
38     New-Item -Path .local\opt -ItemType Directory -Force | out-null
39 }
40 # TODO windows version of mktemp -d
41 if (!(Test-Path -Path .local\tmp))
42 {
43     New-Item -Path .local\tmp -ItemType Directory -Force | out-null
44 }
45
46 # TODO SetStrictMode
47 # TODO Test-Path variable:global:Env:WEBI_HOST ???
48 IF($Env:WEBI_HOST -eq $null -or $Env:WEBI_HOST -eq "")
49 {
50     $Env:WEBI_HOST = "https://webinstall.dev"
51 }
52
53 if (!(Test-Path -Path .local\bin\pathman.exe))
54 {
55     & curl.exe -fsSL -A "$Env:WEBI_UA" "$Env:WEBI_HOST/packages/pathman/install.ps1" -o .\.local\tmp\pathman-setup.ps1
56     powershell .\.local\tmp\pathman-setup.ps1
57     # TODO del .\.local\tmp\pathman-setup.bat
58 }
59
60 # Run pathman to set up the folder
61 # (using unix style path because... cmd vs powershell vs whatever)
62 & "$Env:USERPROFILE\.local\bin\pathman.exe" add ~/.local/bin
63
64 # {{ baseurl }}
65 # {{ version }}
66
67 # Fetch <whatever>.ps1
68 # TODO detect formats
69 $PKG_URL = "$Env:WEBI_HOST/api/installers/$exename.ps1?formats=zip,exe,tar"
70 echo "Downloading $PKG_URL"
71 # Invoke-WebRequest -UserAgent "Windows amd64" "$PKG_URL" -OutFile ".\.local\tmp\$exename.install.ps1"
72 & curl.exe -fsSL -A "$Env:WEBI_UA" "$PKG_URL" -o .\.local\tmp\$exename.install.ps1
73
74 # Run <whatever>.ps1
75 powershell .\.local\tmp\$exename.install.ps1
76
77 # Done
78 popd