Skip to content

Instantly share code, notes, and snippets.

@romero126
Last active February 14, 2021 01:38
Show Gist options
  • Save romero126/1dc7269b7f1cd427e8c808a2c05e11cb to your computer and use it in GitHub Desktop.
Save romero126/1dc7269b7f1cd427e8c808a2c05e11cb to your computer and use it in GitHub Desktop.
Convert-ToCSharp
function Convert-ToCSharp
{
param(
[Parameter(Mandatory)]
[System.String]$Path,
[Parameter(Mandatory)]
[System.String]$OutFile
)
begin
{
}
process
{
if (-not (Test-Path -Path $Path -PathType Leaf))
{
throw "Could not find file"
}
# Get the AST of the file
$tokens = $errors = $null
$ast = [System.Management.Automation.Language.Parser]::ParseFile( $path, [ref]$tokens, [ref]$errors )
# Get only function definition ASTs
$functionDefinitions = $ast.FindAll({
param([System.Management.Automation.Language.Ast] $Ast)
$Ast -is [System.Management.Automation.Language.FunctionDefinitionAst] -and
($PSVersionTable.PSVersion.Major -lt 5 -or
$Ast.Parent -isnot [System.Management.Automation.Language.FunctionMemberAst])
}, $true)
& {
"using System;"
"using System.Collections;"
"using System.Collections.Generic;"
"using System.Management.Automation;"
""
"/// Code Automatically Generated by {0}" -f $PSCmdlet.CommandRuntime
""
"namespace x.Powershell.Commands"
"{"
foreach ($Function in $FunctionDefinitions)
{
$FXVerb, $FXNoun = $function.Name -Split "-"
$FXVerbGroup = Get-Verb $FXVerb | % Group
"`t/// <summary>"
"`t/// {0}-{1} Cmdlet." -f $FXVerb, $FXNoun
"`t/// </summary>"
"`t[Cmdlet(Verbs{0}.{1}, `"{2}`")]" -f $FXVerbGroup, $FXVerb, $FXNoun
"`tpublic sealed class {0}{1} : PSCmdlet" -f $FXVerb, $FXNoun
"`t{"
"`t"
foreach ($Parameter in $Function.Body.ParamBlock.Parameters)
{
$ParameterName = $Parameter.Name.VariablePath.ToString()
"`t`t/// <summary>"
"`t`t/// Sets value for {0}" -f $ParameterName
"`t`t/// </summary>"
foreach ($Attribute in ($Parameter.Attributes | Select-Object -SkipLast 1))
{
$Param = $Attribute.NamedArguments | % {
$value = if ($_.ArgumentName -eq $_.Argument) { $_.ExpressionOmitted.ToString().ToLower() } else { $_.Argument }
"{0} = {1}" -f $_.ArgumentName, $Value
}
"`t`t[{0}({1})]" -f $Attribute.TypeName, ($Param -join ", ")
}
"`t`tpublic $($Parameter.Attributes[-1].TypeName) ${ParameterName} { get; set; }"
"`t`t"
}
$Blocks = [Ordered]@{
'BeginProcessing' = $Function.Body.BeginBlock.ToString()
'ProcessRecord' = $Function.Body.ProcessBlock.ToString()
'EndProcessing' = $Function.Body.EndBlock.ToString()
}
foreach ($Block in $Blocks.GetEnumerator())
{
"`t`t/// <summary>"
"`t`t/// {0}" -f $Block.Key
"`t`t/// </summary>"
"`t`tprotected override void {0}()" -f $Block.Key
"`t`t{"
"`t`t`t/// Code is not Generated Automatically you still need to program"
"`t`t`t/*"
$Block.Value -split "`n" | Select-Object -Skip 2 | Select-Object -SkipLast 1 | % {
"`t`t`t`t{0}" -f $_
}
"`t`t`t*/"
"`t`t}"
""
}
}
"`t}"
"}"
} | Out-File -FilePath $OutFile
}
end
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment