From b07f4ce23bb157bddc412411db1efa57b032a1f3 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Wed, 20 Mar 2024 23:49:21 +0100 Subject: [PATCH] Allow global installation of powershell configs --- scripts/Common/Config/powershell/lib.ps1 | 41 +++++++++++++++++------- scripts/Windows/Scripts/Context.ps1 | 28 +++++++--------- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/scripts/Common/Config/powershell/lib.ps1 b/scripts/Common/Config/powershell/lib.ps1 index 40b23bb0..50245b3d 100644 --- a/scripts/Common/Config/powershell/lib.ps1 +++ b/scripts/Common/Config/powershell/lib.ps1 @@ -2,32 +2,49 @@ $null = New-Module { function Add-PowerShellProfileStatement() { param ( + [Parameter(ParameterSetName="Global", Mandatory)] + [switch]$System, + [Parameter(ParameterSetName="Home")] [string]$HomeDir = "~", + [Parameter(ParameterSetName="Profile", Mandatory)] + [string]$ProfileRoot, + [Parameter(ParameterSetName="Profile", Mandatory)] + [Parameter(ParameterSetName="Global", Mandatory)] + [Parameter(ParameterSetName="Home")] [string]$Category = $null, [Parameter(Position=0, Mandatory=$true)] [string]$Statement, [switch]$Overwrite ) - [System.Collections.Generic.List[string]] $shells = @(); [System.Collections.Generic.List[string]] $profiles = @(); - if (Get-Command pwsh -ErrorAction SilentlyContinue) { - $shells.Add("pwsh"); + if ($System) { + $ProfileRoot = "$env:ProgramData/powershell"; } - if (Get-Command powershell -ErrorAction SilentlyContinue) { - $shells.Add("powershell"); - } + if (-not $ProfileRoot) { + [System.Collections.Generic.List[string]] $shells = @(); - foreach ($shell in $shells) { - $path = & $shell -NoProfile -c '$PROFILE'; - $profiles.Add($path); + if (Get-Command pwsh -ErrorAction SilentlyContinue) { + $shells.Add("pwsh"); + } + + if (Get-Command powershell -ErrorAction SilentlyContinue) { + $shells.Add("powershell"); + } + + foreach ($shell in $shells) { + $path = & $shell -NoProfile -c '$PROFILE'; + $profiles.Add($path); + } + + Push-Location ~; + $profiles = $profiles | ForEach-Object { [System.IO.Path]::GetRelativePath($(Get-Location), $_); }; + } else { + $profiles = @("$ProfileRoot/."); } - Push-Location ~; - $profiles = $profiles | ForEach-Object { [System.IO.Path]::GetRelativePath($(Get-Location), $_); }; - if ($Category) { if (-not $($Overwrite.IsPresent)) { $Overwrite = $true; diff --git a/scripts/Windows/Scripts/Context.ps1 b/scripts/Windows/Scripts/Context.ps1 index 8620d6d4..647fc66a 100644 --- a/scripts/Windows/Scripts/Context.ps1 +++ b/scripts/Windows/Scripts/Context.ps1 @@ -44,34 +44,30 @@ class Context { $this.AddPowerShellProfileStatement($true, $category, $statement); } - [void] AddPowerShellProfileStatement([bool] $defaultUser, [string] $statement) { - $this.AddPowerShellProfileStatement($defaultUser, $null, $statement); + [void] AddPowerShellProfileStatement([bool] $system, [string] $statement) { + $this.AddPowerShellProfileStatement($system, $null, $statement); } - [void] AddPowerShellProfileStatement([bool] $defaultUser, [string] $category, [string] $statement) { - [string] $homeDir = $null; - - if ($defaultUser) { - $homeDir = "C:\Users\Default"; - } else { - $homeDir = "~"; - } - - $this.AddPowerShellProfileStatement($homeDir, $category, $statement); + [void] AddPowerShellProfileStatement([bool] $system, [string] $category, [string] $statement) { + $this.AddPowerShellProfileStatement($system, $category, $statement); } - [void] AddPowerShellProfileStatement([string] $homeDir, [string] $category, [string] $statement) { + [void] AddPowerShellProfileStatement([bool] $system, [string] $category, [string] $statement) { if ($category) { $overwrite = $true; } else { $overwrite = $false; } - $this.AddPowerShellProfileStatement($homeDir, $category, $statement, $overwrite); + $this.AddPowerShellProfileStatement($system, $category, $statement, $overwrite); } - [void] AddPowerShellProfileStatement([string] $homeDir, [string] $category, [string] $statement, [bool] $overwrite) { - Add-PowerShellProfileStatement -HomeDir $homeDir -Category $category -Statement $statement -Overwrite $overwrite; + [void] AddPowerShellProfileStatement([bool] $system, [string] $category, [string] $statement, [bool] $overwrite) { + if ($system) { + Add-PowerShellProfileStatement -System -Category $category -Statement $statement -Overwrite $overwrite; + } else { + Add-PowerShellProfileStatement -Category $category -Statement $statement -Overwrite $overwrite; + } } [Microsoft.Win32.RegistryKey] EnsureConfigKey() {