Refactor software script environment

This commit is contained in:
Manuel Thalmann 2024-08-05 00:07:41 +02:00
parent 337715c629
commit 60877942f8
2 changed files with 50 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(
[InstallerAction] $Action,
[string] $Name,
[scriptblock] $Installer = { },
[scriptblock] $Configurator = { },
[scriptblock] $UserConfigurator = { },
[InstallerAction] $Action = [InstallerAction]::Install,
[hashtable] $Arguments
)
$null = $softwareName;
if (-not $Name) {
$Name = Split-Path -Leaf (Split-Path -Parent ((Get-PSCallStack)[1].ScriptName));
}
if ($null -ne (Get-SoftwareName)) {
$softwareName = "``$(Get-SoftwareName)``";
if ($null -ne $Name) {
$Name = "``$Name``";
} else {
$softwareName = "unknown software";
$Name = "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;
foreach ($user in Get-Users) {
$Arguments.Add($userArgument, $user);
Start-SoftwareInstaller -Action ([InstallerAction]::ConfigureUser) @Arguments;
}
} elseif ($action -eq ([InstallerAction]::ConfigureUser)) {
if ((-not $Arguments.Contains($userArgument)) -or ($null -eq $Arguments[$userArgument])) {
$Arguments.Add($userArgument, ($env:UserName));
}
Write-Host "Configuring $softwareName for user ``$($Arguments[$userArgument])``";
Set-SoftwareUserConfiguration @Arguments;
function Invoke-SoftwareInstaller {
param(
[InstallerAction] $Action,
[hashtable] $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);
Invoke-SoftwareInstaller -Action ([InstallerAction]::ConfigureUser) -Arguments $Arguments;
}
} elseif ($Action -eq ([InstallerAction]::ConfigureUser)) {
if ((-not $Arguments.ContainsKey($userArgument)) -or ($null -eq $Arguments[$userArgument])) {
$Arguments.Add($userArgument, ($env:UserName));
}
Write-Host "Configuring $Name for user ``$($Arguments[$userArgument])``";
& $UserConfigurator @Arguments;
}
}
Invoke-SoftwareInstaller -Action $Action -Arguments $Arguments;
}
}

View file

@ -6,25 +6,16 @@ param(
. "$PSScriptRoot/../../Scripts/Software.ps1";
. "$PSScriptRoot/../../Types/InstallerAction.ps1";
function Install-Winget {
param(
[InstallerAction] $Action = [InstallerAction]::Install,
[hashtable] $Arguments
)
Start-SoftwareInstaller -Action $Action -Arguments $Arguments;
}
Start-SoftwareInstaller @PSBoundParameters `
-Installer {
function Install-Software {
$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;
$file = Rename-Item $file "$file.msixbundle" -PassThru;
Invoke-WebRequest "$xamlDownloadLink" -OutFile "$file";
Add-AppxPackage "$file";
Invoke-WebRequest "$downloadLink" -OutFile "$file";
Add-AppxPackage "$file";
Remove-Item $file;
}
Install-Winget @PSBoundParameters;
$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;
$file = Rename-Item $file "$file.msixbundle" -PassThru;
Invoke-WebRequest "$xamlDownloadLink" -OutFile "$file";
Add-AppxPackage "$file";
Invoke-WebRequest "$downloadLink" -OutFile "$file";
Add-AppxPackage "$file";
Remove-Item $file;
};