From 0ad1baab88cf1793c79e7505d0da7595fd3b07ff Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Wed, 10 Feb 2016 06:15:38 -0600 Subject: [PATCH] (GH-584) split get config file from save Split up getting config file settings, setting the configuration from those and then later saving the config file settings that have been added. --- .../builders/ConfigurationBuilder.cs | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs b/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs index d5232b9e08..c11d901cee 100644 --- a/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs +++ b/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs @@ -66,20 +66,36 @@ public static void set_up_configuration(IList args, ChocolateyConfigurat { var fileSystem = container.GetInstance(); var xmlService = container.GetInstance(); - set_file_configuration(config, fileSystem, xmlService, notifyWarnLoggingAction); + var configFileSettings = get_config_file_settings(fileSystem, xmlService); + set_file_configuration(config, configFileSettings, fileSystem, notifyWarnLoggingAction); ConfigurationOptions.reset_options(); set_global_options(args, config, container); set_environment_options(config); set_license_options(config, license); set_environment_variables(config); + set_config_file_settings(configFileSettings, xmlService); } - private static void set_file_configuration(ChocolateyConfiguration config, IFileSystem fileSystem, IXmlService xmlService, Action notifyWarnLoggingAction) + private static ConfigFileSettings get_config_file_settings(IFileSystem fileSystem, IXmlService xmlService) { var globalConfigPath = ApplicationParameters.GlobalConfigFileLocation; AssemblyFileExtractor.extract_text_file_from_assembly(fileSystem, Assembly.GetExecutingAssembly(), ApplicationParameters.ChocolateyConfigFileResource, globalConfigPath); - var configFileSettings = xmlService.deserialize(globalConfigPath); + return xmlService.deserialize(globalConfigPath); + } + + private static void set_config_file_settings(ConfigFileSettings configFileSettings, IXmlService xmlService) + { + var globalConfigPath = ApplicationParameters.GlobalConfigFileLocation; + // save so all updated configuration items get set to existing config + FaultTolerance.try_catch_with_logging_exception( + () => xmlService.serialize(configFileSettings, globalConfigPath), + "Error updating '{0}'. Please ensure you have permissions to do so".format_with(globalConfigPath), + logWarningInsteadOfError: true); + } + + private static void set_file_configuration(ChocolateyConfiguration config, ConfigFileSettings configFileSettings, IFileSystem fileSystem, Action notifyWarnLoggingAction) + { var sources = new StringBuilder(); var defaultSourcesInOrder = configFileSettings.Sources.Where(s => !s.Disabled).or_empty_list_if_null().ToList(); @@ -108,12 +124,6 @@ private static void set_file_configuration(ChocolateyConfiguration config, IFile logWarningInsteadOfError: true); set_feature_flags(config, configFileSettings); - - // save so all updated configuration items get set to existing config - FaultTolerance.try_catch_with_logging_exception( - () => xmlService.serialize(configFileSettings, globalConfigPath), - "Error updating '{0}'. Please ensure you have permissions to do so".format_with(globalConfigPath), - logWarningInsteadOfError: true); } private static void set_machine_sources(ChocolateyConfiguration config, ConfigFileSettings configFileSettings)