-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathPSScriptHelp.cs
More file actions
291 lines (236 loc) · 10 KB
/
PSScriptHelp.cs
File metadata and controls
291 lines (236 loc) · 10 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
namespace Microsoft.PowerShell.PSResourceGet.UtilClasses
{
/// <summary>
/// This class contains information for a PSScriptFileInfo (representing a .ps1 file contents).
/// </summary>
public sealed class PSScriptHelp
{
#region Properties
/// <summary>
/// The description of the script.
/// </summary>
public string Description { get; private set; } = String.Empty;
/// <summary>
/// This contains all help content aside from Description
/// </summary>
public List<string> HelpContent { get; private set; } = new List<string>();
#endregion
#region Constructor
/// <summary>
/// This constructor takes a value for description and creates a new PSScriptHelp instance.
/// </summary>
public PSScriptHelp (string description)
{
Description = description;
}
/// <summary>
/// This constructor is called by internal cmdlet methods and creates a PSScriptHelp with default values
/// for the parameters. Calling a method like PSScriptHelp.ParseContentIntoObj() would then populate those properties.
/// </summary>
internal PSScriptHelp() {}
#endregion
#region Internal Methods
/// <summary>
/// Parses HelpInfo metadata out of the HelpInfo comment lines found while reading the file
/// and populates PSScriptHelp properties from that metadata.
/// </summary>
internal bool ParseContentIntoObj(string[] commentLines, out ErrorRecord error)
{
error = null;
// Parse content into a hashtable.
Hashtable parsedHelpMetadata = ParseHelpContentHelper(commentLines);
if (!ValidateParsedContent(parsedHelpMetadata, out ErrorRecord validationError))
{
error = validationError;
return false;
}
// Populate object.
List<string> descriptionValue = (List<string>) parsedHelpMetadata["DESCRIPTION"];
Description = String.Join(Environment.NewLine, descriptionValue);
if (parsedHelpMetadata.ContainsKey("HELPCONTENT"))
{
HelpContent = (List<string>) parsedHelpMetadata["HELPCONTENT"];
}
return true;
}
/// <summary>
/// Parses metadata out of PSScriptCommentInfo comment block's lines (which are passed in) into a hashtable.
/// </summary>
public static Hashtable ParseHelpContentHelper(string[] commentLines)
{
/**
Comment lines can look like this:
.KEY1 value
.KEY2 value
.KEY2 value2
.KEY3
value
.KEY4 value
value continued
*/
// Parse out Description and everything else into a bucket list.
List<string> helpContent = new List<string>();
List<string> descriptionValue = new List<string>();
bool parsingDescription = false;
for(int i = 0; i < commentLines.Length; i++)
{
string line = commentLines[i];
if (line.Trim().StartsWith(".DESCRIPTION", StringComparison.OrdinalIgnoreCase))
{
parsingDescription = true;
}
else if (line.Trim().StartsWith("."))
{
parsingDescription = false;
helpContent.Add(line);
}
else if (!String.IsNullOrEmpty(line))
{
if (parsingDescription)
{
descriptionValue.Add(line);
}
else
{
helpContent.Add(line);
}
}
}
Hashtable parsedHelpMetadata = new Hashtable(StringComparer.OrdinalIgnoreCase);
parsedHelpMetadata.Add("DESCRIPTION", descriptionValue);
if (helpContent.Count != 0)
{
parsedHelpMetadata.Add("HELPCONTENT", helpContent);
}
return parsedHelpMetadata;
}
/// <summary>
/// Validates parsed help info content from the hashtable to ensure required help metadata (Description) is present
/// and does not contain empty values.
/// </summary>
internal bool ValidateParsedContent(Hashtable parsedHelpMetadata, out ErrorRecord error)
{
error = null;
if (!parsedHelpMetadata.ContainsKey("DESCRIPTION"))
{
error = new ErrorRecord(
new ArgumentException( "PSScript file must contain value for Description. Ensure value for Description is passed in and try again."),
"PSScriptInfoMissingDescription",
ErrorCategory.InvalidArgument,
null);
return false;
}
List<string> descriptionValue = (List<string>) parsedHelpMetadata["DESCRIPTION"];
string descriptionString = String.Join("", descriptionValue);
if (descriptionValue.Count == 0 || (String.IsNullOrEmpty(descriptionString)) || String.IsNullOrWhiteSpace(descriptionString))
{
error = new ErrorRecord(
new ArgumentException("PSScript file value for Description cannot be null, empty or whitespace. Ensure value for Description meets these conditions and try again."),
"PSScriptInfoMissingDescription",
ErrorCategory.InvalidArgument,
null);
return false;
}
if (StringContainsComment(descriptionString))
{
error = new ErrorRecord(
new ArgumentException("PSScript file's value for Description cannot contain '<#' or '#>'. Pass in a valid value for Description and try again."),
"DescriptionContainsComment",
ErrorCategory.InvalidArgument,
null);
return false;
}
return true;
}
/// <summary>
/// Validates help info properties contain required script Help properties
/// i.e Description.
/// </summary>
internal bool ValidateContent(out ErrorRecord error)
{
error = null;
if (String.IsNullOrEmpty(Description))
{
error = new ErrorRecord(
new ArgumentException("PSScript file must contain value for Description. Ensure value for Description is passed in and try again."),
"PSScriptInfoMissingDescription",
ErrorCategory.InvalidArgument,
null);
return false;
}
if (StringContainsComment(Description))
{
error = new ErrorRecord(
new ArgumentException("PSScript file's value for Description cannot contain '<#' or '#>'. Pass in a valid value for Description and try again."),
"DescriptionContainsComment",
ErrorCategory.InvalidArgument,
null);
return false;
}
return true;
}
/// <summary>
/// Emits string representation of 'HelpInfo <# ... #>' comment and its metadata contents.
/// </summary>
internal string[] EmitContent()
{
// Note: we add a newline to the end of each property entry in HelpInfo so that there's an empty line separating them.
List<string> psHelpInfoLines = new List<string>();
psHelpInfoLines.Add($"<#{Environment.NewLine}");
psHelpInfoLines.Add($".DESCRIPTION");
psHelpInfoLines.Add($"{Description}{Environment.NewLine}");
if (HelpContent.Count != 0)
{
psHelpInfoLines.AddRange(HelpContent);
}
psHelpInfoLines.Add("#>");
return psHelpInfoLines.ToArray();
}
/// <summary>
/// Updates contents of the HelpInfo properties from any (non-default) values passed in.
/// </summary>
internal bool UpdateContent(string description, out ErrorRecord error)
{
error = null;
if (!String.IsNullOrEmpty(description))
{
if (String.Equals(description.Trim(), String.Empty))
{
error = new ErrorRecord(
new ArgumentException("Description value can't be updated to whitespace as this would invalidate the script."),
"descriptionUpdateValueIsWhitespaceError",
ErrorCategory.InvalidArgument,
null);
return false;
}
if (StringContainsComment(description))
{
error = new ErrorRecord(
new ArgumentException("Description value can't be updated to value containing comment '<#' or '#>' as this would invalidate the script."),
"descriptionUpdateValueContainsCommentError",
ErrorCategory.InvalidArgument,
null);
return false;
}
Description = description;
}
return true;
}
#endregion
#region Private Methods
/// <summary>
/// Ensure description field (passed as stringToValidate) does not contain '<#' or '#>'.
/// </summary>
private bool StringContainsComment(string stringToValidate)
{
return stringToValidate.Contains("<#") || stringToValidate.Contains("#>");
}
#endregion
}
}