Skip to content

Commit d3fbc2c

Browse files
Merge pull request #62 from StartAutomating/PipeScriptUpdates
Pipe script updates
2 parents fd2854d + 6c9c7db commit d3fbc2c

File tree

11 files changed

+290
-2
lines changed

11 files changed

+290
-2
lines changed

.github/workflows/TestAndPublish.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,14 @@ jobs:
485485
}
486486
}
487487
} @Parameters
488+
BuildPipeScript:
489+
runs-on: ubuntu-latest
490+
if: ${{ success() }}
491+
steps:
492+
- name: Check out repository
493+
uses: actions/checkout@v2
494+
- name: BuildPipeScript
495+
uses: StartAutomating/PipeScript@main
488496
HelpOut:
489497
runs-on: ubuntu-latest
490498
if: ${{ success() }}

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## 0.0.3
2+
* Adding Inline Python Support (#63)
3+
* Adding [OutputFile] transpiler (#64)
4+
* Building PipeScript with PipeScript (#54)
5+
---
6+
17
## 0.0.2
28
* New Transpilers:
39
* .>ValidatePlatform (#58)

PipeScript.GitHubWorkflow.PSDevOps.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#requires -Module PSDevOps
2-
New-GitHubWorkflow -Name "Analyze, Test, Tag, and Publish" -On Push, PullRequest, Demand -Job PowerShellStaticAnalysis, TestPowerShellOnLinux, TagReleaseAndPublish, HelpOut, RunEZOut -Environment @{
2+
New-GitHubWorkflow -Name "Analyze, Test, Tag, and Publish" -On Push, PullRequest, Demand -Job PowerShellStaticAnalysis, TestPowerShellOnLinux, TagReleaseAndPublish, BuildPipeScript, HelpOut, RunEZOut -Environment @{
33
NoCoverage = $true
44
}|
55
Set-Content .\.github\workflows\TestAndPublish.yml -Encoding UTF8 -PassThru

PipeScript.psd1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717

1818
Tags = 'PipeScript','PowerShell', 'Transpilation', 'Compiler'
1919
ReleaseNotes = @'
20+
## 0.0.3
21+
* Adding Inline Python Support (#63)
22+
* Adding [OutputFile] transpiler (#64)
23+
* Building PipeScript with PipeScript (#54)
24+
---
25+
2026
## 0.0.2
2127
* New Transpilers:
2228
* .>ValidatePlatform (#58)
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+
}

Transpilers/Inline/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
This directory contains Inline PipeScript transpilers for several languages.
22

3-
PipeScript can currently be embedded in 15 languages.
3+
PipeScript can currently be embedded in 17 languages.
44

55
Transpilers in this directory should be named ```Inline.NameOfLanguage.psx.ps1```.
66
Each file should handle one and only one language (better explicit than terse).
@@ -19,7 +19,9 @@ Transpilers should call ```.>PipeScript.Inline``` to simplify and standarize pro
1919
|[JavaScript](Inline.JavaScript.psx.ps1)|[JavaScript Inline PipeScript Transpiler.](Inline.JavaScript.psx.ps1)|
2020
|[Json](Inline.Json.psx.ps1) |[JSON PipeScript Transpiler.](Inline.Json.psx.ps1) |
2121
|[Markdown](Inline.Markdown.psx.ps1) |[Markdown File Transpiler.](Inline.Markdown.psx.ps1) |
22+
|[ObjectiveC](Inline.ObjectiveC.psx.ps1)|[Objective C PipeScript Transpiler.](Inline.ObjectiveC.psx.ps1) |
2223
|[OpenSCAD](Inline.OpenSCAD.psx.ps1) |[OpenSCAD Inline PipeScript Transpiler.](Inline.OpenSCAD.psx.ps1) |
24+
|[Python](Inline.Python.psx.ps1) |[Python Inline PipeScript Transpiler.](Inline.Python.psx.ps1) |
2325
|[Ruby](Inline.Ruby.psx.ps1) |[Ruby Inline PipeScript Transpiler.](Inline.Ruby.psx.ps1) |
2426
|[Rust](Inline.Rust.psx.ps1) |[Rust Inline PipeScript Transpiler.](Inline.Rust.psx.ps1) |
2527
|[TOML](Inline.TOML.psx.ps1) |[TOML Inline PipeScript Transpiler.](Inline.TOML.psx.ps1) |

Transpilers/OutputFile.psx.ps1

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<#
2+
.SYNOPSIS
3+
Outputs to a File
4+
.DESCRIPTION
5+
Outputs the result of a script into a file.
6+
.EXAMPLE
7+
Invoke-PipeScript {
8+
[OutputFile("hello.txt")]
9+
param()
10+
11+
'hello world'
12+
}
13+
.Example
14+
Invoke-PipeScript {
15+
param()
16+
17+
$Message = 'hello world'
18+
[Save(".\Hello.txt")]$Message
19+
}
20+
#>
21+
[Alias('Save')]
22+
param(
23+
# The Output Path
24+
[Parameter(Mandatory)]
25+
[string]
26+
$OutputPath,
27+
28+
# The Script Block that will be run.
29+
[Parameter(ValueFromPipeline)]
30+
[scriptblock]
31+
$ScriptBlock,
32+
33+
[Parameter(ValueFromPipeline)]
34+
[Management.Automation.Language.VariableExpressionast]
35+
$VariableAst,
36+
37+
# The encoding parameter.
38+
[string]
39+
$Encoding,
40+
41+
# If set, will force output, overwriting existing files.
42+
[switch]
43+
$Force,
44+
45+
# The export script
46+
[scriptblock]
47+
$ExportScript,
48+
49+
# The serialization depth. Currently only used when saving to JSON files.
50+
[int]
51+
$Depth = 100
52+
)
53+
54+
begin {
55+
function SaveJson {
56+
# Determine if the content appears to already be JSON
57+
if ($jsonToSave -match '^[\s\r\n]{0,}[\[\{\`"/]') {
58+
$jsonToSave | Set-Content -Path '$safeOutputPath' $OtherParams
59+
} else {
60+
ConvertTo-JSON -InputObject `$jsonToSave -Depth $depth |
61+
Set-Content -Path '$safeOutputPath' $otherParams
62+
}
63+
}
64+
}
65+
66+
process {
67+
$safeOutputPath = $OutputPath.Replace("'","''")
68+
$otherParams = @(
69+
if ($Encoding) {
70+
"-Encoding '$($encoding.Replace("'", "''"))'"
71+
}
72+
if ($Force) {
73+
"-Force"
74+
}
75+
) -join ' '
76+
77+
$inputObjectScript =
78+
if ($VariableAst) {
79+
$VariableAst.Extent.ToString()
80+
} elseif ($ScriptBlock.Ast.ParamBlock.Parameters.Count)
81+
{
82+
"`$ParameterCopy = [Ordered]@{} + `$psBoundParameters; & { $ScriptBlock } @ParameterCopy"
83+
} else {
84+
"& { $ScriptBlock }"
85+
}
86+
87+
if ($OutputPath -match '\.json') {
88+
[ScriptBlock]::Create("
89+
`$jsonToSave = $inputObjectScript
90+
# Determine if the content appears to already be JSON
91+
if (`$jsonToSave -match '^[\s\r\n]{0,}[\[\{\`"/]') {
92+
`$jsonToSave | Set-Content -Path '$safeOutputPath' $OtherParams
93+
} else {
94+
ConvertTo-JSON -InputObject `$jsonToSave -Depth $depth | Set-Content -Path '$safeOutputPath' $otherParams
95+
}
96+
97+
")
98+
}
99+
elseif ($OutputPath -match '\.[c|t]sv$') {
100+
[ScriptBlock]::Create("$inputObjectScript | Export-Csv -Path '$safeOutputPath' $otherParams")
101+
}
102+
elseif ($OutputPath -match '\.(?>clixml|clix|xml)$') {
103+
[ScriptBlock]::Create("
104+
`$toSave = $inputObjectScript
105+
if (`$toSave -as [xml]) {
106+
`$strWrite = [IO.StringWriter]::new()
107+
`$configurationXml.Save(`$strWrite)
108+
(`"$strWrite`" -replace '^\<\?xml version=`"1.0`" encoding=`"utf-16`"\?\>') | Set-Content -Path '$safeOutputPath' $otherParams
109+
} else {
110+
`$toSave | Export-Clixml -Path '$safeOutputPath' $otherParams
111+
}
112+
")
113+
}
114+
else {
115+
[ScriptBlock]::Create("$inputObjectScript | Set-Content -Path '$safeOutputPath' $otherParams")
116+
}
117+
}

Transpilers/Parameters/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
This directory and it's subdirectories contain Transpilers that transform parameter attributes.
2+
3+
Parameter Transpilers do not need to take any values from the pipeline.
4+
They will be called by the Core PipeScript Transpiler ```PipeScript.ParameterAttribute```.
5+
6+
When Transpiling a Parameter, the Transpiler should return one of two things.
7+
8+
1. An empty ```[ScriptBlock]``` preceeded by attributes or help. This will replace the Transpiled attribute with a real one.
9+
2. A ```[Collections.IDictionary]``` can be used to send arguments directly back to ```Update-PipeScript```.
10+
11+
Many parameter transpilers can also apply to a ```[Management.Automation.Language.VariableExpressionAst]```.
12+
13+
When this is the case it is common for the transpiler to add a ```[ValidateScript]``` attribute to the variable. This will constraint the value of that variable.
14+
15+
## List Of Parameter Transpilers
16+
17+
18+
|DisplayName |Synopsis |
19+
|----------------------------------------------------|---------------------------------------------------------------------|
20+
|[ValidatePlatform](ValidatePlatform.psx.ps1) |[Validates the Platform](ValidatePlatform.psx.ps1) |
21+
|[ValidatePropertyName](ValidatePropertyName.psx.ps1)|[Validates Property Names](ValidatePropertyName.psx.ps1) |
22+
|[ValidateTypes](ValidateTypes.psx.ps1) |[Validates if an object is one or more types.](ValidateTypes.psx.ps1)|
23+
|[VBN](VBN.psx.ps1) |[ValueFromPipline Shorthand](VBN.psx.ps1) |
24+
|[VFP](VFP.psx.ps1) |[ValueFromPipline Shorthand](VFP.psx.ps1) |
25+
26+
27+
28+
29+
30+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
This directory and it's subdirectories contain Transpilers that transform parameter attributes.
2+
3+
Parameter Transpilers do not need to take any values from the pipeline.
4+
They will be called by the Core PipeScript Transpiler ```PipeScript.ParameterAttribute```.
5+
6+
When Transpiling a Parameter, the Transpiler should return one of two things.
7+
8+
1. An empty ```[ScriptBlock]``` preceeded by attributes or help. This will replace the Transpiled attribute with a real one.
9+
2. A ```[Collections.IDictionary]``` can be used to send arguments directly back to ```Update-PipeScript```.
10+
11+
Many parameter transpilers can also apply to a ```[Management.Automation.Language.VariableExpressionAst]```.
12+
13+
When this is the case it is common for the transpiler to add a ```[ValidateScript]``` attribute to the variable. This will constraint the value of that variable.
14+
15+
## List Of Parameter Transpilers
16+
17+
~~~PipeScript{
18+
[PSCustomObject]@{
19+
Table = Get-Transpiler -TranspilerPath $pwd |
20+
Select-Object DisplayName, @{
21+
Name='Synopsis'
22+
Expression= { $_.Synopsis -replace '[\s\r\n]+$' }
23+
}, @{
24+
Name='Link'
25+
Expression = { $_.Name }
26+
}
27+
}
28+
}
29+
~~~
30+
31+
32+

Transpilers/Wrappers/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Files in this directory and it's subdirectories generate wrappers for PipeScript and PowerShell.
2+
3+
These wrappers allow PipeScript or PowerShell to be called from other programming languages.
4+
5+
6+
|DisplayName |Synopsis |
7+
|------------------------------------------|---------------------------------------------------------------------|
8+
|[Bash](Bash.psx.ps1) |[Wraps PowerShell in a Bash Script](Bash.psx.ps1) |
9+
|[Batch](Batch.psx.ps1) |[Wraps PowerShell in a Windows Batch Script](Batch.psx.ps1) |
10+
|[BatchPowerShell](BatchPowerShell.psx.ps1)|[Wraps PowerShell in a Windows Batch Script](BatchPowerShell.psx.ps1)|
11+
12+

0 commit comments

Comments
 (0)