Created
October 28, 2025 23:06
-
-
Save StartAutomating/fb68aa34038055d8f4a07f8a3151456f to your computer and use it in GitHub Desktop.
Gist get me the Doc Ratio
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 DocRatio { | |
| <# | |
| .SYNOPSIS | |
| DocRatio | |
| .DESCRIPTION | |
| Gets the ratio of comments to non-comments in a script | |
| .EXAMPLE | |
| docratio docratio | |
| #> | |
| $allInputAndArgs = @($input) + $args | |
| foreach ($thing in $allInputAndArgs) { | |
| $originalThing = $thing | |
| if ($thing -is [string] -and (Test-Path $thing -ErrorAction Ignore)) { | |
| $thing = Get-Item -Path $thing | |
| } | |
| if ($thing -is [string] -and $( | |
| $thingFunction = $ExecutionContext.SessionState.InvokeCommand.GetCommand($thing,'Function') | |
| $thingFunction | |
| )) { | |
| $thing = $thingFunction.ScriptBlock | |
| } | |
| if ($thing -is [IO.FileInfo] -and $thing.Extension -eq '.ps1') { | |
| $thing = Get-Command $thing.FullName | |
| } | |
| if ($thing -is [Management.Automation.ExternalScriptInfo]) { | |
| $thing = $thing.ScriptBlock | |
| } | |
| if ($thing -is [ScriptBlock]) { | |
| $tokens = [Management.Automation.PSParser]::Tokenize("$thing", [ref]$null) | |
| $totalLength = 0 | |
| $commentLength = 0 | |
| foreach ($token in $tokens) { | |
| $totalLength += $token.Length | |
| if ($token.Type -eq 'Comment') { | |
| $commentLength += $token.Length | |
| } | |
| } | |
| if ($totalLength) { | |
| $ratio = $commentLength / $totalLength | |
| @{$originalThing=$ratio} | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment