5bb6966e966c4504fafdc52b7ebb7486e46c8e65
[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 # TODO get arch
10 $Env:WEBI_UA = 'Windows/10 amd64'
11 $exename = $args[0]
12
13 # Switch to userprofile
14 pushd $Env:USERPROFILE
15
16 # Make paths if needed
17 if (!(Test-Path -Path .local\bin))
18 {
19     New-Item -Path .local\bin -ItemType Directory
20 }
21 Set-Content -Path .local\bin\webi.bat -Value "echo off`r`npushd %USERPROFILE%`r`npowershell -ExecutionPolicy Bypass .local\bin\webi.ps1 %1`r`npopd"
22 if (!(Test-Path -Path .local\opt))
23 {
24     New-Item -Path .local\opt -ItemType Directory
25 }
26 # TODO windows version of mktemp -d
27 if (!(Test-Path -Path .local\tmp))
28 {
29     New-Item -Path .local\tmp -ItemType Directory
30 }
31
32 # TODO SetStrictMode
33 # TODO Test-Path variable:global:Env:WEBI_HOST ???
34 IF($Env:WEBI_HOST -eq $null -or $Env:WEBI_HOST -eq "")
35 {
36     $Env:WEBI_HOST = "https://webinstall.dev"
37 }
38
39 if (!(Test-Path -Path .local\bin\pathman.exe))
40 {
41     & curl.exe -fsSL -A "$Env:WEBI_UA" "$Env:WEBI_HOST/packages/pathman/install.ps1" -o .\.local\tmp\pathman-setup.ps1
42     powershell .\.local\tmp\pathman-setup.ps1
43     # TODO del .\.local\tmp\pathman-setup.bat
44 }
45
46 # Run pathman to set up the folder
47 #& "$Env:USERPROFILE\.local\bin\pathman.exe" add "$Env:USERPROFILE\.local\bin"
48 & "$Env:USERPROFILE\.local\bin\pathman.exe" add .local\bin
49
50 # {{ baseurl }}
51 # {{ version }}
52
53 # Fetch <whatever>.ps1
54 # TODO detect formats
55 # Invoke-WebRequest -UserAgent "Windows amd64" "$Env:WEBI_HOST/api/installers/$exename.ps1?formats=zip,tar" -OutFile ".\.local\tmp\$exename.install.ps1"
56 echo "Downloading $Env:WEBI_HOST/api/installers/$exename.ps1?formats=zip,tar"
57 & curl.exe -fsSL -A "$Env:WEBI_UA" "$Env:WEBI_HOST/api/installers/$exename.ps1?formats=zip,tar" -o .\.local\tmp\$exename.install.ps1
58
59 # Run <whatever>.ps1
60 powershell .\.local\tmp\$exename.install.ps1
61
62 # Done
63 popd