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*- \[`(?[\w.-]+(?: [\w.-]+)*)`\]\((?:\./)?(?[\w.-]+\.md)\)') $switch = '(?i)(?:^|[\s\[\{|,`*])(?[-/+][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, '\[(?