From: Mike McLaughlin Date: Sat, 16 Mar 2019 00:10:51 +0000 (-0700) Subject: Add more general purpose service provider to CommandProcessor. X-Git-Tag: submit/tizen/20190813.035844~46^2~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3e40ee93a5dc47a5cc90e07255b64f46600430c7;p=platform%2Fcore%2Fdotnet%2Fdiagnostics.git Add more general purpose service provider to CommandProcessor. Add the "ConsoleProvider" instance as a service. Change the "exit" command to use it. --- diff --git a/src/Microsoft.Diagnostic.Repl/Command/CommandProcessor.cs b/src/Microsoft.Diagnostic.Repl/Command/CommandProcessor.cs index 5d42b35c9..a7b2dbe62 100644 --- a/src/Microsoft.Diagnostic.Repl/Command/CommandProcessor.cs +++ b/src/Microsoft.Diagnostic.Repl/Command/CommandProcessor.cs @@ -19,11 +19,7 @@ namespace Microsoft.Diagnostic.Repl { private readonly Parser _parser; private readonly Command _rootCommand; - - /// - /// Domain specific context passed to commands - /// - public object CommandContext { get; set; } + private readonly Dictionary _services = new Dictionary(); /// /// Create an instance of the command processor; @@ -31,6 +27,7 @@ namespace Microsoft.Diagnostic.Repl /// The list of assemblies to look for commands public CommandProcessor(IEnumerable assemblies) { + _services.Add(typeof(CommandProcessor), this); var rootBuilder = new CommandLineBuilder(); rootBuilder.UseHelp() .UseParseDirective() @@ -42,6 +39,26 @@ namespace Microsoft.Diagnostic.Repl _parser = rootBuilder.Build(); } + /// + /// Adds a service or context to inject into an command. + /// + /// type of service + /// service instance + public void AddService(T instance) + { + AddService(typeof(T), instance); + } + + /// + /// Adds a service or context to inject into an command. + /// + /// service type + /// service instance + public void AddService(Type type, object instance) + { + _services.Add(type, instance); + } + /// /// Parse the command line. /// @@ -194,7 +211,7 @@ namespace Microsoft.Diagnostic.Repl { IEnumerable optionResults = context.ParseResult.CommandResult.Children.OfType(); - foreach (var property in _properties) + foreach ((PropertyInfo Property, Option Option) property in _properties) { object value = property.Property.GetValue(instance); @@ -210,11 +227,8 @@ namespace Microsoft.Diagnostic.Repl else if (propertyType == typeof(IConsole)) { value = context.Console; } - else if (propertyType == typeof(CommandProcessor)) { - value = _commandProcessor; - } - else if (propertyType == _commandProcessor.CommandContext?.GetType()) { - value = _commandProcessor.CommandContext; + else if (_commandProcessor._services.TryGetValue(propertyType, out object service)) { + value = service; } else if (property.Option != null) { diff --git a/src/Tools/dotnet-dump/AnalyzeContext.cs b/src/Tools/dotnet-dump/AnalyzeContext.cs index 9fff5af27..d347a0e25 100644 --- a/src/Tools/dotnet-dump/AnalyzeContext.cs +++ b/src/Tools/dotnet-dump/AnalyzeContext.cs @@ -20,11 +20,10 @@ namespace Microsoft.Diagnostic.Tools.Dump private ClrRuntime _runtime; private SOSHost _sosHost; - public AnalyzeContext(IConsole console, DataTarget target, Action exit) + public AnalyzeContext(IConsole console, DataTarget target) { _console = console; Target = target; - Exit = exit; } /// diff --git a/src/Tools/dotnet-dump/Analyzer.cs b/src/Tools/dotnet-dump/Analyzer.cs index 69939f3b7..be033bd99 100644 --- a/src/Tools/dotnet-dump/Analyzer.cs +++ b/src/Tools/dotnet-dump/Analyzer.cs @@ -20,6 +20,7 @@ namespace Microsoft.Diagnostic.Tools.Dump { _consoleProvider = new ConsoleProvider(); _commandProcessor = new CommandProcessor(new Assembly[] { typeof(Analyzer).Assembly }); + _commandProcessor.AddService(_consoleProvider); } public async Task Analyze(FileInfo dump_path, string[] command) @@ -45,10 +46,10 @@ namespace Microsoft.Diagnostic.Tools.Dump _consoleProvider.Out.WriteLine("Type 'quit' or 'exit' to exit the session."); // Create common analyze context for commands - var analyzeContext = new AnalyzeContext(_consoleProvider, target, _consoleProvider.Stop) { + var analyzeContext = new AnalyzeContext(_consoleProvider, target) { CurrentThreadId = unchecked((int)target.DataReader.EnumerateAllThreads().FirstOrDefault()) }; - _commandProcessor.CommandContext = analyzeContext; + _commandProcessor.AddService(analyzeContext); // Automatically enable symbol server support on Linux and MacOS if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { diff --git a/src/Tools/dotnet-dump/Commands/ExitCommand.cs b/src/Tools/dotnet-dump/Commands/ExitCommand.cs index a9fe36a41..716d3e38c 100644 --- a/src/Tools/dotnet-dump/Commands/ExitCommand.cs +++ b/src/Tools/dotnet-dump/Commands/ExitCommand.cs @@ -9,11 +9,11 @@ namespace Microsoft.Diagnostic.Tools.Dump [CommandAlias(Name = "quit")] public class ExitCommand : CommandBase { - public AnalyzeContext AnalyzeContext { get; set; } + public ConsoleProvider ConsoleProvider { get; set; } public override Task InvokeAsync() { - AnalyzeContext.Exit(); + ConsoleProvider.Stop(); return Task.CompletedTask; } }