PortValhalla/scripts/lib/Settings.ps1

283 lines
7.5 KiB
PowerShell
Raw Normal View History

using namespace Microsoft.Win32;
using namespace System.Management.Automation.Host;
$null = New-Module {
2024-09-23 01:33:17 +00:00
. "$PSScriptRoot/SoftwareManagement.ps1";
2024-11-01 12:12:16 +00:00
. "$PSScriptRoot/../Windows/lib/Registry.ps1";
. "$PSScriptRoot/../Windows/lib/WSL.ps1";
. "$PSScriptRoot/../Windows/Types/WindowsInstallerAction.ps1";
<#
.SYNOPSIS
Prompts the user to select a profile to act on.
#>
function Show-ProfileNamePrompt {
$profiles = & {
if (-not $IsWindows -or (Test-Command "wsl")) {
return Invoke-ConfigScript "getProfiles";
2024-10-06 19:25:34 +00:00
}
else {
2024-11-01 12:12:16 +00:00
return Get-ChildItem "$PSScriptRoot/../../.config" | ForEach-Object { Split-Path -LeafBase $_ };
}
};
$choice = $Host.UI.PromptForChoice(
"Select Profile",
(& {
if ($IsWindows) {
switch (Get-Stage) {
([WindowsInstallerAction]::Backup) {
"Which profile do you wish to back up?";
}
([WindowsInstallerAction]::Install) {
"Which profile do you wish to install?";
}
$null {
"Which profile do you wish to set up?";
}
}
2024-10-06 19:25:34 +00:00
}
else {
"Please select a profile:";
}
}),
(& {
for ($i = 0; $i -lt $profiles.Count; $i++) {
[ChoiceDescription]"&$i - $($profiles[$i])";
}
[ChoiceDescription]"&Abort";
}),
$profiles.Count);
if ($choice -eq $profiles.Count) {
exit;
2024-10-06 19:25:34 +00:00
}
else {
$env:CONFIG_NAME = $profiles[$choice];
}
}
<#
.SYNOPSIS
2024-10-31 23:38:33 +00:00
Runs a script based on the `settings.fish` script.
.PARAMETER Script
The script to run.
#>
function Invoke-ConfigScript {
param(
[string] $Script
)
2024-11-01 12:12:16 +00:00
$scriptPath = "$PSScriptRoot/settings.fish";
if ($env:CONFIG_NAME -or ($Script -eq "getProfiles")) {
2024-08-22 23:52:04 +00:00
$output = & {
if (-not $IsWindows) {
$escapedPath = (fish -c 'string escape $argv' "$scriptPath");
fish -c ". $escapedPath; $Script";
2024-10-06 19:25:34 +00:00
}
else {
2024-09-28 14:13:56 +00:00
if (-not $env:VALHALLA_FLAKE_ROOT) {
$cleanup = { };
2024-11-01 12:12:16 +00:00
$projectRoot = "$PSScriptRoot/../..";
2024-09-28 14:13:56 +00:00
$archisoDir = "$projectRoot/archiso";
if (Test-Path -PathType Container "$archisoDir") {
$git = {
git -C "$projectRoot" -c safe.directory="$("$(Resolve-Path $projectRoot)".Replace("\", "/"))" @args;
};
& $git rm -r --cached "$archisoDir" *> $null;
$cleanup = { & $git restore --staged "$archisoDir" };
}
2024-09-08 15:09:01 +00:00
2024-11-01 12:12:16 +00:00
$env:VALHALLA_FLAKE_ROOT = (Invoke-Nix flake metadata (ConvertTo-LinuxPath "$PSScriptRoot/../..") --json | ConvertFrom-Json).path;
2024-09-28 14:13:56 +00:00
& $cleanup *> $null;
2024-09-08 15:09:01 +00:00
}
$output = Invoke-Fish -c ". $(ConvertTo-LinuxPath $scriptPath); $Script";
2024-08-22 23:52:04 +00:00
if (-not $?) {
Write-Error "The configuration could not be retrieved!";
2024-10-06 19:25:34 +00:00
}
else {
2024-08-22 23:52:04 +00:00
$output;
}
2024-08-17 11:51:10 +00:00
}
2024-10-06 19:25:34 +00:00
};
2024-08-22 23:52:04 +00:00
if (-not ($output -and ($output | Test-Json))) {
Write-Error "The value ``$output`` is not valid JSON.";
2024-10-06 19:25:34 +00:00
}
else {
2024-08-22 23:52:04 +00:00
$output | ConvertFrom-Json;
}
2024-10-06 19:25:34 +00:00
}
else {
2024-08-22 23:52:04 +00:00
$null;
}
}
<#
.SYNOPSIS
Gets a configuration option.
.PARAMETER Name
2024-09-22 15:50:05 +00:00
The name of the configuration value to get.
.PARAMETER ArgumentList
The arguments to send to the configuration script.
#>
function Get-Config {
param(
[string] $Name,
[Parameter(ValueFromRemainingArguments)]
[string[]] $ArgumentList
)
Invoke-ConfigScript "getConfig $Name --json $ArgumentList";
}
<#
.SYNOPSIS
Gets the name of the config root.
#>
2024-09-22 15:50:05 +00:00
function Get-OSConfigRoot {
2024-08-23 17:24:49 +00:00
return "valhalla.$($IsWindows ? "windows" : "linux")";
}
2024-09-22 15:50:05 +00:00
<#
.SYNOPSIS
Gets the configuration value for the current operating system.
.PARAMETER Name
The name of the configuration value to get.
.PARAMETER ArgumentList
The arguments to send to the configuration script.
#>
function Get-OSConfig {
param(
[string] $Name,
[Parameter(ValueFromRemainingArguments)]
[string[]] $ArgumentList
)
return Get-Config -Name "$(Get-OSConfigRoot).$Name" @PSBoundParameters;
}
2024-10-13 20:07:15 +00:00
<#
.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
)
try {
(Get-ProgramConfig @PSBoundParameters).enable;
} catch {
$false;
}
2024-10-13 20:07:15 +00:00
}
<#
.SYNOPSIS
Gets the name of the user root.
#>
2024-09-22 15:50:05 +00:00
function Get-UserConfigRoot {
return "$(Get-OSConfigRoot).$($IsWindows ? "winUsers" : "users")";
}
2024-08-08 15:49:43 +00:00
<#
.SYNOPSIS
Gets a user configuration.
.PARAMETER UserName
The name of the user to get the configuration for.
.PARAMETER Name
The name of the configuration to get.
#>
function Get-UserConfig {
param(
[string] $UserName = ($IsWindows ? $env:UserName : $env:USER),
2024-08-08 15:49:43 +00:00
[Parameter(Mandatory, Position = 0)]
[string] $Name
)
if ((Get-Users) -contains $UserName) {
2024-09-22 15:50:05 +00:00
Get-Config "$(Get-UserConfigRoot).$UserName.$Name";
2024-10-06 19:25:34 +00:00
}
else {
2024-08-08 15:49:43 +00:00
return $null;
}
}
<#
.SYNOPSIS
Gets the attributes of a configuration object.
.PARAMETER Name
The name of the configuration to get the attributes of.
#>
function Get-Attributes {
param(
[string] $Name
)
Invoke-ConfigScript "getAttributes $Name";
}
<#
.SYNOPSIS
Gets the names of the users to create.
#>
function Get-Users {
2024-08-09 01:02:17 +00:00
[OutputType([string[]])]
param()
2024-09-22 15:50:05 +00:00
Get-Attributes "$(Get-UserConfigRoot)";
}
2024-08-09 02:19:49 +00:00
<#
.SYNOPSIS
Gets the name of the setup user.
#>
function Get-SetupUser {
[OutputType([string])]
param()
2024-09-22 15:50:05 +00:00
Get-OSConfig "setupUser.name";
2024-08-09 02:19:49 +00:00
}
}