Przeglądaj źródła

Common:

Extens DisableCommands for custom commands
Dalibor Votruba 6 lat temu
rodzic
commit
d5b22c6882

+ 1 - 1
QConsole/QConsole.DevBench/Program.cs

@@ -17,7 +17,7 @@ namespace Quadarax.Foundation.QConsole.DevBench
             config.AllowInteractive = false;
             config.WaitOnKeyInNonInteractiveMode = true;
             config.CommandDefinitionAssebmly = new Assembly[]{Assembly.GetEntryAssembly()};
-            config.DisableDefaultCommands = true;
+            config.DisableDefaultCommands();
 
              var console = new Engine(config, args);
             console.Start();

+ 44 - 3
QConsole/QConsole/Configuration/StartupConfiguration.cs

@@ -1,4 +1,6 @@
 using System;
+using System.Collections.Generic;
+using System.Linq;
 using System.Reflection;
 using System.Xml.XPath;
 
@@ -8,6 +10,9 @@ namespace Quadarax.Foundation.QConsole.Configuration
     {
         #region *** Private Fields ***
         private bool _isConsoleDebug;
+        private string[] _defaultCommands = {"CmdClear", "CmdExit", "CmdPrint", "CmdSelection"};
+
+        private readonly IList<string> _disabledCommands = new List<string>();
         #endregion
 
         #region *** Properties ***
@@ -75,9 +80,9 @@ namespace Quadarax.Foundation.QConsole.Configuration
         public Assembly[] CommandDefinitionAssebmly { get; set; }
 
         /// <summary>
-        /// Defines if default (build-in) commands CLEAR,EXIT,PRINT,SELECTION will be disabled (hidden).
+        /// Returns name of command classes that will be disabled (hidden)
         /// </summary>
-        public bool DisableDefaultCommands { get; set; }
+        public string[] DisabledCommands => _disabledCommands.ToArray();
 
         /// <summary>
         /// Defines if waits on key at the end of process in non-interactive mode (inline mode)
@@ -131,6 +136,43 @@ namespace Quadarax.Foundation.QConsole.Configuration
             IsConsoleDebug = false;
             return this;
         }
+
+        public StartupConfiguration DisableDefaultCommands()
+        {
+            return DisableCommands(_defaultCommands);
+        }
+
+        public StartupConfiguration EnableDefaultCommands()
+        {
+            return EnableCommands(_defaultCommands);
+        }
+
+        public StartupConfiguration EnableCommands(params string[] commandClassNames)
+        {
+            if (commandClassNames == null)
+                return this;
+
+            foreach (var className in commandClassNames)
+            {
+                if (_disabledCommands.Contains(className))
+                    _disabledCommands.Remove(className);
+            }
+            return this;
+        }
+
+        public StartupConfiguration DisableCommands(params string[] commandClassNames)
+        {
+            if (commandClassNames == null)
+                return this;
+            foreach (var className in commandClassNames)
+            {
+                if (!_disabledCommands.Contains(className))
+                    _disabledCommands.Add(className);
+            }
+
+            return this;
+        }
+
         #endregion
 
         #region *** Private Operations ***
@@ -149,7 +191,6 @@ namespace Quadarax.Foundation.QConsole.Configuration
             AllowInteractive = Defaults.Console.InteractiveAllowed;
             AllowPreset = Defaults.Console.PresetAllowed;
             IsConsoleDebug = Defaults.Console.ConsoleDebugMode;
-            DisableDefaultCommands = Defaults.Console.DisableDefaultCommands;
             WaitOnKeyInNonInteractiveMode = Defaults.Console.WaitOnKeyInNonInteractiveMode;
 
         }

+ 15 - 3
QConsole/QConsole/Engine.cs

@@ -220,9 +220,10 @@ namespace Quadarax.Foundation.QConsole
         }
         private IEnumerable<Type> GetTypesWithHelpAttribute(Attribute attribute)
         {
-            var defaultTypes = _configuration.DisableDefaultCommands ? 
-                new List<Type>(new []{typeof(CmdHelp)}): 
-            Assembly.GetExecutingAssembly().GetTypes().Where(type => type.GetCustomAttributes(attribute.GetType(), true).Length > 0).ToList();
+           
+            WriteDebugInfo($"Loading command assemblies ...");
+
+            var defaultTypes = Assembly.GetExecutingAssembly().GetTypes().Where(type => type.GetCustomAttributes(attribute.GetType(), true).Length > 0).ToList();
             
             var customTypes = new List<Type>();
             if (_configuration.CommandDefinitionAssebmly != null)
@@ -230,6 +231,7 @@ namespace Quadarax.Foundation.QConsole
                 foreach (var assembly in _configuration.CommandDefinitionAssebmly)
                 {
                     customTypes.AddRange(assembly.GetTypes().Where(type => type.GetCustomAttributes(attribute.GetType(), true).Length > 0).ToArray());
+                    WriteDebugInfo(assembly.GetName().Name + " was scanned for commands.");
                 }
             }
             else
@@ -238,6 +240,16 @@ namespace Quadarax.Foundation.QConsole
             }
             WriteDebugInfo($"Command definition obtaining process found {defaultTypes.Count} default definitions and {customTypes.Count} custom definitions.");
             defaultTypes.AddRange(customTypes);
+
+            foreach (var disabledCommand in _configuration.DisabledCommands)
+            {
+                var disabled = defaultTypes.FirstOrDefault(x => x.Name == disabledCommand);
+                if (disabled==null)
+                    continue;
+                defaultTypes.Remove(disabled);
+                WriteDebugInfo($"Commnad class '{disabled.Name}' was skipped by configuration.");
+            }
+
             return defaultTypes.ToArray();
         }