-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathUpdate-ModifiedCmdletDocs.ps1
More file actions
170 lines (143 loc) · 5.97 KB
/
Update-ModifiedCmdletDocs.ps1
File metadata and controls
170 lines (143 loc) · 5.97 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
<#
.SYNOPSIS
Updates markdown documentation for modified PowerShell cmdlets based on git changes.
.DESCRIPTION
This script checks git status for modified .ps1 files in the Public directory and automatically
updates their corresponding markdown documentation files.
.PARAMETER Path
The base path to the repository. Defaults to current directory.
.PARAMETER DocumentationPath
Path to the documentation directory. Defaults to "./Documentation/"
.PARAMETER PublicPath
Path to the Public cmdlets directory. Defaults to "./Public/"
.PARAMETER IncludeStaged
Include staged files in addition to unstaged changes. Default is $true.
.EXAMPLE
.\Update-ModifiedCmdletDocs.ps1
.EXAMPLE
.\Update-ModifiedCmdletDocs.ps1 -Path "C:\Projects\Logic.Monitor"
#>
[CmdletBinding()]
param(
[string]$Path = ".",
[string]$DocumentationPath = "$PSScriptRoot\Documentation\",
[string]$PublicPath = "$PSScriptRoot\Public\",
[bool]$IncludeStaged = $true,
[bool]$RecreateUpdatedDocs = $true
)
# Change to the repository directory
Push-Location $Path
try {
# Dynamically find the Dev module file (Dev.*.psd1)
$devModule = Get-ChildItem -Path $PSScriptRoot -Filter "Dev.*.psd1" -File | Select-Object -First 1
if (-not $devModule) {
throw "No Dev module file (Dev.*.psd1) found in $PSScriptRoot"
}
Write-Host "Loading module: $($devModule.Name)" -ForegroundColor Cyan
Import-Module $devModule.FullName -Force
# Extract the production module name (remove "Dev." prefix)
$productionModuleName = $devModule.BaseName -replace '^Dev\.', ''
Write-Host "Production module name: $productionModuleName" -ForegroundColor Gray
}
catch {
Write-Error "Failed to load Dev module: $_"
Pop-Location
return
}
try {
Write-Host "Checking for modified PowerShell files..." -ForegroundColor Cyan
# Get modified files from git (both staged and unstaged)
$gitStatus = if ($IncludeStaged) {
git status --porcelain
} else {
git status --porcelain | Where-Object { $_ -match '^\s*M' }
}
# Filter for .ps1 files in the Public directory
$modifiedCmdlets = $gitStatus | Where-Object {
$_ -match "Public/.*\.ps1$"
} | ForEach-Object {
# Extract filename without path and extension
if ($_ -match "Public/([^/]+)\.ps1") {
$matches[1]
}
} | Select-Object -Unique
if (-not $modifiedCmdlets) {
Write-Host "No modified cmdlet files found in git status." -ForegroundColor Yellow
return
}
Write-Host "Found $($modifiedCmdlets.Count) modified cmdlet(s):" -ForegroundColor Green
$modifiedCmdlets | ForEach-Object { Write-Host " - $_" -ForegroundColor Gray }
Write-Host ""
# Update markdown documentation for each modified cmdlet
$successCount = 0
$failCount = 0
$createCount = 0
foreach ($cmdlet in $modifiedCmdlets) {
$mdPath = Join-Path $DocumentationPath "$cmdlet.md"
if (Test-Path $mdPath) {
try {
Write-Host "Updating $mdPath..." -ForegroundColor Cyan
if ($RecreateUpdatedDocs) {
#Delete the existing documentation file
Remove-Item $mdPath -ErrorAction SilentlyContinue
#Create the new documentation file
New-MarkdownHelp -Command $cmdlet -OutputFolder $DocumentationPath -ErrorAction Stop | Out-Null
} else {
Update-MarkdownHelp -Path $mdPath -Force -ErrorAction Stop | Out-Null
}
# Fix module name in the generated markdown file
$content = Get-Content $mdPath -Raw
$content = $content -replace "Module Name: $($devModule.BaseName)", "Module Name: $productionModuleName"
Set-Content -Path $mdPath -Value $content -NoNewline
Write-Host " ✓ Successfully updated $cmdlet.md" -ForegroundColor Green
$successCount++
}
catch {
Write-Host " ✗ Failed to update $cmdlet.md: $_" -ForegroundColor Red
$failCount++
}
}
else {
Write-Host "Creating $mdPath..." -ForegroundColor Cyan
Try {
New-MarkdownHelp -Command $cmdlet -OutputFolder $DocumentationPath -ErrorAction Stop | Out-Null
# Fix module name in the generated markdown file
$content = Get-Content $mdPath -Raw
$content = $content -replace "Module Name: $($devModule.BaseName)", "Module Name: $productionModuleName"
Set-Content -Path $mdPath -Value $content -NoNewline
Write-Host " ✓ Successfully created $cmdlet.md" -ForegroundColor Green
$createCount++
}
catch {
Write-Host " ✗ Failed to create $cmdlet.md: $_" -ForegroundColor Red
$failCount++
}
}
}
Write-Host ""
Write-Host "Summary:" -ForegroundColor Cyan
Write-Host " Updated: $successCount" -ForegroundColor Green
Write-Host " Created: $createCount" -ForegroundColor Green
if ($failCount -gt 0) {
Write-Host " Failed/Missing: $failCount" -ForegroundColor Yellow
}
# Regenerate external help after all documentation updates
if ($successCount -gt 0 -or $createCount -gt 0) {
Write-Host ""
Write-Host "Regenerating external help files..." -ForegroundColor Cyan
try {
$enUSPath = Join-Path $PSScriptRoot "en-US"
New-ExternalHelp -OutputPath $enUSPath -Path $DocumentationPath -Force | Out-Null
Write-Host " ✓ Successfully regenerated external help in $enUSPath" -ForegroundColor Green
}
catch {
Write-Host " ✗ Failed to regenerate external help: $_" -ForegroundColor Red
}
}
}
catch {
Write-Error "An error occurred: $_"
}
finally {
Pop-Location
}