Skip to content

Commit 27d5ffe

Browse files
authored
Additional updates for OPS release (#664)
Fix issue with module parsing when schema was not present. Fix up tests for same and add exchange module file (for tests against multiple command groups. Refactor a number of files. Change metadata dictionary to be <string,object> rather than <string,string>. Add handling of HelpUri and HelpInfoUri to new-markdowncommandhelp (transform settings does not preserve these). Update metadata serializer to handle arrays in flow format. Add a reasonable implementation of ToString for DiagnosticMessages to improve formatting. Be sure to include aliases in commandhelp. Add support for metadata as a paragraph in addition to normal handling. Add a number of diagnostic messages throughout. Change alias boilerplate to appear only if the ALIAS header was missing. This means that the first conversion from V1 -> V2 will include boilerplate, but V2 -> V2 will not. Don't add empty lines for the description of ModuleCommands, only if description is present. Add a number of format entries for DiagnosticMessage, Iditify, Diagnostics (now a custom format). Add Update-MarkdownModuleFile, Measure-PlatyPSMarkdown (identify what kind of markdown we have). * Add metadata during import of MAML file. * Change parameter name to from OutputDirectory to OutputFolder for Export-MamlCommandHelp. Be sure to save command help notes as AlertSet. Be sure to create metadata when importing MAML file. * fix issue in Measure-PlatyPSMarkdown. It was not identifying a V2 module file. Add a test for same. * Set diagnostics and ParameterNames as skipped property names for Compare-CommandHelp. * updates to helpinfouri and helpuri. Be sure that they go in the right file. * use CurrentCulture rather than CurrentUICulture * Be sure to default metadata Locale to en-US. * Skip implicit loading of module in Windows PowerShell. * When testing, be sure to execute the right powershell. * Fixes to build script to ensure testing on Windows PowerShell. * Rewrite of tests to support running on Windows PowerShell.
1 parent 6313253 commit 27d5ffe

File tree

55 files changed

+4810
-665
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+4810
-665
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"files.exclude": {
44
"out/**": true
55
},
6-
"dotnet.defaultSolution": "platyPS.sln"
6+
"dotnet.defaultSolution": "platyPS.sln",
7+
"sarif-viewer.connectToGithubCodeScanning": "off"
78
}

build.ps1

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ if ($PSCmdlet.ParameterSetName -eq 'Build') {
7171
$moduleFiles += $expectedPdbPath
7272
}
7373

74-
Copy-Item -Path "$expectedBuildPath/Markdig.Signed.dll", "$expectedBuildPath/YamlDotNet.dll" -Destination $depsFolder -Verbose
74+
$neededAssemblies = "System.Buffers.dll", "System.Memory.dll",
75+
"System.Numerics.Vectors.dll", "System.Runtime.CompilerServices.Unsafe.dll",
76+
"Markdig.Signed.dll","YamlDotNet.dll"
77+
78+
$neededAssemblyLocations = $neededAssemblies | ForEach-Object { "${expectedBuildPath}/${_}" }
79+
80+
Copy-Item -Path $neededAssemblyLocations -Destination $depsFolder -Verbose
7581
Copy-Item -Path $moduleFiles -Destination $moduleRoot -Verbose
7682
}
7783
finally {
@@ -91,7 +97,10 @@ elseif ($PSCmdlet.ParameterSetName -eq 'Test') {
9197

9298
write-verbose -verbose -message "$sb"
9399

94-
pwsh -noprofile -c "$sb"
100+
# we need to run on both pwsh and powershell
101+
$PSEXE = (Get-Process -Id $PID).MainModule.FileName
102+
Write-Verbose -Verbose -Message ("Running tests on PowerShell Version: " + $PSVersionTable.PSVersion)
103+
& $PSEXE -noprofile -c "$sb"
95104

96105
$results = [xml](Get-Content $PesterLogPath)
97106
if ($results."test-results".failures -ne 0) {
@@ -114,4 +123,4 @@ if ($Package) {
114123
Unregister-PSRepository -Name $localRepoName -ErrorAction SilentlyContinue
115124
}
116125
Get-ChildItem -Path $PSScriptRoot/*.nupkg
117-
}
126+
}

src/Command/CompareCommandHelpCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Management.Automation;
1111
using Microsoft.PowerShell.PlatyPS.Model;
1212
using System.Linq;
13+
using System.Runtime.InteropServices;
1314

1415
namespace Microsoft.PowerShell.PlatyPS
1516
{
@@ -31,7 +32,7 @@ public class CompareCommandHelpCommand : PSCmdlet
3132
public CommandHelp? Difference { get; set; }
3233

3334
[Parameter]
34-
public string[] PropertyNamesToExclude { get; set; } = new string[0];
35+
public string[] PropertyNamesToExclude { get; set; } = new string[] { "Diagnostics", "ParameterNames" };
3536

3637
List<String> DiagnosticMessages = new();
3738

src/Command/ExportMamlCommandHelp.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public sealed class ExportMamlCommandHelpCommand : PSCmdlet
3636
public SwitchParameter Force { get; set; }
3737

3838
[Parameter(Mandatory = true, Position = 1)]
39-
public string OutputDirectory { get; set; } = string.Empty;
39+
public string OutputFolder { get; set; } = string.Empty;
4040

4141
private List<CommandHelp> _commandHelps = new List<CommandHelp>();
4242
#endregion
@@ -57,9 +57,9 @@ protected override void ProcessRecord()
5757

5858
protected override void EndProcessing()
5959
{
60-
if (ShouldProcess(OutputDirectory))
60+
if (ShouldProcess(OutputFolder))
6161
{
62-
outputDirectory = PathUtils.CreateOrGetOutputDirectory(this, OutputDirectory, Force);
62+
outputDirectory = PathUtils.CreateOrGetOutputDirectory(this, OutputFolder, Force);
6363
}
6464
else
6565
{
@@ -69,7 +69,7 @@ protected override void EndProcessing()
6969

7070
if (outputDirectory is null)
7171
{
72-
ThrowTerminatingError(new ErrorRecord(new InvalidOperationException("file is null"), "fileInfo is null", ErrorCategory.InvalidOperation, OutputDirectory));
72+
ThrowTerminatingError(new ErrorRecord(new InvalidOperationException("file is null"), "fileInfo is null", ErrorCategory.InvalidOperation, OutputFolder));
7373
throw new InvalidOperationException("fileInfo is null"); // not reached
7474
}
7575

src/Command/ExportMarkdownModuleFileCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protected override void ProcessRecord()
9292
}
9393
else
9494
{
95-
moduleFile.Metadata[key] = (string)kv.Value;
95+
moduleFile.Metadata[key] = kv.Value;
9696
}
9797
}
9898
}

src/Command/ExportYamlModuleFileCommand.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
using System;
55
using System.Collections;
6-
using System.Collections.ObjectModel;
7-
using System.Globalization;
86
using System.IO;
97
using System.Management.Automation;
108
using Microsoft.PowerShell.PlatyPS.Model;
@@ -59,7 +57,7 @@ protected override void ProcessRecord()
5957

6058
foreach (ModuleFileInfo moduleFile in ModuleFile)
6159
{
62-
if (moduleFile.Metadata.ContainsKey("Module Name"))
60+
if (moduleFile.Metadata.Contains("Module Name"))
6361
{
6462
moduleName = moduleFile.Metadata["Module Name"].ToString();
6563
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Management.Automation;
7+
using Microsoft.PowerShell.PlatyPS.Model;
8+
9+
namespace Microsoft.PowerShell.PlatyPS
10+
{
11+
/// <summary>
12+
/// Cmdlet to determine the type of markdown file.
13+
/// </summary>
14+
[Cmdlet(VerbsDiagnostic.Measure, "PlatyPSMarkdown", DefaultParameterSetName = "Path", HelpUri = "")]
15+
[OutputType(typeof(CommandHelp))]
16+
public sealed class MeasurePlatyPSMarkdown : PSCmdlet
17+
{
18+
#region cmdlet parameters
19+
20+
[Parameter(Mandatory=true, Position=0, ValueFromPipeline=true, ParameterSetName= "Path")]
21+
[ValidateNotNullOrEmpty]
22+
[SupportsWildcards]
23+
public string[] Path { get; set; } = Array.Empty<string>();
24+
25+
[Parameter(Mandatory=true, ValueFromPipeline=true, ParameterSetName= "LiteralPath")]
26+
[ValidateNotNullOrEmpty]
27+
public string[] LiteralPath { get; set; } = Array.Empty<string>();
28+
29+
#endregion
30+
31+
protected override void ProcessRecord()
32+
{
33+
List<string> resolvedPaths;
34+
try
35+
{
36+
// This is a list because the resolution process can result in multiple paths (in the case of non-literal path).
37+
resolvedPaths = PathUtils.ResolvePath(this, ParameterSetName == "LiteralPath" ? LiteralPath : Path, ParameterSetName == "LiteralPath" ? true : false);
38+
}
39+
catch (Exception e)
40+
{
41+
WriteError(new ErrorRecord(e, "Could not resolve Path", ErrorCategory.InvalidOperation, ParameterSetName == "LiteralPath" ? LiteralPath : Path));
42+
return;
43+
}
44+
45+
// These should be resolved paths, whether -LiteralPath was used or not.
46+
foreach (string path in resolvedPaths)
47+
{
48+
try
49+
{
50+
var markdownInfo = MarkdownProbe.Identify(path);
51+
WriteObject(markdownInfo);
52+
}
53+
catch (Exception e)
54+
{
55+
WriteError(new ErrorRecord(e, "FailedToImportMarkdown", ErrorCategory.InvalidOperation, path));
56+
}
57+
}
58+
}
59+
}
60+
}

src/Command/NewMarkdownHelpCommand.cs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using System.Linq;
1111
using System.Management.Automation;
1212
using System.Management.Automation.Runspaces;
13-
13+
using Microsoft.PowerShell.Commands;
1414
using Microsoft.PowerShell.PlatyPS.MarkdownWriter;
1515
using Microsoft.PowerShell.PlatyPS.Model;
1616

@@ -118,6 +118,10 @@ protected override void EndProcessing()
118118

119119
List<CommandHelp> cmdHelpObjs = new List<CommandHelp>();
120120
Dictionary<string, PSModuleInfo> moduleTable = new();
121+
foreach (var providedModule in Module)
122+
{
123+
moduleTable[providedModule.Name] = providedModule;
124+
}
121125

122126
if (cmdCollection.Count > 0)
123127
{
@@ -126,7 +130,6 @@ protected override void EndProcessing()
126130
{
127131
if (cmd.Module is not null && ! string.IsNullOrEmpty(cmd.Module.Name) && ! moduleTable.ContainsKey(cmd.Module.Name))
128132
{
129-
moduleTable[cmd.Module.Name] = cmd.Module;
130133
if (HelpInfoUri == string.Empty && ! string.IsNullOrEmpty(cmd.Module.HelpInfoUri))
131134
{
132135
HelpInfoUri = cmd.Module.HelpInfoUri;
@@ -141,7 +144,7 @@ protected override void EndProcessing()
141144
ExcludeDontShow = false,
142145
FwLink = HelpInfoUri,
143146
HelpVersion = HelpVersion.ToString(),
144-
Locale = Locale is null ? CultureInfo.CurrentUICulture : new CultureInfo(Locale),
147+
Locale = Locale is null ? CultureInfo.CurrentCulture : new CultureInfo(Locale),
145148
ModuleName = cmd.ModuleName is null ? string.Empty : cmd.ModuleName,
146149
ModuleGuid = cmd.Module?.Guid is null ? Guid.Empty : cmd.Module.Guid,
147150
OnlineVersionUrl = HelpUri,
@@ -154,7 +157,25 @@ protected override void EndProcessing()
154157
var pr = new ProgressRecord(0, "Transforming cmdlet", $"{cmd.ModuleName}\\{cmd.Name}");
155158
pr.PercentComplete = (int)Math.Round(((double)currentOffset++ / (double)(cmdCollection.Count)) * 100);
156159
WriteProgress(pr);
157-
cmdHelpObjs.Add(new TransformCommand(transformSettings).Transform(cmd));
160+
var transformedCommand = new TransformCommand(transformSettings).Transform(cmd);
161+
162+
if (transformedCommand is null)
163+
{
164+
throw new InvalidDataException("Command transformation failed");
165+
}
166+
167+
if (transformedCommand.Metadata is null)
168+
{
169+
transformedCommand.Metadata = new();
170+
}
171+
172+
// update the HelpUri if the parameter was used.
173+
if (! string.IsNullOrEmpty(HelpUri))
174+
{
175+
transformedCommand.Metadata["HelpUri"] = HelpUri;
176+
}
177+
178+
cmdHelpObjs.Add(transformedCommand);
158179
}
159180
catch (Exception e)
160181
{
@@ -199,6 +220,11 @@ protected override void EndProcessing()
199220
}
200221
}
201222

223+
if (! string.IsNullOrEmpty(HelpInfoUri))
224+
{
225+
moduleFileInfo.Metadata["HelpInfoUri"] = HelpInfoUri;
226+
}
227+
202228
List<ModuleCommandInfo> mciList = new();
203229
foreach(var cmdHelp in group)
204230
{

0 commit comments

Comments
 (0)