-
Notifications
You must be signed in to change notification settings - Fork 1
/
CmdletHelpGenerator.cs
536 lines (457 loc) · 23.9 KB
/
CmdletHelpGenerator.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Text;
using System.Reflection;
using System.Xml;
using System.IO;
using Lapointe.PowerShell.MamlGenerator.Attributes;
namespace Lapointe.PowerShell.MamlGenerator
{
public class CmdletHelpGenerator
{
private static string _copyright = null;
private static XmlTextWriter _writer = null;
/*
[System.Reflection.Assembly]::LoadFile("W:\Github\PowerShell-MamlGenerator\bin\Debug\Lapointe.PowerShell.MamlGenerator.dll")
[Lapointe.PowerShell.MamlGenerator.CmdletHelpGenerator]::GenerateHelp("W:\Github\PowerShell-SPCmdlets\Lapointe.SharePoint2013.PowerShell\bin\Debug\Lapointe.SharePoint.PowerShell.dll", "c:\temp\", $true, "Download-SPFile")
*/
public static void GenerateHelp(string outputPath, bool oneFile)
{
//System.Diagnostics.Debugger.Launch();
GenerateHelp(Assembly.GetExecutingAssembly(), outputPath, oneFile, null);
}
public static void GenerateHelp(string outputPath, bool oneFile, string cmdletName)
{
GenerateHelp(Assembly.GetExecutingAssembly(), outputPath, oneFile, cmdletName);
}
public static void GenerateHelp(string inputFile, string outputPath, bool oneFile)
{
GenerateHelp(Assembly.LoadFrom(inputFile), outputPath, oneFile, null);
}
public static void GenerateHelp(string inputFile, string outputPath, bool oneFile, string cmdletName)
{
GenerateHelp(Assembly.LoadFrom(inputFile), outputPath, oneFile, cmdletName);
}
public static void GenerateHelp(Assembly asm, string outputPath, bool oneFile)
{
GenerateHelp(asm, outputPath, oneFile, null);
}
public static void GenerateHelp(Assembly asm, string outputPath, bool oneFile, string cmdletName)
{
Console.WriteLine(string.Format("MamlGenerator.GenerateHelp(): asm={0}; outputPath={1}; oneFile={2}", asm.FullName, outputPath, oneFile));
var attr = asm.GetCustomAttributes(typeof (AssemblyCopyrightAttribute), false);
if (attr.Length == 1)
_copyright = ((AssemblyCopyrightAttribute) attr[0]).Copyright;
attr = asm.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
if (attr.Length == 1)
_copyright += ((AssemblyDescriptionAttribute)attr[0]).Description;
StringBuilder sb = new StringBuilder();
_writer = new XmlTextWriter(new StringWriter(sb));
_writer.Formatting = Formatting.Indented;
if (oneFile)
{
_writer.WriteStartElement("helpItems");
_writer.WriteAttributeString("xmlns", "http://msh");
_writer.WriteAttributeString("schema", "maml");
}
foreach (Type type in asm.GetExportedTypes())
{
CmdletAttribute ca = GetAttribute<CmdletAttribute>(type);
if (ca != null)
{
if (!string.IsNullOrEmpty(cmdletName) && cmdletName.ToLower() != string.Format("{0}-{1}", ca.VerbName, ca.NounName).ToLower()) continue;
Console.WriteLine(string.Format("MamlGenerator.GenerateHelp(): Found Cmdlet: {0}-{1}", ca.VerbName, ca.NounName));
if (!oneFile)
{
_writer.WriteStartElement("helpItems");
_writer.WriteAttributeString("xmlns", "http://msh");
_writer.WriteAttributeString("schema", "maml");
}
_writer.WriteStartElement("command", "command", "http://schemas.microsoft.com/maml/dev/command/2004/10");
_writer.WriteAttributeString("xmlns", "maml", null, "http://schemas.microsoft.com/maml/2004/1");
_writer.WriteAttributeString("xmlns", "dev", null, "http://schemas.microsoft.com/maml/dev/2004/10");
_writer.WriteAttributeString("xmlns", "gl", null, "http://schemas.falchionconsulting.com/maml/gl/2013/02");
_writer.WriteStartElement("command", "details", null);
_writer.WriteElementString("command", "name", null, string.Format("{0}-{1}", ca.VerbName, ca.NounName));
CmdletGroupAttribute group = GetAttribute<CmdletGroupAttribute>(type);
if (group != null && !string.IsNullOrEmpty(group.Group))
_writer.WriteElementString("gl", "group", null, group.Group);
else
_writer.WriteElementString("gl", "group", null, ca.NounName);
WriteDescription(type, true, false);
WriteCopyright();
_writer.WriteElementString("command", "verb", null, ca.VerbName);
_writer.WriteElementString("command", "noun", null, ca.NounName);
_writer.WriteElementString("dev", "version", null, asm.GetName().Version.ToString());
_writer.WriteEndElement(); //command:details
WriteDescription(type, false, true);
WriteSyntax(ca, type);
_writer.WriteStartElement("command", "parameters", null);
foreach (PropertyInfo pi in type.GetProperties())
{
List<ParameterAttribute> pas = GetAttribute<ParameterAttribute>(pi);
if (pas == null)
continue;
ParameterAttribute pa = null;
if (pas.Count == 1)
pa = pas[0];
else
{
// Determine the defualt property parameter set to use for details.
ParameterAttribute defaultPA = null;
foreach (ParameterAttribute temp in pas)
{
string defaultSet = ca.DefaultParameterSetName;
if (string.IsNullOrEmpty(ca.DefaultParameterSetName))
defaultSet = string.Empty;
string set = temp.ParameterSetName;
if (string.IsNullOrEmpty(set) || set == ALL_PARAMETER_SETS_NAME)
{
set = string.Empty;
defaultPA = temp;
}
if (set.ToLower() == defaultSet.ToLower())
{
pa = temp;
defaultPA = temp;
break;
}
}
if (pa == null && defaultPA != null)
pa = defaultPA;
if (pa == null)
pa = pas[0];
}
_writer.WriteStartElement("command", "parameter", null);
_writer.WriteAttributeString("required", pa.Mandatory.ToString().ToLower());
bool supportsWildcard = GetAttribute<SupportsWildcardsAttribute>(pi) != null;
_writer.WriteAttributeString("globbing", supportsWildcard.ToString().ToLower());
if (!pa.ValueFromPipeline && !pa.ValueFromPipelineByPropertyName)
_writer.WriteAttributeString("pipelineInput", "false");
else if (pa.ValueFromPipeline && pa.ValueFromPipelineByPropertyName)
_writer.WriteAttributeString("pipelineInput", "true (ByValue, ByPropertyName)");
else if (!pa.ValueFromPipeline && pa.ValueFromPipelineByPropertyName)
_writer.WriteAttributeString("pipelineInput", "true (ByPropertyName)");
else if (pa.ValueFromPipeline && !pa.ValueFromPipelineByPropertyName)
_writer.WriteAttributeString("pipelineInput", "true (ByValue)");
if (pa.Position < 0)
_writer.WriteAttributeString("position", "named");
else
_writer.WriteAttributeString("position", (pa.Position + 1).ToString());
bool variableLength = pi.PropertyType.IsArray;
_writer.WriteAttributeString("variableLength", variableLength.ToString().ToLower());
_writer.WriteElementString("maml", "name", null, pi.Name);
if (pi.PropertyType.Name == "SPAssignmentCollection")
WriteSPAssignmentCollectionDescription();
else
WriteDescription(pa.HelpMessage, false);
_writer.WriteStartElement("command", "parameterValue", null);
_writer.WriteAttributeString("required", pa.Mandatory.ToString().ToLower());
_writer.WriteAttributeString("variableLength", variableLength.ToString().ToLower());
_writer.WriteValue(pi.PropertyType.Name);
_writer.WriteEndElement(); //command:parameterValue
WriteDevType(pi.PropertyType.Name, null);
_writer.WriteEndElement(); //command:parameter
}
_writer.WriteEndElement(); //command:parameters
//TODO: Find out what is supposed to go here
_writer.WriteStartElement("command", "inputTypes", null);
_writer.WriteStartElement("command", "inputType", null);
WriteDevType(null, null);
_writer.WriteEndElement(); //command:inputType
_writer.WriteEndElement(); //command:inputTypes
_writer.WriteStartElement("command", "returnValues", null);
_writer.WriteStartElement("command", "returnValue", null);
WriteDevType(null, null);
_writer.WriteEndElement(); //command:returnValue
_writer.WriteEndElement(); //command:returnValues
_writer.WriteElementString("command", "terminatingErrors", null, null);
_writer.WriteElementString("command", "nonTerminatingErrors", null, null);
_writer.WriteStartElement("maml", "alertSet", null);
_writer.WriteElementString("maml", "title", null, null);
_writer.WriteStartElement("maml", "alert", null);
WritePara(string.Format("For more information, type \"Get-Help {0}-{1} -detailed\". For technical information, type \"Get-Help {0}-{1} -full\".",
ca.VerbName, ca.NounName));
_writer.WriteEndElement(); //maml:alert
_writer.WriteEndElement(); //maml:alertSet
WriteExamples(type);
WriteRelatedLinks(type);
_writer.WriteEndElement(); //command:command
if (!oneFile)
{
_writer.WriteEndElement(); //helpItems
_writer.Flush();
File.WriteAllText(Path.Combine(outputPath, string.Format("{0}.dll-help.xml", type.Name)), sb.ToString());
sb = new StringBuilder();
_writer = new XmlTextWriter(new StringWriter(sb));
_writer.Formatting = Formatting.Indented;
}
}
}
if (oneFile)
{
_writer.WriteEndElement(); //helpItems
_writer.Flush();
File.WriteAllText(Path.Combine(outputPath, string.Format("{0}.dll-help.xml", asm.GetName().Name)), sb.ToString());
}
}
const string ALL_PARAMETER_SETS_NAME = "__AllParameterSets";
private static void WriteSyntax(CmdletAttribute ca, Type type)
{
Dictionary<string, List<PropertyInfo>> parameterSets = new Dictionary<string, List<PropertyInfo>>();
List<PropertyInfo> allParameterSets = null;
foreach (PropertyInfo pi in type.GetProperties())
{
List<ParameterAttribute> pas = GetAttribute<ParameterAttribute>(pi);
if (pas == null)
continue;
foreach (ParameterAttribute temp in pas)
{
string set = temp.ParameterSetName + "";
List<PropertyInfo> piList = null;
if (!parameterSets.ContainsKey(set))
{
piList = new List<PropertyInfo>();
parameterSets.Add(set, piList);
}
else
piList = parameterSets[set];
parameterSets[set].Add(pi);
}
}
if (parameterSets.Count == 0)
return;
if (parameterSets.ContainsKey(ALL_PARAMETER_SETS_NAME))
allParameterSets = parameterSets[ALL_PARAMETER_SETS_NAME];
if (parameterSets.Count > 1 && parameterSets.ContainsKey(ALL_PARAMETER_SETS_NAME))
parameterSets.Remove(ALL_PARAMETER_SETS_NAME);
if (parameterSets.Count == 1)
allParameterSets = null;
_writer.WriteStartElement("command", "syntax", null);
foreach (string parameterSetName in parameterSets.Keys)
{
WriteSyntaxItem(ca, parameterSets, parameterSetName, allParameterSets);
}
_writer.WriteEndElement(); //command:syntax
}
private static void WriteSyntaxItem(CmdletAttribute ca, Dictionary<string, List<PropertyInfo>> parameterSets, string parameterSetName, List<PropertyInfo> allParameterSets)
{
_writer.WriteStartElement("command", "syntaxItem", null);
_writer.WriteElementString("maml", "name", null, string.Format("{0}-{1}", ca.VerbName, ca.NounName));
foreach (PropertyInfo pi in parameterSets[parameterSetName])
{
ParameterAttribute pa = GetParameterAttribute(pi, parameterSetName);
if (pa == null)
continue;
WriteParameter(pi, pa);
}
if (allParameterSets != null)
{
foreach (PropertyInfo pi in allParameterSets)
{
List<ParameterAttribute> pas = GetAttribute<ParameterAttribute>(pi);
if (pas == null)
continue;
WriteParameter(pi, pas[0]);
}
}
_writer.WriteEndElement(); //command:syntaxItem
}
private static ParameterAttribute GetParameterAttribute(PropertyInfo pi, string parameterSetName)
{
List<ParameterAttribute> pas = GetAttribute<ParameterAttribute>(pi);
if (pas == null)
return null;
ParameterAttribute pa = null;
foreach (ParameterAttribute temp in pas)
{
if (temp.ParameterSetName.ToLower() == parameterSetName.ToLower())
{
pa = temp;
break;
}
}
return pa;
}
private static void WriteParameter(PropertyInfo pi, ParameterAttribute pa)
{
_writer.WriteStartElement("command", "parameter", null);
_writer.WriteAttributeString("required", pa.Mandatory.ToString().ToLower());
//_writer.WriteAttributeString("parameterSetName", pa.ParameterSetName);
if (pa.Position < 0)
_writer.WriteAttributeString("position", "named");
else
_writer.WriteAttributeString("position", (pa.Position + 1).ToString());
_writer.WriteElementString("maml", "name", null, pi.Name);
_writer.WriteStartElement("command", "parameterValue", null);
if (pi.DeclaringType == typeof(PSCmdlet))
_writer.WriteAttributeString("required", "false");
else
_writer.WriteAttributeString("required", "true");
if (pi.PropertyType.Name == "Nullable`1")
{
Type coreType = pi.PropertyType.GetGenericArguments()[0];
if (coreType.IsEnum)
_writer.WriteValue(string.Join(" | ", Enum.GetNames(coreType)));
else
_writer.WriteValue(coreType.Name);
}
else
{
if (pi.PropertyType.IsEnum)
_writer.WriteValue(string.Join(" | ", Enum.GetNames(pi.PropertyType)));
else
_writer.WriteValue(pi.PropertyType.Name);
}
_writer.WriteEndElement(); //command:parameterValue
_writer.WriteEndElement(); //command:parameter
}
private static void WriteDevType(string name, string description)
{
_writer.WriteStartElement("dev", "type", null);
_writer.WriteElementString("maml", "name", null, name);
_writer.WriteElementString("maml", "uri", null, null);
WriteDescription(description, false);
_writer.WriteEndElement(); //dev:type
}
private static void WriteSPAssignmentCollectionDescription()
{
WriteDescription("Manages objects for the purpose of proper disposal. Use of objects, such as SPWeb or SPSite, can use large amounts of memory and use of these objects in Windows PowerShell scripts requires proper memory management. Using the SPAssignment object, you can assign objects to a variable and dispose of the objects after they are needed to free up memory. When SPWeb, SPSite, or SPSiteAdministration objects are used, the objects are automatically disposed of if an assignment collection or the Global parameter is not used.\r\n\r\nWhen the Global parameter is used, all objects are contained in the global store. If objects are not immediately used, or disposed of by using the Stop-SPAssignment command, an out-of-memory scenario can occur.", false);
}
private static void WriteDescription(Type type, bool synopsis, bool addCopyright)
{
_writer.WriteStartElement("maml", "description", null);
CmdletDescriptionAttribute da = GetAttribute<CmdletDescriptionAttribute>(type);
string desc = string.Empty;
if (synopsis)
{
if (da != null && !string.IsNullOrEmpty(da.Synopsis))
{
desc = da.Synopsis;
}
}
else
{
if (da != null && !string.IsNullOrEmpty(da.Description))
{
desc = da.Description;
}
}
WritePara(desc);
if (addCopyright)
{
WritePara(null);
WritePara(_copyright);
}
_writer.WriteEndElement(); //maml:description
}
private static void WriteDescription(string desc, bool addCopyright)
{
_writer.WriteStartElement("maml", "description", null);
WritePara(desc);
if (addCopyright)
{
WritePara(null);
WritePara(_copyright);
}
_writer.WriteEndElement(); //maml:description
}
private static void WriteExamples(Type type)
{
object[] attrs = type.GetCustomAttributes(typeof(ExampleAttribute), true);
if (attrs == null || attrs.Length == 0)
{
_writer.WriteElementString("command", "examples", null, null);
}
else
{
_writer.WriteStartElement("command", "examples", null);
for (int i = 0; i < attrs.Length; i++)
{
ExampleAttribute ex = (ExampleAttribute)attrs[i];
_writer.WriteStartElement("command", "example", null);
if (attrs.Length == 1)
_writer.WriteElementString("maml", "title", null, "------------------EXAMPLE------------------");
else
_writer.WriteElementString("maml", "title", null, string.Format("------------------EXAMPLE {0}-----------------------", i + 1));
_writer.WriteElementString("dev", "code", null, ex.Code);
_writer.WriteStartElement("dev", "remarks", null);
WritePara(ex.Remarks);
_writer.WriteEndElement(); //dev:remarks
_writer.WriteEndElement(); //command:example
}
_writer.WriteEndElement(); //command:examples
}
}
private static void WriteRelatedLinks(Type type)
{
RelatedCmdletsAttribute attr = GetAttribute<RelatedCmdletsAttribute>(type);
if (attr == null)
{
_writer.WriteElementString("maml", "relatedLinks", null, null);
}
else
{
_writer.WriteStartElement("maml", "relatedLinks", null);
foreach (Type t in attr.RelatedCmdlets)
{
CmdletAttribute ca = GetAttribute<CmdletAttribute>(t);
if (ca == null)
continue;
_writer.WriteStartElement("maml", "navigationLink", null);
_writer.WriteElementString("maml", "linkText", null, ca.VerbName + "-" + ca.NounName);
_writer.WriteElementString("maml", "uri", null, null);
_writer.WriteEndElement(); //maml:navigationLink
}
if (attr.ExternalCmdlets != null)
{
foreach (string s in attr.ExternalCmdlets)
{
_writer.WriteStartElement("maml", "navigationLink", null);
_writer.WriteElementString("maml", "linkText", null, s);
_writer.WriteElementString("maml", "uri", null, null);
_writer.WriteEndElement(); //maml:navigationLink
}
}
_writer.WriteEndElement(); //maml:relatedLinks
}
}
private static T GetAttribute<T>(Type type)
{
object[] attrs = type.GetCustomAttributes(typeof(T), true);
if (attrs == null || attrs.Length == 0)
return default(T);
return (T)attrs[0];
}
private static List<T> GetAttribute<T>(PropertyInfo pi)
{
object[] attrs = pi.GetCustomAttributes(typeof(T), true);
List<T> attributes = new List<T>();
if (attrs == null || attrs.Length == 0)
return null;
foreach (T t in attrs)
{
attributes.Add(t);
}
return attributes;
}
private static void WriteCopyright()
{
_writer.WriteStartElement("maml", "copyright", null);
WritePara(_copyright);
_writer.WriteEndElement(); //maml:copyright
}
private static void WritePara(string para)
{
if (string.IsNullOrEmpty(para))
{
_writer.WriteElementString("maml", "para", null, null);
return;
}
string[] paragraphs = para.Split(new[] {"\r\n"}, StringSplitOptions.None);
foreach (string p in paragraphs)
_writer.WriteElementString("maml", "para", null, p);
}
}
}