Functions/GenXdev.Coding.PowerShell.Modules/Get-Refactor.ps1
<##############################################################################
Part of PowerShell module : GenXdev.Coding.PowerShell.Modules Original cmdlet filename : Get-Refactor.ps1 Original author : René Vaessen / GenXdev Version : 1.270.2025 ################################################################################ MIT License Copyright 2021-2025 GenXdev Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ################################################################################> ############################################################################### <# .SYNOPSIS Retrieves refactor definitions from GenXdev preferences based on name patterns. .DESCRIPTION Searches for and loads refactor definition sets stored in GenXdev preferences. Each refactor set is stored as a JSON string in a preference with name starting with 'refactor_set_'. The function deserializes matching sets into objects. .PARAMETER Name One or more name patterns to match against refactor set names. Patterns are matched against the portion of the preference name after 'refactor_set_' prefix. Supports wildcards. If omitted, returns all refactor sets. .EXAMPLE Get-Refactor -Name "CodeStyle*" Returns refactor definitions matching pattern "CodeStyle*" .EXAMPLE refactor "UnitTest" Uses alias to find refactor definitions containing "UnitTest" #> function Get-Refactor { [CmdletBinding()] [Alias('refactors')] [OutputType([GenXdev.Helpers.RefactorDefinition[]])] param ( ######################################################################## [Parameter( Mandatory = $false, Position = 0, HelpMessage = 'Pattern(s) to search for refactor definitions', ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true )] [ValidateNotNullOrEmpty()] [SupportsWildcards()] [Alias('PreferenceName')] [string[]] $Name = @('*'), ######################################################################## [Parameter( Mandatory = $false, HelpMessage = 'Database path for preference data files' )] [Alias('DatabasePath')] [string] $PreferencesDatabasePath, ######################################################################## [Parameter( Mandatory = $false, HelpMessage = 'The default value if preference is not found' )] [AllowNull()] [AllowEmptyString()] [Alias('DefaultPreference')] [string] $DefaultValue, ######################################################################## [Parameter( Mandatory = $false, HelpMessage = ('Use alternative settings stored in session for Data ' + 'preferences like Language, Database paths, etc') )] [switch] $SessionOnly, ######################################################################## [Parameter( Mandatory = $false, HelpMessage = ('Clear the session setting (Global variable) before ' + 'retrieving') )] [switch] $ClearSession, ######################################################################## [Parameter( Mandatory = $false, HelpMessage = ('Dont use alternative settings stored in session for ' + 'Data preferences like Language, Database paths, etc') )] [Alias('FromPreferences')] [switch] $SkipSession ######################################################################## ) begin { # no initialization needed } process { # get all preference names that could contain refactor definitions Microsoft.PowerShell.Utility\Write-Verbose 'Searching for refactor set preferences...' # Copy identical parameters for Get-GenXdevPreferenceNames $prefNamesParams = GenXdev.Helpers\Copy-IdenticalParamValues ` -BoundParameters $PSBoundParameters ` -FunctionName 'GenXdev.Data\Get-GenXdevPreferenceNames' ` -DefaultValues (Microsoft.PowerShell.Utility\Get-Variable -Scope Local -ErrorAction SilentlyContinue) $prefNames = GenXdev.Data\Get-GenXdevPreferenceNames @prefNamesParams | Microsoft.PowerShell.Core\Where-Object { $_ -like 'refactor_set_*' } & { foreach ($prefName in $prefNames) { Microsoft.PowerShell.Utility\Write-Verbose "Processing preference: $prefName" # check each provided name pattern against current preference foreach ($pattern in $Name) { # extract name portion after refactor_set_ prefix $actualName = $prefName.Substring('refactor_set_'.Length) # skip if pattern doesn't match the actual name if (-not ($actualName -like $pattern)) { continue } # attempt to load and parse the JSON content # Copy identical parameters for Get-GenXdevPreference $prefParams = GenXdev.Helpers\Copy-IdenticalParamValues ` -BoundParameters $PSBoundParameters ` -FunctionName 'GenXdev.Data\Get-GenXdevPreference' ` -DefaultValues (Microsoft.PowerShell.Utility\Get-Variable -Scope Local -ErrorAction SilentlyContinue) $prefParams.Name = $prefName $existingJson = GenXdev.Data\Get-GenXdevPreference @prefParams -ErrorAction SilentlyContinue $existingJson = GenXdev.Data\Get-GenXdevPreference @prefParams -ErrorAction SilentlyContinue # process non-empty JSON content if (-not [string]::IsNullOrWhiteSpace($existingJson)) { try { # deserialize JSON into refactor definition object [GenXdev.Helpers.RefactorDefinition] $refactorSet = (Microsoft.PowerShell.Utility\ConvertFrom-Json $existingJson) -as [GenXdev.Helpers.RefactorDefinition] # check containers if (($null -eq $refactorSet.State.Unselected) -or ($null -eq $refactorSet.State.Selected) -or ($null -eq $refactorSet.State.Refactored) -or ($null -eq $refactorSet.Log)) { $newSet = [GenXdev.Helpers.RefactorDefinition]::new() $newSet.Name = $refactorSet.Name $newSet.Priority = $refactorSet.Priority $newSet.SelectionSettings.Script = (!!$refactorSet.SelectionSettings ? $refactorSet.SelectionSettings.Script : '') $refactorSet = $newSet } Microsoft.PowerShell.Utility\Write-Verbose ('Successfully parsed refactor set from ' + "preference: $prefName") Microsoft.PowerShell.Utility\Write-Output $refactorSet } catch { $errorMsg = "Failed to parse refactor set from $prefName" Microsoft.PowerShell.Utility\Write-Error $errorMsg Microsoft.PowerShell.Utility\Write-Verbose "$errorMsg : $_" } } break; } } }.GetNewClosure() | Microsoft.PowerShell.Utility\Sort-Object -Property Priority -Descending } end { } } |