コード例 #1
0
        static SubsystemManager()
        {
            var subsystems = new SubsystemInfo[]
            {
                SubsystemInfo.Create <ICommandPredictor>(
                    SubsystemKind.CommandPredictor,
                    allowUnregistration: true,
                    allowMultipleRegistration: true),

                SubsystemInfo.Create <ICrossPlatformDsc>(
                    SubsystemKind.CrossPlatformDsc,
                    allowUnregistration: true,
                    allowMultipleRegistration: false),
            };

            var subSystemTypeMap = new Dictionary <Type, SubsystemInfo>(subsystems.Length);
            var subSystemKindMap = new Dictionary <SubsystemKind, SubsystemInfo>(subsystems.Length);

            foreach (var subsystem in subsystems)
            {
                subSystemTypeMap.Add(subsystem.SubsystemType, subsystem);
                subSystemKindMap.Add(subsystem.Kind, subsystem);
            }

            s_subsystems       = new ReadOnlyCollection <SubsystemInfo>(subsystems);
            s_subSystemTypeMap = new ReadOnlyDictionary <Type, SubsystemInfo>(subSystemTypeMap);
            s_subSystemKindMap = new ReadOnlyDictionary <SubsystemKind, SubsystemInfo>(subSystemKindMap);
        }
コード例 #2
0
        private static void UnregisterSubsystem(SubsystemInfo subsystemInfo, Guid id)
        {
            if (subsystemInfo.RequiredCmdlets.Count > 0 || subsystemInfo.RequiredFunctions.Count > 0)
            {
                throw new NotSupportedException("NotSupported yet: unregister subsystem that introduced new cmdlets/functions.");
            }

            ISubsystem impl = subsystemInfo.UnregisterImplementation(id);

            if (impl is IDisposable disposable)
            {
                disposable.Dispose();
            }
        }
コード例 #3
0
        private static void RegisterSubsystem(SubsystemInfo subsystemInfo, ISubsystem proxy)
        {
            if (proxy.Id == Guid.Empty)
            {
                throw new ArgumentException(
                          StringUtil.Format(
                              SubsystemStrings.EmptyImplementationId,
                              subsystemInfo.Kind.ToString()),
                          nameof(proxy));
            }

            if (string.IsNullOrEmpty(proxy.Name))
            {
                throw new ArgumentException(
                          StringUtil.Format(
                              SubsystemStrings.NullOrEmptyImplementationName,
                              subsystemInfo.Kind.ToString()),
                          nameof(proxy));
            }

            if (string.IsNullOrEmpty(proxy.Description))
            {
                throw new ArgumentException(
                          StringUtil.Format(
                              SubsystemStrings.NullOrEmptyImplementationDescription,
                              subsystemInfo.Kind.ToString()),
                          nameof(proxy));
            }

            if (subsystemInfo.RequiredCmdlets.Count > 0 || subsystemInfo.RequiredFunctions.Count > 0)
            {
                // Process 'proxy.CmdletImplementationAssembly' and 'proxy.FunctionsToDefine'
                // Functions are added to global scope.
                // Cmdlets are loaded in a way like a snapin, making the 'Source' of the cmdlets to be 'Microsoft.PowerShell.Core'.
                //
                // For example, let's say the Job adapter is made a subsystem, then all `*-Job` cmdlets will be moved out of S.M.A
                // into a subsystem implementation DLL. After registration, all `*-Job` cmdlets should be back in the
                // 'Microsoft.PowerShell.Core' namespace to keep backward compatibility.
                //
                // Both cmdlets and functions are added to the default InitialSessionState used for creating a new Runspace,
                // so the subsystem works for all subsequent new runspaces after it's registered.
                // Take the Job adapter subsystem as an instance again, so when creating another Runspace after the registration,
                // all '*-Job' cmdlets should be available in the 'Microsoft.PowerShell.Core' namespace by default.
            }

            subsystemInfo.RegisterImplementation(proxy);
        }
コード例 #4
0
        private static void UnregisterSubsystem(SubsystemInfo subsystemInfo, Guid id)
        {
            if (subsystemInfo.RequiredCmdlets.Count > 0 || subsystemInfo.RequiredFunctions.Count > 0)
            {
                throw new NotSupportedException("NotSupported yet: unregister subsystem that introduced new cmdlets/functions.");
            }

            ISubsystem impl = subsystemInfo.UnregisterImplementation(id);

            if (impl is IDisposable disposable)
            {
                try
                {
                    disposable.Dispose();
                }
                catch
                {
                    // It's OK to ignore all exceptions when disposing the object.
                }
            }
        }