Console.Waterworks.Core is the .Net Core version of Console.Waterworks.
https://www.craigoates.net/Software/project/8
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
2.9 KiB
61 lines
2.9 KiB
7 years ago
|
using Console.Waterworks.Core.Assistants;
|
||
|
using Console.Waterworks.Core.Models;
|
||
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Reflection;
|
||
|
|
||
|
namespace Console.Waterworks.Core.Specialists
|
||
|
{
|
||
|
class CommandsSpecialist
|
||
|
{
|
||
|
CommandsAssistant _assistant = new CommandsAssistant();
|
||
|
|
||
|
internal List<Type> GetCommandClasses(string commandsNameSpace)
|
||
|
{
|
||
|
string theNamespace = commandsNameSpace;
|
||
|
var q = from t in Assembly.GetEntryAssembly().GetTypes()
|
||
|
where t.GetTypeInfo().IsClass && t.Namespace == theNamespace
|
||
|
select t;
|
||
|
return q.ToList();
|
||
|
}
|
||
|
|
||
|
internal Dictionary<string, Dictionary<string, IEnumerable<ParameterInfo>>> GetCommandLibraries(List<Type> commandClasses)
|
||
|
{
|
||
|
var commandLibraries = new Dictionary<string, Dictionary<string, IEnumerable<ParameterInfo>>>();
|
||
|
foreach (var commandClass in commandClasses)
|
||
|
{
|
||
|
var methods = commandClass.GetMethods(BindingFlags.Static | BindingFlags.Public);
|
||
|
var methodDictionary = new Dictionary<string, IEnumerable<ParameterInfo>>();
|
||
|
foreach (var method in methods)
|
||
|
{
|
||
|
string commandName = method.Name;
|
||
|
methodDictionary.Add(commandName, method.GetParameters());
|
||
|
}
|
||
|
commandLibraries.Add(commandClass.Name, methodDictionary);
|
||
|
}
|
||
|
return commandLibraries;
|
||
|
}
|
||
|
|
||
|
internal string ExecuteCommand(string commandsNamespace, Command command, List<Type> commandClasses, Dictionary<string, Dictionary<string, IEnumerable<ParameterInfo>>> commandLibraries, ConsoleIOSpecialist consoleSpec)
|
||
|
{
|
||
|
var classValidated = _assistant.ValidateClass(command, commandLibraries);
|
||
|
var commandNameValidated = _assistant.ValidateCommand(command, commandLibraries);
|
||
|
Dictionary<string, IEnumerable<ParameterInfo>> methodDictionary;
|
||
|
if (classValidated != true || commandNameValidated != true)
|
||
|
return _assistant.ExecuteBadCommandProcedure(command, consoleSpec);
|
||
|
else
|
||
|
methodDictionary = commandLibraries[command.ClassName];
|
||
|
var paramInfoList = methodDictionary[command.Name].ToList();
|
||
|
var paramsValidated = _assistant.ValidateParamArguments(command, paramInfoList);
|
||
|
if (paramsValidated == false)
|
||
|
return _assistant.ExecuteMissingArgumentProcedure(command, paramInfoList, consoleSpec);
|
||
|
var methodParametreValueList = _assistant.GetParametreValueList(command, paramInfoList);
|
||
|
var typeInfo = _assistant.BuildCommandLibraryClass(command, commandsNamespace);
|
||
|
var inputArguments = _assistant.GetInputArguments(methodParametreValueList);
|
||
|
var result = _assistant.InvokeCommand(command, typeInfo, inputArguments);
|
||
|
return result;
|
||
|
}
|
||
|
}
|
||
|
}
|