private void LoadHelpFile(string helpFile, string helpFileIdentifier) { XmlDocument document = InternalDeserializer.LoadUnsafeXmlDocument(new FileInfo(helpFile), false, null); this._helpFiles[helpFile] = 0; System.Xml.XmlNode helpItemsNode = null; if (document.HasChildNodes) { for (int i = 0; i < document.ChildNodes.Count; i++) { System.Xml.XmlNode node2 = document.ChildNodes[i]; if ((node2.NodeType == XmlNodeType.Element) && (string.Compare(node2.LocalName, "helpItems", StringComparison.OrdinalIgnoreCase) == 0)) { helpItemsNode = node2; break; } } } if (helpItemsNode == null) { tracer.WriteLine("Unable to find 'helpItems' element in file {0}", new object[] { helpFile }); } else { bool flag = IsMamlHelp(helpFile, helpItemsNode); using (base.HelpSystem.Trace(helpFile)) { if (helpItemsNode.HasChildNodes) { for (int j = 0; j < helpItemsNode.ChildNodes.Count; j++) { System.Xml.XmlNode xmlNode = helpItemsNode.ChildNodes[j]; if ((xmlNode.NodeType == XmlNodeType.Element) && (string.Compare(xmlNode.LocalName, "command", StringComparison.OrdinalIgnoreCase) == 0)) { MamlCommandHelpInfo helpInfo = null; if (flag) { helpInfo = MamlCommandHelpInfo.Load(xmlNode, System.Management.Automation.HelpCategory.Cmdlet); } if (helpInfo != null) { base.HelpSystem.TraceErrors(helpInfo.Errors); this.AddToCommandCache(helpFileIdentifier, helpInfo.Name, helpInfo); } } if ((xmlNode.NodeType == XmlNodeType.Element) && (string.Compare(xmlNode.Name, "UserDefinedData", StringComparison.OrdinalIgnoreCase) == 0)) { UserDefinedHelpData userDefinedHelpData = UserDefinedHelpData.Load(xmlNode); this.ProcessUserDefineddHelpData(helpFileIdentifier, userDefinedHelpData); } } } } } }
internal static HelpInfo CreateFromComments( ExecutionContext context, CommandInfo commandInfo, List <Token> comments, List <List <Token> > parameterComments, out string helpFile) { HelpCommentsParser helpCommentsParser = new HelpCommentsParser(commandInfo, parameterComments); helpCommentsParser.AnalyzeCommentBlock(comments); helpFile = helpCommentsParser.GetHelpFile(commandInfo); RemoteHelpInfo remoteHelpInfo = helpCommentsParser.GetRemoteHelpInfo(context, commandInfo); if (remoteHelpInfo != null) { return((HelpInfo)remoteHelpInfo); } MamlCommandHelpInfo helpInfo = MamlCommandHelpInfo.Load((XmlNode)helpCommentsParser.BuildXmlFromComments().DocumentElement, commandInfo.HelpCategory); if (helpInfo != null) { helpCommentsParser.SetAdditionalData(helpInfo); if (!string.IsNullOrEmpty(helpCommentsParser.sections.forwardHelpTargetName) || !string.IsNullOrEmpty(helpCommentsParser.sections.fowardHelpCategory)) { if (string.IsNullOrEmpty(helpCommentsParser.sections.forwardHelpTargetName)) { helpInfo.ForwardTarget = helpInfo.Name; } else { helpInfo.ForwardTarget = helpCommentsParser.sections.forwardHelpTargetName; } if (!string.IsNullOrEmpty(helpCommentsParser.sections.fowardHelpCategory)) { try { helpInfo.ForwardHelpCategory = (HelpCategory)Enum.Parse(typeof(HelpCategory), helpCommentsParser.sections.fowardHelpCategory, true); } catch (ArgumentException ex) { } } else { helpInfo.ForwardHelpCategory = HelpCategory.Alias | HelpCategory.Cmdlet | HelpCategory.ScriptCommand | HelpCategory.Function | HelpCategory.Filter | HelpCategory.ExternalScript; } } } return((HelpInfo)helpInfo); }
/// <summary> /// Get provider specific help info. /// </summary> internal MamlCommandHelpInfo GetProviderSpecificHelpInfo(string helpItemName) { if (InternalTestHooks.BypassOnlineHelpRetrieval) { // By returning null, we force get-help to return generic help // which includes a helpUri that points to the fwlink defined in the cmdlet code. return(null); } // Get the provider. ProviderInfo providerInfo = null; PSDriveInfo driveInfo = null; string resolvedProviderPath = null; CmdletProviderContext cmdletProviderContext = new CmdletProviderContext(_executionContext); try { string psPath = _requestedPath; if (string.IsNullOrEmpty(_requestedPath)) { psPath = _pathIntrinsics.CurrentLocation.Path; } resolvedProviderPath = _executionContext.LocationGlobber.GetProviderPath( psPath, cmdletProviderContext, out providerInfo, out driveInfo); } // ignore exceptions caused by provider resolution catch (ArgumentNullException) { } catch (ProviderNotFoundException) { } catch (DriveNotFoundException) { } catch (ProviderInvocationException) { } catch (NotSupportedException) { } catch (InvalidOperationException) { } catch (ItemNotFoundException) { } if (providerInfo == null) { return(null); } // Does the provider know how to generate MAML. CmdletProvider cmdletProvider = providerInfo.CreateInstance(); ICmdletProviderSupportsHelp provider = cmdletProvider as ICmdletProviderSupportsHelp; // Under JEA sessions the resolvedProviderPath will be null, we should allow get-help to continue. if (provider == null) { return(null); } bool isJEASession = false; if (this._executionContext.InitialSessionState != null && this._executionContext.InitialSessionState.Providers != null && providerInfo != null) { foreach ( Runspaces.SessionStateProviderEntry sessionStateProvider in this._executionContext.InitialSessionState.Providers[providerInfo.Name]) { if (sessionStateProvider.Visibility == SessionStateEntryVisibility.Private) { isJEASession = true; break; } } } if (resolvedProviderPath == null) { if (isJEASession) { return(null); } else { throw new ItemNotFoundException(_requestedPath, "PathNotFound", SessionStateStrings.PathNotFound); } } // ok we have path and valid provider that supplys content..initialize the provider // and get the help content for the path. cmdletProvider.Start(providerInfo, cmdletProviderContext); // There should be exactly one resolved path. string providerPath = resolvedProviderPath; // Get the MAML help info. Don't catch exceptions thrown by provider. string mamlXmlString = provider.GetHelpMaml(helpItemName, providerPath); if (string.IsNullOrEmpty(mamlXmlString)) { return(null); } // process the MAML content only if it is non-empty. XmlDocument mamlDoc = InternalDeserializer.LoadUnsafeXmlDocument( mamlXmlString, false, /* ignore whitespace, comments, etc. */ null); /* default maxCharactersInDocument */ MamlCommandHelpInfo providerSpecificHelpInfo = MamlCommandHelpInfo.Load(mamlDoc.DocumentElement, HelpCategory.Provider); return(providerSpecificHelpInfo); }
/// <summary> /// Load help file for HelpInfo objects. The HelpInfo objects will be /// put into help cache. /// </summary> /// <remarks> /// 1. Needs to pay special attention about error handling in this function. /// Common errors include: file not found and invalid xml. None of these error /// should cause help search to stop. /// 2. a helpfile cache is used to avoid same file got loaded again and again. /// </remarks> private void LoadHelpFile(string helpFile, string helpFileIdentifier) { Dbg.Assert(!String.IsNullOrEmpty(helpFile), "HelpFile cannot be null or empty."); Dbg.Assert(!String.IsNullOrEmpty(helpFileIdentifier), "helpFileIdentifier cannot be null or empty."); XmlDocument doc = InternalDeserializer.LoadUnsafeXmlDocument( new FileInfo(helpFile), false, /* ignore whitespace, comments, etc. */ null); /* default maxCharactersInDocument */ // Add this file into _helpFiles hashtable to prevent it to be loaded again. _helpFiles[helpFile] = 0; XmlNode helpItemsNode = null; if (doc.HasChildNodes) { for (int i = 0; i < doc.ChildNodes.Count; i++) { XmlNode node = doc.ChildNodes[i]; if (node.NodeType == XmlNodeType.Element && String.Compare(node.LocalName, "helpItems", StringComparison.OrdinalIgnoreCase) == 0) { helpItemsNode = node; break; } } } if (helpItemsNode == null) { s_tracer.WriteLine("Unable to find 'helpItems' element in file {0}", helpFile); return; } bool isMaml = IsMamlHelp(helpFile, helpItemsNode); using (this.HelpSystem.Trace(helpFile)) { if (helpItemsNode.HasChildNodes) { for (int i = 0; i < helpItemsNode.ChildNodes.Count; i++) { XmlNode node = helpItemsNode.ChildNodes[i]; string nodeLocalName = node.LocalName; bool isDscResource = (String.Compare(nodeLocalName, "dscResource", StringComparison.OrdinalIgnoreCase) == 0); if (node.NodeType == XmlNodeType.Element && isDscResource) { MamlCommandHelpInfo helpInfo = null; if (isMaml) { if (isDscResource) { helpInfo = MamlCommandHelpInfo.Load(node, HelpCategory.DscResource); } } if (helpInfo != null) { this.HelpSystem.TraceErrors(helpInfo.Errors); AddCache(helpFileIdentifier, helpInfo); } } } } } }
internal MamlCommandHelpInfo GetProviderSpecificHelpInfo(string helpItemName) { ProviderInfo info = null; PSDriveInfo drive = null; string str = null; CmdletProviderContext context = new CmdletProviderContext(this._executionContext); try { string str2 = this._requestedPath; if (string.IsNullOrEmpty(this._requestedPath)) { str2 = this._pathIntrinsics.CurrentLocation.Path; } str = this._executionContext.LocationGlobber.GetProviderPath(str2, context, out info, out drive); } catch (ArgumentNullException) { } catch (ProviderNotFoundException) { } catch (DriveNotFoundException) { } catch (ProviderInvocationException) { } catch (NotSupportedException) { } catch (InvalidOperationException) { } catch (ItemNotFoundException) { } if (info == null) { return(null); } CmdletProvider provider = info.CreateInstance(); ICmdletProviderSupportsHelp help = provider as ICmdletProviderSupportsHelp; if (help == null) { return(null); } if (str == null) { throw new ItemNotFoundException(this._requestedPath, "PathNotFound", SessionStateStrings.PathNotFound); } provider.Start(info, context); string path = str; string helpMaml = help.GetHelpMaml(helpItemName, path); if (string.IsNullOrEmpty(helpMaml)) { return(null); } return(MamlCommandHelpInfo.Load(InternalDeserializer.LoadUnsafeXmlDocument(helpMaml, false, null).DocumentElement, HelpCategory.Provider)); }
/// <summary> /// Get provider specific help info. /// </summary> internal MamlCommandHelpInfo GetProviderSpecificHelpInfo(string helpItemName) { // Get the provider. ProviderInfo providerInfo = null; PSDriveInfo driveInfo = null; string resolvedProviderPath = null; CmdletProviderContext cmdletProviderContext = new CmdletProviderContext(_executionContext); try { string psPath = _requestedPath; if (string.IsNullOrEmpty(_requestedPath)) { psPath = _pathIntrinsics.CurrentLocation.Path; } resolvedProviderPath = _executionContext.LocationGlobber.GetProviderPath( psPath, cmdletProviderContext, out providerInfo, out driveInfo); } // ignore exceptions caused by provider resolution catch (ArgumentNullException) { } catch (ProviderNotFoundException) { } catch (DriveNotFoundException) { } catch (ProviderInvocationException) { } catch (NotSupportedException) { } catch (InvalidOperationException) { } catch (ItemNotFoundException) { } if (providerInfo == null) { return(null); } // Does the provider know how to generate MAML. CmdletProvider cmdletProvider = providerInfo.CreateInstance(); ICmdletProviderSupportsHelp provider = cmdletProvider as ICmdletProviderSupportsHelp; if (provider == null) { return(null); } if (resolvedProviderPath == null) { throw new ItemNotFoundException(_requestedPath, "PathNotFound", SessionStateStrings.PathNotFound); } // ok we have path and valid provider that supplys content..initialize the provider // and get the help content for the path. cmdletProvider.Start(providerInfo, cmdletProviderContext); // There should be exactly one resolved path. string providerPath = resolvedProviderPath; // Get the MAML help info. Don't catch exceptions thrown by provider. string mamlXmlString = provider.GetHelpMaml(helpItemName, providerPath); if (string.IsNullOrEmpty(mamlXmlString)) { return(null); } // process the MAML content only if it is non-empty. XmlDocument mamlDoc = InternalDeserializer.LoadUnsafeXmlDocument( mamlXmlString, false, /* ignore whitespace, comments, etc. */ null); /* default maxCharactersInDocument */ MamlCommandHelpInfo providerSpecificHelpInfo = MamlCommandHelpInfo.Load(mamlDoc.DocumentElement, HelpCategory.Provider); return(providerSpecificHelpInfo); }
internal MamlCommandHelpInfo GetProviderSpecificHelpInfo(string helpItemName) { ProviderInfo provider = (ProviderInfo)null; PSDriveInfo drive = (PSDriveInfo)null; string str = (string)null; CmdletProviderContext cmdletProviderContext = new CmdletProviderContext(this._executionContext); try { string path = this._requestedPath; if (string.IsNullOrEmpty(this._requestedPath)) { path = this._pathIntrinsics.CurrentLocation.Path; } str = this._executionContext.LocationGlobber.GetProviderPath(path, cmdletProviderContext, out provider, out drive); } catch (ArgumentNullException ex) { } catch (ProviderNotFoundException ex) { } catch (DriveNotFoundException ex) { } catch (ProviderInvocationException ex) { } catch (NotSupportedException ex) { } catch (InvalidOperationException ex) { } catch (ItemNotFoundException ex) { } if (provider == null) { return((MamlCommandHelpInfo)null); } CmdletProvider instance = provider.CreateInstance(); if (!(instance is ICmdletProviderSupportsHelp providerSupportsHelp)) { return((MamlCommandHelpInfo)null); } if (str == null) { throw new ItemNotFoundException(this._requestedPath, "PathNotFound"); } instance.Start(provider, cmdletProviderContext); string path1 = str; string helpMaml = providerSupportsHelp.GetHelpMaml(helpItemName, path1); if (string.IsNullOrEmpty(helpMaml)) { return((MamlCommandHelpInfo)null); } XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(helpMaml); return(MamlCommandHelpInfo.Load((XmlNode)xmlDocument.DocumentElement, HelpCategory.Provider)); }
internal static HelpInfo CreateFromComments(ExecutionContext context, CommandInfo commandInfo, HelpCommentsParser helpCommentsParser, bool dontSearchOnRemoteComputer) { if (!dontSearchOnRemoteComputer) { RemoteHelpInfo remoteHelpInfo = helpCommentsParser.GetRemoteHelpInfo(context, commandInfo); if (remoteHelpInfo != null) { if (remoteHelpInfo.GetUriForOnlineHelp() == null) { DefaultCommandHelpObjectBuilder.AddRelatedLinksProperties(remoteHelpInfo.FullHelp, commandInfo.CommandMetadata.HelpUri); } return(remoteHelpInfo); } } XmlDocument document = helpCommentsParser.BuildXmlFromComments(); HelpCategory helpCategory = commandInfo.HelpCategory; MamlCommandHelpInfo helpInfo = MamlCommandHelpInfo.Load(document.DocumentElement, helpCategory); if (helpInfo != null) { helpCommentsParser.SetAdditionalData(helpInfo); if (!string.IsNullOrEmpty(helpCommentsParser._sections.ForwardHelpTargetName) || !string.IsNullOrEmpty(helpCommentsParser._sections.ForwardHelpCategory)) { if (string.IsNullOrEmpty(helpCommentsParser._sections.ForwardHelpTargetName)) { helpInfo.ForwardTarget = helpInfo.Name; } else { helpInfo.ForwardTarget = helpCommentsParser._sections.ForwardHelpTargetName; } if (!string.IsNullOrEmpty(helpCommentsParser._sections.ForwardHelpCategory)) { try { helpInfo.ForwardHelpCategory = (HelpCategory)Enum.Parse(typeof(HelpCategory), helpCommentsParser._sections.ForwardHelpCategory, true); } catch (ArgumentException) { } } else { helpInfo.ForwardHelpCategory = HelpCategory.Workflow | HelpCategory.ExternalScript | HelpCategory.Filter | HelpCategory.Function | HelpCategory.ScriptCommand | HelpCategory.Cmdlet | HelpCategory.Alias; } } WorkflowInfo info3 = commandInfo as WorkflowInfo; if (info3 != null) { bool flag = DefaultCommandHelpObjectBuilder.HasCommonParameters(commandInfo.Parameters); bool flag2 = (commandInfo.CommandType & CommandTypes.Workflow) == CommandTypes.Workflow; helpInfo.FullHelp.Properties.Add(new PSNoteProperty("CommonParameters", flag)); helpInfo.FullHelp.Properties.Add(new PSNoteProperty("WorkflowCommonParameters", flag2)); DefaultCommandHelpObjectBuilder.AddDetailsProperties(helpInfo.FullHelp, info3.Name, info3.Noun, info3.Verb, "MamlCommandHelpInfo", helpInfo.Synopsis); DefaultCommandHelpObjectBuilder.AddSyntaxProperties(helpInfo.FullHelp, info3.Name, info3.ParameterSets, flag, flag2, "MamlCommandHelpInfo"); } if (helpInfo.GetUriForOnlineHelp() == null) { DefaultCommandHelpObjectBuilder.AddRelatedLinksProperties(helpInfo.FullHelp, commandInfo.CommandMetadata.HelpUri); } } return(helpInfo); }