2024-07-31 13:42:02 +00:00
|
|
|
using namespace Microsoft.Win32;
|
2024-09-08 15:47:49 +00:00
|
|
|
using namespace System.Management.Automation.Host;
|
2024-07-31 14:25:32 +00:00
|
|
|
|
2024-07-31 13:42:02 +00:00
|
|
|
$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";
|
2024-07-31 13:42:02 +00:00
|
|
|
|
2024-09-08 15:47:49 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Prompts the user to select a profile to act on.
|
|
|
|
#>
|
|
|
|
function Show-ProfileNamePrompt {
|
|
|
|
|
|
|
|
$profiles = & {
|
2024-09-19 19:59:01 +00:00
|
|
|
if (-not $IsWindows -or (Test-Command "wsl")) {
|
2024-09-08 15:47:49 +00:00
|
|
|
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 $_ };
|
2024-09-08 15:47:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$choice = $Host.UI.PromptForChoice(
|
|
|
|
"Select Profile",
|
|
|
|
(& {
|
2024-09-19 19:59:01 +00:00
|
|
|
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-09-08 15:47:49 +00:00
|
|
|
}
|
2024-10-06 19:25:34 +00:00
|
|
|
}
|
|
|
|
else {
|
2024-09-19 19:59:01 +00:00
|
|
|
"Please select a profile:";
|
2024-09-08 15:47:49 +00:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
(& {
|
|
|
|
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 {
|
2024-09-08 15:47:49 +00:00
|
|
|
$env:CONFIG_NAME = $profiles[$choice];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-01 14:12:58 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
2024-10-31 23:38:33 +00:00
|
|
|
Runs a script based on the `settings.fish` script.
|
2024-08-01 14:12:58 +00:00
|
|
|
|
2024-08-01 15:56:42 +00:00
|
|
|
.PARAMETER Script
|
|
|
|
The script to run.
|
2024-08-01 14:12:58 +00:00
|
|
|
#>
|
2024-08-01 15:56:42 +00:00
|
|
|
function Invoke-ConfigScript {
|
2024-08-01 14:12:58 +00:00
|
|
|
param(
|
2024-08-01 15:56:42 +00:00
|
|
|
[string] $Script
|
2024-08-01 14:12:58 +00:00
|
|
|
)
|
|
|
|
|
2024-11-01 12:12:16 +00:00
|
|
|
$scriptPath = "$PSScriptRoot/settings.fish";
|
2024-08-01 14:12:58 +00:00
|
|
|
|
2024-09-08 22:04:06 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-09-28 14:13:07 +00:00
|
|
|
$output = Invoke-Fish -c ". $(ConvertTo-LinuxPath $scriptPath); $Script";
|
2024-08-11 16:27:06 +00:00
|
|
|
|
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;
|
2024-08-11 16:27:06 +00:00
|
|
|
}
|
2024-08-01 15:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
<#
|
|
|
|
.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.
|
2024-08-01 15:56:42 +00:00
|
|
|
#>
|
|
|
|
function Get-Config {
|
2024-08-08 12:45:34 +00:00
|
|
|
param(
|
|
|
|
[string] $Name,
|
|
|
|
[Parameter(ValueFromRemainingArguments)]
|
|
|
|
[string[]] $ArgumentList
|
|
|
|
)
|
|
|
|
|
|
|
|
Invoke-ConfigScript "getConfig $Name --json $ArgumentList";
|
|
|
|
}
|
|
|
|
|
2024-08-22 23:57:53 +00:00
|
|
|
<#
|
|
|
|
.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-08-22 23:57:53 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2024-10-15 21:29:00 +00:00
|
|
|
try {
|
|
|
|
(Get-ProgramConfig @PSBoundParameters).enable;
|
|
|
|
} catch {
|
|
|
|
$false;
|
|
|
|
}
|
2024-10-13 20:07:15 +00:00
|
|
|
}
|
|
|
|
|
2024-08-24 01:25:28 +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-24 01:25:28 +00:00
|
|
|
}
|
|
|
|
|
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(
|
2024-08-22 23:57:53 +00:00
|
|
|
[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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-08 12:45:34 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Gets the attributes of a configuration object.
|
|
|
|
|
|
|
|
.PARAMETER Name
|
|
|
|
The name of the configuration to get the attributes of.
|
|
|
|
#>
|
|
|
|
function Get-Attributes {
|
2024-08-01 15:56:42 +00:00
|
|
|
param(
|
|
|
|
[string] $Name
|
|
|
|
)
|
|
|
|
|
2024-08-08 12:45:34 +00:00
|
|
|
Invoke-ConfigScript "getAttributes $Name";
|
2024-08-01 15:56:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
<#
|
|
|
|
.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-01 14:12:58 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2024-07-31 13:42:02 +00:00
|
|
|
}
|