Skip to content

Instantly share code, notes, and snippets.

@DBalashov
Created March 20, 2024 15:04
Show Gist options
  • Save DBalashov/c9840363846319b0a6250bc24311d066 to your computer and use it in GitHub Desktop.
Save DBalashov/c9840363846319b0a6250bc24311d066 to your computer and use it in GitHub Desktop.
Run in solution directory (example): powershell -F ..\count-loc.ps1
$extensions = @(".cs", ".vue", ".scss", ".ts")
$exclude = @("\bin\", "\obj\", "\node_modules\", "\.nuxt\", "\.output\")
$path = $args[0]
$files = Get-ChildItem -Recurse -Include ($extensions | % { '*' + $_ }) $path
$statLength = @{}
$statLOC = @{}
$totalLOC = 0;
$totalLength = 0;
foreach($f in $files) {
$excluded = $false
foreach($ex in $exclude) {
if($f.FullName.IndexOf($ex) -ge 0) {
$excluded = $true
break;
}
}
if($excluded) {
continue
}
foreach($ext in $extensions) {
if($f.Name.EndsWith($ext)) {
$statLength[$ext] = $statLength[$ext] + $f.Length
$loc = [System.IO.File]::ReadAllLines($f.FullName).Length
$statLOC[$ext] = $statLOC[$ext] + $loc
$totalLOC = $totalLOC + $loc
$totalLength = $totalLength + $f.Length
break
}
}
}
$extensions | % {
$o = New-Object -TypeName PSObject
$o | Add-Member -Name 'Extension' -MemberType Noteproperty -Value $_
$o | Add-Member -Name 'LOC' -MemberType Noteproperty -Value ($statLOC[$_])
$o | Add-Member -Name 'Length' -MemberType Noteproperty -Value (($statLength[$_]/1024).ToString("F2")+' KB')
return $o
} | Format-Table
$t = New-Object -TypeName PSObject
$t | Add-Member -Name 'Extension' -MemberType Noteproperty -Value 'Total'
$t | Add-Member -Name 'LOC' -MemberType Noteproperty -Value ($totalLOC)
$t | Add-Member -Name 'Length' -MemberType Noteproperty -Value (($totalLength/1024).ToString("F2")+' KB')
$t | Add-Member -Name 'AvgLineLength' -MemberType Noteproperty -Value (($totalLength/$totalLOC).ToString("F0")+' chars')
$t | Format-Table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment