-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathNewMarkownHelpCommand.cs
211 lines (172 loc) · 8.5 KB
/
NewMarkownHelpCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using Microsoft.PowerShell.PlatyPS.MarkdownWriter;
using Microsoft.PowerShell.PlatyPS.Model;
namespace Microsoft.PowerShell.PlatyPS
{
/// <summary>
/// Cmdlet to generate the markdown help for commands, all commands in a module or from a MAML file.
/// </summary>
[Cmdlet(VerbsCommon.New, "MarkdownHelp", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096483")]
[OutputType(typeof(FileInfo[]))]
public sealed class NewMarkdownHelpCommand : PSCmdlet
{
#region cmdlet parameters
[Parameter(Mandatory = true, ParameterSetName = "FromCommand")]
public string[] Command { get; set; } = Array.Empty<string>();
[Parameter()]
public System.Text.Encoding Encoding { get; set; } = new System.Text.UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
[Parameter()]
public SwitchParameter Force { get; set; }
[Parameter(ParameterSetName = "FromModule")]
[Parameter(ParameterSetName = "FromMaml")]
public string? FwLink { get; set; }
[Parameter(ParameterSetName = "FromModule")]
[Parameter(ParameterSetName = "FromMaml")]
public string? HelpVersion { get; set; }
[Parameter(ParameterSetName = "FromModule")]
[Parameter(ParameterSetName = "FromMaml")]
public string? Locale { get; set; }
[Parameter(Mandatory = true, ParameterSetName = "FromMaml")]
public string[] MamlFile { get; set; } = Array.Empty<string>();
[Parameter()]
public Hashtable? Metadata { get; set; }
[Parameter(Mandatory = true, ValueFromPipeline = true, ParameterSetName = "FromModule")]
public string[] Module { get; set; } = Array.Empty<string>();
[Parameter(ParameterSetName = "FromMaml")]
public string? ModuleGuid { get; set; }
[Parameter(ParameterSetName = "FromMaml")]
public string? ModuleName { get; set; }
[Parameter()]
public SwitchParameter NoMetadata { get; set; }
[Parameter(ParameterSetName = "FromCommand")]
public string? OnlineVersionUrl { get; set; }
[Parameter(Mandatory = true)]
public string OutputFolder { get; set; } = Environment.CurrentDirectory;
[Parameter(ParameterSetName = "FromModule")]
[Parameter(ParameterSetName = "FromMaml")]
public SwitchParameter WithModulePage { get; set; }
[Parameter(ParameterSetName = "FromMaml")]
public SwitchParameter ConvertNotesToList { get; set; }
[Parameter(ParameterSetName = "FromMaml")]
public SwitchParameter ConvertDoubleDashLists { get; set; }
[Parameter()]
public SwitchParameter AlphabeticParamsOrder { get; set; } = true;
[Parameter()]
public SwitchParameter UseFullTypeName { get; set; }
[Parameter(ParameterSetName = "FromModule")]
[Parameter(ParameterSetName = "FromCommand")]
public PSSession? Session { get; set; }
[Parameter(ParameterSetName = "FromModule")]
[Parameter(ParameterSetName = "FromMaml")]
public string? ModulePagePath { get; set; }
[Parameter()]
public SwitchParameter ExcludeDontShow { get; set; }
#endregion
protected override void BeginProcessing()
{
if (Metadata is not null && NoMetadata)
{
var exception = new InvalidOperationException(Microsoft_PowerShell_PlatyPS_Resources.NoMetadataAndMetadata);
ErrorRecord err = new ErrorRecord(exception, "NoMetadataAndMetadata", ErrorCategory.InvalidOperation, Metadata);
ThrowTerminatingError(err);
}
}
protected override void EndProcessing()
{
string fullPath = this.SessionState.Path.GetUnresolvedProviderPathFromPSPath(OutputFolder);
if (File.Exists(fullPath))
{
var exception = new InvalidOperationException(string.Format(Microsoft_PowerShell_PlatyPS_Resources.PathIsNotFolder, fullPath));
ErrorRecord err = new ErrorRecord(exception, "PathIsNotFolder", ErrorCategory.InvalidOperation, fullPath);
ThrowTerminatingError(err);
}
if (!Directory.Exists(fullPath))
{
Directory.CreateDirectory(fullPath);
}
Collection<CommandHelp>? cmdHelpObjs = null;
TransformSettings transformSettings = new TransformSettings
{
AlphabeticParamsOrder = AlphabeticParamsOrder,
CreateModulePage = WithModulePage,
DoubleDashList = ConvertDoubleDashLists,
ExcludeDontShow = ExcludeDontShow,
FwLink = FwLink,
HelpVersion = HelpVersion,
Locale = Locale is null ? CultureInfo.GetCultureInfo("en-US") : new CultureInfo(Locale),
ModuleGuid = ModuleGuid is null ? null : Guid.Parse(ModuleGuid),
ModuleName = ModuleName,
OnlineVersionUrl = OnlineVersionUrl,
Session = Session,
UseFullTypeName = UseFullTypeName
};
try
{
if (string.Equals(this.ParameterSetName, "FromCommand", StringComparison.OrdinalIgnoreCase))
{
if (Command.Length > 0)
{
cmdHelpObjs = new TransformCommand(transformSettings).Transform(Command);
}
}
else if (string.Equals(this.ParameterSetName, "FromModule", StringComparison.OrdinalIgnoreCase))
{
if (Module.Length > 0)
{
cmdHelpObjs = new TransformModule(transformSettings).Transform(Module);
}
}
else if (string.Equals(this.ParameterSetName, "FromMaml", StringComparison.OrdinalIgnoreCase))
{
if (MamlFile.Length > 0)
{
cmdHelpObjs = new TransformMaml(transformSettings).Transform(MamlFile);
}
}
}
catch (ItemNotFoundException infe)
{
var exception = new ItemNotFoundException(string.Format(Microsoft_PowerShell_PlatyPS_Resources.ModuleNotFound, infe.Message));
ErrorRecord err = new ErrorRecord(exception, "ModuleNotFound", ErrorCategory.ObjectNotFound, infe.Message);
ThrowTerminatingError(err);
}
catch (CommandNotFoundException cnfe)
{
var exception = new CommandNotFoundException(string.Format(Microsoft_PowerShell_PlatyPS_Resources.CommandNotFound, cnfe.CommandName));
ErrorRecord err = new ErrorRecord(exception, "CommandNotFound", ErrorCategory.ObjectNotFound, cnfe.CommandName);
ThrowTerminatingError(err);
}
catch (FileNotFoundException fnfe)
{
var exception = new CommandNotFoundException(string.Format(Microsoft_PowerShell_PlatyPS_Resources.FileNotFound, fnfe.FileName));
ErrorRecord err = new ErrorRecord(exception, "FileNotFound", ErrorCategory.ObjectNotFound, fnfe.FileName);
ThrowTerminatingError(err);
}
if (cmdHelpObjs != null)
{
foreach (var cmdletHelp in cmdHelpObjs)
{
var settings = new MarkdownWriterSettings(Encoding, $"{fullPath}{Constants.DirectorySeparator}{cmdletHelp.Title}.md");
using var cmdWrt = new CommandHelpMarkdownWriter(settings);
WriteObject(cmdWrt.Write(cmdletHelp, NoMetadata, Metadata));
}
if (WithModulePage)
{
string modulePagePath = ModulePagePath ?? fullPath;
string resolvedPathModulePagePath = this.SessionState.Path.GetUnresolvedProviderPathFromPSPath(modulePagePath);
var modulePageSettings = new MarkdownWriterSettings(Encoding, resolvedPathModulePagePath);
using var modulePageWriter = new ModulePageWriter(modulePageSettings);
WriteObject(modulePageWriter.Write(cmdHelpObjs));
}
}
}
}
}