Last active
October 7, 2018 15:40
-
-
Save potatoqualitee/5fffc08d05b27d5503d5c78a9ece4c44 to your computer and use it in GitHub Desktop.
reformat and standardize comment based help in powershell
This file contains hidden or 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('<#') | |
$text.SubString(0, $start - 2) | |
} | |
function Format-Help ($text) { | |
$start = $text.IndexOf('<#') | |
$end = $text.IndexOf('#>') | |
$help = $text.SubString($start + 2, $end - $start - 2) | |
$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