コード例 #1
0
        public void Execute(CommandParameters parameters)
        {
            var matches = MatchCommands(parameters);

            if (matches.Count() == 1)
            {
                var match = matches.Single();
                match.CommandHandlerFactory().Execute(match.Context);
            }
            else
            {
                var commandMatch = string.Join(" ", parameters.Arguments.ToArray());
                var commandList  = string.Join(",", GetCommandDescriptors().Select(d => d.Name).ToArray());
                if (matches.Any())
                {
                    throw new BoyingCoreException(T("Multiple commands found matching arguments \"{0}\". Commands available: {1}.",
                                                    commandMatch, commandList));
                }
                throw new BoyingCoreException(T("No command found matching arguments \"{0}\". Commands available: {1}.",
                                                commandMatch, commandList));
            }
        }
コード例 #2
0
        public CommandReturnCodes RunCommand(TextReader input, TextWriter output, string tenant, string[] args, Dictionary <string, string> switches)
        {
            try
            {
                tenant = tenant ?? ShellSettings.DefaultName;

                using (var env = CreateStandaloneEnvironment(tenant))
                {
                    var commandManager = env.Resolve <ICommandManager>();

                    ITransactionManager transactionManager;
                    if (!env.TryResolve(out transactionManager))
                    {
                        transactionManager = null;
                    }

                    var parameters = new CommandParameters
                    {
                        Arguments = args,
                        Switches  = switches,
                        Input     = input,
                        Output    = output
                    };

                    try
                    {
                        commandManager.Execute(parameters);
                    }
                    catch
                    {
                        // any database changes in this using(env) scope are invalidated
                        if (transactionManager != null)
                        {
                            transactionManager.Cancel();
                        }

                        // exception handling performed below
                        throw;
                    }
                }

                // in effect "pump messages" see PostMessage circa 1980
                var processingEngine = _hostContainer.Resolve <IProcessingEngine>();
                while (processingEngine.AreTasksPending())
                {
                    processingEngine.ExecuteNextTask();
                }

                return(CommandReturnCodes.Ok);
            }
            catch (BoyingCommandHostRetryException ex)
            {
                // Special "Retry" return code for our host
                output.WriteLine(T("{0} (Retrying...)", ex.Message));
                return(CommandReturnCodes.Retry);
            }
            catch (Exception ex)
            {
                if (ex.IsFatal())
                {
                    throw;
                }
                if (ex is TargetInvocationException &&
                    ex.InnerException != null)
                {
                    // If this is an exception coming from reflection and there is an innerexception which is the actual one, redirect
                    ex = ex.InnerException;
                }
                OutputException(output, T("Error executing command \"{0}\"", string.Join(" ", args)), ex);
                return(CommandReturnCodes.Fail);
            }
        }