forked from ironmansoftware/code-conversion
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeConversion.psm1
More file actions
91 lines (75 loc) · 2.1 KB
/
CodeConversion.psm1
File metadata and controls
91 lines (75 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
function Invoke-CSharpConversion
{
[OutputType(ParameterSetName = 'Code', [string])]
[OutputType(ParameterSetName = 'FileSystem', [void])]
[CmdletBinding(DefaultParameterSetName = 'Code')]
param
(
[Parameter(ParameterSetName = 'Code', Position = 0, Mandatory)]
[Alias('Code')]
[string]
$InputCode,
[Parameter(ParameterSetName = 'FileSystem', Position = 0, Mandatory)]
[Alias('InFile')]
[string]
$InputFile,
[Parameter(Position = 1, ParameterSetName = 'Code')]
[Parameter(Position = 1, ParameterSetName = 'FileSystem')]
[Alias('OutFile')]
[string]
$OutputFile,
[Parameter(Position = 2)]
[ValidateSet('Function', 'Type')]
[string]
$As
)
begin
{
$sep = '-' * 80
$parser = [CodeConversion.CSharpSyntaxTreeVisitor]::new()
if ($PSBoundParameters.ContainsKey('As') -and $As -eq 'Type')
{
$writer = [CodeConversion.PowerShell5CodeWriter]::new()
}
else
{
$writer = [CodeConversion.PowerShellCodeWriter]::new()
}
}
process
{
if ($PSBoundParameters.ContainsKey('InputFile'))
{
$InputCode = Get-Content -Path $InputFile -Encoding UTF8 -Raw
}
Write-Verbose "C# Input:`n$sep`n$InputCode`n$sep`n"
try
{
$ast = $parser.Visit($InputCode)
}
catch
{
$PSCmdlet.ThrowTerminatingError($_)
}
try
{
$output = $writer.Write($ast)
}
catch
{
$PSCmdlet.ThrowTerminatingError($_)
}
if ($PSBoundParameters.ContainsKey('OutputFile'))
{
Set-Content -Path $OutputFile -Value $output -Encoding UTF8 -Force
}
else
{
$output
}
Write-Verbose "PowerShell Output:`n$sep`n$output`n$sep"
}
end
{
}
}