diff --git a/scripts/Common/Software/PowerShell/Manage.ps1 b/scripts/Common/Software/PowerShell/Manage.ps1 new file mode 100644 index 00000000..904bdf26 --- /dev/null +++ b/scripts/Common/Software/PowerShell/Manage.ps1 @@ -0,0 +1,60 @@ +param ( + $Action, + [hashtable] $Arguments +) + +. "$PSScriptRoot/../../../Windows/Scripts/Software.ps1"; +. "$PSScriptRoot/../../../Windows/Scripts/Scripting.ps1"; +. "$PSScriptRoot/../../../Windows/Types/InstallerAction.ps1"; + +Start-SoftwareInstaller @PSBoundParameters ` + -Installer { + param( + [scriptblock] $Installer + ) + + & $Installer -Action ([InstallerAction]::Configure); + } ` + -Configurator { + param( + [hashtable] $Arguments + ) + + [string] $globalDir = $null; + $indicator = "# Profile Files"; + + if ($Arguments.Linux) { + $globalDir = '"/etc/powershell/conf.d"'; + } else { + $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 (@( + $indicator, + "`$globalDir = $globalDir", + ({ + $profileRoot = Split-Path -Parent $PROFILE; + + $profilePaths = @( + "$profileRoot/conf.d/*.ps1", + "$globalDir/*.ps1" + ) + + foreach ($profilePath in $profilePaths) { + if (Test-Path $profilePath) { + Get-Item $profilePath | ForEach-Object { . $_; }; + } + } + }).ToString()) -join "`n") ` + -Append; + } + } + }; diff --git a/scripts/Windows/OS/Install.ps1 b/scripts/Windows/OS/Install.ps1 index 02f04ed3..b6e03f18 100644 --- a/scripts/Windows/OS/Install.ps1 +++ b/scripts/Windows/OS/Install.ps1 @@ -63,6 +63,7 @@ $null = New-Module { Invoke-Hook "Install-PSModules" -Fallback { Install-Module -AcceptLicense -Force PSWindowsUpdate; + Install-Module -AcceptLicense -Force PSScriptAnalyzer; }; if (-not (Test-Winget)) { @@ -181,6 +182,7 @@ $null = New-Module { if (Test-Collection "essential") { # Essentials . "$softwarePath/OpenSSH/Manage.ps1" @arguments; + . "$softwarePath/PowerShell/Manage.ps1"; } if (Test-Collection "common") { diff --git a/scripts/Windows/Scripts/Scripting.ps1 b/scripts/Windows/Scripts/Scripting.ps1 index e0a9f912..b9b0ebbb 100644 --- a/scripts/Windows/Scripts/Scripting.ps1 +++ b/scripts/Windows/Scripts/Scripting.ps1 @@ -14,3 +14,49 @@ function ConvertTo-Injection { [System.Convert]::ToBase64String([System.Text.Encoding]::Default.GetBytes($value)) )')))" } + +<# + .SYNOPSIS + Writes a PowerShell script to a specified location. + + .PARAMETER FileName + The name of the file to write the script to. + + .PARAMETER Script + The script to write to the file. + + .PARAMETER Replace + A value indicating whether the file should be overwritten if it exists. + + .PARAMETER Append + A value indicating whether the content should be appended if the file already exists. +#> +function Write-PSScript { + param( + [string] $FileName, + [string] $Script, + [Parameter(ParameterSetName="Replace")] + [switch] $Replace, + [Parameter(ParameterSetName="Append")] + [switch] $Append + ) + + Import-Module PSScriptAnalyzer; + $dirName = Split-Path -Parent $FileName; + $content = Invoke-Formatter -ScriptDefinition $Script; + $exists = Test-Path -PathType Leaf $FileName; + + if (-not (Test-Path -PathType Container $dirName)) { + $null = New-Item -ItemType Directory $dirName; + } + + if ($exists -and ($Append.IsPresent)) { + Add-Content -Force $FileName "`n$content"; + } else { + if ((-not $exists) -or $Replace.IsPresent) { + Set-Content -Force $FileName $content; + } else { + Write-Host "The file ``$FileName`` already exists!"; + } + } +} diff --git a/scripts/Windows/Software/PowerShell/Manage.ps1 b/scripts/Windows/Software/PowerShell/Manage.ps1 new file mode 100644 index 00000000..a52a0452 --- /dev/null +++ b/scripts/Windows/Software/PowerShell/Manage.ps1 @@ -0,0 +1,21 @@ +param ( + $Action, + [hashtable] $Arguments +) + +. "$PSScriptRoot/../../Scripts/Software.ps1"; +. "$PSScriptRoot/../../Types/InstallerAction.ps1"; + +Start-SoftwareInstaller @PSBoundParameters ` + -Installer { + param( + [scriptblock] $Installer + ) + + & $Installer -Action ([InstallerAction]::Configure); + } ` + -Configurator { + . "$PSScriptRoot/../../../Common/Software/PowerShell/Manage.ps1" ` + -Action ([InstallerAction]::Configure) ` + -Arguments @{ Linux = $false }; + };