Refactor PowerShell profile script

This commit is contained in:
Manuel Thalmann 2024-08-07 21:34:40 +02:00
commit a34aa6c143
10 changed files with 126 additions and 122 deletions
scripts/Common/Software/PowerShell

View file

@ -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;
}
}
};

View file

@ -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;
};
}
};

View file

@ -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`"))";
}
}