Ejemplo n.º 1
0
        /// <summary>
        /// Primary Program Execution
        /// </summary>
        private void Run(string[] args)
        {
            string      executableName = Process.GetCurrentProcess().ProcessName + ".exe";
            ArgumentSet allArguments   = ArgumentSet.Parse(args);

            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);
                }
            }

            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);

                        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);
                }
            }

            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();
            }
        }