Private/New-PstFunctionSignature.ps1
|
function New-PstFunctionSignature { <# .SYNOPSIS Generates PowerShell function signature. .DESCRIPTION Creates the function declaration line with appropriate CmdletBinding attributes based on complexity level. .PARAMETER FunctionName The name of the function (should follow Verb-Noun pattern). .PARAMETER Complexity The complexity level: Basic, Standard, or Advanced. .EXAMPLE New-PstFunctionSignature -FunctionName "Get-UserData" -Complexity Standard Generates: function Get-UserData { [CmdletBinding(SupportsShouldProcess)] .NOTES Version: 1.0 Author: numidia Creation Date: 2025-12-01 Purpose: Generate function signatures with CmdletBinding #> [CmdletBinding()] [OutputType([string])] param( [Parameter(Mandatory = $true)] [string]$FunctionName, [Parameter(Mandatory = $true)] [ValidateSet('Basic', 'Standard', 'Advanced')] [string]$Complexity ) begin { Write-Debug "Begin '$($MyInvocation.MyCommand.Name)' at '$(Get-Date)'" } process { try { $features = Get-PstComplexityFeatures -Complexity $Complexity -Type Function $sb = [System.Text.StringBuilder]::new() # Function declaration [void]$sb.AppendLine("function $FunctionName {") # Add CmdletBinding for Standard and Advanced if ($features.CmdletBinding.Enabled) { $cmdletBindingParams = [System.Collections.ArrayList]::new() # Add SupportsShouldProcess if ($features.CmdletBinding.SupportsShouldProcess) { [void]$cmdletBindingParams.Add("SupportsShouldProcess") } # Add DefaultParameterSetName for Advanced if ($features.CmdletBinding.DefaultParameterSetName) { [void]$cmdletBindingParams.Add("DefaultParameterSetName = '$($features.CmdletBinding.DefaultParameterSetName)'") } if ($cmdletBindingParams.Count -gt 0) { [void]$sb.Append(" [CmdletBinding($($cmdletBindingParams -join ', '))]") } else { [void]$sb.Append(" [CmdletBinding()]") } } Write-Verbose "Generated $Complexity function signature for '$FunctionName'" return $sb.ToString() } catch { Write-Error "Failed to generate function signature: $($_.Exception.Message)" $PSCmdlet.ThrowTerminatingError($_) } } end { Write-Debug "End '$($MyInvocation.MyCommand.Name)' at '$(Get-Date)'" } } |