Advertisement
justinooo

PowerShell 'Get-Code' utility

Oct 18th, 2024 (edited)
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. param(
  2.     [switch]$c,
  3.     [switch]$t,
  4.     [switch]$m,
  5.     [switch]$all,
  6.     [string[]]$ExcludeExtensions = @(),
  7.     [string[]]$ExcludePaths = @()
  8. )
  9.  
  10. $codeExtensions = @("*.cs", "*.js", "*.ts", "*.py", "*.html", "*.css", "*.cpp", "*.h", "*.java", "*.rb", "*.go", "*.rs", "*.sh", "*.ps1", "*.php", "*.xml")
  11. $baseDir = Get-Location
  12.  
  13. # define excluded directory names
  14. $excludedDirs = @("__pycache__", "node_modules", "bin", "obj", "dist", "build", ".git", ".svn")
  15.  
  16. $excludeExtensionsHashSet = [System.Collections.Generic.HashSet[string]]::new()
  17. $ExcludeExtensions | ForEach-Object { $excludeExtensionsHashSet.Add($_) }
  18.  
  19. # get code files excluding specific directories and extensions
  20. $codeFiles = Get-ChildItem -Path $baseDir -Recurse -Include $codeExtensions | Where-Object {
  21.     ($excludeExtensionsHashSet.Contains($_.Extension) -eq $false) -and
  22.     ($ExcludePaths -notcontains $_.DirectoryName) -and
  23.     (-not ($_.FullName -match "($($excludedDirs -join '|'))"))
  24. }
  25.  
  26. $allCode = ""
  27. foreach ($file in $codeFiles) {
  28.     $relativePath = $file.FullName.Substring($baseDir.Length + 1)
  29.  
  30.     # add metadata if -m or -all is set
  31.     if ($m -or $all) {
  32.         $fileMetadata = @(
  33.             @{ "RelativePath" = $relativePath },
  34.             @{ "Size" = "$([math]::Round($file.Length / 1KB, 2)) KB" },
  35.             @{ "LastModified" = $file.LastWriteTime }
  36.         )
  37.  
  38.         $metadataString = "### File Metadata ###`n"
  39.         $fileMetadata | ForEach-Object { $metadataString += "$($_.Keys[0]): $($_.Values[0])`n" }
  40.         $metadataString += "`n"
  41.         $allCode += $metadataString
  42.     }
  43.  
  44.     $fileContent = Get-Content $file.FullName -Raw
  45.     $allCode += $fileContent + "`n" + ("-" * 80) + "`n"
  46. }
  47.  
  48. # add file tree if -t or -all is set
  49. if ($t -or $all) {
  50.     $fileTree = ""
  51.     $fileTreeEntries = Get-ChildItem -Path $baseDir -Recurse
  52.     foreach ($entry in $fileTreeEntries) {
  53.         if (-not ($entry.FullName -match "($($excludedDirs -join '|'))")) {
  54.             $relativePath = $entry.FullName.Substring($baseDir.Length + 1)
  55.             $fileTree += if ($entry.PSIsContainer) {
  56.                 "[Dir] $relativePath`n"
  57.             } else {
  58.                 "[File] $relativePath`n"
  59.             }
  60.         }
  61.     }
  62.     $allCode += "`n### File Tree ###`n`n$fileTree"
  63. }
  64.  
  65. # output all code and metadata
  66. $allCode
  67.  
  68. # copy to clipboard if -c or -all is set
  69. if ($c -or $all) {
  70.     $allCode | Set-Clipboard
  71.     Write-Host "Code and metadata have been copied to clipboard."
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement