From 61cd0847d4198828965626f32c4a3461bb4ee604 Mon Sep 17 00:00:00 2001 From: Matt Wrock Date: Sat, 28 Feb 2015 00:59:33 -0800 Subject: [PATCH] (GH-133) defer container lookups until Run() This fixes calls to `RegisterContainerComponents` complaining that components cannot be registered after calls to `GetInstance`, etc. The container is not touched in the constructor and configuration setup is is performed just prior to `Run`. --- src/chocolatey.tests/GetChocolateySpecs.cs | 6 --- src/chocolatey/GetChocolatey.cs | 43 ++++++++++------------ 2 files changed, 20 insertions(+), 29 deletions(-) diff --git a/src/chocolatey.tests/GetChocolateySpecs.cs b/src/chocolatey.tests/GetChocolateySpecs.cs index 2b998947f8..973e8bab8f 100644 --- a/src/chocolatey.tests/GetChocolateySpecs.cs +++ b/src/chocolatey.tests/GetChocolateySpecs.cs @@ -71,12 +71,6 @@ public void should_get_instantiated_chocolotey2() { _chocolatey2.ShouldNotBeNull(); } - - [Fact] - public void should_have_distinct_configurations() - { - _chocolatey1.GetConfiguration().ShouldNotEqual(_chocolatey2.GetConfiguration()); - } } } } \ No newline at end of file diff --git a/src/chocolatey/GetChocolatey.cs b/src/chocolatey/GetChocolatey.cs index e85da552f3..fdf5d97878 100644 --- a/src/chocolatey/GetChocolatey.cs +++ b/src/chocolatey/GetChocolatey.cs @@ -49,9 +49,8 @@ public static GetChocolatey GetChocolatey() /// public class GetChocolatey { - private readonly ChocolateyConfiguration _configuration; private readonly Container _container; - private readonly IFileSystem _fileSystem; + private Action _propConfig; /// /// Initializes a new instance of the class. @@ -60,20 +59,7 @@ public GetChocolatey() { Log4NetAppenderConfiguration.configure(); Bootstrap.initialize(); - _configuration = new ChocolateyConfiguration(); _container = SimpleInjectorContainer.Container; - _fileSystem = _container.GetInstance(); - - set_defaults(); - } - - private void set_defaults() - { - ConfigurationBuilder.set_up_configuration(new List(), _configuration, _fileSystem, _container.GetInstance(), null); - Config.initialize_with(_configuration); - - _configuration.PromptForConfirmation = false; - _configuration.AcceptLicense = true; } /// @@ -94,15 +80,10 @@ public GetChocolatey SetCustomLogging(ILog logger) /// This instance public GetChocolatey Set(Action propConfig) { - propConfig.Invoke(_configuration); + _propConfig = propConfig; return this; } - public ChocolateyConfiguration GetConfiguration() - { - return _configuration; - } - /// /// Registers a container component. Does not require a dependency on Simple Injector. /// Will override existing component if registered. @@ -195,9 +176,25 @@ public void Run() "tools" }; - AssemblyFileExtractor.extract_all_resources_to_relative_directory(_fileSystem, Assembly.GetAssembly(typeof (ChocolateyResourcesAssembly)), ApplicationParameters.InstallLocation, folders, ApplicationParameters.ChocolateyFileResources); + AssemblyFileExtractor.extract_all_resources_to_relative_directory(_container.GetInstance(), Assembly.GetAssembly(typeof(ChocolateyResourcesAssembly)), ApplicationParameters.InstallLocation, folders, ApplicationParameters.ChocolateyFileResources); + var configuration = create_configuration(new List()); var runner = new GenericRunner(); - runner.run(_configuration, _container, isConsole: false, parseArgs: null); + runner.run(configuration, _container, isConsole: false, parseArgs: null); + } + + private ChocolateyConfiguration create_configuration(IList args) + { + var configuration = new ChocolateyConfiguration(); + ConfigurationBuilder.set_up_configuration(args, configuration, _container.GetInstance(), _container.GetInstance(), null); + Config.initialize_with(configuration); + + configuration.PromptForConfirmation = false; + configuration.AcceptLicense = true; + if (_propConfig != null) + { + _propConfig.Invoke(configuration); + } + return configuration; } }