Skip to content

Instantly share code, notes, and snippets.

@rbleattler
Created March 3, 2021 15:29
Show Gist options
  • Save rbleattler/d81d80ebe3a8de2b56a40baceef90c09 to your computer and use it in GitHub Desktop.
Save rbleattler/d81d80ebe3a8de2b56a40baceef90c09 to your computer and use it in GitHub Desktop.
A useful tool that can determine Work Directory information such as number of files and directories, as well as the number of characters and lines in the files + some Easter Eggs ;)
function Get-RepositoryStats {
[CmdletBinding()]
param (
[string]
$WorkDirectory,
[switch]
$UseGitignore,
[switch]
$Novels
)
begin {
Write-Debug "Enter [$($PSCmdlet.MyInvocation.MyCommand.Name)]..."
$PSBoundParameters.Keys.ForEach{
if ($PSBoundParameters.PSItem -is [string]) {
Write-Debug "$_ : $($PSBoundParameters.Item($_))"
} else {
Write-Debug "$_ : $($PSBoundParameters.Item($_).GetType())"
}
}
$Directories = Get-ChildItem -Path $WorkDirectory -Directory -Recurse
$FilesToMeasure = Get-ChildItem -Path $WorkDirectory -File -Recurse
if ($UseGitignore) {
Write-Progress -Activity 'Getting GitIgnore Data' -Status "Working..." -CurrentOperation 'Getting GitIgnore Data'
$GitignoreData = ($FilesToMeasure.Where{ $_.Name -like '*.gitignore*' } | Get-Content -ErrorAction Ignore).Where{ -not $PSItem.StartsWith('#') } | Select-Object -Unique
$NewGitIgnoreData = $GitignoreData.Where{ -not [string]::IsNullOrEmpty($PSItem) }.ForEach{
$Item = $PSItem
if (-not $Item.EndsWith('*')) {
$Item = $Item.Insert($Item.Length, "*")
}
if (-not $Item.StartsWith('*')) {
$Item = $Item.Insert(0, "*")
}
$Item
}
Write-Progress -Activity 'Determining Files to Ignore' -Status "Working..." -CurrentOperation 'Getting Files to Ignore from Gitignore Data'
$FilesToIgnore = $FilesToMeasure.Where{
$file = $PSItem
$NewGitIgnoreData.Where{ $file.FullName -like $PSItem }
}
Write-Progress -Activity 'Determining Files to Measure' -Status "Working..." -CurrentOperation 'Getting Files to Measure...'
$FilesToMeasure = $FilesToMeasure.Where{ $PSItem -notin $FilesToIgnore }
Write-Progress -Activity 'Getting Files to Measure' -Status "DONE" -CurrentOperation 'Starting Calculations...'
}
$DebugMessage = 'Processing {0} Files in {1} Directories...' -f $FilesToMeasure.Count, $Directories.Count
Write-Debug -Message $DebugMessage
}
process {
$FileCount = $FilesToMeasure.Count
$FileCharCounted = 0
$FileLineCounted = 0
$Characters = 0
$FilesToMeasure.ForEach{
$Characters = $Characters + $PSItem.Length
$FileCharCounted++
$PCT = '{0:N0}' -f $(($FileCharCounted/$FileCount)*100)
Write-Progress -Activity 'Calculating Characters' -Status "$PCT% Complete" -PercentComplete $PCT -CurrentOperation "Counting Characters in $PSItem"
}
$Lines = 0
$FilesToMeasure.ForEach{
$File = $PSItem
try {
$ItemLineCount = $([System.IO.File]::ReadLines($File)).Count
$Lines = $Lines + $ItemLineCount
} catch {
Write-Warning "Error getting length of $File. Skipping..."
$_
}
$FileLineCounted++
$PCT = '{0:N0}' -f $(($FileLineCounted/$FileCount)*100)
Write-Progress -Activity 'Calculating Line Counts' -Status "$PCT% Complete" -PercentComplete $PCT -CurrentOperation "Counting Lines in $PSItem"
}
$AverageEnglishWordLength = 4.7
$WordCount = $Characters / $AverageEnglishWordLength
$NovelCount = [math]::Round($($WordCount / 80000), 2)
$OutputItem = [pscustomobject]@{
Directories = $Directories.Count.ToString('N0')
Files = $FilesToMeasure.Count.ToString('N0')
Characters = $Characters.ToString('N0')
Lines = $Lines.ToString('N0')
WordCount = $WordCount.ToString('N0')
}
if ($Novels) {
$OutputItem | Add-Member -MemberType NoteProperty -Name NovelCount -Value $NovelCount -Force
}
$OutputItem
}
end {
Write-Debug "Exit [$($PSCmdlet.MyInvocation.MyCommand.Name)]..."
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment