public static IConfigurationSourceRoot RegisterConfiguration([NotNull] this Startup startup, ConfigurationOptions options, [NotNull] string toolsDirectory, [NotNull] string projectDirectory, [NotNull] string systemConfigFileName, [CanBeNull, ItemNotNull] string[] commandLine)
        {
            var configuration = new ConfigurationModel.Configuration();

            configuration.Add(new MemoryConfigurationSource());

            if ((options & ConfigurationOptions.DoNotLoadConfig) == ConfigurationOptions.DoNotLoadConfig)
            {
                configuration.Set(Constants.Configuration.ToolsDirectory, toolsDirectory);
                configuration.Set(Constants.Configuration.ProjectDirectory, projectDirectory);
                configuration.Set(Constants.Configuration.SystemConfigFileName, systemConfigFileName);

                return(configuration);
            }

            if (commandLine == null)
            {
                commandLine = (options & ConfigurationOptions.Interactive) == ConfigurationOptions.Interactive ? configuration.GetCommandLine() : new string[0];
            }

            var configurationService = new ConfigurationService(configuration);

            try
            {
                configurationService.Load(options, toolsDirectory, projectDirectory, systemConfigFileName, commandLine);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                if (configuration.GetBool(Constants.Configuration.System.ShowStackTrace))
                {
                    Console.WriteLine(ex.StackTrace);
                }

                return(null);
            }

            return(configuration);
        }
Ejemplo n.º 2
0
        public void AddCommandLineTest()
        {
            var configuration = new ConfigurationModel.Configuration();

            configuration.Add(new MemoryConfigurationSource());
            var configurationService = new ConfigurationService(configuration);

            IEnumerable <string> args = new List <string>()
            {
                "build", "/param1=1", "/param2=2 2", "/param3", "3", "posarg1", "--switch1", "true", "/param4", "4 4"
            };

            configurationService.AddCommandLine(configuration, args);

            Assert.AreEqual("build", configuration.GetString("arg0"));
            Assert.AreEqual("posarg1", configuration.GetString("arg1"));

            Assert.AreEqual("1", configuration.GetString("param1"));
            Assert.AreEqual("2 2", configuration.GetString("param2"));
            Assert.AreEqual("3", configuration.GetString("param3"));
            Assert.AreEqual("4 4", configuration.GetString("param4"));

            Assert.AreEqual("true", configuration.GetString("switch1"));
        }