Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- param(
- [switch]$c,
- [switch]$t,
- [switch]$m,
- [switch]$all,
- [string[]]$ExcludeExtensions = @(),
- [string[]]$ExcludePaths = @()
- )
- $codeExtensions = @("*.cs", "*.js", "*.ts", "*.py", "*.html", "*.css", "*.cpp", "*.h", "*.java", "*.rb", "*.go", "*.rs", "*.sh", "*.ps1", "*.php", "*.xml")
- $baseDir = Get-Location
- # define excluded directory names
- $excludedDirs = @("__pycache__", "node_modules", "bin", "obj", "dist", "build", ".git", ".svn")
- $excludeExtensionsHashSet = [System.Collections.Generic.HashSet[string]]::new()
- $ExcludeExtensions | ForEach-Object { $excludeExtensionsHashSet.Add($_) }
- # get code files excluding specific directories and extensions
- $codeFiles = Get-ChildItem -Path $baseDir -Recurse -Include $codeExtensions | Where-Object {
- ($excludeExtensionsHashSet.Contains($_.Extension) -eq $false) -and
- ($ExcludePaths -notcontains $_.DirectoryName) -and
- (-not ($_.FullName -match "($($excludedDirs -join '|'))"))
- }
- $allCode = ""
- foreach ($file in $codeFiles) {
- $relativePath = $file.FullName.Substring($baseDir.Length + 1)
- # add metadata if -m or -all is set
- if ($m -or $all) {
- $fileMetadata = @(
- @{ "RelativePath" = $relativePath },
- @{ "Size" = "$([math]::Round($file.Length / 1KB, 2)) KB" },
- @{ "LastModified" = $file.LastWriteTime }
- )
- $metadataString = "### File Metadata ###`n"
- $fileMetadata | ForEach-Object { $metadataString += "$($_.Keys[0]): $($_.Values[0])`n" }
- $metadataString += "`n"
- $allCode += $metadataString
- }
- $fileContent = Get-Content $file.FullName -Raw
- $allCode += $fileContent + "`n" + ("-" * 80) + "`n"
- }
- # add file tree if -t or -all is set
- if ($t -or $all) {
- $fileTree = ""
- $fileTreeEntries = Get-ChildItem -Path $baseDir -Recurse
- foreach ($entry in $fileTreeEntries) {
- if (-not ($entry.FullName -match "($($excludedDirs -join '|'))")) {
- $relativePath = $entry.FullName.Substring($baseDir.Length + 1)
- $fileTree += if ($entry.PSIsContainer) {
- "[Dir] $relativePath`n"
- } else {
- "[File] $relativePath`n"
- }
- }
- }
- $allCode += "`n### File Tree ###`n`n$fileTree"
- }
- # output all code and metadata
- $allCode
- # copy to clipboard if -c or -all is set
- if ($c -or $all) {
- $allCode | Set-Clipboard
- Write-Host "Code and metadata have been copied to clipboard."
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement