Ejemplo n.º 1
0
        private RootSchema ResolveRootSchema()
        {
            _logger.LogDebug("Resolving root schema...");

            var timer = new Stopwatch();

            timer.Start();

            RootSchemaResolver rootSchemaResolver = new RootSchemaResolver(_configuration.CommandTypes,
                                                                           _configuration.DirectiveTypes,
                                                                           _configuration.ModeTypes);

            RootSchema?rootScheam = rootSchemaResolver.Resolve();

            timer.Stop();

            _logger.LogInformation("Root schema resolved after {Duration}.", timer.Elapsed);

            return(rootScheam);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Runs the application with specified command line arguments and environment variables, and returns the exit code.
        /// </summary>
        /// <remarks>
        /// If a <see cref="CommandException"/> or <see cref="TypinException"/> is thrown during command execution, it will be handled and routed to the console.
        /// Additionally, if the debugger is not attached (i.e. the app is running in production), all other exceptions thrown within
        /// this method will be handled and routed to the console as well.
        /// </remarks>
        public async ValueTask <int> RunAsync(IReadOnlyList <string> commandLineArguments,
                                              IReadOnlyDictionary <string, string> environmentVariables)
        {
            try
            {
                _console.ResetColor();
                _console.ForegroundColor = ConsoleColor.Gray;

                CliContext.EnvironmentVariables = environmentVariables;

                PrintStartupMessage();

                RootSchema root = new RootSchemaResolver(_configuration).Resolve();
                CliContext.RootSchema = root;

                //TODO: when in commandLineArguments is a string.Empty application crashes
                int exitCode = await ParseInput(commandLineArguments, root);

                return(exitCode);
            }
            // This may throw pre-execution resolving exceptions which are useful only to the end-user
            catch (TypinException ex)
            {
                _configuration.ExceptionHandler.HandleTypinException(CliContext, ex);

                return(ExitCodes.FromException(ex));
            }
            // To prevent the app from showing the annoying Windows troubleshooting dialog,
            // we handle all exceptions and route them to the console nicely.
            // However, we don't want to swallow unhandled exceptions when the debugger is attached,
            // because we still want the IDE to show them to the developer.
            catch (Exception ex) when(!Debugger.IsAttached)
            {
                _configuration.ExceptionHandler.HandleException(CliContext, ex);

                return(ExitCodes.FromException(ex));
            }
        }