예제 #1
0
        public override void Execute(List <CommandParameter> parameters)
        {
            var path        = GetStringParameterValue(parameters, CommandPathParameter.Name);
            var name        = GetStringParameterValue(parameters, CommandNameParameter.Name);
            var description = GetStringParameterValue(parameters, CommandDescriptionParameter.Name);

            if (StoredDataService.ExistsTemplate(name))
            {
                throw new TemplateNameRepeatedException();
            }
            if (!FileService.ExistsDirectory(path))
            {
                throw new PathNotFoundException(path);
            }
            if (!FileService.ExistsTemplateConfigFile(path))
            {
                throw new TemplateConfigFileNotFoundException(path);
            }
            if (!StringFormats.IsValidLogicalName(name))
            {
                throw new InvalidStringFormatException("Name can only contains alphanumeric characters");
            }

            StoredDataService.AddTemplate(path, name, description);

            Log($"Template stored!");
        }
예제 #2
0
        public override void Execute(List <CommandParameter> parameters)
        {
            var name = GetStringParameterValue(parameters, CommandNameParameter.Name);

            if (!StoredDataService.ExistsTemplate(name))
            {
                throw new TemplateNotFoundException();
            }
            StoredDataService.DeleteTemplate(name);
            Log($"Template deleted!");
        }
예제 #3
0
        private List <CommandBase> SearchCommandAndAlias(InputRequest inputRequest)
        {
            var commands = Commands.Where(k => k.CommandName.ToLowerInvariant() == inputRequest.CommandName)
                           .ToList();

            if (commands.Count == 0 && inputRequest.CommandName.Length > MandatoryCommandSufix.Length)
            {
                var aliasName = inputRequest.CommandName.Substring(0, inputRequest.CommandName.Length - MandatoryCommandSufix.Length);
                var isAlias   = StoredDataService.ExistsAlias(aliasName);
                if (isAlias)
                {
                    var commandName = StoredDataService.GetAliasedCommand(aliasName);
                    commands = Commands.Where(k => k.GetInvocationCommandName() == commandName)
                               .ToList();
                }
            }

            return(commands);
        }
예제 #4
0
        public override void Execute(List <CommandParameter> parameters)
        {
            var commandName = GetStringParameterValue(parameters, CommandNameParameter.Name);
            var aliasName   = GetStringParameterValue(parameters, CommandAliasParameter.Name);

            var commandAliased = RegisteredCommands
                                 .FirstOrDefault(k => k.GetInvocationCommandName() == commandName);

            if (commandAliased == null)
            {
                throw new CommandNotFoundException(commandName);
            }

            if (StoredDataService.ExistsAlias(aliasName))
            {
                throw new AliasRepeatedException(aliasName);
            }

            StoredDataService.AddAlias(commandName, aliasName);

            Log($"Alias stored!");
        }
예제 #5
0
        static void Main(string[] args)
        {
            //var argsV2 = StringFormats.StringToParams(string.Join(" ", args));
            _loggerService = new LoggerService();
            _loggerService.Log("###### INITIALIZED DYNAMICS CLI ######");
            LogRecievedArgs(args);
            bool isRecursive = args.Any(k => k.Length > 0 && k.First() == '\"');
            var  argsV2      = isRecursive ? StringFormats.StringToParams(string.Join(" ", args)) : args;

            LogProcessedArgs(argsV2);

            var storedData = StoredDataManager.GetStoredData();

            _loggerService = new LoggerService();
            IRegistryService   registryService   = new RegistryService();
            ICryptoService     cryptoService     = new CryptoService(registryService);
            IStoredDataService storedDataService = new StoredDataService(storedData, cryptoService);

            commandManager        = new CommandManager(_loggerService, storedDataService, cryptoService);
            commandManager.OnLog += CommandManager_OnLog;

            RegisterCommands(storedDataService, registryService, cryptoService);

            try
            {
                var inputCommand = new InputRequest(argsV2);
                commandManager.ExecuteInputRequest(inputCommand);
            }
            catch (PathNotFoundException ex)
            {
                ExceptionManager.RaiseException(_loggerService, $"Path '{ex.Message}' does not exists");
            }
            catch (Exception ex)
            {
                ExceptionManager.RaiseException(_loggerService, $"Throwed uncatched exception: {ex.ToString()}");
            }
        }
예제 #6
0
        public override void Execute(List <CommandParameter> parameters)
        {
            string path = string.Empty;

            if (IsParamOk(parameters, CommandNameParameter.Name))
            {
                var templateName = GetStringParameterValue(parameters, CommandNameParameter.Name);
                path = StoredDataService.GetTemplatePath(templateName);
            }
            else
            {
                var regardingPath = GetStringParameterValue(parameters, CommandPathParameter.Name);
                if (FileService.IsFile(regardingPath))
                {
                    path = FileService.GetFilePath(regardingPath);
                }
                else
                {
                    path = regardingPath;
                }
            }
            if (!FileService.ExistsDirectory(path))
            {
                throw new PathNotFoundException(path);
            }
            if (!FileService.ExistsTemplateConfigFile(path))
            {
                throw new TemplateConfigFileNotFoundException(path);
            }

            var templateConfig =
                ExceptionInLine.Run <DDTemplateConfig>(
                    () => { return(FileService.GetTemplateConfig(path)); },
                    (ex) => { throw new InvalidTemplateConfigFileException(ex.Message); });

            if (templateConfig == null)
            {
                throw new InvalidTemplateConfigFileException();
            }


            var destinationPathRequest = GetStringParameterValue(parameters, DestinationPathParameter.Name);
            var destinationPath        = string.Empty;

            if (string.IsNullOrEmpty(destinationPathRequest))
            {
                Log($"Set up for template '{templateConfig.TemplateName}'");
                Log($"Type destination folder:");
                destinationPath = ConsoleService.ReadLine();
            }
            else
            {
                destinationPath = destinationPathRequest;
            }
            var valuesRequest = GetStringParameterValue(parameters, ValuesParameter.Name);

            if (string.IsNullOrEmpty(valuesRequest))
            {
                Log($"Complete the paris for replace in the base project");
                foreach (var pair in templateConfig.ReplacePairs)
                {
                    Log($"\t{pair.ReplaceDescription}: (Old value = {pair.OldValue})");
                    var value        = ConsoleService.ReadLine();
                    var replacedPair = new ReplacePairValue(pair, value);
                    UserTemplateSetupReplaceStrings.Add(replacedPair);
                }
            }
            else
            {
                UserTemplateSetupReplaceStrings.AddRange(valuesRequest.Split(';').Select(k =>
                {
                    if (k.IndexOf("=") == -1)
                    {
                        throw new Exception("Invalid pair param=value");
                    }
                    var keyValue = k.Split('=');
                    var key      = keyValue[0];
                    var value    = keyValue[1];
                    if (string.IsNullOrEmpty(key) ||
                        string.IsNullOrEmpty(value))
                    {
                        throw new Exception("Invalid pair param=value");
                    }
                    var pair = templateConfig.ReplacePairs.FirstOrDefault(l => l.OldValue == key);
                    if (pair == null)
                    {
                        throw new Exception($"Param '{key}' is not defined in the template.json");
                    }
                    return(new ReplacePairValue(pair, value));
                }));
            }

            var absoluteDestionPath = FileService.GetAbsoluteCurrentPath(destinationPath);

            Log($"The template will be cloned at '{absoluteDestionPath}'");
            if (!FileService.ExistsDirectory(absoluteDestionPath))
            {
                FileService.CreateDirectory(absoluteDestionPath, true);
            }
            Log($"Cloning files...");
            var clonedFiles = FileService.CloneDirectory(path, absoluteDestionPath, templateConfig.IgnorePathPatterns);

            Log(clonedFiles.ToDisplayList($"Cloned {clonedFiles.Count} files:", "", false));

            foreach (var replaceString in UserTemplateSetupReplaceStrings)
            {
                Log($"Applying replacemene of string '{replaceString.ReplacedPair.OldValue}'->'{replaceString.Value}'. Apply for directories:{replaceString.ReplacedPair.ApplyForDirectories}, Apply for file names:{replaceString.ReplacedPair.ApplyForFileNames}, Apply for directories:{replaceString.ReplacedPair.ApplyForFileContents}");

                var oldValue = replaceString.ReplacedPair.OldValue;
                var newValue = replaceString.Value;
                var rootPath = absoluteDestionPath;
                var pattern  = replaceString.ReplacedPair.ApplyForFilePattern;
                if (replaceString.ReplacedPair.ApplyForDirectories)
                {
                    FileService.ReplaceAllSubDirectoriesName(rootPath, oldValue, newValue);
                }
                if (replaceString.ReplacedPair.ApplyForFileNames)
                {
                    FileService.ReplaceAllFilesName(rootPath, oldValue, newValue);
                }
                if (replaceString.ReplacedPair.ApplyForFileContents)
                {
                    FileService.ReplaceFilesContents(rootPath, oldValue, newValue, pattern);
                }
            }
        }
예제 #7
0
        public void ExecuteInputRequest(InputRequest inputRequest, List <string> consoleInputs = null)
        {
            List <CommandBase> commands = SearchCommandAndAlias(inputRequest);

            if (commands.Count > 1)
            {
                throw new DuplicateCommandException(commands.Select(k => k.GetInvocationCommandName()).ToList());
            }

            if (commands.Count == 0)
            {
                throw new CommandNotFoundException($"{inputRequest.CommandNamespace}.{inputRequest.CommandName} is not registered");
            }

            var command = commands[0];

            var commandsParameters = new List <CommandParameter>();

            if (command.CommandParametersDefinition.Where(k => k.Name != "help").Count() == 1 &&
                inputRequest.InputParameters.Count == 1 &&
                inputRequest.InputParameters[0].IsOnlyOne)
            {
                var targetCommandParameter = command.CommandParametersDefinition.FirstOrDefault(k => k.Name != "help");
                var parameter = GetParsedCommandParameter(
                    command, targetCommandParameter, inputRequest.InputParameters[0]);
                commandsParameters.Add(parameter);
            }
            else
            {
                foreach (var parameterDefinition in command.CommandParametersDefinition)
                {
                    var itemInput = GetImputParameterFromRequest(inputRequest, parameterDefinition);
                    if (itemInput != null)
                    {
                        var parameter = GetParsedCommandParameter(command, parameterDefinition, itemInput);
                        commandsParameters.Add(parameter);
                    }
                }
            }

            if (command.CanExecute(commandsParameters) || command.IsHelpCommand(commandsParameters))
            {
                _parameterManager.OnReplacedEncrypted     += _parameterManager_OnReplacedEncrypted;
                _parameterManager.OnReplacedAutoIncrement += _parameterManager_OnReplacedAutoIncrement;

                try
                {
                    ExecuteCommand(command, commandsParameters, consoleInputs);
                    if (command.CommandName != "ShowComandsHistoryCommand")
                    {
                        StoredDataService.AddCommandToHistorical(new HistoricalCommand(inputRequest));
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    _parameterManager.OnReplacedEncrypted     -= _parameterManager_OnReplacedEncrypted;
                    _parameterManager.OnReplacedAutoIncrement -= _parameterManager_OnReplacedAutoIncrement;
                    if (ExecutionMode == ExecutionModeTypes.Single)
                    {
                        StoredDataService.UpdateAutoIncrements(this._autoincrementParametersReplaced);
                    }
                }
            }
            else
            {
                throw new InvalidParamsException("Cannot execute this command with this parameters");
            }
        }
예제 #8
0
        static void Main(string[] args)
        {
            _loggerService = new LoggerService();
            _loggerService.Log("###### INITIALIZED CLI ######");
            LogRecievedArgs(args);
            bool isRecursive = args.Any(k => k.Length > 0 && k.First() == '\"');
            var  argsV2      = isRecursive ? StringFormats.StringToParams(string.Join(" ", args)) : args;

            LogProcessedArgs(argsV2);
            var storedData = StoredDataManager.GetStoredData();

            IRegistryService   registryService   = new RegistryService();
            ICryptoService     cryptoService     = new CryptoService(registryService);
            IStoredDataService storedDataService = new StoredDataService(storedData, cryptoService);

            commandManager        = new CommandManager(_loggerService, storedDataService, cryptoService);
            commandManager.OnLog += CommandManager_OnLog;

            RegisterCommands(storedDataService, registryService, cryptoService);

            try
            {
                var inputCommand = new InputRequest(argsV2);
                commandManager.ExecuteInputRequest(inputCommand);
            }
            catch (DuplicateCommandException ex)
            {
                ExceptionManager.RaiseException(_loggerService, $"Found {ex.Commands.Count} commands with the same name in different namespaces. In this case is necessary use the namespace for execute it. Commands: {string.Join(",", ex.Commands.ToArray())}");
            }
            catch (CommandNotFoundException)
            {
                ExceptionManager.RaiseException(_loggerService, $"Command not found. Use 'help' for check the available commands");
            }
            catch (InvalidParamsException)
            {
                ExceptionManager.RaiseException(_loggerService, $"This command cannot be executed with this combination of parameters");
            }
            catch (InvalidParamNameException ex)
            {
                ExceptionManager.RaiseException(_loggerService, $"Invalid parameter name '{ex.Message}'");
            }
            catch (NotArgumentsException)
            {
                ExceptionManager.RaiseException(_loggerService, $"Check all the params with 'help' command");
            }
            catch (NotValidCommandNameException ex)
            {
                ExceptionManager.RaiseException(_loggerService, $"Invalid command name '{ex.Message}'");
            }
            catch (AliasRepeatedException ex)
            {
                ExceptionManager.RaiseException(_loggerService, $"Alias '{ex.Message}' is already used");
            }
            catch (AliasNotFoundException ex)
            {
                ExceptionManager.RaiseException(_loggerService, $"Alias '{ex.Message}' is not registered");
            }
            catch (ParameterRepeatedException ex)
            {
                ExceptionManager.RaiseException(_loggerService, $"Parameter '{ex.Message}' is already used");
            }
            catch (ParameterNotFoundException ex)
            {
                ExceptionManager.RaiseException(_loggerService, $"Parameter '{ex.Message}' is not registered");
            }
            catch (InvalidParamException ex)
            {
                ExceptionManager.RaiseException(_loggerService, $"Cannot resolver parameter {ex.Message}");
            }
            catch (PathNotFoundException ex)
            {
                ExceptionManager.RaiseException(_loggerService, $"Path '{ex.Message}' does not exists");
            }
            catch (TemplateConfigFileNotFoundException)
            {
                ExceptionManager.RaiseException(_loggerService, $"Can't find '{Definitions.TemplateConfigFilename}' file in path");
            }
            catch (InvalidTemplateConfigFileException ex)
            {
                ExceptionManager.RaiseException(_loggerService, $"Config file '{Definitions.TemplateConfigFilename}' is invalid. Error parsing: {ex.Message}");
            }
            catch (InvalidStringFormatException ex)
            {
                ExceptionManager.RaiseException(_loggerService, $"Invalid string format. {ex.Message}");
            }
            catch (TemplateNameRepeatedException)
            {
                ExceptionManager.RaiseException(_loggerService, $"Template name repeated");
            }
            catch (TemplateNotFoundException)
            {
                ExceptionManager.RaiseException(_loggerService, $"Can't find any template with this name");
            }
            catch (RepositoryNotFoundException)
            {
                ExceptionManager.RaiseException(_loggerService, $"Can't find any repository with this name");
            }
            catch (PipelineConfigFileNotFoundException)
            {
                ExceptionManager.RaiseException(_loggerService, $"Can't find '{Definitions.PipelineConfigFilename}' file in path");
            }
            catch (InvalidPipelineConfigFileException ex)
            {
                ExceptionManager.RaiseException(_loggerService, $"Config file '{Definitions.PipelineConfigFilename}' is invalid. Error parsing: {ex.Message}");
            }
            catch (PipelineNameRepeatedException)
            {
                ExceptionManager.RaiseException(_loggerService, $"Pipeline name repeated");
            }
            catch (PipelineNotFoundException)
            {
                ExceptionManager.RaiseException(_loggerService, $"Can't find any pipeline with this name");
            }
            catch (Exception ex)
            {
                ExceptionManager.RaiseException(_loggerService, $"Throwed uncatched exception: {ex.ToString()}");
            }
            finally
            {
                _loggerService.Log("###### FINISHED! ######");
            }
        }