Install apps only if enabled

This commit is contained in:
Manuel Thalmann 2024-10-13 22:07:15 +02:00
parent 6b06a5d2ce
commit 9f94b792a3
5 changed files with 119 additions and 20 deletions

View file

@ -169,6 +169,45 @@ $null = New-Module {
return Get-Config -Name "$(Get-OSConfigRoot).$Name" @PSBoundParameters;
}
<#
.SYNOPSIS
Gets the configuration of the specified program.
.PARAMETER Name
The name of the program to get the configuration for.
#>
function Get-ProgramConfig {
param(
$User,
[Parameter(Position = 0)]
$Name
)
$programs = & {
if ($User) {
return Get-UserConfig -UserName $User @args;
} else {
return Get-OSConfig @args;
}
} "programs";
return $programs.$Name;
}
<#
.SYNOPSIS
Tests whether the program
#>
function Test-Program {
param(
$User,
[Parameter(Position = 0)]
$Name
)
return (Get-ProgramConfig @PSBoundParameters).enable;
}
<#
.SYNOPSIS
Gets the name of the user root.

View file

@ -1,3 +1,4 @@
. "$PSScriptRoot/Config.ps1";
. "$PSScriptRoot/Operations.ps1";
. "$PSScriptRoot/System.ps1";
. "$PSScriptRoot/../Types/InstallerAction.ps1";
@ -9,6 +10,7 @@ $null = New-Module {
function Start-SoftwareInstaller {
param(
[string] $Name,
[switch] $Force,
[scriptblock] $Base = $null,
[scriptblock] $Backup = $null,
[scriptblock] $Installer = $null,
@ -40,7 +42,16 @@ $null = New-Module {
if ($Inherit) {
foreach ($script in @("Backup", "Installer", "Configurator", "UserBackup", "UserConfigurator")) {
if (-not (Get-Variable $script).Value) {
Set-Variable $script { & $Inherit -Action $Action @args };
Set-Variable $script {
param([switch]$Force)
$parameters = $MyInvocation.UnboundArguments;
if ($Force.ToBool()) {
$parameters = $parameters + @("-Force");
}
& $Inherit -Action $Action @parameters
};
}
}
}
@ -49,6 +60,7 @@ $null = New-Module {
$installHandler = {
param(
[string] $Name,
[switch] $Force,
[InstallerAction] $Action,
[hashtable] $Arguments,
[hashtable] $Context
@ -65,9 +77,11 @@ $null = New-Module {
$Arguments ??= @{ };
$Context ??= @{ };
$install = $Force -or (Test-Program "$Name");
$argumentList = @{
name = $Name;
force = $Force;
installer = $installHandler;
arguments = $Arguments;
context = $Context;
@ -76,13 +90,13 @@ $null = New-Module {
switch ($Action) {
([InstallerAction]::Backup) {
if ($Backup) {
if ($Backup -and $install) {
Write-Host "Backing up $DisplayName";
& $Backup @argumentList;
}
}
([InstallerAction]::Install) {
if ($Installer) {
if ($Installer -and $install) {
Write-Host "Installing $DisplayName";
& $Installer @argumentList;
}
@ -94,7 +108,7 @@ $null = New-Module {
}
}
([InstallerAction]::Configure) {
if ($Configurator) {
if ($Configurator -and $install) {
Write-Host "Configuring $DisplayName";
& $Configurator @argumentList;
}
@ -106,16 +120,17 @@ $null = New-Module {
}
$user = $Arguments[$userArgument];
$install = $Force -or (Test-Program -User $user "$Name");
switch ($_) {
([InstallerAction]::BackupUser) {
if ($UserBackup) {
if ($UserBackup -and $install) {
Write-Host "Backing up $DisplayName for user ``$user``";
& $UserBackup @argumentList;
}
}
([InstallerAction]::ConfigureUser) {
if ($UserConfigurator) {
if ($UserConfigurator -and $install) {
Write-Host "Configuring $DisplayName for user ``$user``";
& $UserConfigurator @argumentList;
}
@ -125,7 +140,7 @@ $null = New-Module {
}
};
& $installHandler -Name $Name -Action $Action -Arguments $Arguments -Context $Context;
& $installHandler -Name $Name -Force:$($Force.ToBool()) -Action $Action -Arguments $Arguments -Context $Context;
};
}
}

View file

@ -40,6 +40,16 @@ function getOSConfig -S -a property
getConfig "$(getOSConfigRoot).$property" $argv[2..]
end
function getProgramConfig -S -a name user
set -l option "programs.$name"
if [ -z "$user" ]
getOSConfig "$option" $argv[3..] --fallback "{}"
else
getUserConfig "$user" "$option" $argv[3..] --fallback "{}"
end
end
function getAttributes -S -a property
getConfig "$property" --apply "builtins.attrNames" --json
end
@ -68,6 +78,10 @@ function isEnabled -S -a property
getConfig "$property" --json | jq --exit-status >/dev/null
end
function isProgramEnabled -S -a name user
getProgramConfig "$name" "$user" --json 2>/dev/null | jq --exit-status ".enable" >/dev/null
end
function isOSEnabled -S -a property
isEnabled "$(getOSConfigRoot).$property"
end

View file

@ -2,7 +2,7 @@
function evalFlake --argument-names config property
set -l argv $argv[3..]
set -l flakePath "$(realpath (status dirname))/../../.."
argparse --ignore-unknown "apply=" json -- $argv
argparse --ignore-unknown "apply=" "fallback=" json -- $argv
if [ -z "$_flag_json" ]
set -a argv --raw
@ -28,4 +28,12 @@ function evalFlake --argument-names config property
--apply "$_flag_apply" \
"$flakePath#valhalla$config" \
$argv
or begin
if [ -n "$_flag_fallback" ]
echo "$_flag_fallback"
else
false
end
end
end

View file

@ -42,22 +42,39 @@ begin
runInstallerAction $name $argv
end
function runInstallerAction -V dir -a name action
function runInstallerAction -V dir
argparse "force" -- $argv
set -l install
set -l args $_flag_force
set -l name $argv[1]
set -l action $argv[2]
source "$dir/config.fish"
if [ -n "$_flag_force" ]
set force true
else
set force false
end
if isProgramEnabled $name || $force
set install true
else
set install false
end
if [ -z "$action" ] || [ "$action" = install ]
if functions -q installSW
if functions -q installSW && $install
echo "Installing `$name`..."
installSW $argv[3..]
end
runInstallerAction $name configure
runInstallerAction $args $name configure
if not isConfigured || [ "$USER" != (getConfig "valhalla.setupUser.name") ]
runInstallerAction $name userConfig
runInstallerAction $args $name userConfig
end
else if [ "$action" = configure ]
if functions -q configureSW
if functions -q configureSW && $install
echo "Configuring `$name`..."
configureSW $argv[3..]
end
@ -68,7 +85,13 @@ begin
set user "$USER"
end
if functions -q userConfig
if isProgramEnabled "$name" "$user" || $force
set install true
else
set install false
end
if functions -q userConfig && $install
echo "Configuring `$name` for `$user`..."
userConfig "$user" $argv[4..]
end