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.
107 lines
4.6 KiB
107 lines
4.6 KiB
7 years ago
|
using Console.Waterworks.Core.Models;
|
||
|
using Console.Waterworks.Core.Specialists;
|
||
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Reflection;
|
||
|
|
||
|
namespace Console.Waterworks.Core.Assistants
|
||
|
{
|
||
|
class CommandsAssistant
|
||
|
{
|
||
|
internal bool ValidateClass(Command command, Dictionary<string, Dictionary<string, IEnumerable<ParameterInfo>>> commandLibraries)
|
||
|
=> (commandLibraries.ContainsKey(command.ClassName)) ? true : false;
|
||
|
|
||
|
internal bool ValidateCommand(Command command, Dictionary<string, Dictionary<string, IEnumerable<ParameterInfo>>> commandLibraries)
|
||
|
{
|
||
|
var methodDict = commandLibraries[command.ClassName];
|
||
|
return (methodDict.ContainsKey(command.Name)) ? true : false;
|
||
|
}
|
||
|
|
||
|
internal string ExecuteBadCommandProcedure(Command command, ConsoleIOSpecialist consoleSpec)
|
||
|
{
|
||
|
consoleSpec.WriteErrorSuffix();
|
||
|
return $"The command \'{command.Name}\' is not recognised.";
|
||
|
}
|
||
|
|
||
|
internal bool ValidateParamArguments(Command command, List<ParameterInfo> paramInfoList)
|
||
|
{
|
||
|
var requiredParams = paramInfoList.Where(p => p.IsOptional == false);
|
||
|
var optionalParams = paramInfoList.Where(p => p.IsOptional == true);
|
||
|
int requiredCount = requiredParams.Count();
|
||
|
int optionalCount = optionalParams.Count();
|
||
|
int providedCount = command.Arguments.Count();
|
||
|
return (requiredCount > providedCount) ? false : true;
|
||
|
}
|
||
|
|
||
|
internal string ExecuteMissingArgumentProcedure(Command command, List<ParameterInfo> paramInfoList, ConsoleIOSpecialist consoleSpec)
|
||
|
{
|
||
|
var requiredParams = paramInfoList.Where(p => p.IsOptional == false);
|
||
|
var optionalParams = paramInfoList.Where(p => p.IsOptional == true);
|
||
|
int requiredCount = requiredParams.Count();
|
||
|
int optionalCount = optionalParams.Count();
|
||
|
int providedCount = command.Arguments.Count();
|
||
|
return $"Missing required argument. {requiredCount} required, {optionalCount} optional, {providedCount} provided.";
|
||
|
}
|
||
|
|
||
|
internal List<object> GetParametreValueList(Command command, List<ParameterInfo> paramInfoList)
|
||
|
{
|
||
|
var methodParameterValueList = new List<object>();
|
||
|
if (paramInfoList.Count() > 0)
|
||
|
{
|
||
|
foreach (var param in paramInfoList)
|
||
|
{
|
||
|
methodParameterValueList.Add(param.DefaultValue);
|
||
|
}
|
||
|
for (int i = 0; i < command.Arguments.Count(); i++)
|
||
|
{
|
||
|
var methodParam = paramInfoList.ElementAt(i);
|
||
|
var typeRequired = methodParam.ParameterType;
|
||
|
object value = null;
|
||
|
try
|
||
|
{
|
||
|
value = CoercionSpecialist.CoerceArgument(typeRequired, command.Arguments.ElementAt(i));
|
||
|
methodParameterValueList.RemoveAt(i);
|
||
|
methodParameterValueList.Insert(i, value);
|
||
|
}
|
||
|
catch (ArgumentException ex)
|
||
|
{
|
||
|
string message = $"The value passed for argument '{methodParam.Name}' cannot be parsed to type '{typeRequired.Name}'"; // The exception message is being used instead, for now.
|
||
|
throw new ArgumentException(ex.Message);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return methodParameterValueList;
|
||
|
}
|
||
|
|
||
|
internal Type BuildCommandLibraryClass(Command command, string commandsNamespace)
|
||
|
{
|
||
|
Assembly programAssembly = Assembly.GetEntryAssembly();
|
||
|
Type commandLibraryClass = programAssembly.GetType($"{commandsNamespace}.{command.ClassName}");
|
||
|
return commandLibraryClass;
|
||
|
}
|
||
|
|
||
|
internal string InvokeCommand(Command command, Type typeInfo, object[] inputArguments)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
//var result = typeInfo.GetMethod(command.Name).Invoke(command.Name, inputArguments).ToString();
|
||
|
// return result.ToString();
|
||
|
return typeInfo.GetMethod(command.Name).Invoke(command.Name, inputArguments).ToString();
|
||
|
}
|
||
|
catch (TargetInvocationException ex)
|
||
|
{
|
||
|
throw ex.InnerException;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
internal object[] GetInputArguments(List<object> methodParametreValueList)
|
||
|
{
|
||
|
object[] inputArguments = null;
|
||
|
if (methodParametreValueList.Count > 0)
|
||
|
inputArguments = methodParametreValueList.ToArray();
|
||
|
return inputArguments;
|
||
|
}
|
||
|
}
|
||
|
}
|