Exemplo n.º 1
0
        internal override IServiceProvider GetServiceProvider(IConsole console)
        {
            // Gather all the values supplied by the user in command line
            SourceDir = string.IsNullOrEmpty(SourceDir) ?
                        Directory.GetCurrentDirectory() : Path.GetFullPath(SourceDir);

            // NOTE: Order of the following is important. So a command line provided value has higher precedence
            // than the value provided in a configuration file of the repo.
            var config = new ConfigurationBuilder()
                         .AddIniFile(Path.Combine(SourceDir, Constants.BuildEnvironmentFileName), optional: true)
                         .AddEnvironmentVariables()
                         .Add(GetCommandLineConfigSource())
                         .Build();

            // Override the GetServiceProvider() call in CommandBase to pass the IConsole instance to
            // ServiceProviderBuilder and allow for writing to the console if needed during this command.
            var serviceProviderBuilder = new ServiceProviderBuilder(LogFilePath, console)
                                         .ConfigureServices(services =>
            {
                // Configure Options related services
                // We first add IConfiguration to DI so that option services like
                // `DotNetCoreScriptGeneratorOptionsSetup` services can get it through DI and read from the config
                // and set the options.
                services
                .AddSingleton <IConfiguration>(config)
                .AddOptionsServices()
                .Configure <BuildScriptGeneratorOptions>(options =>
                {
                    // These values are not retrieve through the 'config' api since we do not expect
                    // them to be provided by an end user.
                    options.SourceDir  = SourceDir;
                    options.ScriptOnly = false;
                });
            });

            return(serviceProviderBuilder.Build());
        }
Exemplo n.º 2
0
        internal override IServiceProvider TryGetServiceProvider(IConsole console)
        {
            // NOTE: Order of the following is important. So a command line provided value has higher precedence
            // than the value provided in a configuration file of the repo.
            var config = new ConfigurationBuilder()
                         .AddEnvironmentVariables()
                         .Build();

            // Override the GetServiceProvider() call in CommandBase to pass the IConsole instance to
            // ServiceProviderBuilder and allow for writing to the console if needed during this command.
            var serviceProviderBuilder = new ServiceProviderBuilder(LogFilePath, console)
                                         .ConfigureServices(services =>
            {
                // Configure Options related services
                // We first add IConfiguration to DI so that option services like
                // `DotNetCoreScriptGeneratorOptionsSetup` services can get it through DI and read from the config
                // and set the options.
                services
                .AddSingleton <IConfiguration>(config)
                .AddOptionsServices();
            });

            return(serviceProviderBuilder.Build());
        }
Exemplo n.º 3
0
        internal override IServiceProvider TryGetServiceProvider(IConsole console)
        {
            if (!IsValidInput(console))
            {
                return(null);
            }

            // NOTE: Order of the following is important. So a command line provided value has higher precedence
            // than the value provided in a configuration file of the repo.
            var configBuilder = new ConfigurationBuilder();

            if (string.IsNullOrEmpty(PlatformsAndVersionsFile))
            {
                // Gather all the values supplied by the user in command line
                SourceDir = string.IsNullOrEmpty(SourceDir) ?
                            Directory.GetCurrentDirectory() : Path.GetFullPath(SourceDir);
                configBuilder.AddIniFile(Path.Combine(SourceDir, Constants.BuildEnvironmentFileName), optional: true);
            }
            else
            {
                string versionsFilePath;
                if (PlatformsAndVersionsFile.StartsWith("/"))
                {
                    versionsFilePath = Path.GetFullPath(PlatformsAndVersionsFile);
                }
                else
                {
                    versionsFilePath = Path.Combine(
                        Directory.GetCurrentDirectory(),
                        Path.GetFullPath(PlatformsAndVersionsFile));
                }

                if (!File.Exists(versionsFilePath))
                {
                    throw new FileNotFoundException(
                              $"Could not find the file provided to the '{PlatformsAndVersionsFileTemplate}' switch.",
                              versionsFilePath);
                }

                configBuilder.AddIniFile(versionsFilePath, optional: false);
            }

            configBuilder
            .AddEnvironmentVariables()
            .Add(GetCommandLineConfigSource());

            var config = configBuilder.Build();

            // Override the GetServiceProvider() call in CommandBase to pass the IConsole instance to
            // ServiceProviderBuilder and allow for writing to the console if needed during this command.
            var serviceProviderBuilder = new ServiceProviderBuilder(LogFilePath, console)
                                         .ConfigureServices(services =>
            {
                // Configure Options related services
                // We first add IConfiguration to DI so that option services like
                // `DotNetCoreScriptGeneratorOptionsSetup` services can get it through DI and read from the config
                // and set the options.
                services
                .AddSingleton <IConfiguration>(config)
                .AddOptionsServices()
                .Configure <BuildScriptGeneratorOptions>(options =>
                {
                    // These values are not retrieve through the 'config' api since we do not expect
                    // them to be provided by an end user.
                    options.SourceDir = SourceDir;
                });
            });

            return(serviceProviderBuilder.Build());
        }