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/../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 {
. "$PSScriptRoot/../Types/InstallerAction.ps1";
$userArgument = "name";
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(
[InstallerAction] $Action,
[hashtable] $Arguments
)
$null = $softwareName;
if ($null -ne (Get-SoftwareName)) {
$softwareName = "``$(Get-SoftwareName)``";
} else {
$softwareName = "unknown software";
}
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;
if ($action -eq ([InstallerAction]::Install)) {
Write-Host "Installing $Name";
& $Installer @Arguments;
} elseif ($Action -eq ([InstallerAction]::Configure)) {
Write-Host "Configuring $Name";
& $Configurator @Arguments;
foreach ($user in Get-Users) {
$Arguments.Add($userArgument, $user);
Start-SoftwareInstaller -Action ([InstallerAction]::ConfigureUser) @Arguments;
Invoke-SoftwareInstaller -Action ([InstallerAction]::ConfigureUser) -Arguments $Arguments;
}
} elseif ($action -eq ([InstallerAction]::ConfigureUser)) {
if ((-not $Arguments.Contains($userArgument)) -or ($null -eq $Arguments[$userArgument])) {
} elseif ($Action -eq ([InstallerAction]::ConfigureUser)) {
if ((-not $Arguments.ContainsKey($userArgument)) -or ($null -eq $Arguments[$userArgument])) {
$Arguments.Add($userArgument, ($env:UserName));
}
Write-Host "Configuring $softwareName for user ``$($Arguments[$userArgument])``";
Set-SoftwareUserConfiguration @Arguments;
Write-Host "Configuring $Name for user ``$($Arguments[$userArgument])``";
& $UserConfigurator @Arguments;
}
}
Invoke-SoftwareInstaller -Action $Action -Arguments $Arguments;
}
}

View file

@ -3,19 +3,13 @@ param(
[hashtable] $Arguments
)
. "$PSScriptRoot/../../Scripts/Software.ps1";
. "$PSScriptRoot/../../Types/InstallerAction.ps1";
. "$PSScriptRoot/../../../Common/Scripts/Software.ps1";
function Install-Winget {
param(
[InstallerAction] $Action = [InstallerAction]::Install,
[hashtable] $Arguments
)
& {
param($Parameters)
Start-SoftwareInstaller -Action $Action -Arguments $Arguments;
}
function Install-Software {
Start-SoftwareInstaller @Parameters `
-Installer {
$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";
$file = New-TemporaryFile;
@ -25,6 +19,5 @@ function Install-Software {
Invoke-WebRequest "$downloadLink" -OutFile "$file";
Add-AppxPackage "$file";
Remove-Item $file;
}
Install-Winget @PSBoundParameters;
};
} $PSBoundParameters;