gen/Generate-CfnPseudoParameters.ps1


<#
Usage:
    Generate-CfnPseudoParameters.ps1 > CfnPseudoParameters.generated.ps1
#>


$cfnSchema = ConvertFrom-Json ([System.IO.File]::ReadAllText("$PSScriptRoot\CloudFormationV1.schema"))
$pseudoParameters = $cfnSchema.'pseudo-parameters'
$pseudoDefs = [ordered]@{}

foreach ($pseudoProp in ($pseudoParameters | gm -MemberType NoteProperty <#| select -First 3#>)) {
    $pseudoName    = $pseudoProp.Name
    $pseudo        = $pseudoParameters.$pseudoName
    $pseudoType    = $pseudo.type
    $pseudoSubtype = $pseudo.'array-type'
    $pseudoDesc    = $pseudo.description

    if ($pseudoName -match "AWS::(.+)") {
        $pseudoShortname = $Matches[1]
    }
    else {
        $pseudoShortname = $pseudoName
    }

    $pseudoDefs[$pseudoName] = @{
        type    = $pseudoType
        subtype = $pseudoSubtype
        desc    = $pseudoDesc
        short   = $pseudoShortname
        full    = $pseudoName
    }
}

@"
<#
WARNING: THIS FILE IS AUTO-GENERATED! MANUAL CHANGES TO THIS
          FILE *WILL BE LOST* AFTER THE NEXT AUTO-GENERATION!
 
Generated By: [$($env:USERNAME)]
Generated At: [$([DateTime]::Now.ToString("yyyyMMdd_HHmmss"))]
Generated On: [$($env:COMPUTERNAME)]
Generated W/: [$([System.IO.Path]::GetFileName($PSCommandPath))]
#>
 
function Use-CfnPseudoParameter {
<#
.SYNOPSIS
Pseudo Parameters are parameters that are predefined by AWS CloudFormation. You do not declare them in your template.
.DESCRIPTION
You use them the same way as you would a Template Parameter, except that this cmdlet already returns the Pseudo Parameter in a "Ref function form" by default.
 
The following Pseudo Parameters are defined:
"@


$pseudoDefs.Values | % {
@"
  * $($_.short): $($_.desc)
    o Type: $($_.type) $(if ($_.subtype) { ' (' + $_.subtype + ')' })
    o Full: $($_.full)
"@

}

@"
.LINK
docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/pseudo-parameter-reference.html
#>
    param(
        [Parameter(Mandatory,Position=0)]
        [ValidateSet(
"@


$ndx = $pseudoDefs.Count - 1
$pseudoDefs.Values | % { @"
            '$($_.short)'$(if ($ndx--) { ',' })
"@
 }

@"
            )]
        [string]`$Parameter,
        [switch]`$NameOnly
    )
 
    `$FullName = "AWS::`$(`$Parameter)"
    if (`$NameOnly) {
        return `$FullName
    }
    else {
        return @{ "Ref" = `$FullName }
    }
}
Set-Alias -Name Pseudo -Value Use-CfnPseudoParameter
 
"@