|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + OpenSCAD Inline PipeScript Transpiler. |
| 4 | +.DESCRIPTION |
| 5 | + Transpiles OpenSCAD with Inline PipeScript into OpenSCAD. |
| 6 | +
|
| 7 | + Multiline comments with /*{}*/ will be treated as blocks of PipeScript. |
| 8 | +
|
| 9 | + Multiline comments can be preceeded or followed by 'empty' syntax, which will be ignored. |
| 10 | +
|
| 11 | + This for Inline PipeScript to be used with operators, and still be valid OpenSCAD syntax. |
| 12 | +
|
| 13 | + The OpenSCAD Inline Transpiler will consider the following syntax to be empty: |
| 14 | + |
| 15 | + * ```"[^"]+"``` |
| 16 | + * ```[\d\.]+``` |
| 17 | +.EXAMPLE |
| 18 | + .> { |
| 19 | + $OpenScadWithInlinePipeScript = @' |
| 20 | +Shape = "cube" /*{'"cube"', '"sphere"', '"circle"' | Get-Random}*/; |
| 21 | +Size = 1 /*{Get-Random -Min 1 -Max 100}*/ ; |
| 22 | +
|
| 23 | +if (Shape == "cube") { |
| 24 | + cube(Size); |
| 25 | +} |
| 26 | +if (Shape == "sphere") { |
| 27 | + sphere(Size); |
| 28 | +} |
| 29 | +if (Shape == "circle") { |
| 30 | + circle(Size); |
| 31 | +} |
| 32 | +'@ |
| 33 | +
|
| 34 | + [OutputFile(".\RandomShapeAndSize.ps1.scad")]$OpenScadWithInlinePipeScript |
| 35 | + } |
| 36 | + |
| 37 | + .> .\RandomShapeAndSize.ps1.scad |
| 38 | +#> |
| 39 | +[ValidateScript({ |
| 40 | + $cmdInfo = $_ |
| 41 | + if ($cmdInfo.Source -match '\.scad$') { |
| 42 | + return $true |
| 43 | + } |
| 44 | + return $false |
| 45 | +})] |
| 46 | +param( |
| 47 | +# The command information. This will include the path to the file. |
| 48 | +[Parameter(Mandatory,ValueFromPipeline)] |
| 49 | +$CommandInfo |
| 50 | +) |
| 51 | + |
| 52 | +begin { |
| 53 | + # We start off by declaring a number of regular expressions: |
| 54 | + $startComment = '/\*' # * Start Comments ```\*``` |
| 55 | + $endComment = '\*/' # * End Comments ```/*``` |
| 56 | + $Whitespace = '[\s\n\r]{0,}' |
| 57 | + # * IgnoredContext ```String.empty```, ```null```, blank strings and characters |
| 58 | + $IgnoredContext = "(?<ignore>(?>$('[\d\.]+','"[^"]+"' -join '|'))\s{0,}){0,1}" |
| 59 | + # * StartRegex ```$IgnoredContext + $StartComment + '{' + $Whitespace``` |
| 60 | + $startRegex = "(?<PSStart>${IgnoredContext}${startComment}\{$Whitespace)" |
| 61 | + # * EndRegex ```$whitespace + '}' + $EndComment + $ignoredContext``` |
| 62 | + $endRegex = "(?<PSEnd>$Whitespace\}${endComment}\s{0,}${IgnoredContext})" |
| 63 | + |
| 64 | + $sourcePattern = [Regex]::New("(?>$( |
| 65 | + $startRegex, $endRegex -join ([Environment]::NewLine + '|' + [Environment]::NewLine) |
| 66 | + ))", "IgnoreCase, IgnorePatternWhitespace", "00:00:05") |
| 67 | +} |
| 68 | + |
| 69 | +process { |
| 70 | + |
| 71 | + $fileInfo = $commandInfo.Source -as [IO.FileInfo] |
| 72 | + $fileText = [IO.File]::ReadAllText($fileInfo.Fullname) |
| 73 | + |
| 74 | + .>PipeScript.Inline -SourceFile $CommandInfo.Source -SourceText $fileText -SourcePattern $sourcePattern |
| 75 | +} |
| 76 | + |
0 commit comments