PortValhalla/scripts/lib/Software.ps1

160 lines
5.6 KiB
PowerShell
Raw Normal View History

2024-11-01 12:15:00 +00:00
. "$PSScriptRoot/Settings.ps1";
. "$PSScriptRoot/Operations.ps1";
2024-08-07 01:23:21 +00:00
. "$PSScriptRoot/System.ps1";
2024-11-01 12:12:16 +00:00
. "$PSScriptRoot/../Common/Types/InstallerAction.ps1";
2024-08-01 16:24:26 +00:00
2024-08-01 21:34:35 +00:00
$null = New-Module {
2024-11-01 12:12:16 +00:00
. "$PSScriptRoot/../Common/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-10-13 20:07:15 +00:00
[switch] $Force,
2024-10-12 15:14:53 +00:00
[scriptblock] $Base = $null,
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-10-12 15:14:53 +00:00
[string] $Inherit,
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;
}
};
2024-10-21 23:25:30 +00:00
$runBase = {
$defaults = @{ action = $Action };
& "$Inherit" @defaults @args;
};
if (-not $Name) {
$Name = Split-Path -Leaf (Split-Path -Parent ((Get-PSCallStack)[1].ScriptName));
}
2024-10-12 15:14:53 +00:00
if ($Inherit) {
foreach ($script in @("Backup", "Installer", "Configurator", "UserBackup", "UserConfigurator")) {
if (-not (Get-Variable $script).Value) {
2024-10-13 20:07:15 +00:00
Set-Variable $script {
param([switch]$Force)
$parameters = $MyInvocation.UnboundArguments;
if ($Force.ToBool()) {
$parameters = $parameters + @("-Force");
}
2024-10-21 23:25:30 +00:00
& $runBase @parameters
2024-10-13 20:07:15 +00:00
};
2024-10-12 15:14:53 +00:00
}
}
}
Start-Operation {
$installHandler = {
param(
[string] $Name,
2024-10-13 20:07:15 +00:00
[switch] $Force,
[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-10-13 20:07:15 +00:00
$install = $Force -or (Test-Program "$Name");
2024-08-05 21:08:36 +00:00
$argumentList = @{
2024-10-06 19:25:34 +00:00
name = $Name;
2024-10-13 20:07:15 +00:00
force = $Force;
installer = $installHandler;
arguments = $Arguments;
2024-10-06 19:25:34 +00:00
context = $Context;
2024-11-29 00:13:58 +00:00
base = { & $runBase @args };
};
2024-08-27 22:29:33 +00:00
switch ($Action) {
2024-10-06 19:25:34 +00:00
([InstallerAction]::Backup) {
2024-10-13 20:07:15 +00:00
if ($Backup -and $install) {
Write-Host "Backing up $DisplayName";
2024-08-27 22:29:33 +00:00
& $Backup @argumentList;
}
2024-10-21 23:24:03 +00:00
break;
2024-08-08 20:24:41 +00:00
}
2024-08-27 22:29:33 +00:00
([InstallerAction]::Install) {
2024-10-13 20:07:15 +00:00
if ($Installer -and $install) {
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-10-21 23:24:03 +00:00
break;
2024-08-22 23:41:32 +00:00
}
2024-08-27 22:29:33 +00:00
([InstallerAction]::Configure) {
2024-10-13 20:07:15 +00:00
if ($Configurator -and $install) {
Write-Host "Configuring $DisplayName";
2024-08-27 22:29:33 +00:00
& $Configurator @argumentList;
}
2024-10-21 23:24:03 +00:00
break;
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];
2024-10-13 20:07:15 +00:00
$install = $Force -or (Test-Program -User $user "$Name");
2024-08-27 22:37:04 +00:00
switch ($_) {
([InstallerAction]::BackupUser) {
2024-10-13 20:07:15 +00:00
if ($UserBackup -and $install) {
Write-Host "Backing up $DisplayName for user ``$user``";
2024-08-27 22:37:04 +00:00
& $UserBackup @argumentList;
}
}
([InstallerAction]::ConfigureUser) {
2024-10-13 20:07:15 +00:00
if ($UserConfigurator -and $install) {
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-10-21 23:24:03 +00:00
break;
2024-08-08 20:24:41 +00:00
}
2024-08-04 22:07:41 +00:00
}
};
2024-08-04 22:07:41 +00:00
2024-10-13 20:07:15 +00:00
& $installHandler -Name $Name -Force:$($Force.ToBool()) -Action $Action -Arguments $Arguments -Context $Context;
};
2024-08-01 16:24:26 +00:00
}
}