-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathGenerateDocs.ps1
More file actions
167 lines (144 loc) · 6.74 KB
/
GenerateDocs.ps1
File metadata and controls
167 lines (144 loc) · 6.74 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
# -----------------------------------------------------------------------------
# PowerShell Script for Blazor Component Documentation
# -----------------------------------------------------------------------------
#
# Description:
# This script generates a markdown file documenting all PD* Blazor components
# in the specified project directory. For each component, it extracts public
# parameters from the C# code-behind file. If the documentation file has
# changed, it will prompt to commit the changes to git.
#
# How to run this script:
# 1. Open a PowerShell terminal in the root of your Blazor project
# 2. You may need to adjust the execution policy to run the script.
# You can do this for the current session by running:
# Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
# 3. Run the script:
# .\GenerateDocs.ps1
#
# Output:
# The script will create a file named ComponentDocumentation.md in the same
# directory. If the file has changed, you will be prompted to commit it.
#
# -----------------------------------------------------------------------------
# --- Configuration ---
# Automatically detect the project root (script location)
$projectRoot = $PSScriptRoot
if ([string]::IsNullOrWhiteSpace($projectRoot)) {
$projectRoot = Get-Location
}
Write-Host "Project Root: $projectRoot" -ForegroundColor Cyan
$componentPath = Join-Path $projectRoot "PanoramicData.Blazor"
$outputFile = Join-Path $projectRoot "ComponentDocumentation.md"
# Validate paths
if (-not (Test-Path $componentPath)) {
Write-Host "Error: Component path not found: $componentPath" -ForegroundColor Red
Write-Host "Please run this script from the project root directory." -ForegroundColor Yellow
exit 1
}
# --- Script Body ---
# Create a temporary file to build the new documentation
$tempOutputFile = [System.IO.Path]::GetTempFileName()
# Add a title to the markdown file
Add-Content -Path $tempOutputFile -Value "# PanoramicData.Blazor Component Documentation"
Add-Content -Path $tempOutputFile -Value ""
Add-Content -Path $tempOutputFile -Value "This document provides an overview of the Blazor components in this project."
Add-Content -Path $tempOutputFile -Value ""
Add-Content -Path $tempOutputFile -Value "Generated on: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"
Add-Content -Path $tempOutputFile -Value ""
# Find all .razor files for the components
$razorFiles = Get-ChildItem -Path $componentPath -Filter "PD*.razor"
Write-Host "Found $($razorFiles.Count) components. Generating documentation..."
foreach ($razorFile in $razorFiles) {
$componentName = $razorFile.BaseName
$csFilePath = "$($razorFile.FullName).cs"
Add-Content -Path $tempOutputFile -Value "## $componentName"
Add-Content -Path $tempOutputFile -Value ""
if (Test-Path $csFilePath) {
$lines = Get-Content $csFilePath
$paramObjects = @()
for ($i = 0; $i -lt $lines.Length; $i++) {
if ($lines[$i].Trim() -eq "[Parameter]") {
# Found a parameter, now find the property definition
for ($j = $i + 1; $j -lt $lines.Length; $j++) {
if ($lines[$j] -match "public\s+([^ ]+)\s+([^ ]+)\s*\{") {
$paramType = $Matches[1]
$paramName = $Matches[2]
# Look for summary comments above the [Parameter] attribute
$comment = ""
for ($k = $i - 1; $k -ge 0; $k--) {
$line = $lines[$k].Trim()
if ($line.StartsWith("/// <summary>")) {
# Found the start of the summary, now extract it
for ($l = $k + 1; $l -lt $i; $l++) {
$summaryLine = $lines[$l].Trim()
if ($summaryLine.StartsWith("/// </summary>")) {
break
}
if ($summaryLine.StartsWith("///")) {
$comment += ($summaryLine -replace "///", "").Trim() + " "
}
}
break
}
if (!$line.StartsWith("///")) {
break
}
}
$paramObjects += [pscustomobject]@{
Name = $paramName
Type = $paramType
Description = $comment.Trim()
}
$i = $j # Continue searching from after the property
break
}
}
}
}
if ($paramObjects.Length -gt 0) {
Add-Content -Path $tempOutputFile -Value "**Parameters:**"
Add-Content -Path $tempOutputFile -Value ""
Add-Content -Path $tempOutputFile -Value "| Name | Type | Description |"
Add-Content -Path $tempOutputFile -Value "|------|------|-------------|"
foreach ($param in $paramObjects) {
Add-Content -Path $tempOutputFile -Value ("| `{0}` | `{1}` | {2} |" -f $param.Name, $param.Type, $param.Description)
}
} else {
Add-Content -Path $tempOutputFile -Value "This component has no public parameters."
}
} else {
Add-Content -Path $tempOutputFile -Value "No code-behind file found for this component."
}
Add-Content -Path $tempOutputFile -Value ""
Add-Content -Path $tempOutputFile -Value "---"
Add-Content -Path $tempOutputFile -Value ""
}
Write-Host "Documentation generation complete."
# --- Git Commit Logic ---
# Compare the new file with the existing file
$isDifferent = $true
if (Test-Path $outputFile) {
$diff = Compare-Object -ReferenceObject (Get-Content $outputFile) -DifferenceObject (Get-Content $tempOutputFile)
if ($null -eq $diff) {
$isDifferent = $false
}
}
if ($isDifferent) {
# Replace the old file with the new one
Move-Item -Path $tempOutputFile -Destination $outputFile -Force
Write-Host "ComponentDocumentation.md has been updated."
$confirmation = Read-Host "Do you want to commit the changes? (y/n)"
if ($confirmation -eq 'y') {
Write-Host "Staging and committing changes..."
git add $outputFile
git commit -m "docs: Update component documentation"
Write-Host "Changes have been committed."
} else {
Write-Host "Changes were not committed."
}
} else {
# Clean up the temporary file
Remove-Item -Path $tempOutputFile
Write-Host "No changes detected in ComponentDocumentation.md."
}