-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCollectAndPrint.ps1
More file actions
210 lines (179 loc) · 8.96 KB
/
CollectAndPrint.ps1
File metadata and controls
210 lines (179 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# CollectAndPrint.ps1 - Main script to collect directory structure and file contents.
[CmdletBinding()]
param(
# Default root path to the current directory where the script is run.
[string]$RootPath = (Get-Location).Path,
# Fast mode: use last saved configuration without showing menu
[Alias("f")]
[switch]$Fast,
# Skip menu and use command-line parameters (legacy mode)
[switch]$NoMenu,
# Folders to include. If specified, only files in these folders (and subfolders) are processed.
[string[]]$IncludeFolderPaths = @(),
# Folders to exclude. Default to "node_modules". User can override or add more.
[string[]]$ExcludeFolderPaths = @("node_modules"), # Default exclusion
# File extensions to include. If specified, only files with these extensions are processed.
[string[]]$IncludeExtensions = @(),
# File extensions to exclude. Default to ".env". User can override or add more.
[string[]]$ExcludeExtensions = @(".env"), # Default exclusion
# Minimum file size in bytes. -1 for no limit.
[long]$MinFileSize = -1,
# Maximum file size in bytes. Default to 1MB (1024 * 1024 bytes). -1 for no limit.
[long]$MaxFileSize = 1048576, # Default max size: 1MB
# Prefix for the output file name.
[string]$OutputFileNamePrefix = "LLM_Output"
)
#region Helper Scripts Loading
# Dot-source the helper scripts to make their functions available.
try {
. "$PSScriptRoot\Get-DirectoryStructure.ps1"
. "$PSScriptRoot\Get-FilteredFiles.ps1"
. "$PSScriptRoot\Read-TextFileContent.ps1"
. "$PSScriptRoot\Format-OutputString.ps1"
. "$PSScriptRoot\Config-Manager.ps1"
. "$PSScriptRoot\Get-GitignorePatterns.ps1"
. "$PSScriptRoot\Show-InteractiveMenu.ps1"
}
catch {
Write-Error "Failed to load helper scripts. Ensure they are in the same directory: $PSScriptRoot. Error: $($_.Exception.Message)"
exit 1
}
#endregion
#region Mode Detection and Configuration Loading
$useInteractiveMenu = $false
$config = $null
# Determine execution mode
if ($Fast) {
# Fast mode: load saved config and run immediately
Write-Host "Fast mode (-f): Loading saved configuration..." -ForegroundColor Cyan
$config = Get-SavedConfig
$config = Merge-ConfigWithDefaults -Config $config
# Apply gitignore if enabled
if ($config.useGitignore) {
$gitignorePatterns = Get-GitignorePatterns -RootPath $RootPath
if ($gitignorePatterns.FolderPatterns.Count -gt 0) {
$config.excludeFolders = @($config.excludeFolders) + @($gitignorePatterns.FolderPatterns) | Select-Object -Unique
}
if ($gitignorePatterns.FilePatterns.Count -gt 0) {
$gitExtensions = Convert-GitignoreToExtensions -FilePatterns $gitignorePatterns.FilePatterns
$config.excludeExtensions = @($config.excludeExtensions) + @($gitExtensions) | Select-Object -Unique
}
}
# Apply config to parameters
$ExcludeFolderPaths = $config.excludeFolders
$ExcludeExtensions = $config.excludeExtensions
$IncludeFolderPaths = $config.includeFolders
$IncludeExtensions = $config.includeExtensions
$MaxFileSize = $config.maxFileSize
$MinFileSize = $config.minFileSize
$OutputFileNamePrefix = $config.outputPrefix
}
elseif ($NoMenu -or $PSBoundParameters.Count -gt 1) {
# NoMenu mode or explicit parameters provided: use legacy command-line behavior
Write-Host "Using command-line parameters (legacy mode)..." -ForegroundColor Yellow
#region Parameter Pre-processing for bat file compatibility
if ($IncludeFolderPaths.Count -eq 1 -and $IncludeFolderPaths[0] -match ',') {
$IncludeFolderPaths = $IncludeFolderPaths[0].Split(',') | ForEach-Object {$_.Trim()} | Where-Object {$_}
}
if ($ExcludeFolderPaths.Count -eq 1 -and $ExcludeFolderPaths[0] -match ',') {
$ExcludeFolderPaths = $ExcludeFolderPaths[0].Split(',') | ForEach-Object {$_.Trim()} | Where-Object {$_}
}
if ($IncludeExtensions.Count -eq 1 -and $IncludeExtensions[0] -match ',') {
$IncludeExtensions = $IncludeExtensions[0].Split(',') | ForEach-Object {$_.Trim()} | Where-Object {$_}
}
if ($ExcludeExtensions.Count -eq 1 -and $ExcludeExtensions[0] -match ',') {
$ExcludeExtensions = $ExcludeExtensions[0].Split(',') | ForEach-Object {$_.Trim()} | Where-Object {$_}
}
#endregion
}
else {
# No parameters: show interactive menu
$useInteractiveMenu = $true
}
# Show interactive menu if needed
if ($useInteractiveMenu) {
$menuResult = Show-MainMenu -RootPath $RootPath
if (-not $menuResult.ShouldRun) {
Write-Host "Exiting without generating output." -ForegroundColor Yellow
exit 0
}
# Apply menu config to parameters
$RootPath = $menuResult.RootPath
$config = $menuResult.Config
# Apply gitignore if enabled
if ($config.useGitignore) {
$gitignorePatterns = Get-GitignorePatterns -RootPath $RootPath
if ($gitignorePatterns.FolderPatterns.Count -gt 0) {
$config.excludeFolders = @($config.excludeFolders) + @($gitignorePatterns.FolderPatterns) | Select-Object -Unique
}
if ($gitignorePatterns.FilePatterns.Count -gt 0) {
$gitExtensions = Convert-GitignoreToExtensions -FilePatterns $gitignorePatterns.FilePatterns
$config.excludeExtensions = @($config.excludeExtensions) + @($gitExtensions) | Select-Object -Unique
}
}
$ExcludeFolderPaths = $config.excludeFolders
$ExcludeExtensions = $config.excludeExtensions
$IncludeFolderPaths = $config.includeFolders
$IncludeExtensions = $config.includeExtensions
$MaxFileSize = $config.maxFileSize
$MinFileSize = $config.minFileSize
$OutputFileNamePrefix = $config.outputPrefix
}
#endregion
#region Main Processing
Write-Host ""
Write-Host "Processing directory: $RootPath" -ForegroundColor Cyan
Write-Host "Exclude Folders: $($ExcludeFolderPaths -join ', ')"
Write-Host "Exclude Extensions: $($ExcludeExtensions -join ', ')"
if ($IncludeExtensions.Count -gt 0) {
Write-Host "Include Extensions: $($IncludeExtensions -join ', ')"
}
Write-Host "Max File Size: $(Format-FileSize -Bytes $MaxFileSize)"
Write-Host ""
# 1. Get Directory Structure (with folder exclusions)
Write-Host "Generating directory structure..." -ForegroundColor Yellow
$directoryStructureString = Get-DirectoryStructureFormatted -Path $RootPath -ExcludeFolders $ExcludeFolderPaths
# 2. Get Filtered Files
Write-Host "Filtering files..." -ForegroundColor Yellow
# Normalize extension filters (ensure they start with a dot and are lowercase)
$normalizedIncludeExtensions = @($IncludeExtensions | ForEach-Object { ($_.Trim().ToLowerInvariant() -replace '^\*?(?!\.)','.') } | Where-Object {$_})
$normalizedExcludeExtensions = @($ExcludeExtensions | ForEach-Object { ($_.Trim().ToLowerInvariant() -replace '^\*?(?!\.)','.') } | Where-Object {$_})
# Ensure $filteredFileItems is always an array.
$filteredFileItems = @(Get-FilteredFileItems -RootPath $RootPath `
-IncludeFolders $IncludeFolderPaths `
-ExcludeFolders $ExcludeFolderPaths `
-IncludeExtensions $normalizedIncludeExtensions `
-ExcludeExtensions $normalizedExcludeExtensions `
-MinSize $MinFileSize `
-MaxSize $MaxFileSize)
Write-Host "Found $($filteredFileItems.Count) files matching criteria." -ForegroundColor Green
# 3. Format the output
Write-Host "Formatting output..." -ForegroundColor Yellow
$finalOutput = New-FormattedOutput -RootDirectory $RootPath `
-DirectoryStructure $directoryStructureString `
-FileItems $filteredFileItems `
-ContentReader { param($FileItem) Read-SafeTextFileContent -FileItem $FileItem }
# 4. Output to File
$timestamp = Get-Date -Format "yyyyMMddHHmmss"
$outputFilePath = Join-Path -Path $RootPath -ChildPath "$($OutputFileNamePrefix)_$timestamp.txt"
try {
Write-Host "Writing output to: $outputFilePath" -ForegroundColor Cyan
$finalOutput | Out-File -FilePath $outputFilePath -Encoding UTF8 -Force
Write-Host ""
Write-Host "✓ Successfully generated output file!" -ForegroundColor Green
Write-Host " Path: $outputFilePath" -ForegroundColor Gray
Write-Host " Size: $(Format-FileSize -Bytes (Get-Item $outputFilePath).Length)" -ForegroundColor Gray
}
catch {
Write-Error "Failed to write output file: $($_.Exception.Message)"
}
# Optional: Display a snippet or confirmation
if ((Test-Path $outputFilePath) -and (Get-Item $outputFilePath).Length -lt 20000) {
Write-Host ""
Write-Host "--- Output File Preview (first 15 lines) ---" -ForegroundColor DarkGray
Get-Content $outputFilePath -TotalCount 15 | ForEach-Object { Write-Host " $_" -ForegroundColor DarkGray }
Write-Host "---" -ForegroundColor DarkGray
}
Write-Host ""
Write-Host "Script finished. Use 'FolderToLLM -f' for quick re-run with same settings." -ForegroundColor Cyan
#endregion