-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCombineSources.ps1
More file actions
396 lines (327 loc) · 11 KB
/
CombineSources.ps1
File metadata and controls
396 lines (327 loc) · 11 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
param (
# Core parameters
[string]$OutputDirectory = "./.tmp",
# File patterns and exclusions
[string[]]$ExcludePatterns = @(
'examples?', # Matches 'example' or 'examples'
# 'tests?', # Matches 'test' or 'tests'
'docs?', # Matches 'doc' or 'docs'
'obj', # Build output
'bin', # Build output
'debug', # Debug folders
'release', # Release folders
'packages', # NuGet packages
'node_modules' # NPM packages
),
# Content processing options
[bool]$StripHeaderComments = $true,
[bool]$StripRegions = $true,
[bool]$StripComments = $false,
[bool]$StripEmptyLines = $false,
[bool]$StripUsings = $true,
[bool]$CombineUsings = $true,
[bool]$StripAttributes = $false,
[bool]$SkipGeneratedFiles = $true,
[bool]$UpdateGitIgnore = $true
)
# Statistics tracking
$stats = @{
ProjectsProcessed = 0
ProjectsSkipped = 0
TotalSourceFiles = 0
TotalMarkdownFiles = 0
OriginalSize = 0
FinalSize = 0
TotalUsings = 0
FilesSkipped = 0
StartTime = Get-Date
}
# Logging functions
function Write-Progress-Info {
param([string]$message)
Write-Host "[INFO] $message" -ForegroundColor Cyan
}
function Write-Progress-Warning {
param([string]$message)
Write-Host "[WARN] $message" -ForegroundColor Yellow
}
function Write-Progress-Success {
param([string]$message)
Write-Host "[SUCCESS] $message" -ForegroundColor Green
}
# Progress bar function
function Write-ProgressBar {
param (
[int]$Current,
[int]$Total,
[string]$Activity,
[string]$Status
)
$percentComplete = [math]::Min(100, ($Current / $Total * 100))
Write-Progress -Activity $Activity -Status $Status -PercentComplete $percentComplete
}
# Using statement handling
function Get-UsingStatements {
param (
[string]$content
)
$regex = '(?m)^using\s+(?<using>[^;]+);'
$matches = [regex]::Matches($content, $regex)
return $matches | ForEach-Object { $_.Groups['using'].Value.Trim() }
}
function Remove-UsingStatements {
param (
[string]$content
)
return $content -replace '(?m)^using\s+[^;]+;\r?\n?', ''
}
function Format-UsingStatements {
param (
[string[]]$usings
)
$uniqueUsings = $usings | Select-Object -Unique | Sort-Object
$systemUsings = $uniqueUsings | Where-Object { $_ -like "System*" } | Sort-Object
$microsoftUsings = $uniqueUsings | Where-Object { $_ -like "Microsoft*" } | Sort-Object
$otherUsings = $uniqueUsings | Where-Object { -not ($_ -like "System*" -or $_ -like "Microsoft*") } | Sort-Object
$result = ""
if ($systemUsings) {
$result += ($systemUsings | ForEach-Object { "using $_;" }) -join "`n"
$result += "`n`n"
}
if ($microsoftUsings) {
$result += ($microsoftUsings | ForEach-Object { "using $_;" }) -join "`n"
$result += "`n`n"
}
if ($otherUsings) {
$result += ($otherUsings | ForEach-Object { "using $_;" }) -join "`n"
}
return $result.Trim()
}
# Code cleanup
function Remove-CodeElements {
param (
[string]$content,
[bool]$stripRegions,
[bool]$stripComments,
[bool]$stripEmptyLines,
[bool]$stripAttributes,
[bool]$stripHeaderComments
)
if ($stripHeaderComments) {
# Remove multi-line license headers with /* */
$content = $content -replace '(?sm)^\/\*.*?\*\/', ''
# Remove single-line license headers (including MIT, license references, etc.)
$content = $content -replace '(?m)^\/\/.*MIT.*$\n?', ''
$content = $content -replace '(?m)^\/\/.*[Ll]icense.*$\n?', ''
$content = $content -replace '(?m)^\/\/.*[Cc]opyright.*$\n?', ''
$content = $content -replace '(?m)^\/\/.*[Aa]ll [Rr]ights.*$\n?', ''
$content = $content -replace '(?m)^\/\/.*[Gg]overned by.*$\n?', ''
$content = $content -replace '(?m)^\/\/.*[Uu]se of this source.*$\n?', ''
# Remove any blank lines at the start of the file
$content = $content -replace '^\s*\n', ''
}
if ($stripRegions) {
$content = $content -replace '(?m)^\s*#region.*$\n?', ''
$content = $content -replace '(?m)^\s*#endregion.*$\n?', ''
}
if ($stripComments) {
$content = $content -replace '(?m)^\s*//.*$\n?', ''
$content = $content -replace '(?s)/\*.*?\*/', ''
}
if ($stripEmptyLines) {
$content = $content -replace '(?m)^\s*$\n', ''
}
if ($stripAttributes) {
$content = $content -replace '(?m)^\s*\[.*?\]\r?\n', ''
}
return $content.Trim()
}
function Should-ProcessFile {
param (
[System.IO.FileInfo]$file,
[bool]$skipGenerated,
[string[]]$excludePatterns
)
# Check if path contains any exclude patterns
foreach ($pattern in $excludePatterns) {
if ($file.FullName -match $pattern) {
$stats.FilesSkipped++
Write-Progress-Info "Skipping file: $($file.FullName) (matched pattern: $pattern)"
return $false
}
}
if ($skipGenerated) {
$generatedPatterns = @(
'\.g\.cs$',
'\.designer\.cs$',
'\.generated\.cs$',
'TemporaryGeneratedFile',
'\.AssemblyInfo\.cs$'
)
foreach ($pattern in $generatedPatterns) {
if ($file.Name -match $pattern) {
$stats.FilesSkipped++
Write-Progress-Info "Skipping generated file: $($file.Name)"
return $false
}
}
}
return $true
}
function Get-RelativePath {
param (
[string]$fullPath,
[string]$basePath
)
return $fullPath.Substring($basePath.Length).TrimStart('\', '/')
}
# Main processing
Write-Progress-Info "Starting documentation generation..."
Write-Progress-Info "Output directory: $OutputDirectory"
# Convert relative path to absolute and ensure directory exists
$OutputDirectory = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($OutputDirectory)
New-Item -ItemType Directory -Force -Path $OutputDirectory | Out-Null
# Find all .csproj files
$projects = Get-ChildItem -Recurse -Filter "*.csproj"
$projectCount = $projects.Count
Write-Progress-Info "Found $projectCount projects to process"
$projects | ForEach-Object -Begin {
$currentProject = 0
} -Process {
$currentProject++
Write-ProgressBar -Current $currentProject -Total $projectCount -Activity "Processing Projects" -Status "Project $currentProject of $projectCount"
$projectFile = $_
$projectDir = $projectFile.Directory
$projectName = $projectFile.BaseName
Write-Progress-Info "`nProcessing project: $projectName"
# Skip if excluded
$shouldExclude = $false
foreach ($pattern in $ExcludePatterns) {
if ($projectFile.FullName -match $pattern) {
$shouldExclude = $true
Write-Progress-Warning "Skipping $projectName (matched exclude pattern: $pattern)"
$stats.ProjectsSkipped++
break
}
}
if ($shouldExclude) { return }
$stats.ProjectsProcessed++
# Initialize markdown content
$markdown = "# $projectName`n`n"
# Process source files
$allUsings = @()
$processedFiles = @()
$sourceFiles = @(Get-ChildItem -Path $projectDir -Recurse -Filter "*.cs" |
Where-Object { Should-ProcessFile -file $_ -skipGenerated $SkipGeneratedFiles -excludePatterns $ExcludePatterns })
$totalFiles = $sourceFiles.Count
$currentFile = 0
Write-Progress-Info "Processing $totalFiles source files..."
$sourceFiles | ForEach-Object {
$currentFile++
Write-ProgressBar -Current $currentFile -Total $totalFiles -Activity "Processing Source Files" -Status "File $currentFile of $totalFiles"
$content = Get-Content $_.FullName -Raw
$stats.OriginalSize += $content.Length
# Handle using statements based on settings
if ($CombineUsings -or $StripUsings) {
$usings = Get-UsingStatements -content $content
$stats.TotalUsings += $usings.Count
if ($CombineUsings) {
$allUsings += $usings
}
$content = Remove-UsingStatements -content $content
}
$content = Remove-CodeElements -content $content `
-stripRegions $StripRegions `
-stripComments $StripComments `
-stripEmptyLines $StripEmptyLines `
-stripAttributes $StripAttributes `
-stripHeaderComments $StripHeaderComments
$processedFiles += @{
Path = Get-RelativePath $_.FullName $projectDir.FullName
Content = $content
}
$stats.TotalSourceFiles++
}
# Add combined usings if enabled
if ($CombineUsings -and -not $StripUsings -and $allUsings.Count -gt 0) {
$combinedUsings = Format-UsingStatements -usings $allUsings
$markdown += "## Global Usings`n`n"
$markdown += '```csharp'
$markdown += "`n"
$markdown += $combinedUsings
$markdown += "`n"
$markdown += '```'
}
# Process markdown files
Write-Progress-Info "Processing markdown files..."
Get-ChildItem -Path $projectDir -Recurse -Filter "*.md" |
Where-Object { $_.Name -ne "$projectName.g.md" -and (Should-ProcessFile -file $_ -skipGenerated $false -excludePatterns $ExcludePatterns) } |
ForEach-Object {
$mdContent = Get-Content $_.FullName -Raw
$relativePath = Get-RelativePath $_.FullName $projectDir.FullName
$markdown += "`n`n## Documentation: $relativePath`n`n"
$markdown += $mdContent
$stats.TotalMarkdownFiles++
}
# Add processed source files
foreach ($file in $processedFiles) {
$markdown += "`n`n## Source: $($file.Path)`n`n"
$markdown += '```csharp'
$markdown += "`n"
$markdown += $file.Content
$markdown += "`n"
$markdown += '```'
}
# Write output file
$outputFile = Join-Path $OutputDirectory "$projectName.g.md"
$markdown | Out-File $outputFile -Encoding UTF8
$stats.FinalSize += (Get-Item $outputFile).Length
Write-Progress-Success "Generated documentation for $projectName"
} -End {
Write-Progress -Activity "Processing Projects" -Completed
}
# Update .gitignore if needed
if ($UpdateGitIgnore) {
$gitignorePath = Join-Path $PSScriptRoot ".gitignore"
$outputDirName = Split-Path $OutputDirectory -Leaf
$ignoreEntry = "/$outputDirName/"
if (Test-Path $gitignorePath) {
$gitignoreContent = Get-Content $gitignorePath
if ($gitignoreContent -notcontains $ignoreEntry) {
Add-Content $gitignorePath "`n$ignoreEntry"
Write-Progress-Info "Added /$outputDirName/ to .gitignore"
}
}
else {
Set-Content $gitignorePath $ignoreEntry
Write-Progress-Info "Created .gitignore with /$outputDirName/"
}
}
# Generate final statistics report
$endTime = Get-Date
$duration = $endTime - $stats.StartTime
$report = @"
Generation Statistics Report
==========================
Duration: $($duration.ToString('hh\:mm\:ss'))
Projects
--------
Total Projects Found: $projectCount
Projects Processed: $($stats.ProjectsProcessed)
Projects Skipped: $($stats.ProjectsSkipped)
Files
-----
Source Files: $($stats.TotalSourceFiles)
Markdown Files: $($stats.TotalMarkdownFiles)
Files Skipped: $($stats.FilesSkipped)
Total Files: $($stats.TotalSourceFiles + $stats.TotalMarkdownFiles)
Total Using Stmts: $($stats.TotalUsings)
Size
----
Original Size: $([math]::Round($stats.OriginalSize / 1KB, 2)) KB
Final Size: $([math]::Round($stats.FinalSize / 1KB, 2)) KB
Size Reduction: $([math]::Round(100 - ($stats.FinalSize / $stats.OriginalSize * 100), 1))%
Output Location: $OutputDirectory
"@
Write-Host $report -ForegroundColor Cyan
Write-Progress-Success "Documentation generation complete!"