コード例 #1
0
ファイル: Program.cs プロジェクト: drewburlingame/GitTools
        private static AppRunner RegisterContainer(this AppRunner appRunner, AppConfigs configs)
        {
            var config = configs.Default ?? new AppConfig();

            var containerBuilder = new ContainerBuilder();

            containerBuilder.RegisterInstance(configs);
            containerBuilder.RegisterInstance(config);

            containerBuilder.RegisterType <BitBucketRepoCommand>().InstancePerLifetimeScope();
            containerBuilder.RegisterType <GlobalConfigCommand>().InstancePerLifetimeScope();
            containerBuilder.RegisterType <LocalRepoCommand>().InstancePerLifetimeScope();

            containerBuilder.RegisterType <BbService>().InstancePerLifetimeScope();
            containerBuilder.RegisterType <GitService>().InstancePerLifetimeScope();

            containerBuilder.RegisterServerBbApi(config);

            containerBuilder.RegisterType <CommandContextHolder>().InstancePerLifetimeScope();
            containerBuilder.Register(c => c.Resolve <CommandContextHolder>().Context)
            .As <CommandContext>()
            .InstancePerLifetimeScope();
            containerBuilder
            .Register(c => c.Resolve <CommandContext>().Console)
            .As <IConsole>()
            .InstancePerLifetimeScope();

            appRunner.Configure(r => r.UseMiddleware(
                                    SetCommandContextForDependencyResolver,
                                    MiddlewareSteps.DependencyResolver.BeginScope + 1));

            return(appRunner.UseAutofac(containerBuilder.Build()));
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: drewburlingame/GitTools
        private static int Main(string[] args)
        {
            Debugger.AttachIfDebugDirective(args);

            AppDomain.CurrentDomain.UnhandledException += (_, eventArgs)
                                                          => ((Exception)eventArgs.ExceptionObject).Print();

            try
            {
                var configs = AppConfigs.Load();

                var appSettings = new AppSettings
                {
                    Arguments = { DefaultOptionSplit = ',', DefaultPipeTargetSymbol = "$*" },
                    ArgumentTypeDescriptors =
                    {
                        new DelegatedTypeDescriptor <Regex>("regex", p =>
                        {
                            // make it easier to specify NOT matching
                            if (p.StartsWith("$!"))
                            {
                                p = $"^((?!{p.Substring(2)}).)*$";
                            }
                            return(new Regex(p, RegexOptions.Compiled));
                        })
                    }
                };
                var appRunner = new AppRunner <GitApplication>(appSettings)
                                .UseDefaultMiddleware()
                                .GiveCancellationTokenToFlurl()
                                .Configure(c => c.UseParameterResolver(_ => AnsiConsole.Console))
                                .UseNameCasing(Case.KebabCase)
                                .UseDataAnnotationValidations(showHelpOnError: true)
                                .UseTimerDirective()
                                .UseDefaultsFromAppSetting(configs.Settings, true)
                                .UseErrorHandler((ctx, ex) =>
                {
                    var errorWriter = (ctx?.Console.Error ?? Console.Error);
                    ex.Print(errorWriter.WriteLine,
                             includeProperties: true,
                             includeData: true,
                             includeStackTrace: false);

                    // use CommandLogger if it has not already logged for this CommandContext
                    if (ctx is not null && !CommandLogger.HasLoggedFor(ctx))
                    {
                        CommandLogger.Log(ctx,
                                          writer: errorWriter.WriteLine,
                                          includeSystemInfo: true,
                                          includeAppConfig: false
                                          );

                        errorWriter.WriteLine();
                    }

                    // print help for the target command or root command
                    // if the exception occurred before a command could be parsed
                    ctx?.PrintHelp();

                    return(ExitCodes.Error.Result);
                })
                                .UseCommandLogger()
                                .RegisterContainer(configs);

                return(appRunner.Run(args));
            }
            catch (OperationCanceledException)
            {
                return(1);
            }
            catch (Exception e)
            {
                e.Print();
                return(1);
            }
        }