Refactor PowerShell profile script
This commit is contained in:
parent
2dfb9d58bf
commit
a34aa6c143
|
@ -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"),
|
||||
|
|
|
@ -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")";
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
105
scripts/Common/Software/PowerShell/Profile.ps1
Normal file
105
scripts/Common/Software/PowerShell/Profile.ps1
Normal 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;
|
||||
};
|
||||
}
|
||||
};
|
|
@ -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`"))";
|
||||
}
|
||||
}
|
|
@ -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")
|
||||
|
|
|
@ -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";';
|
||||
|
|
|
@ -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";'
|
||||
|
|
Loading…
Reference in a new issue