From 2a043a0f357362573125a6661aeba6680baaa33e Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Wed, 7 Aug 2024 21:34:40 +0200 Subject: [PATCH] Refactor PowerShell profile script --- scripts/Common/Config/Oh My Posh/install.ps1 | 4 +- scripts/Common/Config/aliae/install.ps1 | 6 +- scripts/Common/Config/zoxide/install.ps1 | 4 +- scripts/Common/Scripts/Context.ps1 | 6 +- scripts/Common/Software/PowerShell/Manage.ps1 | 16 +-- .../Common/Software/PowerShell/Profile.ps1 | 105 ++++++++++++++++++ .../Common/Software/PowerShell/profile.ps1 | 95 ---------------- scripts/Common/Software/zoxide/install.ps1 | 4 +- .../Windows/Config/Terminal-Icons/Install.ps1 | 4 +- scripts/Windows/Config/posh-git/Install.ps1 | 4 +- 10 files changed, 126 insertions(+), 122 deletions(-) create mode 100644 scripts/Common/Software/PowerShell/Profile.ps1 delete mode 100644 scripts/Common/Software/PowerShell/profile.ps1 diff --git a/scripts/Common/Config/Oh My Posh/install.ps1 b/scripts/Common/Config/Oh My Posh/install.ps1 index cc24d77f..c06f1af1 100644 --- a/scripts/Common/Config/Oh My Posh/install.ps1 +++ b/scripts/Common/Config/Oh My Posh/install.ps1 @@ -1,10 +1,10 @@ #!/bin/pwsh -. "$PSScriptRoot/../../Software/PowerShell/profile.ps1"; +. "$PSScriptRoot/../../Software/PowerShell/Profile.ps1"; Add-PowerShellProfileStatement ` -System ` -Category "oh-my-posh" ` - -Statement $( + -Script $( @( "# Oh My Posh!", $(Get-ScriptInitializer "oh-my-posh init pwsh"), diff --git a/scripts/Common/Config/aliae/install.ps1 b/scripts/Common/Config/aliae/install.ps1 index d4de4848..82cae57b 100644 --- a/scripts/Common/Config/aliae/install.ps1 +++ b/scripts/Common/Config/aliae/install.ps1 @@ -1,8 +1,8 @@ #!/bin/pwsh -. "$PSScriptRoot/../../Software/PowerShell/profile.ps1"; +. "$PSScriptRoot/../../Software/PowerShell/Profile.ps1"; foreach ($defaultUser in @($true, $false)) { - Add-PowerShellProfileStatement -DefaultUser:$defaultUser -Statement "# aliae`naliae init pwsh | Invoke-Expression"; + Add-PowerShellProfileStatement -DefaultUser:$defaultUser -Script "# aliae`naliae init pwsh | Invoke-Expression"; } -Add-PowerShellProfileStatement -System -Category "aliae" -Statement "# aliae`n$(Get-ScriptInitializer "aliae completion powershell")"; +Add-PowerShellProfileStatement -System -Category "aliae" -Script "# aliae`n$(Get-ScriptInitializer "aliae completion powershell")"; diff --git a/scripts/Common/Config/zoxide/install.ps1 b/scripts/Common/Config/zoxide/install.ps1 index 0932a91e..6b35f717 100644 --- a/scripts/Common/Config/zoxide/install.ps1 +++ b/scripts/Common/Config/zoxide/install.ps1 @@ -1,10 +1,10 @@ #!/bin/pwsh -. "$PSScriptRoot/../../Software/PowerShell/profile.ps1"; +. "$PSScriptRoot/../../Software/PowerShell/Profile.ps1"; Add-PowerShellProfileStatement ` -System ` -Category "zoxide" ` - -Statement $( + -Script $( @( "# zoxide", $(Get-ScriptInitializer "zoxide init powershell | Out-String") diff --git a/scripts/Common/Scripts/Context.ps1 b/scripts/Common/Scripts/Context.ps1 index 30ea877e..17505809 100644 --- a/scripts/Common/Scripts/Context.ps1 +++ b/scripts/Common/Scripts/Context.ps1 @@ -1,6 +1,6 @@ #!/bin/pwsh . "$PSScriptRoot/../../Common/Scripts/Entrypoints.ps1"; -. "$PSScriptRoot/../../Common/Software/PowerShell/profile.ps1"; +. "$PSScriptRoot/../../Common/Software/PowerShell/Profile.ps1"; class Context { [string]$EntryPoint; @@ -68,9 +68,9 @@ class Context { [void] AddPowerShellProfileStatement([bool] $system, [string] $category, [string] $statement, [bool] $overwrite) { if ($system) { - Add-PowerShellProfileStatement -System -Category $category -Statement $statement -Overwrite $overwrite; + Add-PowerShellProfileStatement -System -Category $category -Script $statement -Overwrite:$overwrite; } else { - Add-PowerShellProfileStatement -Category $category -Statement $statement -Overwrite $overwrite; + Add-PowerShellProfileStatement -Category $category -Script $statement -Overwrite:$overwrite; } } diff --git a/scripts/Common/Software/PowerShell/Manage.ps1 b/scripts/Common/Software/PowerShell/Manage.ps1 index bb7e950f..066b6312 100644 --- a/scripts/Common/Software/PowerShell/Manage.ps1 +++ b/scripts/Common/Software/PowerShell/Manage.ps1 @@ -4,7 +4,7 @@ param ( ) . "$PSScriptRoot/../../Scripts/Software.ps1"; -. "$PSScriptRoot/../../Scripts/Scripting.ps1"; +. "$PSScriptRoot/../../Software/PowerShell/Profile.ps1"; . "$PSScriptRoot/../../Types/InstallerAction.ps1"; Start-SoftwareInstaller @PSBoundParameters ` @@ -29,15 +29,10 @@ Start-SoftwareInstaller @PSBoundParameters ` $globalDir = '"$env:ProgramData/PowerShell/conf.d"'; } - [System.Collections.ArrayList] $files = @($PROFILE); - - if (Test-Command powershell) { - $null = $files.Add((powershell -c '$PROFILE')); - } - - foreach ($file in $files) { - if (-not ((Test-Path -PathType Leaf $file) -and ((Get-Content $file) -contains $indicator))) { - Write-PSScript -FileName $file -Script (@( + if (-not ((Test-Path -PathType Leaf $PROFILE) -and ((Get-Content $PROFILE) -contains $indicator))) { + Add-PowerShellProfileStatement ` + -DefaultUser ` + -Script (@( $indicator, "`$globalDir = $globalDir", ({ @@ -55,6 +50,5 @@ Start-SoftwareInstaller @PSBoundParameters ` } }).ToString()) -join "`n") ` -Append; - } } }; diff --git a/scripts/Common/Software/PowerShell/Profile.ps1 b/scripts/Common/Software/PowerShell/Profile.ps1 new file mode 100644 index 00000000..1376be54 --- /dev/null +++ b/scripts/Common/Software/PowerShell/Profile.ps1 @@ -0,0 +1,105 @@ +$null = New-Module { + . "$PSScriptRoot/../../Scripts/Scripting.ps1"; + . "$PSScriptRoot/../../Scripts/Software.ps1"; + + <# + .SYNOPSIS + Adds an initialization script to the profile. + + .PARAMETER System + A value indicating whether the script should be installed globally. + + .PARAMETER DefaultUser + A value indicating whether the script should be installed to users by default. + + .PARAMETER HomeDir + The path to the home directory of the user to install the script to. + + .PARAMETER Category + The category name of the script to install. + + .PARAMETER Script + The script to install. + + .PARAMETER Replace + A value indicating whether the script should be replaced if it already exists. + + .PARAMETER Append + A value indicating whether the content should be appended to the script if it already exists. + #> + function Add-PowerShellProfileStatement { + param( + [Parameter(ParameterSetName = "Global", Mandatory)] + [switch] $System, + [Parameter(ParameterSetName = "DefaultUser", Mandatory)] + [switch] $DefaultUser, + [Parameter(ParameterSetName = "Home")] + [string] $HomeDir = "~", + [Parameter(ParameterSetName = "Global", Mandatory)] + [Parameter(ParameterSetName = "DefaultUser")] + [Parameter(ParameterSetName = "Home")] + [string] $Category = $null, + [Parameter(Position = 0, Mandatory)] + [string] $Script, + [switch] $Replace, + [switch] $Append + ) + + [System.Collections.ArrayList] $profiles = @(); + + if ($System) { + [string] $configRoot = $null; + + if ($IsWindows) { + # ToDo Change to "PowerShell" + $configRoot = "$env:ProgramData"; + } else { + $configRoot = "/etc"; + } + + $profiles = @("$configRoot/powershell/."); + } else { + if ($DefaultUser) { + if (-not $IsWindows) { + $HomeDir = "/etc/skel"; + } else { + $HomeDir = "C:/Users/Default"; + } + } + + foreach ($shell in @("pwsh", "powershell")) { + if (Test-Command $shell) { + $null = $profiles.Add((& $shell -NoProfile -c '$PROFILE')); + } + } + + Push-Location ~; + + $profiles = $profiles | + ForEach-Object { [System.IO.Path]::GetRelativePath((Get-Location), $_) } | + ForEach-Object { "$HomeDir/$_" }; + + Pop-Location; + } + + if ($Category) { + $profiles = $profiles | ForEach-Object { Join-Path (Split-Path -Parent $_) "conf.d" "$Category.ps1" }; + } + + $profiles | ForEach-Object { + $arguments = @{}; + + if ($Replace.IsPresent) { + $null = $arguments.Add("Replace", $Replace); + } + + if ($Append.IsPresent) { + $null = $arguments.Add("Append", $Append); + } + + Write-PSScript @arguments ` + -FileName $_ ` + -Script $Script; + }; + } +}; diff --git a/scripts/Common/Software/PowerShell/profile.ps1 b/scripts/Common/Software/PowerShell/profile.ps1 deleted file mode 100644 index 0612d26e..00000000 --- a/scripts/Common/Software/PowerShell/profile.ps1 +++ /dev/null @@ -1,95 +0,0 @@ -#!/bin/pwsh -$null = New-Module { - function Add-PowerShellProfileStatement() { - param ( - [Parameter(ParameterSetName="Global", Mandatory)] - [switch]$System, - [Parameter(ParameterSetName="DefaultUser", Mandatory)] - [switch]$DefaultUser, - [Parameter(ParameterSetName="Home")] - [string]$HomeDir = "~", - [Parameter(ParameterSetName="Global", Mandatory)] - [Parameter(ParameterSetName="DefaultUser")] - [Parameter(ParameterSetName="Home")] - [string]$Category = $null, - [Parameter(Position=0, Mandatory=$true)] - [string]$Statement, - [switch]$Overwrite - ) - - [System.Collections.Generic.List[string]] $profiles = @(); - - if ($System) { - $configRoot; - - if ($IsWindows) { - $configRoot = "$env:ProgramData"; - } else { - $configRoot = "/etc"; - } - - $profiles = @("$configRoot/powershell/."); - } else { - [System.Collections.Generic.List[string]] $shells = @(); - - if ($DefaultUser) { - if ($IsWindows) { - $HomeDir = "C:/Users/Default"; - } else { - $HomeDir = "/etc/skel" - } - } - - 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), $_); } | - ForEach-Object { "$HomeDir/$_" }; - } - - if ($Category) { - if (-not $($Overwrite.IsPresent)) { - $Overwrite = $true; - } - - $profiles = $profiles | ForEach-Object { Join-Path (Split-Path -Parent $_) "conf.d" "$Category.ps1"; }; - } - - $profiles | ForEach-Object { - $dirName = Split-Path -Parent $_; - - if (-not (Test-Path -PathType Container $dirName)) { - $null = New-Item -ItemType Directory -Force $dirName; - } - - if ((Test-Path -PathType Leaf $_) -and (-not $Overwrite)) { - Add-Content -Force "$_" "`n$Statement"; - } else { - Set-Content -Force "$_" "$Statement"; - } - }; - - Pop-Location; - } - - function Get-ScriptInitializer() { - param ( - [Parameter(Position=0, Mandatory=$true)] - $Initializer - ) - - return ". ([scriptblock]::Create(($Initializer) -join `"``n`"))"; - } -} diff --git a/scripts/Common/Software/zoxide/install.ps1 b/scripts/Common/Software/zoxide/install.ps1 index 5d745a83..518f81b3 100644 --- a/scripts/Common/Software/zoxide/install.ps1 +++ b/scripts/Common/Software/zoxide/install.ps1 @@ -1,10 +1,10 @@ #!/bin/pwsh -. "$PSScriptRoot/../PowerShell/profile.ps1"; +. "$PSScriptRoot/../PowerShell/Profile.ps1"; Add-PowerShellProfileStatement ` -System ` -Category "zoxide" ` - -Statement $( + -Script $( @( "# zoxide", $(Get-ScriptInitializer "zoxide init powershell | Out-String") diff --git a/scripts/Windows/Config/Terminal-Icons/Install.ps1 b/scripts/Windows/Config/Terminal-Icons/Install.ps1 index 8676ef08..095f25f3 100644 --- a/scripts/Windows/Config/Terminal-Icons/Install.ps1 +++ b/scripts/Windows/Config/Terminal-Icons/Install.ps1 @@ -1,3 +1,3 @@ #!/bin/pwsh -. "$PSScriptRoot/../../../Common/Software/PowerShell/profile.ps1"; -Add-PowerShellProfileStatement -System -Category "Terminal-Icons" -Statement 'Import-Module "Terminal-Icons";'; +. "$PSScriptRoot/../../../Common/Software/PowerShell/Profile.ps1"; +Add-PowerShellProfileStatement -System -Category "Terminal-Icons" -Script 'Import-Module "Terminal-Icons";'; diff --git a/scripts/Windows/Config/posh-git/Install.ps1 b/scripts/Windows/Config/posh-git/Install.ps1 index 35a26480..003939ae 100644 --- a/scripts/Windows/Config/posh-git/Install.ps1 +++ b/scripts/Windows/Config/posh-git/Install.ps1 @@ -1,4 +1,4 @@ #!/bin/pwsh -. "$PSScriptRoot/../../../Common/Software/PowerShell/profile.ps1"; +. "$PSScriptRoot/../../../Common/Software/PowerShell/Profile.ps1"; Write-Host "Configuring posh-git"; -Add-PowerShellProfileStatement -System -Category "posh-git" -Statement 'Import-Module "posh-git";' +Add-PowerShellProfileStatement -System -Category "posh-git" -Script 'Import-Module "posh-git";'