Skip to content

Commit a7262e1

Browse files
authored
Add GenerateCmdletDesignMarkdown.ps1 script (Azure#16390)
* add GenerateCmdletDesignMarkdown.ps1 file. * Modified GenerateCmdletDesignMarkdown.ps1 file. * modified example in GenerateCmdletDesignMarkdown.ps1 * format ps1 file. * modified variable name and add one comments.
1 parent efa0726 commit a7262e1

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<#
2+
.SYNOPSIS
3+
4+
Generate cmdlet design markdown for the specified azure module.
5+
6+
.EXAMPLE
7+
8+
PS> GenerateCmdletDesignMarkdown.ps1 -Path 'azure-powershell\src\Databricks\docs' -OutPath 'azure-powershell\ModuleCmdletDesign' -OutputFileName 'Az.Databricks.Cmdlet.Design.md' -NounPriority 'AzDatabricksWorkspace','AzDatabricksVNetPeering'
9+
10+
Genereated azure-powershell\ModuleCmdletDesign\Az.Databricks.Cmdlet.Design.md completed.
11+
#>
12+
[CmdletBinding()]
13+
param (
14+
[Parameter(Mandatory = $false,
15+
HelpMessage = "The path is the docs folder path. Default current script path if not pass value."
16+
)]
17+
[string]
18+
$Path,
19+
20+
[Parameter(Mandatory = $false,
21+
HelpMessage = "The value the Path parameter and the OutPath parameter are the same if not passed OutPath parameter."
22+
)]
23+
[string]
24+
$OutPath,
25+
26+
[Parameter(Mandatory = $false,
27+
HelpMessage = "Automatic generate output file name if not passed value.")]
28+
[string]
29+
$OutputFileName,
30+
31+
[Parameter(Mandatory = $false,
32+
HelpMessage = "Specify the order of cmdlets in the design document.")]
33+
[string[]]
34+
$NounPriority
35+
)
36+
37+
try {
38+
# If the path parameter is null, let the current path as the value of the path parameter
39+
if (!$PSBoundParameters.ContainsKey("Path")) {
40+
$Path = $PSScriptRoot
41+
}
42+
43+
if (!$PSBoundParameters.ContainsKey("OutPath")) {
44+
$OutPath = $Path
45+
}
46+
47+
if (!$PSBoundParameters.ContainsKey("OutputFileName")) {
48+
$OutputFileName = 'Az.' + (Get-ChildItem -Path $Path -Filter 'Az.*.md' -ErrorAction Stop).Name.Split(".")[1] + 'Cmdlet.Design.md'
49+
}
50+
51+
# Get all name and path of the cmdlets.
52+
Write-Debug "Get all cmdlets md file under the $Path folder."
53+
$cmdlets = Get-ChildItem -Path $Path -Filter '*-*.md' | Select-Object -Property FullName, Name
54+
55+
# Add Cmdlet, Verb, Noun property for sort object.
56+
$cmdlets | Add-Member -NotePropertyName Cmdlet -NotePropertyValue $null
57+
$cmdlets | Add-Member -NotePropertyName Verb -NotePropertyValue $null
58+
$cmdlets | Add-Member -NotePropertyName Noun -NotePropertyValue $null
59+
60+
# set priority for the specified cmdlets.
61+
if ($PSBoundParameters.ContainsKey("NounPriority")) {
62+
$priority = 0
63+
$NounPriorityHash = @{}
64+
foreach ($cmdlet in $NounPriority) {
65+
$NounPriorityHash.Add($cmdlet, $priority++)
66+
}
67+
}
68+
69+
for ($index = 0; $index -lt $cmdlets.Length; $index++) {
70+
# Join 0 prefix with New verb so that make New verb as top item in the same Noun.
71+
$verb = $cmdlets[$index].Name.Split("-")[0] -eq 'New' ? '0New' : $cmdlets[$index].Name.Split("-")[0]
72+
# Join priority with Noun.
73+
$originNoun = $cmdlets[$index].Name.Split("-")[1].Split(".")[0];
74+
$Noun = $null -eq $NounPriorityHash ? $originNoun : ($null -eq $NounPriorityHash[$originNoun] ? $originNoun : $NounPriorityHash[$originNoun].ToString() + $originNoun)
75+
76+
$cmdlets[$index].Cmdlet = $cmdlets[$index].Name.Split(".")[0];
77+
$cmdlets[$index].Verb = $verb
78+
$cmdlets[$index].Noun = $Noun
79+
}
80+
81+
82+
$sortedCmdlets = $cmdlets | Sort-Object -Property @{Expression = "Noun"; Descending = $false }, @{Expression = "Verb"; Descending = $false }
83+
84+
"The sorted cmdles list : " | Write-Debug
85+
$sortedCmdlets | Out-String | Write-Debug
86+
87+
88+
$outFilePath = Join-Path $OutPath $OutputFileName
89+
90+
# Try remove output file
91+
Write-Debug "Delete the $OutputFileName file if it exists."
92+
Remove-Item -Path $outFilePath -ErrorAction SilentlyContinue
93+
94+
95+
# Get cmdlet design message from the Verb-Noun.md file.
96+
foreach ($cmdlet in $sortedCmdlets) {
97+
$content = Get-Content -Path $cmdlet.FullName -ErrorAction Stop
98+
$contentStr = ($content | Out-String)
99+
$designDoc = $contentStr.Substring($contentStr.IndexOf("# $($cmdlet.Cmdlet)"), $contentStr.IndexOf('## PARAMETERS') - $contentStr.IndexOf("# $($cmdlet.Cmdlet)"))
100+
101+
if ($designDoc.Contains("{{ Add title here }}")) {
102+
$designDoc = $designDoc.Remove($designDoc.IndexOf('## DESCRIPTION'))
103+
}
104+
else {
105+
$designDoc = $designDoc.Remove($designDoc.IndexOf('## DESCRIPTION'), $designDoc.IndexOf('## EXAMPLES') - $designDoc.IndexOf('## DESCRIPTION'))
106+
}
107+
108+
$designDoc = $designDoc -replace '```(\r\n\w{1})', '```powershell$1'
109+
$designDoc = $designDoc -replace '###', '+'
110+
$designDoc = $designDoc -replace '#+', '####'
111+
112+
$designDoc | Out-File -FilePath $outFilePath -Append -ErrorAction Stop
113+
}
114+
115+
Write-Host "Genereated $outFilePath completed."
116+
return
117+
}
118+
catch {
119+
throw
120+
}

0 commit comments

Comments
 (0)