cleanup: use -Force making directories
[webi-installers/.git] / _webi / webi.ps1
index ebe0a02ac93759bc9ce8aba9afb0e3fd34d4b392..3a2b9c64752bdf8a5f6bc8d541017f110158ea7a 100644 (file)
@@ -1,32 +1,44 @@
 #!/usr/bin/env pwsh
 
+# this allows us to call ps1 files, which allows us to have spaces in filenames
+# ('powershell "$Env:USERPROFILE\test.ps1" foo' will fail if it has a space in
+# the path but '& "$Env:USERPROFILE\test.ps1" foo' will work even with a space)
+Set-ExecutionPolicy -Scope Process Bypass
+
 # If a command returns an error, halt the script.
 $ErrorActionPreference = 'Stop'
 
 # Ignore progress events from cmdlets so Invoke-WebRequest is not painfully slow
 $ProgressPreference = 'SilentlyContinue'
 
-# TODO get arch
-$Env:WEBI_UA = 'Windows/10 amd64'
+# This is the canonical CPU arch when the process is emulated
+$my_arch = "$Env:PROCESSOR_ARCHITEW6432"
+IF ($my_arch -eq $null -or $my_arch -eq "") {
+  # This is the canonical CPU arch when the process is native
+  $my_arch = "$Env:PROCESSOR_ARCHITECTURE"
+}
+# TODO API should know to prefer x86 for windows when arm binary is not available
+$Env:WEBI_UA = "Windows/10 $my_arch"
 $exename = $args[0]
 
 # Switch to userprofile
 pushd $Env:USERPROFILE
 
 # Make paths if needed
-if (!(Test-Path -Path .local\bin))
-{
-    New-Item -Path .local\bin -ItemType Directory
-}
+New-Item -Path .local\bin -ItemType Directory -Force
+# TODO replace all xbin with opt\bin\
+New-Item -Path .local\xbin -ItemType Directory -Force
+
+# See note on Set-ExecutionPolicy above
 Set-Content -Path .local\bin\webi.bat -Value "@echo off`r`npushd %USERPROFILE%`r`npowershell -ExecutionPolicy Bypass .local\bin\webi.ps1 %1`r`npopd"
 if (!(Test-Path -Path .local\opt))
 {
-    New-Item -Path .local\opt -ItemType Directory
+    New-Item -Path .local\opt -ItemType Directory -Force
 }
 # TODO windows version of mktemp -d
 if (!(Test-Path -Path .local\tmp))
 {
-    New-Item -Path .local\tmp -ItemType Directory
+    New-Item -Path .local\tmp -ItemType Directory -Force
 }
 
 # TODO SetStrictMode
@@ -44,17 +56,18 @@ if (!(Test-Path -Path .local\bin\pathman.exe))
 }
 
 # Run pathman to set up the folder
-#& "$Env:USERPROFILE\.local\bin\pathman.exe" add "$Env:USERPROFILE\.local\bin"
-& "$Env:USERPROFILE\.local\bin\pathman.exe" add .local\bin
+# (using unix style path because... cmd vs powershell vs whatever)
+& "$Env:USERPROFILE\.local\bin\pathman.exe" add ~/.local/bin
 
 # {{ baseurl }}
 # {{ version }}
 
 # Fetch <whatever>.ps1
 # TODO detect formats
-# Invoke-WebRequest -UserAgent "Windows amd64" "$Env:WEBI_HOST/api/installers/$exename.ps1?formats=zip,tar" -OutFile ".\.local\tmp\$exename.install.ps1"
-echo "Downloading $Env:WEBI_HOST/api/installers/$exename.ps1?formats=zip,tar"
-& curl.exe -fsSL -A "$Env:WEBI_UA" "$Env:WEBI_HOST/api/installers/$exename.ps1?formats=zip,tar" -o .\.local\tmp\$exename.install.ps1
+$PKG_URL = "$Env:WEBI_HOST/api/installers/$exename.ps1?formats=zip,exe,tar"
+echo "Downloading $PKG_URL"
+# Invoke-WebRequest -UserAgent "Windows amd64" "$PKG_URL" -OutFile ".\.local\tmp\$exename.install.ps1"
+& curl.exe -fsSL -A "$Env:WEBI_UA" "$PKG_URL" -o .\.local\tmp\$exename.install.ps1
 
 # Run <whatever>.ps1
 powershell .\.local\tmp\$exename.install.ps1