Advertisement
justinooo

PowerShell 'Get-Code' utility

Oct 18th, 2024 (edited)
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. param(
  2.     [switch]$all, # do all
  3.     [switch]$a, # shortcut for all
  4.     [switch]$c, # copy to clipboard
  5.     [switch]$t, # file tree
  6.     [switch]$m, # file metadata
  7.     [string[]]$ExcludeExtensions = @(),
  8.     [string[]]$ExcludePaths = @()
  9. )
  10.  
  11. if ($all -or $a) {
  12.     $c = $true
  13.     $t = $true
  14.     $m = $true
  15.     $a = $true
  16.     $all = $true
  17. }
  18.  
  19. # base configuration
  20. $codeExtensions = @("*.cs", "*.js", "*.jsx", "*.vue", "*.ts", "*.py", "*.html", "*.css", "*.cpp", "*.h", "*.java", "*.rb", "*.go", "*.rs", "*.sh", "*.ps1", "*.php", "*.xml")
  21. $baseDir = Get-Location
  22.  
  23. # excluded directory names
  24. $excludedDirs = @("__pycache__", "node_modules", "bin", "obj", "dist", "build", ".git", ".svn", ".pio", ".vscode", "venv", ".typed")
  25.  
  26. # initialize exclude extensions set
  27. $excludeExtensionsHashSet = [System.Collections.Generic.HashSet[string]]::new()
  28. $ExcludeExtensions | ForEach-Object { $excludeExtensionsHashSet.Add($_) }
  29.  
  30. # initialize output string
  31. $output = ""
  32.  
  33. # function to get filtered files
  34. function Get-FilteredFiles {
  35.     return Get-ChildItem -Path $baseDir -Recurse -Include $codeExtensions | Where-Object {
  36.         ($excludeExtensionsHashSet.Contains($_.Extension) -eq $false) -and
  37.         ($ExcludePaths -notcontains $_.DirectoryName) -and
  38.         (-not ($_.FullName -match "($($excludedDirs -join '|'))"))
  39.     }
  40. }
  41.  
  42. # function to generate file tree
  43. function Get-FileTree {
  44.     $tree = "### File Tree ###`n`n"
  45.     $fileTreeEntries = Get-ChildItem -Path $baseDir -Recurse
  46.     foreach ($entry in $fileTreeEntries) {
  47.         if (-not ($entry.FullName -match "($($excludedDirs -join '|'))")) {
  48.             $relativePath = $entry.FullName.Substring($baseDir.Length + 1)
  49.             $tree += if ($entry.PSIsContainer) {
  50.                 "[Dir] $relativePath`n"
  51.             } else {
  52.                 "[File] $relativePath`n"
  53.             }
  54.         }
  55.     }
  56.     return $tree
  57. }
  58.  
  59. # function to get file metadata
  60. function Get-FileMetadata {
  61.     param($file)
  62.     $relativePath = $file.FullName.Substring($baseDir.Length + 1)
  63.     $metadata = "Path: $relativePath`n"
  64.     $metadata += "Size: $([math]::Round($file.Length / 1KB, 2)) KB`n"
  65.     $metadata += "Last Modified: $($file.LastWriteTime)`n"
  66.     return $metadata
  67. }
  68.  
  69. # function to get file content
  70. function Get-FileContentWithHeader {
  71.     param($file)
  72.     $relativePath = $file.FullName.Substring($baseDir.Length + 1)
  73.     $content = Get-Content $file.FullName -Raw
  74.     return $content
  75. }
  76.  
  77. # handle file content and metadata
  78. if ($m) {
  79.     $files = Get-FilteredFiles
  80.     foreach ($file in $files) {
  81.         $output += "### File Metadata ###`n"
  82.         $output += Get-FileMetadata $file
  83.         if ($all) {
  84.             $output += "`n### File Content ###`n"
  85.             $output += Get-FileContentWithHeader $file
  86.         }
  87.         $output += ("-" * 80) + "`n`n"
  88.     }
  89. }
  90.  
  91. # handle tree output at the end
  92. if ($t) {
  93.     $output += Get-FileTree
  94.     # $output += ("-" * 80) + "`n"
  95. }
  96.  
  97. # output the result
  98. $output
  99.  
  100. # copy to clipboard if requested
  101. if ($c) {
  102.     $output | Set-Clipboard
  103.     Write-Host "Content copied to clipboard."
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement