@@ -109,84 +109,25 @@ function Get-ExamplesDetailsFromMd {
109
109
if ($description -ne " " ) {
110
110
$exampleDescriptions += $description
111
111
}
112
-
113
- # If there is no ```output``` split codelines and outputlines
114
- if ($exampleOutputBlocks.Count -eq 0 ) {
115
- foreach ($exampleCodeBlock in $exampleCodeBlocks ) {
116
- $codeRegex = " (\n|\r\n)(" +
117
- " (.*ForEach-Object {((.*(\n|\r\n))|(\w*=.*))*\s*})|" +
118
- " ((.*[A-Za-z]\w+-[A-Za-z]\w+\s*(`` (\n|\r\n))?)((\s*-.*`` (\n|\r\n))*(.*@{((.*(\n|\r\n))|(\w*=.*))*\s*}\s*`` ?)+)+(?=\n|\r\n))|" +
119
- " ((([A-Za-z \t])*(PS|[A-Za-z]:)(\w|[\\/\[\].\- ])*(>|>)+( PS)*)*[ \t]*((([A-Za-z]\w+-[A-Za-z]\w+\b(.ps1)?(?!(-| +\w)))|(" +
120
- " (@?\((?>\((?<pair>)|[^\(\)]+|\)(?<-pair>))*(?(pair)(?!))\) *[|.-] *\w)|" + # match ()
121
- " (\[(?>\[(?<pair>)|[^\[\]]+|\](?<-pair>))*(?(pair)(?!))\]\$)|" + # match []
122
- " ((\$\w*\s*=.*)?@{(?>{(?<pair>)|[^{}]+|}(?<-pair>))*(?(pair)(?!))})|" + # match @{}
123
- " ('(?>'(?<pair>)|[^']+|'(?<-pair>))*(?(pair)(?!))' *[|.-] *\w)|" + # match ''
124
- " ((?<!`` )`" (?>(?<!`` )`" (?<pair>)|[\s\S]|(?<!`` )`" (?<-pair>))*(?(pair)(?!))(?<!`` )`" *[|.-] *\w)|" + # match ""
125
- " (\$\w*\s*=.*)?(@`" \s*(\n|\r\n)?(\{\s*)?)(.*(\n|\r\n))*((\s*\})?\s*(\n|\r\n)?`" @)|" + # match @" "@
126
- " \$))(?!\.)([\w-~`` '`" $= \t:;<>@()\[\]{},.+*/|\\&!?%#]*[`` |][ \t]*(\n|\r\n)?)*([\w-~`` '`" $= \t:;<>@()\[\]{},.+*/|\\&!?%#]*(?=\n|\r\n|#))))" +
127
- " )"
128
- $exampleCodeLines = ($exampleCodeBlock.Value | Select-String - Pattern $codeRegex - CaseSensitive - AllMatches).Matches
129
- if ($exampleCodeLines.Count -eq 0 ) {
130
- $exampleCodes = @ ()
131
- $exampleOutputs = @ ()
132
- }
133
- else {
134
- for ($i = 0 ; $i -lt $exampleCodeLines.Count ; $i ++ ) {
135
- # If a codeline contains " :", it's not a codeline but an output line of "Format-List".
136
- if ($exampleCodeLines [$i ].Value -notmatch " : *\w" ) {
137
- $exampleCodes += $exampleCodeLines [$i ].Value.Trim()
138
- # Content before the first codeline, between codelines, and after the last codeline is output.
139
- if ($i -eq 0 ) {
140
- $startIndex = $exampleCodeBlock.Value.IndexOf (" `n " )
141
- $output = $exampleCodeBlock.Value.Substring ($startIndex , $exampleCodeLines [$i ].Index - $startIndex ).Trim()
142
- if ($output -ne " " -and $output.Trim () -notlike " #*" ) {
143
- $exampleOutputs += $output
144
- }
145
- }
146
- $startIndex = $exampleCodeLines [$i ].Index + $exampleCodeLines [$i ].Length
147
- if ($i -lt $exampleCodeLines.Count - 1 ) {
148
- $nextStartIndex = $exampleCodeLines [$i + 1 ].Index
149
- }
150
- else {
151
- $nextStartIndex = $exampleCodeBlock.Value.LastIndexOf (" `n " )
152
- }
153
- # If an output line starts with "-", it's an incomplete codeline, but it should still be added to output.
154
- $output = $exampleCodeBlock.Value.Substring ($startIndex , $nextStartIndex - $startIndex ).Trim()
155
- if ($output -match " ^-+\w" ) {
156
- $exampleOutputs += $output
157
- }
158
- elseif ($output -ne " " -and $output.Trim () -notlike " #*" ) {
159
- $exampleOutputs += $output
160
- }
161
- }
162
- }
163
- }
112
+ # Extract code from the first "\n" to the last "\n"
113
+ foreach ($exampleCodeBlock in $exampleCodeBlocks ) {
114
+ $code = $exampleCodeBlock.Value.Substring ($exampleCodeBlock.Value.IndexOf (" `n " ), $exampleCodeBlock.Value.LastIndexOf (" `n " ) - $exampleCodeBlock.Value.IndexOf (" `n " ))
115
+ if ($code -ne " " -and $code -notmatch " {{ Add code here }}" ) {
116
+ $exampleCodes += $code
164
117
}
165
118
}
166
- # If there is ```output```
167
- else {
168
- # Extract code from the first "\n" to the last "\n"
169
- foreach ($exampleCodeBlock in $exampleCodeBlocks ) {
170
- $code = $exampleCodeBlock.Value.Substring ($exampleCodeBlock.Value.IndexOf (" `n " ), $exampleCodeBlock.Value.LastIndexOf (" `n " ) - $exampleCodeBlock.Value.IndexOf (" `n " )).Trim()
171
- if ($code -ne " " -and $code -notmatch " {{ Add code here }}" ) {
172
- $exampleCodes += $code
173
- }
174
- }
175
- # Extract output from the first "\n" to the last "\n"
176
- foreach ($exampleOutputBlock in $exampleOutputBlocks ) {
177
- $output = $exampleOutputBlock.Value.Substring ($exampleOutputBlock.Value.IndexOf (" `n " ), $exampleOutputBlock.Value.LastIndexOf (" `n " ) - $exampleOutputBlock.Value.IndexOf (" `n " )).Trim()
178
- if ($output -ne " " ) {
179
- $exampleOutputs += $output
180
- }
119
+ # Extract output from the first "\n" to the last "\n"
120
+ foreach ($exampleOutputBlock in $exampleOutputBlocks ) {
121
+ $output = $exampleOutputBlock.Value.Substring ($exampleOutputBlock.Value.IndexOf (" `n " ), $exampleOutputBlock.Value.LastIndexOf (" `n " ) - $exampleOutputBlock.Value.IndexOf (" `n " )).Trim()
122
+ if ($output -ne " " ) {
123
+ $exampleOutputs += $output
181
124
}
182
125
}
183
-
184
126
# From the end of the last codeblock to the end is example description.
185
127
if ($null -ne $exampleOutputBlocks ){
186
128
$description = $exampleContent.SubString ($exampleOutputBlocks [-1 ].Index + $exampleOutputBlocks [-1 ].Length).Trim()
187
129
}
188
130
else {
189
-
190
131
$description = $exampleContent.SubString ($exampleCodeBlocks [-1 ].Index + $exampleCodeBlocks [-1 ].Length).Trim()
191
132
}
192
133
if ($description -ne " " ) {
@@ -198,7 +139,6 @@ function Get-ExamplesDetailsFromMd {
198
139
Num = $exampleNumber + 1
199
140
Title = $exampleTitle
200
141
Codes = $exampleCodes
201
- CodeBlocks = $exampleCodeBlocks
202
142
Outputs = $exampleOutputs
203
143
OutputBlocks = $exampleOutputBlocks
204
144
Description = ([string ]$exampleDescriptions ).Trim()
@@ -350,12 +290,13 @@ function Measure-SectionMissingAndOutputScript {
350
290
else {
351
291
foreach ($exampleDetails in $examplesDetails ) {
352
292
$exampleNumber = $exampleDetails.Num
293
+ $exampleCodes = $exampleDetails.Codes
353
294
$_missingExampleTitle = ($exampleDetails.Title | Select-String - Pattern " {{[A-Za-z ]*}}" ).Count
354
295
$_missingExampleCode = ($exampleDetails.Codes | Select-String - Pattern " {{[A-Za-z ]*}}" ).Count
355
296
$_missingExampleOutput = ($exampleDetails.Outputs | Select-String - Pattern " {{[A-Za-z ]*}}" ).Count
356
297
$_missingExampleDescription = ($exampleDetails.Description | Select-String - Pattern " {{[A-Za-z ]*}}" ).Count
357
- $_needDeleting = ($exampleDetails.CodeBlocks | Select-String - Pattern " \ n([A-Za-z \t\\:>])*(PS|[A-Za-z]:)(\w|[\\/\[\].\- ])*(>|>)+( PS)*[ \t]*" - CaseSensitive).Count +
358
- ($exampleDetails.CodeBlocks | Select-String - Pattern " (?<=[A-Za-z]\w+-[A-Za-z]\w+)\.ps1" - CaseSensitive).Count
298
+ $_needDeleting = ($exampleDetails.Codes | Select-String - Pattern " ` n ([A-Za-z \t\\:>])*(PS|[A-Za-z]:)(\w|[\\/\[\].\- ])*(>|>)+( PS)*[ \t]*" - CaseSensitive).Count +
299
+ ($exampleDetails.Codes | Select-String - Pattern " (?<=[A-Za-z]\w+-[A-Za-z]\w+)\.ps1" - CaseSensitive).Count
359
300
switch ($exampleDetails ) {
360
301
{$exampleDetails.Title -eq " " -or $_missingExampleTitle -ne 0 } {
361
302
$missingExampleTitle ++
@@ -402,21 +343,6 @@ function Measure-SectionMissingAndOutputScript {
402
343
}
403
344
$results += $result
404
345
}
405
- {$exampleDetails.OutputBlocks.Count -eq 0 -and $exampleDetails.Outputs.Count -ne 0 } {
406
- $needSplitting ++
407
- $result = [AnalysisOutput ]@ {
408
- Module = $Module
409
- Cmdlet = $Cmdlet
410
- Example = $exampleDetails.Num
411
- Description = " The output need to be split from example."
412
- RuleName = " NeedSplitting"
413
- Severity = $missingSeverity
414
- Extent = " $Module \help\$Cmdlet .md"
415
- ProblemID = 5051
416
- Remediation = " Split output from example."
417
- }
418
- $results += $result
419
- }
420
346
{$exampleDetails.Description -eq " " -or $_missingExampleDescription -ne 0 } {
421
347
$missingExampleDescription ++
422
348
$result = [AnalysisOutput ]@ {
@@ -442,30 +368,28 @@ function Measure-SectionMissingAndOutputScript {
442
368
RuleName = " NeedDeleting"
443
369
Severity = $missingSeverity
444
370
Extent = " $Module \help\$Cmdlet .md"
445
- ProblemID = 5052
371
+ ProblemID = 5051
446
372
Remediation = " Delete the prompt of example."
447
373
}
448
374
$results += $result
375
+ $newCode = $exampleCodes -replace " `n ([A-Za-z \t\\:>])*(PS|[A-Za-z]:)(\w|[\\/\[\].\- ])*(>|>)+( PS)*[ \t]*" , " `n "
376
+ $newCode = $newCode -replace " (?<=[A-Za-z]\w+-[A-Za-z]\w+)\.ps1"
377
+ $exampleCodes = $newCode
449
378
}
450
379
}
451
380
452
- # Delete prompts
453
- $exampleCodes = $exampleDetails.Codes
454
- for ($i = $exampleCodes.Count - 1 ; $i -ge 0 ; $i -- ) {
455
- $newCode = $exampleDetails.Codes [$i ] -replace " ([A-Za-z \t\\:>])*(PS|[A-Za-z]:)(\w|[\\/\[\].\- ])*(>|>)+( PS)*[ \t]*" , " "
456
- $newCode = $newCode -replace " (?<=[A-Za-z]\w+-[A-Za-z]\w+)\.ps1" , " "
457
- $exampleCodes [$i ] = $newCode
458
- }
459
381
460
382
# Output example codes to "TempScript.ps1"
461
383
if ($OutputScriptsInFile.IsPresent ) {
462
384
$cmdletExamplesScriptPath = " $OutputFolder \TempScript.ps1"
463
- $functionHead = " function $Module -$Cmdlet -$exampleNumber {"
464
- Add-Content - Path (Get-Item $cmdletExamplesScriptPath ).FullName - Value $functionHead
465
- $exampleCodes = $exampleCodes -join " `n "
466
- Add-Content - Path (Get-Item $cmdletExamplesScriptPath ).FullName - Value $exampleCodes
467
- $functionTail = " }`n "
468
- Add-Content - Path (Get-Item $cmdletExamplesScriptPath ).FullName - Value $functionTail
385
+ if ($exampleCodes -ne $null ){
386
+ $exampleCodes = $exampleCodes.Trim ()
387
+ $functionHead = " function $Module -$Cmdlet -$exampleNumber {"
388
+ Add-Content - Path (Get-Item $cmdletExamplesScriptPath ).FullName - Value $functionHead
389
+ Add-Content - Path (Get-Item $cmdletExamplesScriptPath ).FullName - Value $exampleCodes
390
+ $functionTail = " }`n "
391
+ Add-Content - Path (Get-Item $cmdletExamplesScriptPath ).FullName - Value $functionTail
392
+ }
469
393
}
470
394
}
471
395
}
0 commit comments