PortValhalla/scripts/Common/Scripts/Software.ps1

121 lines
4.3 KiB
PowerShell
Raw Normal View History

. "$PSScriptRoot/Operations.ps1";
2024-08-07 01:23:21 +00:00
. "$PSScriptRoot/System.ps1";
. "$PSScriptRoot/../Types/InstallerAction.ps1";
2024-08-01 16:24:26 +00:00
2024-08-01 21:34:35 +00:00
$null = New-Module {
. "$PSScriptRoot/../Types/InstallerAction.ps1";
2024-08-01 21:34:35 +00:00
$userArgument = "name";
2024-08-01 16:24:26 +00:00
function Start-SoftwareInstaller {
param(
2024-08-04 22:07:41 +00:00
[string] $Name,
2024-08-27 22:29:33 +00:00
[scriptblock] $Backup = $null,
2024-08-08 20:24:41 +00:00
[scriptblock] $Installer = $null,
[scriptblock] $Configurator = $null,
2024-08-27 22:37:04 +00:00
[scriptblock] $UserBackup = $null,
2024-08-08 20:24:41 +00:00
[scriptblock] $UserConfigurator = $null,
2024-09-30 03:00:20 +00:00
[Parameter(Position = 0)]
2024-08-08 00:56:28 +00:00
[Nullable[InstallerAction]] $Action,
2024-09-30 03:00:20 +00:00
[Parameter(Position = 1)]
[hashtable] $Arguments,
2024-09-30 03:00:20 +00:00
[Parameter()]
[hashtable] $Context
2024-08-01 16:24:26 +00:00
)
2024-08-08 00:56:28 +00:00
[InstallerAction] $Action = & {
2024-08-10 13:15:21 +00:00
if ($null -ne $Action) {
2024-08-08 00:56:28 +00:00
$Action;
2024-10-06 19:25:34 +00:00
}
else {
2024-08-08 00:56:28 +00:00
[InstallerAction]::Install;
}
};
if (-not $Name) {
$Name = Split-Path -Leaf (Split-Path -Parent ((Get-PSCallStack)[1].ScriptName));
}
Start-Operation {
$installHandler = {
param(
[string] $Name,
[InstallerAction] $Action,
[hashtable] $Arguments,
[hashtable] $Context
)
2024-08-04 22:07:41 +00:00
[string] $DisplayName = $null;
if ($null -ne $Name) {
$DisplayName = "``$Name``";
2024-10-06 19:25:34 +00:00
}
else {
$DisplayName = "unknown software";
}
$Arguments ??= @{ };
$Context ??= @{ };
2024-08-05 21:08:36 +00:00
$argumentList = @{
2024-10-06 19:25:34 +00:00
name = $Name;
installer = $installHandler;
arguments = $Arguments;
2024-10-06 19:25:34 +00:00
context = $Context;
};
2024-08-27 22:29:33 +00:00
switch ($Action) {
2024-10-06 19:25:34 +00:00
([InstallerAction]::Backup) {
2024-08-27 22:29:33 +00:00
if ($Backup) {
Write-Host "Backing up $DisplayName";
2024-08-27 22:29:33 +00:00
& $Backup @argumentList;
}
2024-08-08 20:24:41 +00:00
}
2024-08-27 22:29:33 +00:00
([InstallerAction]::Install) {
if ($Installer) {
Write-Host "Installing $DisplayName";
2024-08-27 22:29:33 +00:00
& $Installer @argumentList;
}
2024-08-22 23:41:32 +00:00
2024-08-27 22:29:33 +00:00
& $installHandler @argumentList -Action ([InstallerAction]::Configure);
2024-08-22 23:41:32 +00:00
2024-08-27 22:29:33 +00:00
if ($UserConfigurator -and (-not (Test-SetupUser))) {
& $installHandler @argumentList -Action ([InstallerAction]::ConfigureUser);
}
2024-08-22 23:41:32 +00:00
}
2024-08-27 22:29:33 +00:00
([InstallerAction]::Configure) {
if ($Configurator) {
Write-Host "Configuring $DisplayName";
2024-08-27 22:29:33 +00:00
& $Configurator @argumentList;
}
2024-08-08 20:24:41 +00:00
}
2024-08-27 22:37:04 +00:00
default {
2024-08-27 22:29:33 +00:00
if ((-not $Arguments.ContainsKey($userArgument)) -or (-not $Arguments[$userArgument])) {
2024-09-20 03:46:26 +00:00
$Arguments.Remove($userArgument);
2024-08-27 22:29:33 +00:00
$Arguments.Add($userArgument, ($IsWindows ? $env:UserName : $env:USER));
}
2024-08-27 22:37:04 +00:00
$user = $Arguments[$userArgument];
switch ($_) {
([InstallerAction]::BackupUser) {
if ($UserBackup) {
Write-Host "Backing up $DisplayName for user ``$user``";
2024-08-27 22:37:04 +00:00
& $UserBackup @argumentList;
}
}
([InstallerAction]::ConfigureUser) {
if ($UserConfigurator) {
Write-Host "Configuring $DisplayName for user ``$user``";
2024-08-27 22:37:04 +00:00
& $UserConfigurator @argumentList;
}
}
2024-08-27 22:29:33 +00:00
}
2024-08-08 20:24:41 +00:00
}
2024-08-04 22:07:41 +00:00
}
};
2024-08-04 22:07:41 +00:00
& $installHandler -Name $Name -Action $Action -Arguments $Arguments -Context $Context;
};
2024-08-01 16:24:26 +00:00
}
}