<commit_before>﻿using System;
using Microsoft.Extensions.Configuration;

namespace RockLib.Configuration
{
    public static class RockLibConfigurationBuilderExtensions
    {
        /// <summary>
        /// Adds the RockLib configuration provider to the builder using the configuration file "rocklib.config.json",
        /// relative to the base path stored in <see cref="IConfigurationBuilder.Properties"/> of the builder.
        /// </summary>
        /// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="builder"/> is null.</exception>
        /// <exception cref="ArgumentException">If <paramref name="jsonConfigPath"/> is null or empty.</exception>
        /// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
        public static IConfigurationBuilder AddRockLib(this IConfigurationBuilder builder)
        {
            return builder.AddRockLib("rocklib.config.json");
        }

        /// <summary>
        /// Adds the RockLib configuration provider to the builder using the specified configuration file,
        /// relative to the base path stored in <see cref="IConfigurationBuilder.Properties"/> of the builder.
        /// </summary>
        /// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
        /// <param name="jsonConfigPath">Required value which provides the name of the file to pull the configuration values from</param>
        /// <exception cref="ArgumentNullException">If <paramref name="builder"/> is null.</exception>
        /// <exception cref="ArgumentException">If <paramref name="jsonConfigPath"/> is null or empty.</exception>
        /// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
        public static IConfigurationBuilder AddRockLib(this IConfigurationBuilder builder, string jsonConfigPath)
        {
            if (string.IsNullOrEmpty(jsonConfigPath)) throw new ArgumentNullException(nameof(builder));
            if (string.IsNullOrEmpty(jsonConfigPath)) throw new ArgumentException($"'{nameof(jsonConfigPath)}' must be a non-empty string.", nameof(jsonConfigPath));

            // we want the optional value to be false so that it will throw a runtime exception if the file is not found
            // if this is set to true no exception is throw and no config values are found/returned.
            var builtBuilder = builder
                .AddJsonFile(jsonConfigPath, optional: false);

            return builtBuilder;
        }
    }<commit_msg>Add supplemental config file parameter, specify via environment variable<commit_after>﻿using System;
using Microsoft.Extensions.Configuration;

namespace RockLib.Configuration
{
    public static class RockLibConfigurationBuilderExtensions
    {
        /// <summary>
        /// Adds the RockLib configuration provider to the builder using the configuration file "rocklib.config.json",
        /// relative to the base path stored in <see cref="IConfigurationBuilder.Properties"/> of the builder.
        /// </summary>
        /// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="builder"/> is null.</exception>
        /// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
        public static IConfigurationBuilder AddRockLib(this IConfigurationBuilder builder)
        {
            var environment = Environment.GetEnvironmentVariable("AppSettings:Environment")?.ToLower();
            if (string.IsNullOrEmpty(environment)) environment = "dev";

            return builder.AddRockLib("rocklib.config.json", $"{environment}.rocklib.config.json");
        }

        /// <summary>
        /// Adds the RockLib configuration provider to the builder using the specified configuration file,
        /// relative to the base path stored in <see cref="IConfigurationBuilder.Properties"/> of the builder.
        /// </summary>
        /// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
        /// <param name="jsonConfigPath">The path of the json config file.</param>
        /// <param name="supplementalJsonConfigPath">The path of the supplemental json config file.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="builder"/> is null.</exception>
        /// <exception cref="ArgumentException">If <paramref name="jsonConfigPath"/> is null or empty.</exception>
        /// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
        public static IConfigurationBuilder AddRockLib(
            this IConfigurationBuilder builder, string jsonConfigPath, string supplementalJsonConfigPath = null)
        {
            if (builder == null) throw new ArgumentNullException(nameof(builder));
            if (string.IsNullOrEmpty(jsonConfigPath)) throw new ArgumentException($"'{nameof(jsonConfigPath)}' must be a non-empty string.", nameof(jsonConfigPath));

            // we want the optional value to be false so that it will throw a runtime exception if the file is not found
            // if this is set to true no exception is throw and no config values are found/returned.
            builder = builder.AddJsonFile(jsonConfigPath, optional: false);

            if (!string.IsNullOrEmpty(supplementalJsonConfigPath))
                builder = builder.AddJsonFile(supplementalJsonConfigPath, optional: true);

            return builder;
        }
    }
{'fraction_non_alphanumeric': 0.10877447425670776, 'fraction_numerical': 0.0, 'mean_word_length': 3.083641746854182, 'pattern_counts': {'":': 0, '<': 66, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1675856', 'n_tokens_mistral': 1283, 'n_tokens_neox': 1259, 'n_words': 430}