160 lines
7.3 KiB
PowerShell
160 lines
7.3 KiB
PowerShell
param(
|
|
[Parameter(Mandatory)][string]$DocumentationCheckout,
|
|
[string]$Output = "$PSScriptRoot/../src/files_windows_command_catalog.h"
|
|
)
|
|
$ErrorActionPreference = 'Stop'
|
|
$docs = Join-Path $DocumentationCheckout 'WindowsServerDocs/administration/windows-commands'
|
|
$revision = git -C $DocumentationCheckout rev-parse HEAD
|
|
if ($LASTEXITCODE) { throw 'Could not determine the documentation revision.' }
|
|
$index = Get-Content -LiteralPath (Join-Path $docs 'windows-commands.md') -Raw
|
|
$entries = @{}
|
|
$articles = @{}
|
|
|
|
function Add-Entry([string]$Name) {
|
|
if (!$entries.ContainsKey($Name)) {
|
|
$entries[$Name] = [Collections.Generic.SortedSet[string]]::new([StringComparer]::OrdinalIgnoreCase)
|
|
}
|
|
}
|
|
|
|
function Add-CommandPath([string]$Name, [string]$Article) {
|
|
Add-Entry $Name
|
|
if (!$articles.ContainsKey($Name)) { $articles[$Name] = $Article }
|
|
$words = $Name.Split(' ')
|
|
for ($i = 1; $i -lt $words.Count; ++$i) {
|
|
$parent = $words[0..($i - 1)] -join ' '
|
|
Add-Entry $parent
|
|
[void]$entries[$parent].Add($words[$i])
|
|
}
|
|
}
|
|
|
|
function Add-Options([string]$Name, [string]$Field) {
|
|
$field = $Field -replace '<[^<>\r\n]*>', ''
|
|
foreach ($option in [regex]::Matches($field, $switch)) {
|
|
[void]$entries[$Name].Add($option.Groups['flag'].Value)
|
|
}
|
|
}
|
|
|
|
# Only extract literal command names and options. Ignore prose, examples and placeholders.
|
|
$links = [regex]::Matches($index, '(?m)^\s*- \[`(?<name>[\w.-]+(?: [\w.-]+)*)`\]\((?:\./)?(?<file>[\w.-]+\.md)\)')
|
|
$switch = '(?i)(?:^|[\s\[\{|,`*])(?<flag>[-/+][a-z0-9?][a-z0-9?-]*(?::[a-z0-9]+|[:=])?|[a-z][a-z0-9-]*=)(?=$|[\s\[\]{}|,<>=)`*])'
|
|
$documents = @{}
|
|
foreach ($link in $links) {
|
|
$name = ($link.Groups['name'].Value.ToLowerInvariant() -replace '\.exe(?= |$)', '')
|
|
$documents[$name] = $link.Groups['file'].Value
|
|
}
|
|
foreach ($name in @($documents.Keys)) {
|
|
if ($name.Contains(' ')) { continue }
|
|
$file = Join-Path $docs $documents[$name]
|
|
if (!(Test-Path -LiteralPath $file)) { continue }
|
|
$text = Get-Content -LiteralPath $file -Raw
|
|
foreach ($link in [regex]::Matches($text, '\[(?<label>[\w./ -]+)\]\((?:\./)?(?<file>[\w.-]+\.md)\)')) {
|
|
$childFile = $link.Groups['file'].Value
|
|
$label = $link.Groups['label'].Value.ToLowerInvariant()
|
|
if (!$childFile.StartsWith($name + '-') -or !$label.StartsWith($name + ' ')) { continue }
|
|
$childPath = Join-Path $docs $childFile
|
|
if (!(Test-Path -LiteralPath $childPath)) { continue }
|
|
$firstArgument = [regex]::Match((Get-Content -LiteralPath $childPath -Raw),
|
|
'(?m)^' + [regex]::Escape($name) + '(?:\.exe)?\s+\[?(?<operation>/[\w-]+)(?=[\]\s]|$)')
|
|
if ($firstArgument.Success -and $label -eq ($name + ' ' + $firstArgument.Groups['operation'].Value.TrimStart('/'))) {
|
|
$label = $name + ' ' + $firstArgument.Groups['operation'].Value
|
|
}
|
|
if (!$documents.ContainsKey($label)) { $documents[$label] = $childFile }
|
|
}
|
|
}
|
|
|
|
foreach ($name in $documents.Keys) {
|
|
$article = Join-Path $docs $documents[$name]
|
|
if (!(Test-Path -LiteralPath $article)) { continue }
|
|
$text = Get-Content -LiteralPath $article -Raw
|
|
$slug = [IO.Path]::GetFileNameWithoutExtension($article)
|
|
Add-CommandPath $name $slug
|
|
$articles[$name] = $slug
|
|
$syntax = [regex]::Matches($text, '(?ims)^(?:#{2,6}[^\r\n]*\b)?syntax[ \t]*\r?\n(?<body>.*?)(?=^#{1,6} |\z)')
|
|
$hasChildren = $false
|
|
$root = $name.Split(' ')[0]
|
|
$rootPattern = '^\s*' + [regex]::Escape($root) + '(?:\.exe)?(?=\s|$)\s*'
|
|
$blocks = @(
|
|
foreach ($section in $syntax) {
|
|
[regex]::Matches($section.Groups['body'].Value, '(?ms)^```[^\r\n]*\r?\n(?<code>.*?)^```') |
|
|
ForEach-Object { $_.Groups['code'].Value }
|
|
}
|
|
)
|
|
foreach ($block in $blocks) {
|
|
if ($block -match ($rootPattern + '\[\{')) {
|
|
$operations = [regex]::Matches($block,
|
|
'(?is)\[\{\s*(?<names>[a-z0-9-]+(?:\s*\|\s*[a-z0-9-]+)+)\s*\}(?<options>.*?)(?=\[\{|\z)')
|
|
foreach ($operation in $operations) {
|
|
foreach ($operationName in ($operation.Groups['names'].Value -split '\s*\|\s*')) {
|
|
$context = $name + ' ' + $operationName.ToLowerInvariant()
|
|
Add-CommandPath $context $slug
|
|
Add-Options $context $operation.Groups['options'].Value
|
|
$hasChildren = $true
|
|
}
|
|
}
|
|
if ($operations.Count) { continue }
|
|
}
|
|
$command = $name
|
|
foreach ($line in ($block -split '\r?\n')) {
|
|
if ($line -match $rootPattern) {
|
|
$tail = $line -replace $rootPattern, ''
|
|
$command = $root
|
|
while ($tail -match '^\[?(?<word>[a-z0-9][a-z0-9-]*)\]?(?=\s|$)(?!\s*\|)\s*') {
|
|
$command += ' ' + $Matches.word.ToLowerInvariant()
|
|
$tail = $tail.Substring($Matches[0].Length)
|
|
}
|
|
if ($command.Length -lt $name.Length) {
|
|
$command = $name
|
|
$operation = $name.Substring($root.Length).Trim()
|
|
if ($operation.StartsWith('/')) {
|
|
$tail = $tail -replace ('^\[?' + [regex]::Escape($operation) + '\]?(?=\s|$)\s*'), ''
|
|
}
|
|
}
|
|
Add-CommandPath $command $slug
|
|
if ($command.StartsWith($name + ' ')) { $hasChildren = $true }
|
|
if ($tail -match '^[\[{]+\s*(?<choices>[a-z0-9?-]+(?:\s*\|\s*[a-z0-9?-]+)+)\s*[\]}]') {
|
|
foreach ($choice in ($Matches.choices -split '\s*\|\s*')) {
|
|
[void]$entries[$command].Add($choice)
|
|
}
|
|
$hasChildren = $true
|
|
}
|
|
Add-Options $command $tail
|
|
} else {
|
|
Add-Options $command $line
|
|
}
|
|
foreach ($choice in [regex]::Matches($line, '(?<flag>[/\-][a-z][a-z0-9-]*)\s+\{(?<values>[a-z0-9_-]+(?:\s*\|\s*[a-z0-9_-]+)+)\}', 'IgnoreCase')) {
|
|
$context = $command + ' ' + $choice.Groups['flag'].Value.ToLowerInvariant()
|
|
Add-CommandPath $context $slug
|
|
foreach ($value in ($choice.Groups['values'].Value -split '\s*\|\s*')) {
|
|
[void]$entries[$context].Add($value)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (!$hasChildren) {
|
|
foreach ($parameter in [regex]::Matches($text, '(?m)^\|\s*(?<value>[^|\r\n]+)\|')) {
|
|
$field = $parameter.Groups['value'].Value
|
|
if ($name.Contains(' /') -and $field.Trim() -eq $name.Split(' ')[-1]) { continue }
|
|
Add-Options $name $field
|
|
}
|
|
}
|
|
}
|
|
|
|
[string[]]$names = $entries.Keys
|
|
[Array]::Sort($names, [StringComparer]::Ordinal)
|
|
$lines = @(
|
|
'#pragma once'
|
|
'#include <string_view>'
|
|
''
|
|
'namespace ks::files'
|
|
'{'
|
|
'// Generated by tools/generate-windows-command-completion.ps1 from Microsoft Learn Windows Commands.'
|
|
"// https://github.com/MicrosoftDocs/windowsserverdocs/tree/$revision/WindowsServerDocs/administration/windows-commands"
|
|
'inline constexpr std::string_view windowsCommandCatalog[] = {'
|
|
foreach ($name in $names) {
|
|
' R"catalog(' + $name + "`t" + ($entries[$name] -join ' ') + "`t" + $articles[$name] + ')catalog",'
|
|
}
|
|
'};'
|
|
'}'
|
|
)
|
|
$lines | Set-Content -LiteralPath $Output -Encoding ascii
|