Skip to content

Commit bbb27c9

Browse files
Ensure all the metadata is added to CommandHelp (#784)
* Ensure all the metadata is added to CommandHelp * Add test * Fix Yaml import and export tests
1 parent 779a3b0 commit bbb27c9

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

src/Common/YamlUtils.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ internal static CommandHelp ConvertDictionaryToCommandHelp(OrderedDictionary? di
190190
{
191191
help.Metadata = GetMetadataFromDictionary(metadata);
192192
help.Diagnostics.TryAddDiagnostic(DiagnosticMessageSource.General, "Found Metadata", DiagnosticSeverity.Information, "yaml metadata", -1);
193+
194+
help.ExternalHelpFile = metadata["external help file"] as string ?? string.Empty;
195+
help.SchemaVersion = metadata["PlatyPS schema version"] as string ?? string.Empty;
196+
help.OnlineVersionUrl = metadata["HelpUri"] as string ?? string.Empty;
193197
}
194198

195199
if (dictionary["synopsis"] is string synopsis)

src/MarkdownReader/CommandHelpMarkdownReader.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ internal static CommandHelp GetCommandHelpFromMarkdown(ParsedMarkdownContent mar
224224
}
225225

226226
commandHelp.Metadata = metadata;
227+
commandHelp.ExternalHelpFile = metadata["external help file"] as string ?? string.Empty;
228+
commandHelp.SchemaVersion = metadata["PlatyPS schema version"] as string ?? string.Empty;
229+
commandHelp.OnlineVersionUrl = metadata["HelpUri"] as string ?? string.Empty;
230+
231+
string? moduleGuid = metadata["ModuleGuid"] as string;
232+
commandHelp.ModuleGuid = moduleGuid is not null ? new Guid(moduleGuid) : null;
233+
227234
commandHelp.HasCmdletBinding = GetCmdletBindingState(markdownContent, out var cmdletBindingDiagnostics);
228235
if (cmdletBindingDiagnostics is not null)
229236
{

src/Transform/TransformBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ protected CommandHelp ConvertCmdletInfo(CommandInfo? commandInfo)
5454
addDefaultStrings = true;
5555
}
5656

57-
5857
CommandHelp cmdHelp = new(commandInfo.Name, commandInfo.ModuleName, Settings.Locale);
5958
cmdHelp.Metadata = MetadataUtils.GetCommandHelpBaseMetadataFromCommandInfo(commandInfo);
6059
cmdHelp.ExternalHelpFile = cmdHelp.Metadata["external help file"].ToString() ?? string.Empty;
61-
cmdHelp.OnlineVersionUrl = Settings.OnlineVersionUrl;
60+
cmdHelp.OnlineVersionUrl = Settings.OnlineVersionUrl ?? cmdHelp.Metadata["HelpUri"] as string;
61+
cmdHelp.SchemaVersion = cmdHelp.Metadata["PlatyPS schema version"] as string ?? string.Empty;
6262
cmdHelp.Synopsis = GetSynopsis(helpItem, addDefaultStrings);
6363
cmdHelp.AddSyntaxItemRange(GetSyntaxItem(commandInfo, helpItem));
6464
cmdHelp.Description = GetDescription(helpItem, addDefaultStrings).Trim();

test/Pester/ExportMamlCommandHelp.Tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ Describe "Export-MamlCommandHelp tests" {
77
$markdownFiles = 'get-date.md', 'Import-Module.md', 'Invoke-Command.md', 'Out-Null.md'
88
$chObjects = $markdownFiles | Foreach-Object { Import-MarkdownCommandHelp (Join-Path $assetDir $_) }
99
$outputDirectory = Join-Path $TESTDRIVE MamlBase
10-
$f1 = "$outputDirectory/Microsoft.PowerShell.Core/Microsoft.PowerShell.Core-Help.xml"
11-
$f2 = "$outputDirectory/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Utility-Help.xml"
10+
$f1 = "$outputDirectory/Microsoft.PowerShell.Core/System.Management.Automation.dll-Help.xml"
11+
$f2 = "$outputDirectory/Microsoft.PowerShell.Utility/Microsoft.PowerShell.Commands.Utility.dll-Help.xml"
1212
}
1313

1414
Context "Basic Operations" {

test/Pester/ImportMarkdownCommandHelp.Tests.ps1

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ Describe 'Import-MarkdownCommandHelp Tests' {
6565
It 'Should be able to read multiline metadata' {
6666
$md.Metadata['foo'] | Should -Be "bar"
6767
}
68+
69+
It 'Should be able to set all metadata properties' {
70+
$m = Import-MarkdownCommandHelp -Path "$assetdir/get-date.md"
71+
$m.ExternalHelpFile | Should -Be "Microsoft.PowerShell.Commands.Utility.dll-Help.xml"
72+
$m.Locale | Should -Be "en-US"
73+
$m.SchemaVersion | Should -Be "2024-05-01"
74+
$m.OnlineVersionUrl | Should -Be "https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/get-date?view=powershell-7.4&WT.mc_id=ps-gethelp"
75+
$m.ModuleGuid | Should -BeNullOrEmpty
76+
77+
$maml = $m | Export-MamlCommandHelp -OutputFolder $TestDrive -Force -Verbose
78+
$maml.Name | Should -Be 'Microsoft.PowerShell.Commands.Utility.dll-Help.xml'
79+
}
6880
}
6981

7082
Context "Validate Aliases" {

0 commit comments

Comments
 (0)