File size: 16,298 Bytes
8c763fb | 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 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Globalization;
using System.Text;
using System.Xml;
namespace System.Management.Automation
{
/// <summary>
/// Class MamlCommandHelpInfo keeps track of help information to be returned by
/// command help provider.
/// </summary>
internal class MamlCommandHelpInfo : BaseCommandHelpInfo
{
/// <summary>
/// Constructor for custom HelpInfo object construction
///
/// This is used by the CommandHelpProvider class to generate the
/// default help UX when no help content is present.
/// </summary>
/// <param name="helpObject"></param>
/// <param name="helpCategory"></param>
internal MamlCommandHelpInfo(PSObject helpObject, HelpCategory helpCategory)
: base(helpCategory)
{
_fullHelpObject = helpObject;
this.ForwardHelpCategory = HelpCategory.Provider;
this.AddCommonHelpProperties();
// set user defined data
if (helpObject.Properties["Component"] != null)
{
_component = helpObject.Properties["Component"].Value as string;
}
if (helpObject.Properties["Role"] != null)
{
_role = helpObject.Properties["Role"].Value as string;
}
if (helpObject.Properties["Functionality"] != null)
{
_functionality = helpObject.Properties["Functionality"].Value as string;
}
}
/// <summary>
/// Constructor for MamlCommandHelpInfo. This constructor will call the corresponding
/// constructor in CommandHelpInfo so that xmlNode will be converted a mamlNode.
/// </summary>
/// <remarks>
/// This constructor is intentionally made private so that the only way to create
/// MamlCommandHelpInfo is through static function
/// Load(XmlNode node)
/// where some sanity check is done.
/// </remarks>
private MamlCommandHelpInfo(XmlNode xmlNode, HelpCategory helpCategory) : base(helpCategory)
{
MamlNode mamlNode = new MamlNode(xmlNode);
_fullHelpObject = mamlNode.PSObject;
this.Errors = mamlNode.Errors;
// The type name hierarchy for mshObject doesn't necessary
// reflect the hierarchy in source code. From display's point of
// view MamlCommandHelpInfo is derived from HelpInfo.
_fullHelpObject.TypeNames.Clear();
if (helpCategory == HelpCategory.DscResource)
{
_fullHelpObject.TypeNames.Add("DscResourceHelpInfo");
}
else
{
_fullHelpObject.TypeNames.Add("MamlCommandHelpInfo");
_fullHelpObject.TypeNames.Add("HelpInfo");
}
this.ForwardHelpCategory = HelpCategory.Provider;
}
/// <summary>
/// Override the FullHelp PSObject of this provider-specific HelpInfo with generic help.
/// </summary>
internal void OverrideProviderSpecificHelpWithGenericHelp(HelpInfo genericHelpInfo)
{
PSObject genericHelpMaml = genericHelpInfo.FullHelp;
MamlUtil.OverrideName(_fullHelpObject, genericHelpMaml);
MamlUtil.OverridePSTypeNames(_fullHelpObject, genericHelpMaml);
MamlUtil.PrependSyntax(_fullHelpObject, genericHelpMaml);
MamlUtil.PrependDetailedDescription(_fullHelpObject, genericHelpMaml);
MamlUtil.OverrideParameters(_fullHelpObject, genericHelpMaml);
MamlUtil.PrependNotes(_fullHelpObject, genericHelpMaml);
MamlUtil.AddCommonProperties(_fullHelpObject, genericHelpMaml);
}
#region Basic Help Properties
private readonly PSObject _fullHelpObject;
/// <summary>
/// Full help object for this help item.
/// </summary>
/// <value>Full help object for this help item.</value>
internal override PSObject FullHelp
{
get
{
return _fullHelpObject;
}
}
/// <summary>
/// Examples string of this cmdlet help info.
/// </summary>
private string Examples
{
get
{
return ExtractTextForHelpProperty(this.FullHelp, "Examples");
}
}
/// <summary>
/// Parameters string of this cmdlet help info.
/// </summary>
private string Parameters
{
get
{
return ExtractTextForHelpProperty(this.FullHelp, "Parameters");
}
}
/// <summary>
/// Notes string of this cmdlet help info.
/// </summary>
private string Notes
{
get
{
return ExtractTextForHelpProperty(this.FullHelp, "alertset");
}
}
#endregion
#region Component, Role, Features
// Component, Role, Functionality are required by exchange for filtering
// help contents to be returned from help system.
//
// Following is how this is going to work,
// 1. Each command will optionally include component, role and functionality
// information. This information is discovered from help content
// from xml tags <component>, <role>, <functionality> respectively
// as part of command metadata.
// 2. From command line, end user can request help for commands for
// particular component, role and functionality using parameters like
// -component, -role, -functionality.
// 3. At runtime, help engine will match against component/role/functionality
// criteria before returning help results.
//
private string _component = null;
/// <summary>
/// Component for this command.
/// </summary>
/// <value></value>
internal override string Component
{
get
{
return _component;
}
}
private string _role = null;
/// <summary>
/// Role for this command.
/// </summary>
/// <value></value>
internal override string Role
{
get
{
return _role;
}
}
private string _functionality = null;
/// <summary>
/// Functionality for this command.
/// </summary>
/// <value></value>
internal override string Functionality
{
get
{
return _functionality;
}
}
internal void SetAdditionalDataFromHelpComment(string component, string functionality, string role)
{
_component = component;
_functionality = functionality;
_role = role;
// component,role,functionality is part of common help..
// Update these properties as we have new data now..
this.UpdateUserDefinedDataProperties();
}
/// <summary>
/// Add user-defined command help data to command help.
/// </summary>
/// <param name="userDefinedData">User defined data object.</param>
internal void AddUserDefinedData(UserDefinedHelpData userDefinedData)
{
if (userDefinedData == null)
return;
string propertyValue;
if (userDefinedData.Properties.TryGetValue("component", out propertyValue))
{
_component = propertyValue;
}
if (userDefinedData.Properties.TryGetValue("role", out propertyValue))
{
_role = propertyValue;
}
if (userDefinedData.Properties.TryGetValue("functionality", out propertyValue))
{
_functionality = propertyValue;
}
// component,role,functionality is part of common help..
// Update these properties as we have new data now..
this.UpdateUserDefinedDataProperties();
}
#endregion
#region Load
/// <summary>
/// Create a MamlCommandHelpInfo object from an XmlNode.
/// </summary>
/// <param name="xmlNode">XmlNode that contains help info.</param>
/// <param name="helpCategory">Help category this maml object fits into.</param>
/// <returns>MamlCommandHelpInfo object created.</returns>
internal static MamlCommandHelpInfo Load(XmlNode xmlNode, HelpCategory helpCategory)
{
MamlCommandHelpInfo mamlCommandHelpInfo = new MamlCommandHelpInfo(xmlNode, helpCategory);
if (string.IsNullOrEmpty(mamlCommandHelpInfo.Name))
return null;
mamlCommandHelpInfo.AddCommonHelpProperties();
return mamlCommandHelpInfo;
}
#endregion
#region Provider specific help
#if V2
/// <summary>
/// Merge the provider specific help with current command help.
///
/// The cmdletHelp and dynamicParameterHelp is normally retrieved from ProviderHelpProvider.
/// </summary>
/// <remarks>
/// A new MamlCommandHelpInfo is created to avoid polluting the provider help cache.
/// </remarks>
/// <param name="cmdletHelp">Provider-specific cmdletHelp to merge into current MamlCommandHelpInfo object.</param>
/// <param name="dynamicParameterHelp">Provider-specific dynamic parameter help to merge into current MamlCommandHelpInfo object.</param>
/// <returns>Merged command help info object.</returns>
internal MamlCommandHelpInfo MergeProviderSpecificHelp(PSObject cmdletHelp, PSObject[] dynamicParameterHelp)
{
if (this._fullHelpObject == null)
return null;
MamlCommandHelpInfo result = (MamlCommandHelpInfo)this.MemberwiseClone();
// We will need to use a deep clone of _fullHelpObject
// to avoid _fullHelpObject being get terminated.
result._fullHelpObject = this._fullHelpObject.Copy();
if (cmdletHelp != null)
result._fullHelpObject.Properties.Add(new PSNoteProperty("PS_Cmdlet", cmdletHelp));
if (dynamicParameterHelp != null)
result._fullHelpObject.Properties.Add(new PSNoteProperty("PS_DynamicParameters", dynamicParameterHelp));
return result;
}
#endif
#endregion
#region Helper Methods and Overloads
/// <summary>
/// Extracts text for a given property from the full help object.
/// </summary>
/// <param name="psObject">FullHelp object.</param>
/// <param name="propertyName">
/// Name of the property for which text needs to be extracted.
/// </param>
/// <returns></returns>
private static string ExtractTextForHelpProperty(PSObject psObject, string propertyName)
{
if (psObject == null)
return string.Empty;
if (psObject.Properties[propertyName] == null ||
psObject.Properties[propertyName].Value == null)
{
return string.Empty;
}
return ExtractText(PSObject.AsPSObject(psObject.Properties[propertyName].Value));
}
/// <summary>
/// Given a PSObject, this method will traverse through the objects properties,
/// extracts content from properties that are of type System.String, appends them
/// together and returns.
/// </summary>
/// <param name="psObject"></param>
/// <returns></returns>
private static string ExtractText(PSObject psObject)
{
if (psObject == null)
{
return string.Empty;
}
// I think every cmdlet description should at least have 400 characters...
// so starting with this assumption..I did an average of all the cmdlet
// help content available at the time of writing this code and came up
// with this number.
StringBuilder result = new StringBuilder(400);
foreach (PSPropertyInfo propertyInfo in psObject.Properties)
{
string typeNameOfValue = propertyInfo.TypeNameOfValue;
switch (typeNameOfValue.ToLowerInvariant())
{
case "system.boolean":
case "system.int32":
case "system.object":
case "system.object[]":
continue;
case "system.string":
result.Append((string)LanguagePrimitives.ConvertTo(propertyInfo.Value,
typeof(string), CultureInfo.InvariantCulture));
break;
case "system.management.automation.psobject[]":
PSObject[] items = (PSObject[])LanguagePrimitives.ConvertTo(
propertyInfo.Value,
typeof(PSObject[]),
CultureInfo.InvariantCulture);
foreach (PSObject item in items)
{
result.Append(ExtractText(item));
}
break;
case "system.management.automation.psobject":
result.Append(ExtractText(PSObject.AsPSObject(propertyInfo.Value)));
break;
default:
result.Append(ExtractText(PSObject.AsPSObject(propertyInfo.Value)));
break;
}
}
return result.ToString();
}
/// <summary>
/// Returns true if help content in help info matches the
/// pattern contained in <paramref name="pattern"/>.
/// The underlying code will usually run pattern.IsMatch() on
/// content it wants to search.
/// Cmdlet help info looks for pattern in Synopsis and
/// DetailedDescription.
/// </summary>
/// <param name="pattern"></param>
/// <returns></returns>
internal override bool MatchPatternInContent(WildcardPattern pattern)
{
System.Management.Automation.Diagnostics.Assert(pattern != null, "pattern cannot be null");
string synopsis = Synopsis;
if ((!string.IsNullOrEmpty(synopsis)) && (pattern.IsMatch(synopsis)))
{
return true;
}
string detailedDescription = DetailedDescription;
if ((!string.IsNullOrEmpty(detailedDescription)) && (pattern.IsMatch(detailedDescription)))
{
return true;
}
string examples = Examples;
if ((!string.IsNullOrEmpty(examples)) && (pattern.IsMatch(examples)))
{
return true;
}
string notes = Notes;
if ((!string.IsNullOrEmpty(notes)) && (pattern.IsMatch(notes)))
{
return true;
}
string parameters = Parameters;
if ((!string.IsNullOrEmpty(parameters)) && (pattern.IsMatch(parameters)))
{
return true;
}
return false;
}
internal MamlCommandHelpInfo Copy()
{
MamlCommandHelpInfo result = new MamlCommandHelpInfo(_fullHelpObject.Copy(), this.HelpCategory);
return result;
}
internal MamlCommandHelpInfo Copy(HelpCategory newCategoryToUse)
{
MamlCommandHelpInfo result = new MamlCommandHelpInfo(_fullHelpObject.Copy(), newCategoryToUse);
result.FullHelp.Properties["Category"].Value = newCategoryToUse.ToString();
return result;
}
#endregion
}
}
|