File size: 5,790 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 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Management.Automation.Internal;
using System.Management.Automation.Provider;
using System.Xml;
using Dbg = System.Management.Automation.Diagnostics;
namespace System.Management.Automation
{
/// <summary>
/// The ProviderContext class.
/// </summary>
internal class ProviderContext
{
/// <summary>
/// Requested path.
/// </summary>
private readonly string _requestedPath;
private readonly ExecutionContext _executionContext;
private readonly PathIntrinsics _pathIntrinsics;
/// <summary>
/// Requested path.
/// </summary>
internal string RequestedPath
{
get
{
return _requestedPath;
}
}
/// <summary>
/// Create a new instance of ProviderContext.
/// </summary>
internal ProviderContext(
string requestedPath,
ExecutionContext executionContext,
PathIntrinsics pathIntrinsics)
{
Dbg.Assert(executionContext != null, "ExecutionContext cannot be null.");
_requestedPath = requestedPath;
_executionContext = executionContext;
_pathIntrinsics = pathIntrinsics;
}
/// <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();
if (!(cmdletProvider is ICmdletProviderSupportsHelp provider))
{
// Under JEA sessions the resolvedProviderPath will be null, we should allow get-help to continue.
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 supplies 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;
}
}
}
|