-
-
Save Splaxi/0280430eda6b53ad589db22c30639be2 to your computer and use it in GitHub Desktop.
reformat and standardize comment based help in powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-Header ($text) { | |
$start = $text.IndexOf('<#') | |
$temp = $start - 2 | |
if($temp -gt 0) { | |
$text.SubString(0, $start - 2) | |
} | |
else { | |
"" | |
} | |
} | |
function Format-Help ($text) { | |
$start = $text.IndexOf('<#') | |
$end = $text.IndexOf('#>') | |
$help = $text.SubString($start + 2, $end - $start - 3) | |
$skipfirst = $null # to avoid trailing spaces | |
foreach ($newline in $help.Split("`n")) { | |
if (-not $skipfirst) { $skipfirst = $true; continue } | |
$trimmed = $newline.Trim() | |
foreach ($line in $trimmed) { | |
if ($line.StartsWith(".")) { | |
" $line" | |
} | |
else { | |
" $line" | |
} | |
} | |
} | |
} | |
function Get-Body ($text) { | |
$end = $text.IndexOf('#>') | |
$text.SubString($end, $text.Length - $end) | |
} | |
$files = Get-ChildItem -Recurse C:\github\dbatools\functions\*.ps1 | |
foreach ($file in $files) { | |
$text = ($file | Get-Content -Raw).Trim() | |
Set-Content -Path $file.FullName -Encoding UTF8 -Value (Get-Header $text).TrimEnd() | |
Add-Content -Path $file.FullName -Encoding UTF8 -Value "<#".Trim() | |
Add-Content -Path $file.FullName -Encoding UTF8 -Value (Format-Help $text) | |
Add-Content -Path $file.FullName -Encoding UTF8 -Value (Get-Body $text).TrimEnd() -NoNewline | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment