예제 #1
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);
                }
            }
        }
예제 #2
0
        public override void Execute(List <CommandParameter> parameters)
        {
            string path = string.Empty;

            if (IsParamOk(parameters, CommandNameParameter.Name))
            {
                var pipelineName = GetStringParameterValue(parameters, CommandNameParameter.Name);
                path = StoredDataService.GetPipelinePath(pipelineName);
            }
            else
            {
                path = GetStringParameterValue(parameters, CommandPathParameter.Name);
            }

            var debugMode = GetBoolParameterValue(parameters, DebugParameter.Name, false);;


            if (!FileService.ExistsFile(path))
            {
                throw new PathNotFoundException(path);
            }

            var pipelineConfig =
                ExceptionInLine.Run <DDPipelineConfig>(
                    () => { return(FileService.GetPipelineConfig(path)); },
                    (ex) => { throw new InvalidPipelineConfigFileException(ex.Message); });

            if (!FileService.IsValidPipelineConfiguration(pipelineConfig))
            {
                throw new InvalidPipelineConfigFileException();
            }

            var multipleCommandManager =
                new CommandManager(LoggerService, StoredDataService, CryptoService, CommandManager.ExecutionModeTypes.Multiple);

            multipleCommandManager.RegisterCommands(RegisteredCommands);
            multipleCommandManager.OnLog += MultipleCommandManager_OnLog;
            multipleCommandManager.OnReplacedAutoIncrementInCommand += MultipleCommandManager_OnReplacedAutoIncrementInSubCommand;

            RequestInputVariables(pipelineConfig);

            Log($"## Executing pipeline {pipelineConfig.PipelineName}...");
            Stopwatch sb                 = new Stopwatch(); sb.Start();
            int       counter            = 0;
            int       totalCommandsCound = pipelineConfig.Commands.Count;

            foreach (var commandDefinition in pipelineConfig.Commands)
            {
                var currentCounter = counter++;
                var commandName    = string.IsNullOrEmpty(commandDefinition.Alias) ? "" : commandDefinition.Alias;
                if (commandDefinition.IsDisabled)
                {
                    Log($"### [{currentCounter + 1}/{totalCommandsCound}] {commandName} | Skip command. Reason: Disabled");
                }
                else
                {
                    if (commandDefinition.IsIteration)
                    {
                        ExecuteIterationCommand(
                            multipleCommandManager,
                            commandDefinition.Command,
                            commandDefinition.ConsoleInputs,
                            pipelineConfig.PipelineConstants,
                            totalCommandsCound,
                            currentCounter,
                            commandName,
                            commandDefinition);
                    }
                    else
                    {
                        ExecuteCommand(
                            multipleCommandManager,
                            commandDefinition.Command,
                            commandDefinition.ConsoleInputs,
                            pipelineConfig.PipelineConstants,
                            totalCommandsCound,
                            currentCounter,
                            commandName);
                    }
                }
                if (debugMode)
                {
                    Console.WriteLine("---- ENABLED DEBUG MODE ----");
                    Console.WriteLine("---- Type any character for continue... ");
                    Console.WriteLine("---- ENABLED DEBUG MODE ----");
                    Console.ReadLine();
                }
            }
            var time = StringFormats.MillisecondsToHumanTime(sb.ElapsedMilliseconds);

            Log($"## Completed pipeline {pipelineConfig.PipelineName} in {time}");
        }