MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.IO;5 6namespace System.Management.Automation7{8 /// <summary>9 /// Class HelpFileHelpInfo keeps track of help information to be returned by10 /// command help provider.11 /// </summary>12 internal sealed class HelpFileHelpInfo : HelpInfo13 {14 /// <summary>15 /// Constructor for HelpFileHelpInfo.16 /// </summary>17 /// <remarks>18 /// This is made private intentionally so that the only way to create object of this type19 /// is through20 /// GetHelpInfo(string name, string text, string filename)21 /// </remarks>22 /// <param name="name">Help topic name.</param>23 /// <param name="text">Help text.</param>24 /// <param name="filename">File name that contains the help text.</param>25 private HelpFileHelpInfo(string name, string text, string filename)26 {27 FullHelp = PSObject.AsPSObject(text);28 29 Name = name;30 31 // Take the 5th line as synopsis. This may not be true if32 // format of help file is changed later on.33 _synopsis = GetLine(text, 5);34 if (_synopsis != null)35 {36 _synopsis = _synopsis.Trim();37 }38 else39 {40 // make sure _synopsis is never null41 _synopsis = string.Empty;42 }43 44 _filename = filename;45 }46 47 /// <summary>48 /// Name for the help info.49 /// </summary>50 /// <value>Name for the help info</value>51 internal override string Name { get; } = string.Empty;52 53 private readonly string _filename = string.Empty;54 private readonly string _synopsis = string.Empty;55 /// <summary>56 /// Synopsis for the help info.57 /// </summary>58 /// <value>Synopsis for the help info</value>59 internal override string Synopsis60 {61 get62 {63 return _synopsis;64 }65 }66 67 /// <summary>68 /// Help category for the help info.69 /// </summary>70 /// <value>Help category for the help info</value>71 internal override HelpCategory HelpCategory72 {73 get74 {75 return HelpCategory.HelpFile;76 }77 }78 79 /// <summary>80 /// Full help object for this help info.81 /// </summary>82 /// <value>Full help object for this help info</value>83 internal override PSObject FullHelp { get; }84 85 /// <summary>86 /// Get help info based on name, text and filename.87 /// </summary>88 /// <param name="name">Help topic name.</param>89 /// <param name="text">Help text.</param>90 /// <param name="filename">File name that contains the help text.</param>91 /// <returns>HelpFileHelpInfo object created based on information provided.</returns>92 internal static HelpFileHelpInfo GetHelpInfo(string name, string text, string filename)93 {94 if (string.IsNullOrEmpty(name))95 return null;96 97 HelpFileHelpInfo helpfileHelpInfo = new HelpFileHelpInfo(name, text, filename);98 99 if (string.IsNullOrEmpty(helpfileHelpInfo.Name))100 return null;101 102 helpfileHelpInfo.AddCommonHelpProperties();103 104 return helpfileHelpInfo;105 }106 107 /// <summary>108 /// Get the text corresponding to a line in input text.109 /// </summary>110 /// <param name="text">Text to get the line for.</param>111 /// <param name="line">Line number.</param>112 /// <returns>The part of string in text that is in specified line.</returns>113 private static string GetLine(string text, int line)114 {115 StringReader reader = new StringReader(text);116 117 string result = null;118 119 for (int i = 0; i < line; i++)120 {121 result = reader.ReadLine();122 123 if (result == null)124 return null;125 }126 127 return result;128 }129 130 internal override bool MatchPatternInContent(WildcardPattern pattern)131 {132 Diagnostics.Assert(pattern != null, "pattern cannot be null.");133 134 string helpContent = string.Empty;135 LanguagePrimitives.TryConvertTo<string>(FullHelp, out helpContent);136 return pattern.IsMatch(helpContent);137 }138 }139}140 