2024-08-05 21:09:58 +00:00
|
|
|
. "$PSScriptRoot/Operations.ps1";
|
2024-08-07 01:23:21 +00:00
|
|
|
. "$PSScriptRoot/System.ps1";
|
2024-08-01 20:52:51 +00:00
|
|
|
. "$PSScriptRoot/../Types/InstallerAction.ps1";
|
2024-08-01 16:24:26 +00:00
|
|
|
|
2024-08-01 21:34:35 +00:00
|
|
|
$null = New-Module {
|
2024-08-07 11:22:12 +00:00
|
|
|
. "$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)]
|
2024-09-29 15:12:44 +00:00
|
|
|
[hashtable] $Arguments,
|
2024-09-30 03:00:20 +00:00
|
|
|
[Parameter()]
|
2024-09-29 15:12:44 +00:00
|
|
|
[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-08-06 10:21:48 +00:00
|
|
|
if (-not $Name) {
|
|
|
|
$Name = Split-Path -Leaf (Split-Path -Parent ((Get-PSCallStack)[1].ScriptName));
|
|
|
|
}
|
2024-08-01 19:06:03 +00:00
|
|
|
|
2024-08-06 10:21:48 +00:00
|
|
|
Start-Operation {
|
2024-08-05 21:09:58 +00:00
|
|
|
$installHandler = {
|
|
|
|
param(
|
2024-09-23 00:10:42 +00:00
|
|
|
[string] $Name,
|
2024-08-05 21:09:58 +00:00
|
|
|
[InstallerAction] $Action,
|
2024-09-29 15:12:44 +00:00
|
|
|
[hashtable] $Arguments,
|
|
|
|
[hashtable] $Context
|
2024-08-05 21:09:58 +00:00
|
|
|
)
|
2024-08-04 22:07:41 +00:00
|
|
|
|
2024-09-23 00:10:42 +00:00
|
|
|
[string] $DisplayName = $null;
|
|
|
|
|
|
|
|
if ($null -ne $Name) {
|
|
|
|
$DisplayName = "``$Name``";
|
2024-10-06 19:25:34 +00:00
|
|
|
}
|
|
|
|
else {
|
2024-09-23 00:10:42 +00:00
|
|
|
$DisplayName = "unknown software";
|
|
|
|
}
|
|
|
|
|
2024-08-05 21:09:58 +00:00
|
|
|
$Arguments ??= @{ };
|
2024-09-29 15:12:44 +00:00
|
|
|
$Context ??= @{ };
|
2024-08-05 21:08:36 +00:00
|
|
|
|
2024-08-05 21:09:58 +00:00
|
|
|
$argumentList = @{
|
2024-10-06 19:25:34 +00:00
|
|
|
name = $Name;
|
2024-08-05 21:09:58 +00:00
|
|
|
installer = $installHandler;
|
|
|
|
arguments = $Arguments;
|
2024-10-06 19:25:34 +00:00
|
|
|
context = $Context;
|
2024-08-05 21:09:58 +00:00
|
|
|
};
|
2024-08-05 20:29:20 +00:00
|
|
|
|
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) {
|
2024-09-23 00:10:42 +00:00
|
|
|
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) {
|
2024-09-23 00:10:42 +00:00
|
|
|
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) {
|
2024-09-23 00:10:42 +00:00
|
|
|
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) {
|
2024-09-23 00:10:42 +00:00
|
|
|
Write-Host "Backing up $DisplayName for user ``$user``…";
|
2024-08-27 22:37:04 +00:00
|
|
|
& $UserBackup @argumentList;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
([InstallerAction]::ConfigureUser) {
|
|
|
|
if ($UserConfigurator) {
|
2024-09-23 00:10:42 +00:00
|
|
|
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-05 21:09:58 +00:00
|
|
|
};
|
2024-08-04 22:07:41 +00:00
|
|
|
|
2024-09-29 15:12:44 +00:00
|
|
|
& $installHandler -Name $Name -Action $Action -Arguments $Arguments -Context $Context;
|
2024-08-05 20:29:20 +00:00
|
|
|
};
|
2024-08-01 16:24:26 +00:00
|
|
|
}
|
|
|
|
}
|