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; 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 .SYNOPSIS
Gets the name of the user root. Gets the name of the user root.

View file

@ -1,3 +1,4 @@
. "$PSScriptRoot/Config.ps1";
. "$PSScriptRoot/Operations.ps1"; . "$PSScriptRoot/Operations.ps1";
. "$PSScriptRoot/System.ps1"; . "$PSScriptRoot/System.ps1";
. "$PSScriptRoot/../Types/InstallerAction.ps1"; . "$PSScriptRoot/../Types/InstallerAction.ps1";
@ -9,6 +10,7 @@ $null = New-Module {
function Start-SoftwareInstaller { function Start-SoftwareInstaller {
param( param(
[string] $Name, [string] $Name,
[switch] $Force,
[scriptblock] $Base = $null, [scriptblock] $Base = $null,
[scriptblock] $Backup = $null, [scriptblock] $Backup = $null,
[scriptblock] $Installer = $null, [scriptblock] $Installer = $null,
@ -40,7 +42,16 @@ $null = New-Module {
if ($Inherit) { if ($Inherit) {
foreach ($script in @("Backup", "Installer", "Configurator", "UserBackup", "UserConfigurator")) { foreach ($script in @("Backup", "Installer", "Configurator", "UserBackup", "UserConfigurator")) {
if (-not (Get-Variable $script).Value) { 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 = { $installHandler = {
param( param(
[string] $Name, [string] $Name,
[switch] $Force,
[InstallerAction] $Action, [InstallerAction] $Action,
[hashtable] $Arguments, [hashtable] $Arguments,
[hashtable] $Context [hashtable] $Context
@ -65,9 +77,11 @@ $null = New-Module {
$Arguments ??= @{ }; $Arguments ??= @{ };
$Context ??= @{ }; $Context ??= @{ };
$install = $Force -or (Test-Program "$Name");
$argumentList = @{ $argumentList = @{
name = $Name; name = $Name;
force = $Force;
installer = $installHandler; installer = $installHandler;
arguments = $Arguments; arguments = $Arguments;
context = $Context; context = $Context;
@ -76,13 +90,13 @@ $null = New-Module {
switch ($Action) { switch ($Action) {
([InstallerAction]::Backup) { ([InstallerAction]::Backup) {
if ($Backup) { if ($Backup -and $install) {
Write-Host "Backing up $DisplayName"; Write-Host "Backing up $DisplayName";
& $Backup @argumentList; & $Backup @argumentList;
} }
} }
([InstallerAction]::Install) { ([InstallerAction]::Install) {
if ($Installer) { if ($Installer -and $install) {
Write-Host "Installing $DisplayName"; Write-Host "Installing $DisplayName";
& $Installer @argumentList; & $Installer @argumentList;
} }
@ -94,7 +108,7 @@ $null = New-Module {
} }
} }
([InstallerAction]::Configure) { ([InstallerAction]::Configure) {
if ($Configurator) { if ($Configurator -and $install) {
Write-Host "Configuring $DisplayName"; Write-Host "Configuring $DisplayName";
& $Configurator @argumentList; & $Configurator @argumentList;
} }
@ -106,16 +120,17 @@ $null = New-Module {
} }
$user = $Arguments[$userArgument]; $user = $Arguments[$userArgument];
$install = $Force -or (Test-Program -User $user "$Name");
switch ($_) { switch ($_) {
([InstallerAction]::BackupUser) { ([InstallerAction]::BackupUser) {
if ($UserBackup) { if ($UserBackup -and $install) {
Write-Host "Backing up $DisplayName for user ``$user``"; Write-Host "Backing up $DisplayName for user ``$user``";
& $UserBackup @argumentList; & $UserBackup @argumentList;
} }
} }
([InstallerAction]::ConfigureUser) { ([InstallerAction]::ConfigureUser) {
if ($UserConfigurator) { if ($UserConfigurator -and $install) {
Write-Host "Configuring $DisplayName for user ``$user``"; Write-Host "Configuring $DisplayName for user ``$user``";
& $UserConfigurator @argumentList; & $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..] getConfig "$(getOSConfigRoot).$property" $argv[2..]
end 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 function getAttributes -S -a property
getConfig "$property" --apply "builtins.attrNames" --json getConfig "$property" --apply "builtins.attrNames" --json
end end
@ -68,6 +78,10 @@ function isEnabled -S -a property
getConfig "$property" --json | jq --exit-status >/dev/null getConfig "$property" --json | jq --exit-status >/dev/null
end 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 function isOSEnabled -S -a property
isEnabled "$(getOSConfigRoot).$property" isEnabled "$(getOSConfigRoot).$property"
end end

View file

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

View file

@ -16,12 +16,12 @@ begin
function inherit -a script -d "Inherits the installer from the specified script." function inherit -a script -d "Inherits the installer from the specified script."
set -l actions \ set -l actions \
installSW \ installSW \
install \ install \
configureSW \ configureSW \
configure \ configure \
userConfig \ userConfig \
userConfig userConfig
for i in (seq 1 2 (count $actions)) for i in (seq 1 2 (count $actions))
set -l functionName "$actions[$i]Base" set -l functionName "$actions[$i]Base"
@ -42,22 +42,39 @@ begin
runInstallerAction $name $argv runInstallerAction $name $argv
end 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" 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 [ -z "$action" ] || [ "$action" = install ]
if functions -q installSW if functions -q installSW && $install
echo "Installing `$name`..." echo "Installing `$name`..."
installSW $argv[3..] installSW $argv[3..]
end end
runInstallerAction $name configure runInstallerAction $args $name configure
if not isConfigured || [ "$USER" != (getConfig "valhalla.setupUser.name") ] if not isConfigured || [ "$USER" != (getConfig "valhalla.setupUser.name") ]
runInstallerAction $name userConfig runInstallerAction $args $name userConfig
end end
else if [ "$action" = configure ] else if [ "$action" = configure ]
if functions -q configureSW if functions -q configureSW && $install
echo "Configuring `$name`..." echo "Configuring `$name`..."
configureSW $argv[3..] configureSW $argv[3..]
end end
@ -68,7 +85,13 @@ begin
set user "$USER" set user "$USER"
end 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`..." echo "Configuring `$name` for `$user`..."
userConfig "$user" $argv[4..] userConfig "$user" $argv[4..]
end end