From 1318e0090b8b5dbb11318dbea546224aaa40e0af Mon Sep 17 00:00:00 2001 From: z-Fng <54583083+z-Fng@users.noreply.github.com> Date: Mon, 10 Nov 2025 18:26:39 +0800 Subject: [PATCH] feat: Allow configuring PowerShell preference variables chore: Change default VerbosePreference to SilentlyContinue --- README.md | 9 +++++++++ action.ps1 | 4 ---- src/Variables.psm1 | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fa617a8..3231a95 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,15 @@ Set of automated actions, which bucket maintainers can use to save time managing 1. `SPECIAL_SNOWFLAKES` - String - List of manifest names joined with `,` used as parameter for auto-pr utility. +1. [PowerShell Preference Variables](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables) + - If an environment variable with the same name as the following PowerShell preference variable exists, its value will be assigned to the corresponding PowerShell preference variable: + + | Name | Type | Default Value | + | ------------------------------------------------------------ | ------ | -------------------- | + | [DebugPreference](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7.5#debugpreference) | String | `'SilentlyContinue'` | + | [InformationPreference](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7.5#informationpreference) | String | `'SilentlyContinue'` | + | [VerbosePreference](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7.5#verbosepreference) | String | `'SilentlyContinue'` | + | [WarningPreference](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_preference_variables?view=powershell-7.5#warningpreference) | String | `'Continue'` | ## Available actions diff --git a/action.ps1 b/action.ps1 index 30d0a1d..9bd93dc 100644 --- a/action.ps1 +++ b/action.ps1 @@ -1,7 +1,3 @@ -# Set Global Preference -$Global:ErrorActionPreference = 'Continue' -$Global:VerbosePreference = 'Continue' - # Import all modules Join-Path $PSScriptRoot 'src' | Get-ChildItem -File | Select-Object -ExpandProperty Fullname | Import-Module diff --git a/src/Variables.psm1 b/src/Variables.psm1 index 29fb175..65927a8 100644 --- a/src/Variables.psm1 +++ b/src/Variables.psm1 @@ -1,3 +1,38 @@ +function Initialize-PreferenceVariable { + <# + .SYNOPSIS + Initializes PowerShell preference variables based on environment variables or default values. + #> + + $DefaultPreferences = [ordered]@{ + # Set ErrorActionPreference first and prevent it from being overridden by the environment variable. + ErrorActionPreference = @{ Value = 'Continue'; IgnoreEnv = $true } + DebugPreference = @{ Value = 'SilentlyContinue' } + InformationPreference = @{ Value = 'SilentlyContinue' } + VerbosePreference = @{ Value = 'SilentlyContinue' } + WarningPreference = @{ Value = 'Continue' } + } + + foreach ($Name in $DefaultPreferences.Keys) { + $Preference = $DefaultPreferences[$Name] + $Value = $Preference.Value + + $EnvValue = Get-Item "Env:$Name" -ErrorAction Ignore | Select-Object -ExpandProperty Value + + if ((-not $Preference.IgnoreEnv) -and ($EnvValue)) { + $Value = $EnvValue + } + + # Use built-in output functions instead of Write-Log here to avoid dependency issues + Write-Host "Setting $Name to $Value ..." + + Set-Variable -Name $Name -Value $Value -Scope Global + } +} + +# Initialize PowerShell Preference Variables +Initialize-PreferenceVariable + # Environment $env:SCOOP = Join-Path $env:USERPROFILE 'SCOOP' $env:SCOOP_HOME = Join-Path $env:SCOOP 'apps\scoop\current'