PortValhalla/scripts/Common/Software/Oh My Posh/Manage.ps1

86 lines
2.7 KiB
PowerShell
Raw Normal View History

param (
$Action,
[hashtable] $Arguments
)
2024-08-23 21:53:48 +00:00
. "$PSScriptRoot/../aliae/Manage.ps1";
. "$PSScriptRoot/../PowerShell/Profile.ps1";
. "$PSScriptRoot/../../Scripts/Software.ps1";
. "$PSScriptRoot/../../Types/InstallerAction.ps1";
Start-SoftwareInstaller @PSBoundParameters `
-Configurator {
param([string] $Name)
Add-PowerShellProfileStatement `
-System `
-Category "$Name" `
-Script (
@(
"# Oh My Posh!",
(Get-ScriptInitializer "oh-my-posh init pwsh"),
(Get-ScriptInitializer "oh-my-posh completion powershell")
) -join [System.Environment]::NewLine)
2024-08-23 21:53:48 +00:00
} `
-UserConfigurator {
param(
[hashtable] $Arguments
)
$user = $Arguments.Name;
$theme = Get-UserConfig -UserName "$user" "oh-my-posh.theme";
2024-08-23 21:53:48 +00:00
2024-09-22 22:10:05 +00:00
function New-Theme {
param($info)
$root = "$(& {
if ($IsWindows) {
sudo -u "$user" pwsh -c 'Write-Host $env:AppData';
} else {
sudo -u "$user" bash -c 'realpath ~/.config'
}
})/oh-my-posh";
2024-09-22 22:10:05 +00:00
$path = Join-Path $root "$($info.name).omp.json";
$null = sudo -u "$user" pwsh -c New-Item -Force -ItemType Directory $root;
2024-09-22 22:10:05 +00:00
sudo -u "$user" pwsh -CommandWithArgs 'Set-Content $args[0] -Value $args[1]' $path ([string](
2024-09-22 22:10:05 +00:00
& {
if ($IsWindows) {
2024-09-28 01:41:00 +00:00
wsl cat $info.source | Out-String;
2024-09-22 22:10:05 +00:00
} else {
Get-Content -Raw $info.source;
2024-09-22 22:10:05 +00:00
}
}));
2024-09-22 22:10:05 +00:00
return $path;
}
foreach ($additionalTheme in (Get-UserConfig -UserName "$user" "oh-my-posh.additionalThemes")) {
2024-09-22 22:10:05 +00:00
Write-Host "creating theme $additionalTheme"
$null = New-Theme $additionalTheme;
}
2024-08-23 21:53:48 +00:00
if ($theme) {
$varName = "POSH_THEME";
if ($theme -isnot [string]) {
2024-09-22 22:10:05 +00:00
$theme = [string](New-Theme $theme);
2024-08-23 21:53:48 +00:00
}
if ($IsWindows) {
2024-09-22 22:10:05 +00:00
[System.Environment]::SetEnvironmentVariable($varName, "%AppData%/$([System.IO.Path]::GetRelativePath($env:AppData, $theme))", "User");
2024-08-23 21:53:48 +00:00
} else {
. "$PSScriptRoot/../aliae/Manage.ps1";
$value = $theme;
$relativePath = sudo -u "$user" fish -c 'realpath --relative-base ~ $argv' $theme;
if ($relativePath -ne $theme) {
$value = Join-Path "{{ .Home }}" $relativePath;
}
Add-EnvironmentVariable -User $Arguments.Name $varName $value;
2024-08-23 21:53:48 +00:00
}
}
};