22# based on https://gist.github.com/Splaxi/ff7485a24f6ed9937f3e8da76b5d4840
33# See also https://github.com/fscpscollaborative/fscps.tools/wiki/Building-tools
44$path = " $PSScriptRoot \..\fscps.tools"
5-
5+ # Path to the .psd1 file
6+ $psd1Path = " $path \fscps.tools.psd1"
7+ $psd1Data = Import-PowerShellDataFile - Path $psd1Path
8+ # Extract the Author field
9+ $author = $psd1Data.Author
610function Get-Header ($text ) {
711 $start = $text.IndexOf (' <#' )
812 $temp = $start - 2
@@ -13,25 +17,76 @@ function Get-Header ($text) {
1317 " "
1418 }
1519}
16-
17- function Format-Help ( $text ) {
20+ function Format-Help ( $text ) {
21+
1822 $start = $text.IndexOf (' <#' )
1923 $end = $text.IndexOf (' #>' )
2024 $help = $text.SubString ($start + 2 , $end - $start - 3 )
2125
22- $skipfirst = $null # to avoid trailing spaces
26+ # $skipfirst = $null # to avoid trailing spaces
27+ $formattedHelp = @ () # Collect formatted lines
28+ $indentLevel = 0 # Track indentation level
29+
30+
2331 foreach ($newline in $help.Split (" `n " )) {
24- if (-not $skipfirst ) { $skipfirst = $true ; continue }
32+ # if (-not $skipfirst) { $skipfirst = $true; continue }
2533 $trimmed = $newline.Trim ()
26- foreach ($line in $trimmed ) {
27- if ($line.StartsWith (" ." )) {
28- " $line "
29- }
30- else {
31- " $line "
32- }
34+
35+ # Adjust indentation based on closing braces
36+ if ($trimmed -match ' ^\}' ) { $indentLevel = [math ]::Max(0 , $indentLevel - 1 ) }
37+
38+ if ($trimmed.StartsWith (" ." ))
39+ {
40+ $trimmed = " $ ( $trimmed.Replace (" " , ' ' )) "
41+ }
42+ elseif ($trimmed.StartsWith (" -" )) {
43+ $trimmed = " $ ( $trimmed.Trim ()) "
44+ }
45+ elseif ($trimmed.StartsWith (" @{" )) {
46+ $trimmed = " $ ( $trimmed.Trim ()) "
47+ }
48+ else {
49+ $trimmed = " $trimmed "
50+ }
51+
52+ # Apply indentation
53+ $indentedLine = (' ' * ($indentLevel * 4 )) + $trimmed
54+
55+ $formattedHelp += $indentedLine
56+
57+ # Adjust indentation based on opening braces
58+ if ($trimmed -match ' \{$' ) { $indentLevel ++ }
59+ }
60+ $cnt = 0
61+ $lines = $formattedHelp.Split (" `n " )
62+
63+ foreach ($line in $lines ) {
64+ if ($cnt -eq 0 -and (($line -match ' ^[\s]*$' ))) {
65+ $cnt ++
66+ continue
67+ }
68+
69+ # Check for .NOTES node
70+ if ($line -match ' \.NOTES' ) {
71+ $foundNotes = $true
3372 }
73+
74+ # Check if the author is already present
75+ if ($line -match [regex ]::Escape($author )) {
76+ $foundAuthor = $true
77+ }
78+
79+ # If it's the last line and .NOTES exists but no author, add the author
80+ if ($cnt -eq $lines.Length - 1 -and $foundNotes -and -not $foundAuthor ) {
81+ $line
82+ Write-Host " Adding author to .NOTES section."
83+ $line = " Author: $author "
84+ }
85+
86+ $line
87+ $cnt ++
3488 }
89+
3590}
3691
3792function Get-Body ($text ) {
@@ -40,9 +95,9 @@ function Get-Body ($text) {
4095}
4196
4297$files = New-Object System.Collections.ArrayList
43- $filesPublic = Get-ChildItem - Path " $path \functions\*.ps1"
98+ $filesPublic = @ ( Get-ChildItem - Path " $path \functions\*.ps1" )
4499$files.AddRange ($filesPublic )
45- $filesInternal = Get-ChildItem - Path " $path \internal\functions\*.ps1"
100+ $filesInternal = @ ( Get-ChildItem - Path " $path \internal\functions\*.ps1" )
46101$files.AddRange ($filesInternal )
47102
48103foreach ($file in $files ) {
0 commit comments