justinooo icon

PowerShell 'Get-Code' utility

justinooo | PRO | 02/22/25 08:44:29 PM UTC (Edited) | 0 ⭐ | 8022 👁️ | Never ⏰ | []
PowerShell |

3.08 KB

|

None

|

0 👍

/

0 👎

param(
    [switch]$all, # do all
    [switch]$a, # shortcut for all
    [switch]$c, # copy to clipboard
    [switch]$t, # file tree
    [switch]$m, # file metadata
    [string[]]$ExcludeExtensions = @(),
    [string[]]$ExcludePaths = @()
)
 
if ($all -or $a) {
    $c = $true
    $t = $true
    $m = $true
    $a = $true
    $all = $true
}
 
# base configuration
$codeExtensions = @("*.cs", "*.js", "*.jsx", "*.vue", "*.ts", "*.py", "*.html", "*.css", "*.cpp", "*.h", "*.java", "*.rb", "*.go", "*.rs", "*.sh", "*.ps1", "*.php", "*.xml")
$baseDir = Get-Location
 
# excluded directory names
$excludedDirs = @("__pycache__", "node_modules", "bin", "obj", "dist", "build", ".git", ".svn", ".pio", ".vscode", "venv", ".typed")
 
# initialize exclude extensions set
$excludeExtensionsHashSet = [System.Collections.Generic.HashSet[string]]::new()
$ExcludeExtensions | ForEach-Object { $excludeExtensionsHashSet.Add($_) }
 
# initialize output string
$output = ""
 
# function to get filtered files
function Get-FilteredFiles {
    return Get-ChildItem -Path $baseDir -Recurse -Include $codeExtensions | Where-Object {
        ($excludeExtensionsHashSet.Contains($_.Extension) -eq $false) -and
        ($ExcludePaths -notcontains $_.DirectoryName) -and
        (-not ($_.FullName -match "($($excludedDirs -join '|'))"))
    }
}
 
# function to generate file tree
function Get-FileTree {
    $tree = "### File Tree ###`n`n"
    $fileTreeEntries = Get-ChildItem -Path $baseDir -Recurse
    foreach ($entry in $fileTreeEntries) {
        if (-not ($entry.FullName -match "($($excludedDirs -join '|'))")) {
            $relativePath = $entry.FullName.Substring($baseDir.Length + 1)
            $tree += if ($entry.PSIsContainer) {
                "[Dir] $relativePath`n"
            } else {
                "[File] $relativePath`n"
            }
        }
    }
    return $tree
}
 
# function to get file metadata
function Get-FileMetadata {
    param($file)
    $relativePath = $file.FullName.Substring($baseDir.Length + 1)
    $metadata = "Path: $relativePath`n"
    $metadata += "Size: $([math]::Round($file.Length / 1KB, 2)) KB`n"
    $metadata += "Last Modified: $($file.LastWriteTime)`n"
    return $metadata
}
 
# function to get file content
function Get-FileContentWithHeader {
    param($file)
    $relativePath = $file.FullName.Substring($baseDir.Length + 1)
    $content = Get-Content $file.FullName -Raw
    return $content
}
 
# handle file content and metadata
if ($m) {
    $files = Get-FilteredFiles
    foreach ($file in $files) {
        $output += "### File Metadata ###`n"
        $output += Get-FileMetadata $file
        if ($all) {
            $output += "`n### File Content ###`n"
            $output += Get-FileContentWithHeader $file
        }
        $output += ("-" * 80) + "`n`n"
    }
}
 
# handle tree output at the end
if ($t) {
    $output += Get-FileTree
    # $output += ("-" * 80) + "`n"
}
 
# output the result
$output
 
# copy to clipboard if requested
if ($c) {
    $output | Set-Clipboard
    Write-Host "Content copied to clipboard."
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎