Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
ProviderContext.cs165 linesDownload Raw Back to help
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Management.Automation.Internal;5using System.Management.Automation.Provider;6using System.Xml;7 8using Dbg = System.Management.Automation.Diagnostics;9 10namespace System.Management.Automation11{12    /// <summary>13    /// The ProviderContext class.14    /// </summary>15    internal class ProviderContext16    {17        /// <summary>18        /// Requested path.19        /// </summary>20        private readonly string _requestedPath;21        private readonly ExecutionContext _executionContext;22        private readonly PathIntrinsics _pathIntrinsics;23 24        /// <summary>25        /// Requested path.26        /// </summary>27        internal string RequestedPath28        {29            get30            {31                return _requestedPath;32            }33        }34 35        /// <summary>36        /// Create a new instance of ProviderContext.37        /// </summary>38        internal ProviderContext(39            string requestedPath,40            ExecutionContext executionContext,41            PathIntrinsics pathIntrinsics)42        {43            Dbg.Assert(executionContext != null, "ExecutionContext cannot be null.");44            _requestedPath = requestedPath;45            _executionContext = executionContext;46            _pathIntrinsics = pathIntrinsics;47        }48 49        /// <summary>50        /// Get provider specific help info.51        /// </summary>52        internal MamlCommandHelpInfo GetProviderSpecificHelpInfo(string helpItemName)53        {54            if (InternalTestHooks.BypassOnlineHelpRetrieval)55            {56                // By returning null, we force get-help to return generic help57                // which includes a helpUri that points to the fwlink defined in the cmdlet code.58                return null;59            }60 61            // Get the provider.62            ProviderInfo providerInfo = null;63            PSDriveInfo driveInfo = null;64            string resolvedProviderPath = null;65            CmdletProviderContext cmdletProviderContext = new CmdletProviderContext(_executionContext);66 67            try68            {69                string psPath = _requestedPath;70                if (string.IsNullOrEmpty(_requestedPath))71                {72                    psPath = _pathIntrinsics.CurrentLocation.Path;73                }74 75                resolvedProviderPath = _executionContext.LocationGlobber.GetProviderPath(76                     psPath,77                     cmdletProviderContext,78                     out providerInfo,79                     out driveInfo);80            }81            // ignore exceptions caused by provider resolution82            catch (ArgumentNullException)83            {84            }85            catch (ProviderNotFoundException)86            {87            }88            catch (DriveNotFoundException)89            {90            }91            catch (ProviderInvocationException)92            {93            }94            catch (NotSupportedException)95            {96            }97            catch (InvalidOperationException)98            {99            }100            catch (ItemNotFoundException)101            {102            }103 104            if (providerInfo == null)105            {106                return null;107            }108 109            // Does the provider know how to generate MAML.110            CmdletProvider cmdletProvider = providerInfo.CreateInstance();111            if (!(cmdletProvider is ICmdletProviderSupportsHelp provider))112            {113                // Under JEA sessions the resolvedProviderPath will be null, we should allow get-help to continue.114                return null;115            }116 117            bool isJEASession = false;118            if (this._executionContext.InitialSessionState != null && this._executionContext.InitialSessionState.Providers != null && providerInfo != null)119            {120                foreach (121                    Runspaces.SessionStateProviderEntry sessionStateProvider in122                        this._executionContext.InitialSessionState.Providers[providerInfo.Name])123                {124                    if (sessionStateProvider.Visibility == SessionStateEntryVisibility.Private)125                    {126                        isJEASession = true;127                        break;128                    }129                }130            }131 132            if (resolvedProviderPath == null)133            {134                if (isJEASession)135                {136                    return null;137                }138                else139                {140                    throw new ItemNotFoundException(_requestedPath, "PathNotFound", SessionStateStrings.PathNotFound);141                }142            }143 144            // ok we have path and valid provider that supplies content..initialize the provider145            // and get the help content for the path.146            cmdletProvider.Start(providerInfo, cmdletProviderContext);147            // There should be exactly one resolved path.148            string providerPath = resolvedProviderPath;149            // Get the MAML help info. Don't catch exceptions thrown by provider.150            string mamlXmlString = provider.GetHelpMaml(helpItemName, providerPath);151            if (string.IsNullOrEmpty(mamlXmlString))152            {153                return null;154            }155            // process the MAML content only if it is non-empty.156            XmlDocument mamlDoc = InternalDeserializer.LoadUnsafeXmlDocument(157                mamlXmlString,158                false, /* ignore whitespace, comments, etc. */159                null); /* default maxCharactersInDocument */160            MamlCommandHelpInfo providerSpecificHelpInfo = MamlCommandHelpInfo.Load(mamlDoc.DocumentElement, HelpCategory.Provider);161            return providerSpecificHelpInfo;162        }163    }164}165