From 21b1ac28df4bdf8de7376e382668b1d8c910b74b Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Mon, 5 Aug 2024 23:09:58 +0200 Subject: [PATCH] Ensure the environment variables are always set properly --- scripts/Windows/OS/Install.ps1 | 12 ++-- scripts/Windows/Scripts/Operations.ps1 | 14 ++++ scripts/Windows/Scripts/Software.ps1 | 91 +++++++++++++------------- 3 files changed, 65 insertions(+), 52 deletions(-) create mode 100644 scripts/Windows/Scripts/Operations.ps1 diff --git a/scripts/Windows/OS/Install.ps1 b/scripts/Windows/OS/Install.ps1 index ed28c7ce..312a027e 100644 --- a/scripts/Windows/OS/Install.ps1 +++ b/scripts/Windows/OS/Install.ps1 @@ -1,6 +1,7 @@ #!/bin/pwsh . "$PSScriptRoot/../../../scripts/Windows/Scripts/Prerequisites.ps1"; . "$PSScriptRoot/../Scripts/Context.ps1"; +. "$PSScriptRoot/../Scripts/Operations.ps1"; . "$PSScriptRoot/../Software/Firefox/Install.ps1"; . "$PSScriptRoot/Manage.ps1"; . "$PSScriptRoot/User/Install.ps1"; @@ -17,14 +18,9 @@ $null = New-Module { Finishes the installation of a running Windows machine. #> function Start-WindowsInstallation { - $ErrorActionPreference = 'Inquire'; - $env:WSLENV = "CONFIG_MODULE/p"; - - if ($env:CONFIG_MODULE) { - $env:CONFIG_MODULE = Resolve-Path $env:CONFIG_MODULE; - } - - Start-InstallationLoop; + Start-Operation { + Start-InstallationLoop; + }; } <# diff --git a/scripts/Windows/Scripts/Operations.ps1 b/scripts/Windows/Scripts/Operations.ps1 new file mode 100644 index 00000000..3f74274b --- /dev/null +++ b/scripts/Windows/Scripts/Operations.ps1 @@ -0,0 +1,14 @@ +function Start-Operation { + param( + [scriptblock] $Action + ) + + $ErrorActionPreference = 'Inquire'; + $env:WSLENV = "CONFIG_MODULE/p"; + + if ($env:CONFIG_MODULE) { + $env:CONFIG_MODULE = Resolve-Path $env:CONFIG_MODULE; + } + + & $Action; +} diff --git a/scripts/Windows/Scripts/Software.ps1 b/scripts/Windows/Scripts/Software.ps1 index 4f89e283..30530345 100644 --- a/scripts/Windows/Scripts/Software.ps1 +++ b/scripts/Windows/Scripts/Software.ps1 @@ -1,4 +1,5 @@ . "$PSScriptRoot/Config.ps1"; +. "$PSScriptRoot/Operations.ps1"; . "$PSScriptRoot/../Types/InstallerAction.ps1"; $null = New-Module { @@ -15,51 +16,53 @@ $null = New-Module { [hashtable] $Arguments ) - if (-not $Name) { - $Name = Split-Path -Leaf (Split-Path -Parent ((Get-PSCallStack)[1].ScriptName)); - } - - if ($null -ne $Name) { - $Name = "``$Name``"; - } else { - $Name = "unknown software"; - } - - $installHandler = { - param( - [InstallerAction] $Action, - [hashtable] $Arguments - ) - - $Arguments ??= @{ }; - - $argumentList = @{ - installer = $installHandler; - arguments = $Arguments; - }; - - if ($action -eq ([InstallerAction]::Install)) { - Write-Host "Installing $Name…"; - & $Installer @argumentList; - } elseif ($Action -eq ([InstallerAction]::Configure)) { - Write-Host "Configuring $Name…"; - & $Configurator @argumentList; - - foreach ($user in Get-Users) { - $Arguments.Add($userArgument, $user); - $argumentList.Add("action", [InstallerAction]::ConfigureUser); - & $installHandler @argumentList; - } - } elseif ($Action -eq ([InstallerAction]::ConfigureUser)) { - if ((-not $Arguments.ContainsKey($userArgument)) -or ($null -eq $Arguments[$userArgument])) { - $argumentList.Add($userArgument, ($env:UserName)); - } - - Write-Host "Configuring $Name for user ``$($Arguments[$userArgument])``…"; - & $UserConfigurator @argumentList; + Start-Operation { + if (-not $Name) { + $Name = Split-Path -Leaf (Split-Path -Parent ((Get-PSCallStack)[1].ScriptName)); } + + if ($null -ne $Name) { + $Name = "``$Name``"; + } else { + $Name = "unknown software"; + } + + $installHandler = { + param( + [InstallerAction] $Action, + [hashtable] $Arguments + ) + + $Arguments ??= @{ }; + + $argumentList = @{ + installer = $installHandler; + arguments = $Arguments; + }; + + if ($action -eq ([InstallerAction]::Install)) { + Write-Host "Installing $Name…"; + & $Installer @argumentList; + } elseif ($Action -eq ([InstallerAction]::Configure)) { + Write-Host "Configuring $Name…"; + & $Configurator @argumentList; + + foreach ($user in Get-Users) { + $Arguments.Add($userArgument, $user); + $argumentList.Add("action", [InstallerAction]::ConfigureUser); + & $installHandler @argumentList; + } + } elseif ($Action -eq ([InstallerAction]::ConfigureUser)) { + if ((-not $Arguments.ContainsKey($userArgument)) -or ($null -eq $Arguments[$userArgument])) { + $argumentList.Add($userArgument, ($env:UserName)); + } + + Write-Host "Configuring $Name for user ``$($Arguments[$userArgument])``…"; + & $UserConfigurator @argumentList; + } + }; + + & $installHandler -Action $Action -Arguments $Arguments; }; - - & $installHandler -Action $Action -Arguments $Arguments; } }