File size: 13,460 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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#nullable enable
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation.Internal;
using System.Management.Automation.Subsystem.DSC;
using System.Management.Automation.Subsystem.Feedback;
using System.Management.Automation.Subsystem.Prediction;
namespace System.Management.Automation.Subsystem
{
/// <summary>
/// Class used to manage subsystems.
/// </summary>
public static class SubsystemManager
{
private static readonly ReadOnlyCollection<SubsystemInfo> s_subsystems;
private static readonly ReadOnlyDictionary<Type, SubsystemInfo> s_subSystemTypeMap;
private static readonly ReadOnlyDictionary<SubsystemKind, SubsystemInfo> s_subSystemKindMap;
static SubsystemManager()
{
var subsystems = new SubsystemInfo[]
{
SubsystemInfo.Create<ICommandPredictor>(
SubsystemKind.CommandPredictor,
allowUnregistration: true,
allowMultipleRegistration: true),
SubsystemInfo.Create<ICrossPlatformDsc>(
SubsystemKind.CrossPlatformDsc,
allowUnregistration: true,
allowMultipleRegistration: false),
SubsystemInfo.Create<IFeedbackProvider>(
SubsystemKind.FeedbackProvider,
allowUnregistration: true,
allowMultipleRegistration: true),
};
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);
// Register built-in suggestion providers.
RegisterSubsystem(SubsystemKind.FeedbackProvider, new GeneralCommandErrorFeedback());
}
#region internal - Retrieve subsystem proxy object
/// <summary>
/// Get the proxy object registered for a specific subsystem.
/// Return null when the given subsystem is not registered.
/// </summary>
/// <remarks>
/// Design point:
/// The implementation proxy object is not supposed to expose to users.
/// Users shouldn't depend on a implementation proxy object directly, but instead should depend on PowerShell APIs.
/// <para/>
/// Example: if a user want to use prediction functionality, he/she should use the PowerShell prediction API instead of
/// directly interacting with the implementation proxy object of `IPrediction`.
/// </remarks>
/// <typeparam name="TConcreteSubsystem">The concrete subsystem base type.</typeparam>
/// <returns>The most recently registered implementation object of the concrete subsystem.</returns>
internal static TConcreteSubsystem? GetSubsystem<TConcreteSubsystem>()
where TConcreteSubsystem : class, ISubsystem
{
if (s_subSystemTypeMap.TryGetValue(typeof(TConcreteSubsystem), out SubsystemInfo? subsystemInfo))
{
var subsystemInfoImpl = (SubsystemInfoImpl<TConcreteSubsystem>)subsystemInfo;
return subsystemInfoImpl.GetImplementation();
}
throw new ArgumentException(
StringUtil.Format(
SubsystemStrings.SubsystemTypeUnknown,
typeof(TConcreteSubsystem).FullName));
}
/// <summary>
/// Get all the proxy objects registered for a specific subsystem.
/// Return an empty collection when the given subsystem is not registered.
/// </summary>
/// <typeparam name="TConcreteSubsystem">The concrete subsystem base type.</typeparam>
/// <returns>A readonly collection of all implementation objects registered for the concrete subsystem.</returns>
internal static ReadOnlyCollection<TConcreteSubsystem> GetSubsystems<TConcreteSubsystem>()
where TConcreteSubsystem : class, ISubsystem
{
if (s_subSystemTypeMap.TryGetValue(typeof(TConcreteSubsystem), out SubsystemInfo? subsystemInfo))
{
var subsystemInfoImpl = (SubsystemInfoImpl<TConcreteSubsystem>)subsystemInfo;
return subsystemInfoImpl.GetAllImplementations();
}
throw new ArgumentException(
StringUtil.Format(
SubsystemStrings.SubsystemTypeUnknown,
typeof(TConcreteSubsystem).FullName));
}
#endregion
#region public - Subsystem metadata
/// <summary>
/// Get the information about all subsystems.
/// </summary>
/// <returns>A readonly collection of all <see cref="SubsystemInfo"/> objects.</returns>
public static ReadOnlyCollection<SubsystemInfo> GetAllSubsystemInfo()
{
return s_subsystems;
}
/// <summary>
/// Get the information about a subsystem by the subsystem type.
/// </summary>
/// <param name="subsystemType">The base type of a specific concrete subsystem.</param>
/// <returns>The <see cref="SubsystemInfo"/> object that represents the concrete subsystem.</returns>
public static SubsystemInfo GetSubsystemInfo(Type subsystemType)
{
ArgumentNullException.ThrowIfNull(subsystemType);
if (s_subSystemTypeMap.TryGetValue(subsystemType, out SubsystemInfo? subsystemInfo))
{
return subsystemInfo;
}
throw new ArgumentException(
subsystemType == typeof(ISubsystem)
? SubsystemStrings.MustUseConcreteSubsystemType
: StringUtil.Format(
SubsystemStrings.SubsystemTypeUnknown,
subsystemType.FullName),
nameof(subsystemType));
}
/// <summary>
/// Get the information about a subsystem by the subsystem kind.
/// </summary>
/// <param name="kind">A specific <see cref="SubsystemKind"/>.</param>
/// <returns>The <see cref="SubsystemInfo"/> object that represents the concrete subsystem.</returns>
public static SubsystemInfo GetSubsystemInfo(SubsystemKind kind)
{
if (s_subSystemKindMap.TryGetValue(kind, out SubsystemInfo? subsystemInfo))
{
return subsystemInfo;
}
throw new ArgumentException(
StringUtil.Format(
SubsystemStrings.SubsystemKindUnknown,
kind.ToString()),
nameof(kind));
}
#endregion
#region public - Subsystem registration
/// <summary>
/// Subsystem registration.
/// </summary>
/// <typeparam name="TConcreteSubsystem">The concrete subsystem base type.</typeparam>
/// <typeparam name="TImplementation">The implementation type of that concrete subsystem.</typeparam>
/// <param name="proxy">An instance of the implementation.</param>
public static void RegisterSubsystem<TConcreteSubsystem, TImplementation>(TImplementation proxy)
where TConcreteSubsystem : class, ISubsystem
where TImplementation : class, TConcreteSubsystem
{
ArgumentNullException.ThrowIfNull(proxy);
RegisterSubsystem(GetSubsystemInfo(typeof(TConcreteSubsystem)), proxy);
}
/// <summary>
/// Register an implementation for a subsystem.
/// </summary>
/// <param name="kind">The target <see cref="SubsystemKind"/> of the registration.</param>
/// <param name="proxy">An instance of the implementation.</param>
public static void RegisterSubsystem(SubsystemKind kind, ISubsystem proxy)
{
ArgumentNullException.ThrowIfNull(proxy);
SubsystemInfo info = GetSubsystemInfo(kind);
if (!info.SubsystemType.IsAssignableFrom(proxy.GetType()))
{
throw new ArgumentException(
StringUtil.Format(
SubsystemStrings.ConcreteSubsystemNotImplemented,
kind.ToString(),
info.SubsystemType.Name),
nameof(proxy));
}
RegisterSubsystem(info, proxy);
}
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);
}
#endregion
#region public - Subsystem unregistration
/// <summary>
/// Subsystem unregistration.
/// Throw 'InvalidOperationException' when called for subsystems that cannot be unregistered.
/// </summary>
/// <typeparam name="TConcreteSubsystem">The base type of the target concrete subsystem of the un-registration.</typeparam>
/// <param name="id">The Id of the implementation to be unregistered.</param>
public static void UnregisterSubsystem<TConcreteSubsystem>(Guid id)
where TConcreteSubsystem : class, ISubsystem
{
UnregisterSubsystem(GetSubsystemInfo(typeof(TConcreteSubsystem)), id);
}
/// <summary>
/// Subsystem unregistration.
/// Throw 'InvalidOperationException' when called for subsystems that cannot be unregistered.
/// </summary>
/// <param name="kind">The target <see cref="SubsystemKind"/> of the un-registration.</param>
/// <param name="id">The Id of the implementation to be unregistered.</param>
public static void UnregisterSubsystem(SubsystemKind kind, Guid id)
{
UnregisterSubsystem(GetSubsystemInfo(kind), id);
}
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.
}
}
}
#endregion
}
}
|