/// <summary> /// After the construction of GetChocolatey, we should have a ChocolateyConfiguration or LicensedChocolateyConfiguration loaded into the environment. /// We want that original configuration to live on between calls to the API. This function ensures that the /// original default configuration from new() is reset after each command finishes running, even as each command /// may make changes to the configuration it uses. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="args">The arguments.</param> /// <param name="function">The function.</param> /// <returns></returns> private T ensure_original_configuration <T>(IList <string> args, Func <ChocolateyConfiguration, T> function) { var originalConfig = Config.get_configuration_settings().deep_copy(); var configuration = create_configuration(args); var returnValue = default(T); try { if (function != null) { returnValue = function.Invoke(configuration); } var verboseAppenderName = "{0}LoggingColoredConsoleAppender".format_with(ChocolateyLoggers.Verbose.to_string()); var traceAppenderName = "{0}LoggingColoredConsoleAppender".format_with(ChocolateyLoggers.Trace.to_string()); Log4NetAppenderConfiguration.set_logging_level_debug_when_debug(configuration.Debug, verboseAppenderName, traceAppenderName); Log4NetAppenderConfiguration.set_verbose_logger_when_verbose(configuration.Verbose, configuration.Debug, verboseAppenderName); Log4NetAppenderConfiguration.set_trace_logger_when_trace(configuration.Trace, traceAppenderName); } finally { // reset that configuration each time configuration = originalConfig; Config.initialize_with(originalConfig); } return(returnValue); }
// ReSharper disable InconsistentNaming private static void Main(string[] args) // ReSharper restore InconsistentNaming { try { add_assembly_resolver(); string loggingLocation = ApplicationParameters.LoggingLocation; //no file system at this point if (!Directory.Exists(loggingLocation)) { Directory.CreateDirectory(loggingLocation); } Log4NetAppenderConfiguration.configure(loggingLocation); Bootstrap.initialize(); Bootstrap.startup(); var license = License.validate_license(); var container = SimpleInjectorContainer.Container; "LogFileOnly".Log().Info(() => "".PadRight(60, '=')); var config = container.GetInstance <ChocolateyConfiguration>(); var fileSystem = container.GetInstance <IFileSystem>(); var warnings = new List <string>(); ConfigurationBuilder.set_up_configuration( args, config, container, license, warning => { warnings.Add(warning); } ); Config.initialize_with(config); report_version_and_exit_if_requested(args, config); trap_exit_scenarios(config); warn_on_nuspec_or_nupkg_usage(args, config); if (config.RegularOutput) { #if DEBUG "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} v{1}{2} (DEBUG BUILD)".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion, license.is_licensed_version() ? " {0}".format_with(license.LicenseType) : string.Empty)); #else if (config.Information.ChocolateyVersion == config.Information.ChocolateyProductVersion && args.Any()) { "LogFileOnly".Log().Info(() => "{0} v{1}{2}".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion, license.is_licensed_version() ? " {0}".format_with(license.LicenseType) : string.Empty)); } else { "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} v{1}{2}".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion, license.is_licensed_version() ? " {0}".format_with(license.LicenseType) : string.Empty)); } #endif if (args.Length == 0) { "chocolatley".Log().Info(ChocolateyLoggers.Important, () => "Please run 'choco -?' or 'choco <command> -?' for help menu."); } } if (warnings.Count != 0 && config.RegularOutput) { foreach (var warning in warnings.or_empty_list_if_null()) { "chocolatey".Log().Warn(ChocolateyLoggers.Important, warning); } } if (config.HelpRequested || config.UnsuccessfulParsing) { pause_execution_if_debug(); Environment.Exit(config.UnsuccessfulParsing? 1 : 0); } Log4NetAppenderConfiguration.set_logging_level_debug_when_debug(config.Debug, excludeLoggerName: "{0}LoggingColoredConsoleAppender".format_with(ChocolateyLoggers.Verbose.to_string())); Log4NetAppenderConfiguration.set_verbose_logger_when_verbose(config.Verbose, config.Debug, "{0}LoggingColoredConsoleAppender".format_with(ChocolateyLoggers.Verbose.to_string())); Log4NetAppenderConfiguration.set_trace_logger_when_trace(config.Trace, "{0}LoggingColoredConsoleAppender".format_with(ChocolateyLoggers.Trace.to_string())); "chocolatey".Log().Debug(() => "{0} is running on {1} v {2}".format_with(ApplicationParameters.Name, config.Information.PlatformType, config.Information.PlatformVersion.to_string())); //"chocolatey".Log().Debug(() => "Command Line: {0}".format_with(Environment.CommandLine)); remove_old_chocolatey_exe(fileSystem); AssemblyFileExtractor.extract_all_resources_to_relative_directory(fileSystem, Assembly.GetAssembly(typeof(Program)), ApplicationParameters.InstallLocation, new List <string>(), "chocolatey.console"); //refactor - thank goodness this is temporary, cuz manifest resource streams are dumb IList <string> folders = new List <string> { "helpers", "functions", "redirects", "tools" }; AssemblyFileExtractor.extract_all_resources_to_relative_directory(fileSystem, Assembly.GetAssembly(typeof(ChocolateyResourcesAssembly)), ApplicationParameters.InstallLocation, folders, ApplicationParameters.ChocolateyFileResources); var application = new ConsoleApplication(); application.run(args, config, container); } catch (Exception ex) { if (ApplicationParameters.is_debug_mode_cli_primitive()) { "chocolatey".Log().Error(() => "{0} had an error occur:{1}{2}".format_with( ApplicationParameters.Name, Environment.NewLine, ex.ToString())); } else { "chocolatey".Log().Error(ChocolateyLoggers.Important, () => "{0}".format_with(ex.Message)); } Environment.ExitCode = 1; } finally { "chocolatey".Log().Debug(() => "Exiting with {0}".format_with(Environment.ExitCode)); #if DEBUG "chocolatey".Log().Info(() => "Exiting with {0}".format_with(Environment.ExitCode)); #endif pause_execution_if_debug(); Bootstrap.shutdown(); Environment.Exit(Environment.ExitCode); } }
// ReSharper disable InconsistentNaming private static void Main(string[] args) // ReSharper restore InconsistentNaming { try { string loggingLocation = ApplicationParameters.LoggingLocation; //no file system at this point if (!Directory.Exists(loggingLocation)) { Directory.CreateDirectory(loggingLocation); } Log4NetAppenderConfiguration.configure(loggingLocation); Bootstrap.initialize(); Bootstrap.startup(); var container = SimpleInjectorContainer.Container; var config = container.GetInstance <ChocolateyConfiguration>(); var fileSystem = container.GetInstance <IFileSystem>(); var warnings = new List <string>(); ConfigurationBuilder.set_up_configuration( args, config, fileSystem, container.GetInstance <IXmlService>(), warning => { warnings.Add(warning); } ); Config.initialize_with(config); report_version_and_exit_if_requested(args, config); trap_exit_scenarios(config); if (config.RegularOutput) { "logfile".Log().Info(() => "".PadRight(60, '=')); #if DEBUG "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} v{1} (DEBUG BUILD)".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion)); #else "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} v{1}".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion)); #endif } if (warnings.Count != 0 && config.RegularOutput) { foreach (var warning in warnings.or_empty_list_if_null()) { "chocolatey".Log().Warn(ChocolateyLoggers.Important, warning); } } if (config.HelpRequested) { pause_execution_if_debug(); Environment.Exit(-1); } Log4NetAppenderConfiguration.set_verbose_logger_when_verbose(config.Verbose, ChocolateyLoggers.Verbose.to_string()); Log4NetAppenderConfiguration.set_logging_level_debug_when_debug(config.Debug); "chocolatey".Log().Debug(() => "{0} is running on {1} v {2}".format_with(ApplicationParameters.Name, config.Information.PlatformType, config.Information.PlatformVersion.to_string())); //"chocolatey".Log().Debug(() => "Command Line: {0}".format_with(Environment.CommandLine)); warn_when_admin_needs_elevation(config); remove_old_chocolatey_exe(fileSystem); LicenseValidation.validate(fileSystem); //refactor - thank goodness this is temporary, cuz manifest resource streams are dumb IList <string> folders = new List <string> { "helpers", "functions", "redirects", "tools" }; AssemblyFileExtractor.extract_all_resources_to_relative_directory(fileSystem, Assembly.GetAssembly(typeof(ChocolateyResourcesAssembly)), ApplicationParameters.InstallLocation, folders, ApplicationParameters.ChocolateyFileResources); var application = new ConsoleApplication(); application.run(args, config, container); } catch (Exception ex) { var debug = false; // no access to the good stuff here, need to go a bit primitive in parsing args foreach (var arg in args.or_empty_list_if_null()) { if (arg.Contains("debug") || arg.is_equal_to("-d") || arg.is_equal_to("/d")) { debug = true; break; } } if (debug) { "chocolatey".Log().Error(() => "{0} had an error occur:{1}{2}".format_with( ApplicationParameters.Name, Environment.NewLine, ex.ToString())); } else { "chocolatey".Log().Error(ChocolateyLoggers.Important, () => "{0}".format_with(ex.Message)); } Environment.ExitCode = 1; } finally { "chocolatey".Log().Debug(() => "Exiting with {0}".format_with(Environment.ExitCode)); #if DEBUG "chocolatey".Log().Info(() => "Exiting with {0}".format_with(Environment.ExitCode)); #endif pause_execution_if_debug(); Bootstrap.shutdown(); Environment.Exit(Environment.ExitCode); } }
// ReSharper disable InconsistentNaming private static void Main(string[] args) // ReSharper restore InconsistentNaming { try { string loggingLocation = ApplicationParameters.LoggingLocation; //no file system at this point if (!Directory.Exists(loggingLocation)) { Directory.CreateDirectory(loggingLocation); } Log4NetAppenderConfiguration.configure(loggingLocation); Bootstrap.initialize(); Bootstrap.startup(); var license = LicenseValidation.validate(); if (license.is_licensed_version()) { try { var licensedAssembly = Assembly.LoadFile(ApplicationParameters.LicensedAssemblyLocation); license.AssemblyLoaded = true; license.Assembly = licensedAssembly; license.Version = VersionInformation.get_current_informational_version(licensedAssembly); Type licensedComponent = licensedAssembly.GetType(ApplicationParameters.LicensedComponentRegistry, throwOnError: false, ignoreCase: true); SimpleInjectorContainer.add_component_registry_class(licensedComponent); } catch (Exception ex) { "chocolatey".Log().Error( @"Error when attempting to load chocolatey licensed assembly. Ensure that chocolatey.licensed.dll exists at '{0}'. The error message itself may be helpful as well:{1} {2}".format_with( ApplicationParameters.LicensedAssemblyLocation, Environment.NewLine, ex.Message )); } } var container = SimpleInjectorContainer.Container; var config = container.GetInstance <ChocolateyConfiguration>(); var fileSystem = container.GetInstance <IFileSystem>(); var warnings = new List <string>(); ConfigurationBuilder.set_up_configuration( args, config, container, license, warning => { warnings.Add(warning); } ); Config.initialize_with(config); report_version_and_exit_if_requested(args, config); trap_exit_scenarios(config); if (config.RegularOutput) { "logfile".Log().Info(() => "".PadRight(60, '=')); #if DEBUG "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} v{1}{2} (DEBUG BUILD)".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion, license.is_licensed_version() ? " {0}".format_with(license.LicenseType) : string.Empty)); #else if (config.Information.ChocolateyVersion == config.Information.ChocolateyProductVersion && args.Any()) { "logfile".Log().Info(() => "{0} v{1}{2}".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion, license.is_licensed_version() ? " {0}".format_with(license.LicenseType) : string.Empty)); } else { "chocolatey".Log().Info(ChocolateyLoggers.Important, () => "{0} v{1}{2}".format_with(ApplicationParameters.Name, config.Information.ChocolateyProductVersion, license.is_licensed_version() ? " {0}".format_with(license.LicenseType) : string.Empty)); } #endif } if (warnings.Count != 0 && config.RegularOutput) { foreach (var warning in warnings.or_empty_list_if_null()) { "chocolatey".Log().Warn(ChocolateyLoggers.Important, warning); } } if (config.HelpRequested) { pause_execution_if_debug(); Environment.Exit(-1); } Log4NetAppenderConfiguration.set_logging_level_debug_when_debug(config.Debug, excludeLoggerName: "{0}LoggingColoredConsoleAppender".format_with(ChocolateyLoggers.Verbose.to_string())); Log4NetAppenderConfiguration.set_verbose_logger_when_verbose(config.Verbose, config.Debug, "{0}LoggingColoredConsoleAppender".format_with(ChocolateyLoggers.Verbose.to_string())); "chocolatey".Log().Debug(() => "{0} is running on {1} v {2}".format_with(ApplicationParameters.Name, config.Information.PlatformType, config.Information.PlatformVersion.to_string())); //"chocolatey".Log().Debug(() => "Command Line: {0}".format_with(Environment.CommandLine)); remove_old_chocolatey_exe(fileSystem); //refactor - thank goodness this is temporary, cuz manifest resource streams are dumb IList <string> folders = new List <string> { "helpers", "functions", "redirects", "tools" }; AssemblyFileExtractor.extract_all_resources_to_relative_directory(fileSystem, Assembly.GetAssembly(typeof(ChocolateyResourcesAssembly)), ApplicationParameters.InstallLocation, folders, ApplicationParameters.ChocolateyFileResources); var application = new ConsoleApplication(); application.run(args, config, container); } catch (Exception ex) { var debug = false; // no access to the good stuff here, need to go a bit primitive in parsing args foreach (var arg in args.or_empty_list_if_null()) { if (arg.Contains("debug") || arg.is_equal_to("-d") || arg.is_equal_to("/d")) { debug = true; break; } } if (debug) { "chocolatey".Log().Error(() => "{0} had an error occur:{1}{2}".format_with( ApplicationParameters.Name, Environment.NewLine, ex.ToString())); } else { "chocolatey".Log().Error(ChocolateyLoggers.Important, () => "{0}".format_with(ex.Message)); } Environment.ExitCode = 1; } finally { "chocolatey".Log().Debug(() => "Exiting with {0}".format_with(Environment.ExitCode)); #if DEBUG "chocolatey".Log().Info(() => "Exiting with {0}".format_with(Environment.ExitCode)); #endif pause_execution_if_debug(); Bootstrap.shutdown(); Environment.Exit(Environment.ExitCode); } }