Skip to content

Instantly share code, notes, and snippets.

@potatoqualitee
Last active October 7, 2018 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save potatoqualitee/5fffc08d05b27d5503d5c78a9ece4c44 to your computer and use it in GitHub Desktop.
Save potatoqualitee/5fffc08d05b27d5503d5c78a9ece4c44 to your computer and use it in GitHub Desktop.
reformat and standardize comment based help in powershell
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