Skip to content

Commit 72fb93d

Browse files
author
James Brundage
committed
Adding Support for PipeScript in Bash (Fixes #194)
1 parent 25c1e98 commit 72fb93d

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<#
2+
.SYNOPSIS
3+
Bash PipeScript Transpiler.
4+
.DESCRIPTION
5+
Transpiles Bash with Inline PipeScript into Bash.
6+
7+
Heredocs named PipeScript{} will be treated as blocks of PipeScript.
8+
9+
```bash
10+
<<PipeScript{}
11+
12+
# This will be considered PipeScript / PowerShell, and will return the contents of a bash script.
13+
14+
PipeScript{}
15+
```
16+
.EXAMPLE
17+
Invoke-PipeScript {
18+
$bashScript = @'
19+
echo 'hello world'
20+
21+
<<PipeScript{}
22+
"echo '$('hi','yo','sup' | Get-Random)'"
23+
PipeScript{}
24+
'@
25+
26+
[OutputFile('.\HelloWorld.ps1.sh')]$bashScript
27+
}
28+
29+
Invoke-PipeScript .\HelloWorld.ps1.sh
30+
#>
31+
[ValidateScript({
32+
$cmdInfo = $_
33+
if ($cmdInfo.Source -match '\.sh$') {
34+
return $true
35+
}
36+
return $false
37+
})]
38+
param(
39+
# The command information. This will include the path to the file.
40+
[Parameter(Mandatory,ValueFromPipeline)]
41+
[Management.Automation.CommandInfo]
42+
$CommandInfo,
43+
44+
# A dictionary of parameters.
45+
[Collections.IDictionary]
46+
$Parameter,
47+
48+
# A list of arguments.
49+
[PSObject[]]
50+
$ArgumentList
51+
)
52+
53+
begin {
54+
# We start off by declaring a number of regular expressions:
55+
$startComment = '(?>\<\<PipeScript\{\})'
56+
$endComment = '(?>PipeScript\{\})'
57+
# * StartRegex ```$StartComment + '{' + $Whitespace```
58+
$startRegex = "(?<PSStart>${startComment})"
59+
# * EndRegex ```$whitespace + '}' + $EndComment```
60+
$endRegex = "(?<PSEnd>${endComment})"
61+
62+
# Create a splat containing arguments to the core inline transpiler
63+
$Splat = [Ordered]@{
64+
StartPattern = $startRegex
65+
EndPattern = $endRegex
66+
}
67+
}
68+
69+
process {
70+
# Add parameters related to the file
71+
$Splat.SourceFile = $commandInfo.Source -as [IO.FileInfo]
72+
$Splat.SourceText = [IO.File]::ReadAllText($commandInfo.Source)
73+
if ($Parameter) { $splat.Parameter = $Parameter }
74+
if ($ArgumentList) { $splat.ArgumentList = $ArgumentList }
75+
76+
# Call the core inline transpiler.
77+
.>PipeScript.Inline @Splat
78+
}

0 commit comments

Comments
 (0)