Skip to content

Commit cbf20f5

Browse files
author
James Brundage
committed
Adding Inline Python Transpiler (#63)
1 parent fedf657 commit cbf20f5

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<#
2+
.SYNOPSIS
3+
Python Inline PipeScript Transpiler.
4+
.DESCRIPTION
5+
Transpiles Python with Inline PipeScript into Python.
6+
7+
Because Python does not support multiline comment blocks, PipeScript can be written inline inside of multiline string
8+
9+
PipeScript can be included in a Python string that starts and ends with ```{}```, for example ```"""{}"""```
10+
.Example
11+
.> {
12+
$pythonContent = @'
13+
"""{
14+
$msg = "Hello World", "Hey There", "Howdy" | Get-Random
15+
@"
16+
print("$msg")
17+
"@
18+
}"""
19+
'@
20+
[OutputFile('.\HelloWorld.ps1.py')]$PythonContent
21+
}
22+
23+
.> .\HelloWorld.ps1.py
24+
#>
25+
[ValidateScript({
26+
$cmdInfo = $_
27+
if ($cmdInfo.Source -match '\.py$') {
28+
return $true
29+
}
30+
return $false
31+
})]
32+
param(
33+
# The command information. This will include the path to the file.
34+
[Parameter(Mandatory,ValueFromPipeline)]
35+
$CommandInfo
36+
)
37+
38+
begin {
39+
# We start off by declaring a number of regular expressions:
40+
41+
$startComment = '(?>"""\{)'
42+
$endComment = '(?>\}""")'
43+
44+
$startRegex = "(?<PSStart>${startComment})"
45+
$endRegex = "(?<PSEnd>${endComment})"
46+
47+
$sourcePattern = [Regex]::New("(?>$(
48+
$startRegex, $endRegex -join ([Environment]::NewLine + '|' + [Environment]::NewLine)
49+
))", "IgnoreCase, IgnorePatternWhitespace", "00:00:05")
50+
}
51+
52+
process {
53+
54+
$fileInfo = $commandInfo.Source -as [IO.FileInfo]
55+
$fileText = [IO.File]::ReadAllText($fileInfo.Fullname)
56+
57+
.>PipeScript.Inline -SourceFile $CommandInfo.Source -SourceText $fileText -SourcePattern $sourcePattern
58+
}

0 commit comments

Comments
 (0)