示例#1
0
        public static void Run(string[] args)
        {
            try
            {
                Bootstrapper.Configure();

                var            commandName = string.Empty;
                IList <string> commandArgs = new List <string>();

                // shift the first arg off
                var count = 0;
                foreach (var arg in args)
                {
                    if (count == 0)
                    {
                        count += 1;
                        continue;
                    }

                    commandArgs.Add(arg);
                }

                // We need to initialize the current culture before we
                // use any translatable strings.
                SetUpPreferredLanguage(Bootstrapper.Container.Resolve <IConfigService>());

                var configuration = new ChocolateyGuiConfiguration();
                SetUpGlobalOptions(args, configuration, Bootstrapper.Container);
                SetEnvironmentOptions(configuration);

                if (configuration.RegularOutput)
                {
#if DEBUG
                    Bootstrapper.Logger.Warning(" (DEBUG BUILD)".format_with("Chocolatey GUI", configuration.Information.DisplayVersion));
#else
                    Bootstrapper.Logger.Warning("{0}".format_with(configuration.Information.DisplayVersion));
#endif

                    if (args.Length == 0)
                    {
                        Bootstrapper.Logger.Information(L(nameof(ChocolateyGui.Common.Properties.Resources.Command_CommandsText), "chocolateyguicli"));
                    }
                }

                var runner = new GenericRunner();
                runner.Run(configuration, Bootstrapper.Container, command =>
                {
                    ParseArgumentsAndUpdateConfiguration(
                        commandArgs,
                        configuration,
                        (optionSet) => command.ConfigureArgumentParser(optionSet, configuration),
                        (unparsedArgs) =>
                    {
                        command.HandleAdditionalArgumentParsing(unparsedArgs, configuration);
                    },
                        () =>
                    {
                        Bootstrapper.Logger.Debug("Performing validation checks...");
                        command.HandleValidation(configuration);
                    },
                        () => command.HelpMessage(configuration));
                });
            }
            catch (Exception ex)
            {
                Bootstrapper.Logger.Error(ex.Message);
            }
            finally
            {
                Log.CloseAndFlush();

                if (Bootstrapper.Container != null)
                {
                    if (Bootstrapper.Container.IsRegisteredWithName <LiteDatabase>(Bootstrapper.GlobalConfigurationDatabaseName))
                    {
                        var globalDatabase = Bootstrapper.Container.ResolveNamed <LiteDatabase>(Bootstrapper.GlobalConfigurationDatabaseName);
                        globalDatabase.Dispose();
                    }

                    if (Bootstrapper.Container.IsRegisteredWithName <LiteDatabase>(Bootstrapper.UserConfigurationDatabaseName))
                    {
                        var userDatabase = Bootstrapper.Container.ResolveNamed <LiteDatabase>(Bootstrapper.UserConfigurationDatabaseName);
                        userDatabase.Dispose();
                    }

                    Bootstrapper.Container.Dispose();
                }
            }
        }