1+ <#
2+ . SYNOPSIS
3+ JSON PipeScript Transpiler.
4+ . DESCRIPTION
5+ Transpiles JSON with Inline PipeScript into JSON.
6+
7+ Multiline comments blocks like ```/*{}*/``` 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+ * ```null```
12+ * ```""```
13+ * ```{}```
14+ * ```[]```
15+ #>
16+ [ValidateScript ({
17+ $cmdInfo = $_
18+ if ($cmdInfo.Source -match ' \.json$' ) {
19+ return $true
20+ }
21+ return $false
22+ })]
23+ param (
24+ # The command information. This will include the path to the file.
25+ [Parameter (Mandatory , ValueFromPipeline )]
26+ $CommandInfo
27+ )
28+
29+ begin {
30+ # We start off by declaring a number of regular expressions:
31+ $startComment = ' /\*' # * Start Comments ```\*```
32+ $endComment = ' \*/' # * End Comments ```/*```
33+ $Whitespace = ' [\s\n\r]{0,}'
34+ # * IgnoredContext ```String.empty```, ```null```, blank strings and characters
35+ $IgnoredContext = " (?<ignore>(?>$ ( " null" , ' ""' , " \{\}" , " \[\]" -join ' |' ) )\s{0,}){0,1}"
36+ # * StartRegex ```$IgnoredContext + $StartComment + '{' + $Whitespace```
37+ $startRegex = " (?<PSStart>${IgnoredContext}${startComment} \{$Whitespace )"
38+ # * EndRegex ```$whitespace + '}' + $EndComment + $ignoredContext```
39+ $endRegex = " (?<PSEnd>$Whitespace \}${endComment} \s{0,}${IgnoredContext} )"
40+
41+ $sourcePattern = [Regex ]::New(" (?>$ (
42+ $startRegex , $endRegex -join ([Environment ]::NewLine + ' |' + [Environment ]::NewLine)
43+ ) )" , " IgnoreCase, IgnorePatternWhitespace" , " 00:00:05" )
44+ }
45+
46+ process {
47+
48+ $fileInfo = $commandInfo.Source -as [IO.FileInfo ]
49+ $fileText = [IO.File ]::ReadAllText($fileInfo.Fullname )
50+
51+ .> PipeScript.Inline - SourceFile $CommandInfo.Source - SourceText $fileText - SourcePattern $sourcePattern
52+ }
0 commit comments