the_goth_cardgame_collection/DisplayTree.ps1

96 lines
3.6 KiB
PowerShell
Raw Normal View History

function DisplayTree {
param (
[Parameter(Mandatory = $true)]
[string]$Path,
[string]$Indent = "",
[bool]$IsRoot = $false,
[switch]$Essentials,
[string[]]$ExcludeDirs = @(
# Node / Build
"node_modules", "build", "dist",
# Git
".git",
# Python virtual envs, pip & package caches
".env*", "env", "venv*", ".venv*",
"__pycache__", "site-packages", "Lib", "Include", "Scripts",
"*.egg-info", "*.dist-info", ".eggs", "wheels", "pip-wheel-metadata",
".pytest_cache", ".mypy_cache", ".ruff_cache", ".tox", ".nox"
),
[string[]]$ExcludeFiles = @(
# Git files
".gitignore", ".gitattributes", ".gitmodules", ".gitkeep",
# Python init & compiled bytecode
"__init__.py", "*.pyc", "*.pyo", "*.pyd"
)
)
# Apply extra exclusions once at the root level when --essentials is active
if ($IsRoot -and $Essentials) {
$ExcludeDirs += "build_scripts"
$ExcludeFiles += @(
# Readme files
"readme*",
# Image formats
"*.png", "*.jpg", "*.jpeg", "*.gif", "*.bmp", "*.svg", "*.ico",
"*.webp", "*.tiff", "*.tif", "*.heic", "*.avif", "*.raw", "*.psd",
# Music / Audio formats
"*.mp3", "*.wav", "*.flac", "*.aac", "*.ogg", "*.oga", "*.wma",
"*.m4a", "*.opus", "*.aiff", "*.alac", "*.mid", "*.midi",
# Fonts, Databases & PowerShell scripts
"*.ttf", "*.db", "*.ps1"
)
}
$outputLines = New-Object System.Collections.Generic.List[string]
# Recurse into directories (skipping any match in $ExcludeDirs)
Get-ChildItem -LiteralPath $Path -Directory -Force -ErrorAction SilentlyContinue | Where-Object {
$itemName = $_.Name
-not ($ExcludeDirs | Where-Object { $itemName -like $_ })
} | ForEach-Object {
$subLines = DisplayTree -Path $_.FullName -Indent " $Indent" -Essentials:$Essentials -ExcludeDirs $ExcludeDirs -ExcludeFiles $ExcludeFiles
# In --essentials mode, hide folders that have zero matching files/subfolders left
if (-not $Essentials -or $subLines.Count -gt 0) {
$outputLines.Add("$Indent|--- $($_.Name)")
foreach ($line in $subLines) {
$outputLines.Add($line)
}
}
}
# Collect files (skipping any match in $ExcludeFiles)
Get-ChildItem -LiteralPath $Path -File -Force -ErrorAction SilentlyContinue | Where-Object {
$itemName = $_.Name
-not ($ExcludeFiles | Where-Object { $itemName -like $_ })
} | ForEach-Object {
$lineInfo = ""
if ($Essentials -and $_.Extension -eq ".py") {
$rowCount = [System.IO.File]::ReadAllLines($_.FullName).Length
$lineInfo = " ($rowCount lines)"
}
$outputLines.Add("$Indent|--- $($_.Name)$lineInfo")
}
# Print at root; return list on recursive calls
if ($IsRoot) {
$rootName = (Get-Item -LiteralPath $Path -Force).Name
Write-Host $rootName
foreach ($line in $outputLines) {
Write-Host $line
}
} else {
return ,$outputLines
}
}
# Parse CLI arguments passed to .\DisplayTree.ps1
$useEssentials = ($args -contains "--essentials") -or ($args -contains "-essentials")
$targetPath = ($args | Where-Object { $_ -notlike "-*" } | Select-Object -First 1)
if (-not $targetPath) {
$targetPath = ".\"
}
DisplayTree -Path $targetPath -IsRoot $true -Essentials:$useEssentials