|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + `Turtle.js` definition |
| 4 | +.DESCRIPTION |
| 5 | + Our JavaScipt turtle is actually contained in a PowerShell object first. |
| 6 | +
|
| 7 | + This object has a number of properties ending with `.js`. |
| 8 | + |
| 9 | + These are portions of the class. |
| 10 | +
|
| 11 | + To create our class, we simply join these properties together, and output a javascript object. |
| 12 | +#> |
| 13 | +param() |
| 14 | + |
| 15 | +$objectParts = |
| 16 | + |
| 17 | +foreach ($javaScriptProperty in $this.psobject.properties | Sort-Object Name) { |
| 18 | + # We only want the .js properties |
| 19 | + if ($javaScriptProperty.Name -notmatch '\.js$') { continue } |
| 20 | + # If the property is a function, we need to handle it differently |
| 21 | + if ($javaScriptProperty.value -match '^function.+?\(') { |
| 22 | + # specificically, we need to remove the "function " prefix from the name |
| 23 | + $predicate, $extra = $javaScriptProperty.value -split '\(', 2 |
| 24 | + # and then we need to reassemble it as a javascript method |
| 25 | + $functionName = $predicate -replace 'function\s{1,}' |
| 26 | + if ($functionName -match '^[gs]et_') { |
| 27 | + $getSet = $functionName -replace '_.+$' |
| 28 | + $propertyName = $functionName -replace '^[gs]et_' |
| 29 | + if ($getSet -eq 'get') { |
| 30 | + $extra = $extra -replace '^.{0,}\)\s{0,}' |
| 31 | + "$getSet ${propertyName}() $($extra)" |
| 32 | + } else { |
| 33 | + "$getSet ${propertyName}($($extra)" |
| 34 | + } |
| 35 | + |
| 36 | + } else { |
| 37 | + "${functionName}:function ($extra" |
| 38 | + } |
| 39 | + |
| 40 | + } |
| 41 | + else { |
| 42 | + # Otherwise, include it inline. |
| 43 | + $javaScriptProperty.value |
| 44 | + } |
| 45 | + |
| 46 | +} |
| 47 | +# Since we are building a javascript object, we need to wrap everything in curly braces |
| 48 | +@("{ |
| 49 | +" |
| 50 | +# Indentation does not matter to most machines, but people tend to appreciate it. |
| 51 | +" " |
| 52 | +($objectParts -join (',' + [Environment]::Newline + ' ')) |
| 53 | +"}") -join '' |
0 commit comments