Пример #1
0
        /// <summary>
        /// Primary Program Execution
        /// </summary>
        private int Run(string[] args)
        {
            string executableName = Process.GetCurrentProcess().ProcessName + ".exe";
            ArgumentSet allArguments = ArgumentSet.Parse(args);
            var failure = false;
            var helpWriter = new CommandHelpWriter(_logger);

            bool showHelp = allArguments.ContainsName("help");

            string commandName = showHelp
                                     ? allArguments.GetByName("help")
                                     : allArguments.AnonymousArgs.FirstOrDefault();

            ICommand command = null;

            if (commandName != null)
            {
                command = _commandRepo.GetCommand(commandName);
            }

            if (command == null)
            {
                if (showHelp)
                {
                    //  no command name was found, show the list of available commands
                    WriteAppUsageHelp(executableName);
                    helpWriter.WriteCommandList(_commandRepo.Commands);
                }
                else
                {
                    //  invalid command name was given
                    _logger.WriteError("'{0}' is not a DotNetMigrations command.", commandName);
                    _logger.WriteLine(string.Empty);
                    _logger.WriteError("See '{0} -help' for a list of available commands.", executableName);
                    failure = true;
                }
            }

            if (showHelp && command != null)
            {
                //  show help for the given command
                helpWriter.WriteCommandHelp(command, executableName);
            }
            else if (command != null)
            {
                command.Log = _logger;

                var commandArgumentSet = ArgumentSet.Parse(args.Skip(1).ToArray());
                IArguments commandArgs = command.CreateArguments();
                commandArgs.Parse(commandArgumentSet);

                if (commandArgs.IsValid)
                {
                    var timer = new Stopwatch();
                    timer.Start();

                    try
                    {
                        command.Run(commandArgs);
                    }
                    catch (Exception ex)
                    {
                        //_logger.WriteLine(string.Empty);
                        failure = true;
                        if (_logFullErrors)
                        {
                            _logger.WriteError(ex.ToString());
                        }
                        else
                        {
                            WriteShortErrorMessages(ex);
                        }

                        if (Debugger.IsAttached)
                            throw;
                    }
                    finally
                    {
                        timer.Stop();

                        _logger.WriteLine(string.Empty);
                        _logger.WriteLine(string.Format("Command duration was {0}.",
                                                               decimal.Divide(timer.ElapsedMilliseconds, 1000).ToString(
                                                                   "0.0000s")));
                    }
                }
                else
                {
                    //  argument validation failed, show errors
                    WriteValidationErrors(command.CommandName, commandArgs.Errors);
                    _logger.WriteLine(string.Empty);
                    helpWriter.WriteCommandHelp(command, executableName);
                    failure = true;
                }
            }

            if (_keepConsoleOpen)
            {
                Console.WriteLine();
                Console.WriteLine("  > Uh-oh. It looks like you didn't run me from a console.");
                Console.WriteLine("  > Did you double-click me?");
                Console.WriteLine("  > I don't like to be clicked.");
                Console.WriteLine("  > Please open a command prompt and run me by typing " + executableName + ".");
                Console.WriteLine();
                Console.WriteLine("Press any key to exit...");
                Console.Read();
            }

            return failure ? 1 : 0;
        }
 public void Setup()
 {
     _logger = new MockLog1();
     _helpWriter = new CommandHelpWriter(_logger);
 }
 public void Teardown()
 {
     _helpWriter = null;
     _logger.Dispose();
 }
 private static string GetArgumentSyntax(ICommand command)
 {
     using (var logger = new MockLog1())
     {
         var writer = new CommandHelpWriter(logger);
         writer.WriteArgumentSyntax(command.GetArgumentsType());
         return logger.Output;
     }
 }