Public/New-PstScript.ps1

function New-PstScript {
    <#
.SYNOPSIS
    Creates a new script file from a template with configurable complexity levels.
.DESCRIPTION
    This function takes a name parameter, copies a script template based on the specified
    complexity level, and saves it with the specified name.

    Complexity Levels:
    - Basic: Simple script with linear execution and minimal structure
    - Moderate: Script with parameter validation, basic error handling, and logging
    - Complex: Advanced script with begin/process/end blocks, pipeline support, and comprehensive features
.PARAMETER Name
    (Required) The name for the new script file.
.PARAMETER ComplexityLevel
    (Optional) The complexity level of the script template. Valid values: Basic, Moderate, Complex.
    Defaults to Moderate for typical script scenarios.
.PARAMETER Force
    (Optional) Forces creation even if file exists, useful in automation scenarios.
.EXAMPLE
    New-PstScript -Name MyScript
    Creates a new moderate complexity script file named MyScript.ps1.
.EXAMPLE
    New-PstScript -Name SimpleTask -ComplexityLevel Basic
    Creates a simple script with minimal structure for basic automation.
.EXAMPLE
    New-PstScript -Name AdvancedProcessor -ComplexityLevel Complex
    Creates an advanced script with full begin/process/end structure and pipeline support.
.NOTES
    Ensure that the appropriate template files exist in the Resources/Samples directory.
    Template files: Script-Basic.ps1, Script-Moderate.ps1, Script-Complex.ps1
.LINK
    For more information on PowerShell scripting, visit: https://docs.microsoft.com/en-us/powershell/scripting/overview
#>

    [CmdletBinding(SupportsShouldProcess)]
    param (
        [Parameter(Mandatory = $true, Position = 0,
            HelpMessage = "The name for the new script file.")]
        [ValidatePattern('^[A-Za-z][A-Za-z0-9\-_]*$')]
        [string]$Name,

        [Parameter(Mandatory = $false)]
        [ValidateSet("Basic", "Moderate", "Complex")]
        [string]$ComplexityLevel = "Moderate",

        [Parameter(Mandatory = $false)]
        [switch]$Force
    )
    begin {
        Write-Debug -Message "Begin '$($MyInvocation.MyCommand.Name)' at '$(Get-Date)'"

        # Select template based on complexity level
        $templateFileName = switch ($ComplexityLevel) {
            "Basic"     { "Script-Basic.ps1" }
            "Moderate"  { "Script-Moderate.ps1" }
            "Complex"   { "Script-Complex.ps1" }
            default     { "Script-Moderate.ps1" }  # Fallback to Moderate
        }


        $templatePath = $Samples[$templateFileName]
        $newFilePath = "$Name.ps1"

        Write-Verbose "Using complexity level: $ComplexityLevel"
        Write-Verbose "Selected template: $templateFileName"

        if (-not $templatePath) {
            throw "Template '$templateFileName' not found in Samples hashtable. Available templates: $($Samples.Keys -join ', ')"
        }
        if (-not (Test-Path $templatePath)) {
            throw "Template file not found: $templatePath"
        } else {
            Write-Verbose "Template file found: $templatePath"
        }
    }
    process {
        try {
            # Check if we're in automation mode - inline check to avoid scope issues
            $isAutomationMode = $Force -or
            ($Global:PstAutomationMode -eq $true) -or
            ($env:CI -eq 'true') -or
            ($env:GITHUB_ACTIONS -eq 'true') -or
            ($Host.Name -eq 'ServerRemoteHost') -or
            (-not [Environment]::UserInteractive) -or
            ($global:ConfirmPreference -eq 'None')
            # Check if file already exists
            if ((Test-Path $newFilePath) -and -not $isAutomationMode) {
                if (-not $PSCmdlet.ShouldProcess($newFilePath, "Overwrite existing script file")) {
                    Write-Warning "Operation cancelled by user."
                    return
                }
            }
            elseif ((Test-Path $newFilePath) -and $isAutomationMode -and -not $Force) {
                Write-Warning "File '$newFilePath' already exists. Use -Force to overwrite in automation mode."
                return
            }
            # Proceed with file creation - skip ShouldProcess in automation mode
            if ($isAutomationMode -or $PSCmdlet.ShouldProcess($newFilePath, "Create new script file")) {
                Write-Information -Message "Copy-Item -Path '$($templatePath)' -Destination '$(Join-Path (Get-Location) $newFilePath)'"
                Copy-Item -Path $templatePath -Destination $newFilePath
                Write-Output "New script file created: $newFilePath"
            }
            else {
                Write-Verbose "Operation cancelled or skipped."
            }
        }
        catch {
            if ($_.Exception -and $_.Exception.Message) {
                Write-Error "An error occurred: $($_.Exception.Message)"
            } else {
                Write-Error "An error occurred, but no additional information is available."
            }
        }
    }
    end {
        if ($?) {
            Write-Debug -Message "End '$($MyInvocation.MyCommand.Name)' at '$(Get-Date)'"
        }
    }
}