Refactor software script environment

This commit is contained in:
Manuel Thalmann 2024-08-05 00:07:41 +02:00
parent b37815498a
commit 8564396d86
2 changed files with 52 additions and 84 deletions

View file

@ -1,82 +1,57 @@
. "$PSScriptRoot/Config.ps1"; . "$PSScriptRoot/Config.ps1";
. "$PSScriptRoot/../Types/InstallerAction.ps1"; . "$PSScriptRoot/../Types/InstallerAction.ps1";
<#
.SYNOPSIS
Gets the name of the software.
#>
function Get-SoftwareName {
$path = ${Function:Install-Software}.File;
if ($path -ne "$PSCommandPath") {
Split-Path -Leaf (Split-Path -Parent $path);
} else {
$null;
}
}
<#
.SYNOPSIS
Installs the software.
#>
function Install-Software { }
<#
.SYNOPSIS
Configures the system for the software.
#>
function Set-SoftwareConfiguration { }
<#
.SYNOPSIS
Configures a user for the software.
.PARAMETER Name
The name of the user to configure.
#>
function Set-SoftwareUserConfiguration {
param(
[Parameter(Mandatory = $true)]
[string] $Name
)
}
$null = New-Module { $null = New-Module {
. "$PSScriptRoot/../Types/InstallerAction.ps1"; . "$PSScriptRoot/../Types/InstallerAction.ps1";
$userArgument = "name"; $userArgument = "name";
function Start-SoftwareInstaller { function Start-SoftwareInstaller {
param(
[string] $Name,
[scriptblock] $Installer = { },
[scriptblock] $Configurator = { },
[scriptblock] $UserConfigurator = { },
[InstallerAction] $Action = [InstallerAction]::Install,
[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";
}
function Invoke-SoftwareInstaller {
param( param(
[InstallerAction] $Action, [InstallerAction] $Action,
[hashtable] $Arguments [hashtable] $Arguments
) )
$null = $softwareName; if ($action -eq ([InstallerAction]::Install)) {
Write-Host "Installing $Name";
if ($null -ne (Get-SoftwareName)) { & $Installer @Arguments;
$softwareName = "``$(Get-SoftwareName)``"; } elseif ($Action -eq ([InstallerAction]::Configure)) {
} else { Write-Host "Configuring $Name";
$softwareName = "unknown software"; & $Configurator @Arguments;
}
if (($null -eq $Action) -or ($action -eq ([InstallerAction]::Install))) {
Write-Host "Installing $softwareName";
Install-Software @Arguments;
} elseif ($action -eq ([InstallerAction]::Configure)) {
Write-Host "Configuring $softwareName";
Set-SoftwareConfiguration @Arguments;
foreach ($user in Get-Users) { foreach ($user in Get-Users) {
$Arguments.Add($userArgument, $user); $Arguments.Add($userArgument, $user);
Start-SoftwareInstaller -Action ([InstallerAction]::ConfigureUser) @Arguments; Invoke-SoftwareInstaller -Action ([InstallerAction]::ConfigureUser) -Arguments $Arguments;
} }
} elseif ($action -eq ([InstallerAction]::ConfigureUser)) { } elseif ($Action -eq ([InstallerAction]::ConfigureUser)) {
if ((-not $Arguments.Contains($userArgument)) -or ($null -eq $Arguments[$userArgument])) { if ((-not $Arguments.ContainsKey($userArgument)) -or ($null -eq $Arguments[$userArgument])) {
$Arguments.Add($userArgument, ($env:UserName)); $Arguments.Add($userArgument, ($env:UserName));
} }
Write-Host "Configuring $softwareName for user ``$($Arguments[$userArgument])``"; Write-Host "Configuring $Name for user ``$($Arguments[$userArgument])``";
Set-SoftwareUserConfiguration @Arguments; & $UserConfigurator @Arguments;
} }
} }
Invoke-SoftwareInstaller -Action $Action -Arguments $Arguments;
}
} }

View file

@ -3,19 +3,13 @@ param(
[hashtable] $Arguments [hashtable] $Arguments
) )
. "$PSScriptRoot/../../Scripts/Software.ps1"; . "$PSScriptRoot/../../../Common/Scripts/Software.ps1";
. "$PSScriptRoot/../../Types/InstallerAction.ps1";
function Install-Winget { & {
param( param($Parameters)
[InstallerAction] $Action = [InstallerAction]::Install,
[hashtable] $Arguments
)
Start-SoftwareInstaller -Action $Action -Arguments $Arguments; Start-SoftwareInstaller @Parameters `
} -Installer {
function Install-Software {
$xamlDownloadLink = "https://github.com/microsoft/microsoft-ui-xaml/releases/download/v2.8.6/Microsoft.UI.Xaml.2.8.x64.appx"; $xamlDownloadLink = "https://github.com/microsoft/microsoft-ui-xaml/releases/download/v2.8.6/Microsoft.UI.Xaml.2.8.x64.appx";
$downloadLink = "https://github.com/microsoft/winget-cli/releases/latest/download/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle"; $downloadLink = "https://github.com/microsoft/winget-cli/releases/latest/download/Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle";
$file = New-TemporaryFile; $file = New-TemporaryFile;
@ -25,6 +19,5 @@ function Install-Software {
Invoke-WebRequest "$downloadLink" -OutFile "$file"; Invoke-WebRequest "$downloadLink" -OutFile "$file";
Add-AppxPackage "$file"; Add-AppxPackage "$file";
Remove-Item $file; Remove-Item $file;
} };
} $PSBoundParameters;
Install-Winget @PSBoundParameters;