From 2c1015dc97d9e7901aa56f8f9381a69be52900fc Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 10:28:23 -0500 Subject: [PATCH 01/27] chore: Update package description --- .../src/DotEnv.Extensions.Microsoft.DI.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/Microsoft.Extensions.DI/src/DotEnv.Extensions.Microsoft.DI.csproj b/plugins/Microsoft.Extensions.DI/src/DotEnv.Extensions.Microsoft.DI.csproj index c9f6972..7f53f91 100644 --- a/plugins/Microsoft.Extensions.DI/src/DotEnv.Extensions.Microsoft.DI.csproj +++ b/plugins/Microsoft.Extensions.DI/src/DotEnv.Extensions.Microsoft.DI.csproj @@ -14,7 +14,7 @@ dotenv, env, variables, environment, service, collection, envfile LICENSE False - Adds dotenv.core extensions for Microsoft.Extensions.DependencyInjection + Integrates DotEnv.Core package with Microsoft.Extensions.DependencyInjection 2.0.0 True 1591 From b9543e751631b0e0b37f1280d15ea1a32ae5356c Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 10:29:37 -0500 Subject: [PATCH 02/27] chore: Add root namespace --- .../src/DotEnv.Extensions.Microsoft.DI.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/Microsoft.Extensions.DI/src/DotEnv.Extensions.Microsoft.DI.csproj b/plugins/Microsoft.Extensions.DI/src/DotEnv.Extensions.Microsoft.DI.csproj index 7f53f91..dacbdc6 100644 --- a/plugins/Microsoft.Extensions.DI/src/DotEnv.Extensions.Microsoft.DI.csproj +++ b/plugins/Microsoft.Extensions.DI/src/DotEnv.Extensions.Microsoft.DI.csproj @@ -1,6 +1,7 @@ + Microsoft.Extensions.DependencyInjection netstandard2.0 latest Dotenv.Extensions.Microsoft.DependencyInjection From 7f410064eb62bd5e2274c2400b412f1ca2b397b0 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 11:42:48 -0500 Subject: [PATCH 03/27] chore: Expose internal types to DotEnv.Extensions.Microsoft.Config --- src/DotEnv.Core.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/src/DotEnv.Core.csproj b/src/DotEnv.Core.csproj index 15bd170..222db0b 100644 --- a/src/DotEnv.Core.csproj +++ b/src/DotEnv.Core.csproj @@ -32,6 +32,7 @@ + From a0def17cda49cce9ac25d638f628a267bead61e0 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 11:44:39 -0500 Subject: [PATCH 04/27] refactor: Add constructor with parameter to DictionaryProvider --- src/Providers/DictionaryProvider.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Providers/DictionaryProvider.cs b/src/Providers/DictionaryProvider.cs index 4f7778a..87239e1 100644 --- a/src/Providers/DictionaryProvider.cs +++ b/src/Providers/DictionaryProvider.cs @@ -10,7 +10,13 @@ namespace DotEnv.Core; /// internal class DictionaryProvider : IEnvironmentVariablesProvider { - private readonly IDictionary _keyValuePairs = new Dictionary(); + private readonly Dictionary _keyValuePairs; + + public DictionaryProvider() + => _keyValuePairs = []; + + public DictionaryProvider(StringComparer comparer) + => _keyValuePairs = new(comparer); /// public string this[string variable] From 97113db124232068234e10c7eb3addbac56d5ff5 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 11:45:18 -0500 Subject: [PATCH 05/27] chore: Update packages --- Directory.Packages.props | 1 + 1 file changed, 1 insertion(+) diff --git a/Directory.Packages.props b/Directory.Packages.props index 78c87ad..e0c3a61 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -15,5 +15,6 @@ + \ No newline at end of file From 56991acdc7ec9bc1f8fc9eb49cef205dce66b26f Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 11:56:58 -0500 Subject: [PATCH 06/27] feat: Add support for Microsoft.Extensions.Configuration --- .../DotEnv.Extensions.Microsoft.Config.csproj | 36 +++++++++++ .../src/EnvConfigurationExtensions.cs | 59 +++++++++++++++++++ .../src/EnvConfigurationProvider.cs | 53 +++++++++++++++++ .../src/EnvConfigurationSource.cs | 33 +++++++++++ 4 files changed, 181 insertions(+) create mode 100644 plugins/Microsoft.Extensions.Config/src/DotEnv.Extensions.Microsoft.Config.csproj create mode 100644 plugins/Microsoft.Extensions.Config/src/EnvConfigurationExtensions.cs create mode 100644 plugins/Microsoft.Extensions.Config/src/EnvConfigurationProvider.cs create mode 100644 plugins/Microsoft.Extensions.Config/src/EnvConfigurationSource.cs diff --git a/plugins/Microsoft.Extensions.Config/src/DotEnv.Extensions.Microsoft.Config.csproj b/plugins/Microsoft.Extensions.Config/src/DotEnv.Extensions.Microsoft.Config.csproj new file mode 100644 index 0000000..7bd7fe8 --- /dev/null +++ b/plugins/Microsoft.Extensions.Config/src/DotEnv.Extensions.Microsoft.Config.csproj @@ -0,0 +1,36 @@ + + + + Microsoft.Extensions.Configuration + netstandard2.0 + latest + Dotenv.Extensions.Microsoft.Configuration + True + David Román Amariles + David Román Amariles + https://mrdave1999.github.io/dotenv.core + dotenv-icon-nuget.png + https://github.com/MrDave1999/dotenv.core + dotenv, env, variables, environment, configuration, envfile + LICENSE + False + Integrates DotEnv.Core package with Microsoft.Extensions.Configuration + 1.0.0 + True + 1591 + + + + + + + + + + + + + + + + diff --git a/plugins/Microsoft.Extensions.Config/src/EnvConfigurationExtensions.cs b/plugins/Microsoft.Extensions.Config/src/EnvConfigurationExtensions.cs new file mode 100644 index 0000000..ae1861c --- /dev/null +++ b/plugins/Microsoft.Extensions.Config/src/EnvConfigurationExtensions.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Text; +using DotEnv.Core; + +namespace Microsoft.Extensions.Configuration; + +/// +/// Extension methods for adding . +/// +public static class EnvConfigurationExtensions +{ + /// + /// Adds the ENV configuration provider at to . + /// + /// The to add to. + /// The path to the file. + /// The . + /// + /// , or is null. + /// + public static IConfigurationBuilder AddEnvFile(this IConfigurationBuilder builder, string path) + => builder.AddEnvFile(path, optional: false); + + /// + /// Adds a ENV configuration source to . + /// + /// The to add to. + /// The path to the file. + /// Whether the file is optional. + /// The . + /// + /// , or is null. + /// + public static IConfigurationBuilder AddEnvFile( + this IConfigurationBuilder builder, + string path, + bool optional) + { + ThrowHelper.ThrowIfNull(builder, nameof(builder)); + ThrowHelper.ThrowIfNullOrWhiteSpace(path, nameof(path)); + return builder.AddEnvFile(source => + { + source.Path = path; + source.Optional = optional; + }); + } + + /// + /// Adds a ENV configuration source to . + /// + /// The to add to. + /// Configures the source. + /// The . + public static IConfigurationBuilder AddEnvFile( + this IConfigurationBuilder builder, + Action configureSource) + => builder.Add(configureSource); +} diff --git a/plugins/Microsoft.Extensions.Config/src/EnvConfigurationProvider.cs b/plugins/Microsoft.Extensions.Config/src/EnvConfigurationProvider.cs new file mode 100644 index 0000000..a2115cc --- /dev/null +++ b/plugins/Microsoft.Extensions.Config/src/EnvConfigurationProvider.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using DotEnv.Core; + +namespace Microsoft.Extensions.Configuration; + +/// +/// An ENV file based . +/// +/// +/// KEY1=VALUE1 +/// # comment +/// +public class EnvConfigurationProvider : ConfigurationProvider +{ + private readonly EnvConfigurationSource _source; + + /// + /// Initializes a new instance with the specified source. + /// + /// The source settings. + /// + /// is null. + /// + public EnvConfigurationProvider(EnvConfigurationSource source) + { + ThrowHelper.ThrowIfNull(source, nameof(source)); + _source = source; + } + + /// + /// Loads the data for this provider. + /// + public override void Load() + { + var provider = new DictionaryProvider(StringComparer.OrdinalIgnoreCase); + new EnvLoader() + .AddEnvFile(_source.Path, _source.Optional) + .SetEnvironmentVariablesProvider(provider) + .EnableFileNotFoundException() + .Load(); + + Data = provider.ToDictionary( + keyValuePair => NormalizeKey(keyValuePair.Key), + keyValuePair => keyValuePair.Value, + StringComparer.OrdinalIgnoreCase); + } + + private static string NormalizeKey(string key) + => key.Replace("__", ConfigurationPath.KeyDelimiter); +} diff --git a/plugins/Microsoft.Extensions.Config/src/EnvConfigurationSource.cs b/plugins/Microsoft.Extensions.Config/src/EnvConfigurationSource.cs new file mode 100644 index 0000000..0a177fc --- /dev/null +++ b/plugins/Microsoft.Extensions.Config/src/EnvConfigurationSource.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.Extensions.Configuration; + +/// +/// Represents an ENV file as an . +/// +/// +/// KEY1=VALUE1 +/// # comment +/// +public class EnvConfigurationSource : IConfigurationSource +{ + /// + /// The path to the file. + /// + public string Path { get; set; } + + /// + /// Determines if loading the file is optional. + /// + public bool Optional { get; set; } + + /// + /// Builds the for this source. + /// + /// The . + /// An + public IConfigurationProvider Build(IConfigurationBuilder builder) + => new EnvConfigurationProvider(this); +} From 26fffbfcd147a5117eb25764ff62ed6b48053236 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 11:57:30 -0500 Subject: [PATCH 07/27] chore: Update solution file --- DotEnv.Core.sln | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/DotEnv.Core.sln b/DotEnv.Core.sln index 171796c..abb41df 100644 --- a/DotEnv.Core.sln +++ b/DotEnv.Core.sln @@ -17,6 +17,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotEnv.Extensions.Microsoft EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotEnv.Extensions.Microsoft.DI.Tests", "plugins\Microsoft.Extensions.DI\tests\DotEnv.Extensions.Microsoft.DI.Tests.csproj", "{661B7D3C-977F-432B-911C-A519319A8B97}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Microsoft.Extensions.Config", "Microsoft.Extensions.Config", "{72F0723F-912D-42F3-AA80-928F7509DD94}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotEnv.Extensions.Microsoft.Config", "plugins\Microsoft.Extensions.Config\src\DotEnv.Extensions.Microsoft.Config.csproj", "{B50FB187-F524-46E3-8AC8-E31826294F95}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotEnv.Extensions.Microsoft.Config.Example", "plugins\Microsoft.Extensions.Config\example\DotEnv.Extensions.Microsoft.Config.Example.csproj", "{EE641B31-2439-481A-BCC4-4520B75DD57D}" +EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Elements", "Solution Elements", "{2D684F32-79A1-4904-A921-204D88EB2F48}" ProjectSection(SolutionItems) = preProject Directory.Build.props = Directory.Build.props @@ -53,6 +59,14 @@ Global {661B7D3C-977F-432B-911C-A519319A8B97}.Debug|Any CPU.Build.0 = Debug|Any CPU {661B7D3C-977F-432B-911C-A519319A8B97}.Release|Any CPU.ActiveCfg = Release|Any CPU {661B7D3C-977F-432B-911C-A519319A8B97}.Release|Any CPU.Build.0 = Release|Any CPU + {B50FB187-F524-46E3-8AC8-E31826294F95}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B50FB187-F524-46E3-8AC8-E31826294F95}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B50FB187-F524-46E3-8AC8-E31826294F95}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B50FB187-F524-46E3-8AC8-E31826294F95}.Release|Any CPU.Build.0 = Release|Any CPU + {EE641B31-2439-481A-BCC4-4520B75DD57D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EE641B31-2439-481A-BCC4-4520B75DD57D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EE641B31-2439-481A-BCC4-4520B75DD57D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EE641B31-2439-481A-BCC4-4520B75DD57D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -61,6 +75,8 @@ Global {7309366E-C4C5-4367-98E8-4E2410622537} = {B093601A-E3DB-446A-A650-709F31366AD7} {F5FECE90-E970-4C80-A876-DE19DBDEFC70} = {B093601A-E3DB-446A-A650-709F31366AD7} {661B7D3C-977F-432B-911C-A519319A8B97} = {B093601A-E3DB-446A-A650-709F31366AD7} + {B50FB187-F524-46E3-8AC8-E31826294F95} = {72F0723F-912D-42F3-AA80-928F7509DD94} + {EE641B31-2439-481A-BCC4-4520B75DD57D} = {72F0723F-912D-42F3-AA80-928F7509DD94} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {DADE2FE0-EA2C-41E8-B91E-2A5F19EAAD87} From a0893094a338947f368564a97759e1ecc8832f28 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 12:06:32 -0500 Subject: [PATCH 08/27] docs: Create sample project --- .../Microsoft.Extensions.Config/example/.env | 9 ++++++++ ...Extensions.Microsoft.Config.Example.csproj | 11 ++++++++++ .../example/Program.cs | 21 +++++++++++++++++++ .../example/config.env | 1 + 4 files changed, 42 insertions(+) create mode 100644 plugins/Microsoft.Extensions.Config/example/.env create mode 100644 plugins/Microsoft.Extensions.Config/example/DotEnv.Extensions.Microsoft.Config.Example.csproj create mode 100644 plugins/Microsoft.Extensions.Config/example/Program.cs create mode 100644 plugins/Microsoft.Extensions.Config/example/config.env diff --git a/plugins/Microsoft.Extensions.Config/example/.env b/plugins/Microsoft.Extensions.Config/example/.env new file mode 100644 index 0000000..5a4126a --- /dev/null +++ b/plugins/Microsoft.Extensions.Config/example/.env @@ -0,0 +1,9 @@ +DB_HOST=localhost +DB_PORT=3306 +DB_USERNAME=admin123 +DB_PASSWORD=admin123##$ + +MYSQL__HOST=mysql +MYSQL__PORT=3307 +MYSQL__USERNAME=admin244 +MYSQL__PASSWORD=admin244##$ diff --git a/plugins/Microsoft.Extensions.Config/example/DotEnv.Extensions.Microsoft.Config.Example.csproj b/plugins/Microsoft.Extensions.Config/example/DotEnv.Extensions.Microsoft.Config.Example.csproj new file mode 100644 index 0000000..5b34966 --- /dev/null +++ b/plugins/Microsoft.Extensions.Config/example/DotEnv.Extensions.Microsoft.Config.Example.csproj @@ -0,0 +1,11 @@ + + + + Exe + + + + + + + diff --git a/plugins/Microsoft.Extensions.Config/example/Program.cs b/plugins/Microsoft.Extensions.Config/example/Program.cs new file mode 100644 index 0000000..1e1a5f7 --- /dev/null +++ b/plugins/Microsoft.Extensions.Config/example/Program.cs @@ -0,0 +1,21 @@ +using Microsoft.Extensions.Configuration; + +IConfigurationRoot config = new ConfigurationBuilder() + .AddEnvFile(path: ".env", optional: true) + .AddEnvFile("config.env") + .Build(); + +Console.WriteLine("DB_HOST=" + config["DB_HOST"]); +Console.WriteLine("DB_PORT=" + config["DB_PORT"]); +Console.WriteLine("DB_USERNAME=" + config["DB_USERNAME"]); +Console.WriteLine("DB_PASSWORD=" + config["DB_PASSWORD"]); +Console.WriteLine(); + +Console.WriteLine("BASE_URL=" + config["BASE_URL"]); +Console.WriteLine(); + +IConfigurationSection section = config.GetSection("MYSQL"); +Console.WriteLine("MYSQL__HOST=" + section["HOST"]); +Console.WriteLine("MYSQL__PORT=" + section["PORT"]); +Console.WriteLine("MYSQL__USERNAME=" + section["USERNAME"]); +Console.WriteLine("MYSQL__PASSWORD=" + section["PASSWORD"]); diff --git a/plugins/Microsoft.Extensions.Config/example/config.env b/plugins/Microsoft.Extensions.Config/example/config.env new file mode 100644 index 0000000..fbc9b61 --- /dev/null +++ b/plugins/Microsoft.Extensions.Config/example/config.env @@ -0,0 +1 @@ +BASE_URL=http://localhost:3000/ From f1bade3ab492f1f6fa5d29493a12a1fd41f7d13b Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 13:50:44 -0500 Subject: [PATCH 09/27] ci: Update workflow --- .github/workflows/dotnetcore.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/dotnetcore.yml b/.github/workflows/dotnetcore.yml index 1919b66..04e3396 100644 --- a/.github/workflows/dotnetcore.yml +++ b/.github/workflows/dotnetcore.yml @@ -24,4 +24,5 @@ jobs: - name: Test run: | dotnet test ./tests/DotEnv.Core.Tests.csproj -c Release - dotnet test ./plugins/Microsoft.Extensions.DI/tests/DotEnv.Extensions.Microsoft.DI.Tests.csproj -c Release \ No newline at end of file + dotnet test ./plugins/Microsoft.Extensions.DI/tests/DotEnv.Extensions.Microsoft.DI.Tests.csproj -c Release + dotnet test ./plugins/Microsoft.Extensions.Config/tests/DotEnv.Extensions.Microsoft.Config.Tests.csproj -c Release \ No newline at end of file From 7b84b4c771559c75555d5e92bdf822e289a72756 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 13:51:08 -0500 Subject: [PATCH 10/27] chore: Update solution file --- DotEnv.Core.sln | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/DotEnv.Core.sln b/DotEnv.Core.sln index abb41df..5c8d173 100644 --- a/DotEnv.Core.sln +++ b/DotEnv.Core.sln @@ -23,6 +23,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotEnv.Extensions.Microsoft EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotEnv.Extensions.Microsoft.Config.Example", "plugins\Microsoft.Extensions.Config\example\DotEnv.Extensions.Microsoft.Config.Example.csproj", "{EE641B31-2439-481A-BCC4-4520B75DD57D}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotEnv.Extensions.Microsoft.Config.Tests", "plugins\Microsoft.Extensions.Config\tests\DotEnv.Extensions.Microsoft.Config.Tests.csproj", "{91AA172D-AE1E-445E-B3A1-5841C856E659}" +EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Elements", "Solution Elements", "{2D684F32-79A1-4904-A921-204D88EB2F48}" ProjectSection(SolutionItems) = preProject Directory.Build.props = Directory.Build.props @@ -67,6 +69,10 @@ Global {EE641B31-2439-481A-BCC4-4520B75DD57D}.Debug|Any CPU.Build.0 = Debug|Any CPU {EE641B31-2439-481A-BCC4-4520B75DD57D}.Release|Any CPU.ActiveCfg = Release|Any CPU {EE641B31-2439-481A-BCC4-4520B75DD57D}.Release|Any CPU.Build.0 = Release|Any CPU + {91AA172D-AE1E-445E-B3A1-5841C856E659}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {91AA172D-AE1E-445E-B3A1-5841C856E659}.Debug|Any CPU.Build.0 = Debug|Any CPU + {91AA172D-AE1E-445E-B3A1-5841C856E659}.Release|Any CPU.ActiveCfg = Release|Any CPU + {91AA172D-AE1E-445E-B3A1-5841C856E659}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -77,6 +83,7 @@ Global {661B7D3C-977F-432B-911C-A519319A8B97} = {B093601A-E3DB-446A-A650-709F31366AD7} {B50FB187-F524-46E3-8AC8-E31826294F95} = {72F0723F-912D-42F3-AA80-928F7509DD94} {EE641B31-2439-481A-BCC4-4520B75DD57D} = {72F0723F-912D-42F3-AA80-928F7509DD94} + {91AA172D-AE1E-445E-B3A1-5841C856E659} = {72F0723F-912D-42F3-AA80-928F7509DD94} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {DADE2FE0-EA2C-41E8-B91E-2A5F19EAAD87} From 350ac5467b86bd26ce5d3be06e2b03869a013992 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 14:26:53 -0500 Subject: [PATCH 11/27] test: Create project that contains unit tests --- ...v.Extensions.Microsoft.Config.Tests.csproj | 22 +++++ .../tests/EnvConfigurationExtensionsTests.cs | 82 +++++++++++++++++++ .../tests/GlobalUsings.cs | 3 + .../tests/env_files/.env | 4 + 4 files changed, 111 insertions(+) create mode 100644 plugins/Microsoft.Extensions.Config/tests/DotEnv.Extensions.Microsoft.Config.Tests.csproj create mode 100644 plugins/Microsoft.Extensions.Config/tests/EnvConfigurationExtensionsTests.cs create mode 100644 plugins/Microsoft.Extensions.Config/tests/GlobalUsings.cs create mode 100644 plugins/Microsoft.Extensions.Config/tests/env_files/.env diff --git a/plugins/Microsoft.Extensions.Config/tests/DotEnv.Extensions.Microsoft.Config.Tests.csproj b/plugins/Microsoft.Extensions.Config/tests/DotEnv.Extensions.Microsoft.Config.Tests.csproj new file mode 100644 index 0000000..d6db9fd --- /dev/null +++ b/plugins/Microsoft.Extensions.Config/tests/DotEnv.Extensions.Microsoft.Config.Tests.csproj @@ -0,0 +1,22 @@ + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + + + diff --git a/plugins/Microsoft.Extensions.Config/tests/EnvConfigurationExtensionsTests.cs b/plugins/Microsoft.Extensions.Config/tests/EnvConfigurationExtensionsTests.cs new file mode 100644 index 0000000..bfeae97 --- /dev/null +++ b/plugins/Microsoft.Extensions.Config/tests/EnvConfigurationExtensionsTests.cs @@ -0,0 +1,82 @@ +namespace DotEnv.Extensions.Microsoft.Config.Tests; + +[TestClass] +public class EnvConfigurationExtensionsTests +{ + private const string DefaultName = ".env.default"; + + [TestMethod] + public void Build_WhenEnvFileIsNotOptional_ShouldThrowFileNotFoundExceptionIfNotExist() + { + // Arrange + var builder = new ConfigurationBuilder() + .AddEnvFile(DefaultName, optional: false); + + // Act + Action act = () => builder.Build(); + + // Assert + act.Should().Throw(); + } + + [TestMethod] + public void Build_WhenEnvFileIsOptional_ShouldNotThrowFileNotFoundExceptionIfNotExist() + { + // Arrange + var builder = new ConfigurationBuilder() + .AddEnvFile(DefaultName, optional: true); + + // Act + Action act = () => builder.Build(); + + // Assert + act.Should().NotThrow(); + } + + [TestMethod] + [DataRow("")] + [DataRow(" ")] + [DataRow(null)] + public void AddEnvFile_WhenPathIsNullOrEmptyStringOrWhitespaces_ShouldThrowArgumentException(string path) + { + // Arrange + var builder = new ConfigurationBuilder(); + + // Act + Action act = () => builder.AddEnvFile(path); + + // Assert + act.Should() + .Throw() + .WithParameterName(nameof(path)); + } + + [TestMethod] + [DataRow("MARIADB")] + [DataRow("mariadb")] + [DataRow("MariaDb")] + public void Build_WhenPathIsValid_ShouldLoadKeyValuePairsFromValidEnvFile(string key) + { + // Arrange + var path = Path.Combine("env_files", ".env"); + var builder = new ConfigurationBuilder() + .AddEnvFile(path); + + // Act + IConfigurationRoot config = builder.Build(); + + // Asserts + config["BASE_URL"].Should().Be("localhost"); + config["PORT"].Should().Be("3306"); + config["MARIADB:USER"].Should().Be("user123"); + config["MARIADB:PASSWORD"].Should().Be("1234"); + config["mariadb:user"].Should().Be("user123"); + config["mariadb:password"].Should().Be("1234"); + + IConfigurationSection section = config.GetSection(key); + section["USER"].Should().Be("user123"); + section["PASSWORD"].Should().Be("1234"); + section["user"].Should().Be("user123"); + section["password"].Should().Be("1234"); + } +} diff --git a/plugins/Microsoft.Extensions.Config/tests/GlobalUsings.cs b/plugins/Microsoft.Extensions.Config/tests/GlobalUsings.cs new file mode 100644 index 0000000..cfe48cb --- /dev/null +++ b/plugins/Microsoft.Extensions.Config/tests/GlobalUsings.cs @@ -0,0 +1,3 @@ +global using Microsoft.VisualStudio.TestTools.UnitTesting; +global using Microsoft.Extensions.Configuration; +global using FluentAssertions; \ No newline at end of file diff --git a/plugins/Microsoft.Extensions.Config/tests/env_files/.env b/plugins/Microsoft.Extensions.Config/tests/env_files/.env new file mode 100644 index 0000000..80d62bd --- /dev/null +++ b/plugins/Microsoft.Extensions.Config/tests/env_files/.env @@ -0,0 +1,4 @@ +BASE_URL=localhost +PORT=3306 +MARIADB__USER=user123 +MARIADB__PASSWORD=1234 \ No newline at end of file From 426d0aa6ce81934117e2ad7f534fab1e5877a84c Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 15:55:21 -0500 Subject: [PATCH 12/27] docs: Update title --- docs/extensions/microsoft_extensions_DI.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/extensions/microsoft_extensions_DI.md b/docs/extensions/microsoft_extensions_DI.md index 17a315b..fda0072 100644 --- a/docs/extensions/microsoft_extensions_DI.md +++ b/docs/extensions/microsoft_extensions_DI.md @@ -1,9 +1,9 @@ -# dotenv.core extensions for Microsoft.Extensions.DependencyInjection +# Dotenv.Extensions.Microsoft.DependencyInjection [![dotenv.core](https://img.shields.io/badge/.NET%20Standard-2.0-red)](https://github.com/MrDave1999/dotenv.core) [![Nuget-Badges](https://buildstats.info/nuget/Dotenv.Extensions.Microsoft.DependencyInjection)](https://www.nuget.org/packages/Dotenv.Extensions.Microsoft.DependencyInjection/) -This library adds extension methods for the [Microsoft.Extensions.DependencyInjection](https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection) package, which can be accessed through the [IServiceCollection](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection) interface. So, this library is just a wrapper and behind the scenes what happens is that it uses the classes and methods from the [dotenv.core](https://github.com/MrDave1999/dotenv.core) project. +This library adds extension methods for the [Microsoft.Extensions.DependencyInjection](https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection) package, which can be accessed through the [IServiceCollection](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection) interface. So, this library is just a wrapper and behind the scenes what happens is that it uses the classes and methods from the [DotEnv.Core](https://github.com/MrDave1999/dotenv.core) project. This project was created to add support for DI and can be used in ASP.NET Core projects. Refer to the [API documentation](https://mrdave1999.github.io/dotenv.core/api/Microsoft.Extensions.DependencyInjection.html). From af2939234289b958ab32ddae54610f7c51c9c2ee Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 15:57:15 -0500 Subject: [PATCH 13/27] docs: Create microsoft_extensions_config.md file --- .../extensions/microsoft_extensions_config.md | 54 +++++++++++++++++++ docs/extensions/toc.yml | 4 +- 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 docs/extensions/microsoft_extensions_config.md diff --git a/docs/extensions/microsoft_extensions_config.md b/docs/extensions/microsoft_extensions_config.md new file mode 100644 index 0000000..861d1e9 --- /dev/null +++ b/docs/extensions/microsoft_extensions_config.md @@ -0,0 +1,54 @@ +# Dotenv.Extensions.Microsoft.Configuration + +[![dotenv.core](https://img.shields.io/badge/.NET%20Standard-2.0-red)](https://github.com/MrDave1999/dotenv.core) +[![Nuget-Badges](https://buildstats.info/nuget/Dotenv.Extensions.Microsoft.Configuration)](https://www.nuget.org/packages/Dotenv.Extensions.Microsoft.Configuration/) + +ENV configuration provider implementation for [Microsoft.Extensions.Configuration](https://www.nuget.org/packages/Microsoft.Extensions.Configuration). + +This library adds extension methods for the [Microsoft.Extensions.Configuration](https://www.nuget.org/packages/Microsoft.Extensions.Configuration) package, which can be accessed through the [IConfiguration](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.iconfiguration) interface. So, this library is just a wrapper and behind the scenes what happens is that it uses the classes and methods from the [DotEnv.Core](https://github.com/MrDave1999/dotenv.core) project. + +This project was created to integrate the [DotEnv.Core](https://www.nuget.org/packages/DotEnv.Core) package into the .NET configuration system. +Refer to the [API documentation](https://mrdave1999.github.io/dotenv.core/api/Microsoft.Extensions.Configuration.html). + +## Installation + +If you're want to install the package from Visual Studio, you must open the project/solution in Visual Studio, and open the console using the **Tools** > **NuGet Package Manager** > **Package Manager Console** command and run the install command: +``` +Install-Package Dotenv.Extensions.Microsoft.Configuration +``` +If you are making use of the dotnet CLI, then run the following in your terminal: +``` +dotnet add package Dotenv.Extensions.Microsoft.Configuration +``` + +## Usage + +The following example shows how to read the application configuration from ENV file. +```cs +using System; +using Microsoft.Extensions.Configuration; + +class Program +{ + static void Main() + { + // Build a configuration object from ENV file. + IConfiguration config = new ConfigurationBuilder() + .AddEnvFile("appsettings.env", optional: true) + .Build(); + + // Get a configuration section. + IConfigurationSection section = config.GetSection("Settings"); + + // Read configuration values. + Console.WriteLine($"Server: {section["Server"]}"); + Console.WriteLine($"Database: {section["Database"]}"); + } +} +``` +To run this example, include an `appsettings.env` file with the following content in your project: +```.env +Settings__Server=example.com +Settings__Database=Northwind +``` +It doesn't matter if your .env file is in the root directory of your project, the configuration provider will start searching from the current directory and go up the parent directories until it finds it. \ No newline at end of file diff --git a/docs/extensions/toc.yml b/docs/extensions/toc.yml index 2ca080f..99ab621 100644 --- a/docs/extensions/toc.yml +++ b/docs/extensions/toc.yml @@ -1,2 +1,4 @@ - name: Microsoft.Extensions.DI - href: microsoft_extensions_DI.md \ No newline at end of file + href: microsoft_extensions_DI.md +- name: Microsoft.Extensions.Configuration + href: microsoft_extensions_config.md \ No newline at end of file From 0f1a384bc46a4bb0ac2a977f1d044c498bb525db Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 16:00:30 -0500 Subject: [PATCH 14/27] chore: Add README.md file to package --- .../src/DotEnv.Extensions.Microsoft.Config.csproj | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugins/Microsoft.Extensions.Config/src/DotEnv.Extensions.Microsoft.Config.csproj b/plugins/Microsoft.Extensions.Config/src/DotEnv.Extensions.Microsoft.Config.csproj index 7bd7fe8..9fde84d 100644 --- a/plugins/Microsoft.Extensions.Config/src/DotEnv.Extensions.Microsoft.Config.csproj +++ b/plugins/Microsoft.Extensions.Config/src/DotEnv.Extensions.Microsoft.Config.csproj @@ -10,6 +10,7 @@ David Román Amariles https://mrdave1999.github.io/dotenv.core dotenv-icon-nuget.png + README.md https://github.com/MrDave1999/dotenv.core dotenv, env, variables, environment, configuration, envfile LICENSE @@ -30,6 +31,7 @@ + From 8105fcb26cd92d5d2128297ac1c8fa1946bb13d2 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 16:25:45 -0500 Subject: [PATCH 15/27] chore: Add package reference --- .../example/DotEnv.Extensions.Microsoft.Config.Example.csproj | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/plugins/Microsoft.Extensions.Config/example/DotEnv.Extensions.Microsoft.Config.Example.csproj b/plugins/Microsoft.Extensions.Config/example/DotEnv.Extensions.Microsoft.Config.Example.csproj index 5b34966..b7dccde 100644 --- a/plugins/Microsoft.Extensions.Config/example/DotEnv.Extensions.Microsoft.Config.Example.csproj +++ b/plugins/Microsoft.Extensions.Config/example/DotEnv.Extensions.Microsoft.Config.Example.csproj @@ -7,5 +7,9 @@ + + + + From bdf297deac27ccf26f7650fc827685aa68bf0710 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 16:26:47 -0500 Subject: [PATCH 16/27] docs: Update link --- docs/extensions/microsoft_extensions_config.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/extensions/microsoft_extensions_config.md b/docs/extensions/microsoft_extensions_config.md index 861d1e9..df9fa13 100644 --- a/docs/extensions/microsoft_extensions_config.md +++ b/docs/extensions/microsoft_extensions_config.md @@ -5,9 +5,10 @@ ENV configuration provider implementation for [Microsoft.Extensions.Configuration](https://www.nuget.org/packages/Microsoft.Extensions.Configuration). -This library adds extension methods for the [Microsoft.Extensions.Configuration](https://www.nuget.org/packages/Microsoft.Extensions.Configuration) package, which can be accessed through the [IConfiguration](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.iconfiguration) interface. So, this library is just a wrapper and behind the scenes what happens is that it uses the classes and methods from the [DotEnv.Core](https://github.com/MrDave1999/dotenv.core) project. +This library adds extension methods for the [Microsoft.Extensions.Configuration](https://www.nuget.org/packages/Microsoft.Extensions.Configuration) package, which can be accessed through the [IConfigurationBuilder](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.iconfigurationbuilder) interface. So, this library is just a wrapper and behind the scenes what happens is that it uses the classes and methods from the [DotEnv.Core](https://github.com/MrDave1999/dotenv.core) project. This project was created to integrate the [DotEnv.Core](https://www.nuget.org/packages/DotEnv.Core) package into the .NET configuration system. + Refer to the [API documentation](https://mrdave1999.github.io/dotenv.core/api/Microsoft.Extensions.Configuration.html). ## Installation From 90546259e1a810b5438f197d1e8e03928e698dae Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 16:30:36 -0500 Subject: [PATCH 17/27] chore: Update project name --- DotEnv.Core.sln | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DotEnv.Core.sln b/DotEnv.Core.sln index 5c8d173..134af4b 100644 --- a/DotEnv.Core.sln +++ b/DotEnv.Core.sln @@ -19,11 +19,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotEnv.Extensions.Microsoft EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Microsoft.Extensions.Config", "Microsoft.Extensions.Config", "{72F0723F-912D-42F3-AA80-928F7509DD94}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotEnv.Extensions.Microsoft.Config", "plugins\Microsoft.Extensions.Config\src\DotEnv.Extensions.Microsoft.Config.csproj", "{B50FB187-F524-46E3-8AC8-E31826294F95}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotEnv.Extensions.Microsoft.Configuration", "plugins\Microsoft.Extensions.Config\src\DotEnv.Extensions.Microsoft.Configuration.csproj", "{B50FB187-F524-46E3-8AC8-E31826294F95}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotEnv.Extensions.Microsoft.Config.Example", "plugins\Microsoft.Extensions.Config\example\DotEnv.Extensions.Microsoft.Config.Example.csproj", "{EE641B31-2439-481A-BCC4-4520B75DD57D}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotEnv.Extensions.Microsoft.Config.Example", "plugins\Microsoft.Extensions.Config\example\DotEnv.Extensions.Microsoft.Config.Example.csproj", "{EE641B31-2439-481A-BCC4-4520B75DD57D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotEnv.Extensions.Microsoft.Config.Tests", "plugins\Microsoft.Extensions.Config\tests\DotEnv.Extensions.Microsoft.Config.Tests.csproj", "{91AA172D-AE1E-445E-B3A1-5841C856E659}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotEnv.Extensions.Microsoft.Config.Tests", "plugins\Microsoft.Extensions.Config\tests\DotEnv.Extensions.Microsoft.Config.Tests.csproj", "{91AA172D-AE1E-445E-B3A1-5841C856E659}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Elements", "Solution Elements", "{2D684F32-79A1-4904-A921-204D88EB2F48}" ProjectSection(SolutionItems) = preProject From f6510cc8ea0a92d23681b44641d4bee584cf91fe Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 16:33:04 -0500 Subject: [PATCH 18/27] chore: Rename project file --- ...ig.csproj => DotEnv.Extensions.Microsoft.Configuration.csproj} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename plugins/Microsoft.Extensions.Config/src/{DotEnv.Extensions.Microsoft.Config.csproj => DotEnv.Extensions.Microsoft.Configuration.csproj} (100%) diff --git a/plugins/Microsoft.Extensions.Config/src/DotEnv.Extensions.Microsoft.Config.csproj b/plugins/Microsoft.Extensions.Config/src/DotEnv.Extensions.Microsoft.Configuration.csproj similarity index 100% rename from plugins/Microsoft.Extensions.Config/src/DotEnv.Extensions.Microsoft.Config.csproj rename to plugins/Microsoft.Extensions.Config/src/DotEnv.Extensions.Microsoft.Configuration.csproj From 32cff8633c9cf0755c4d5a2cc1d93e9829f4dc32 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 16:33:47 -0500 Subject: [PATCH 19/27] chore: Update path from a package reference --- .../example/DotEnv.Extensions.Microsoft.Config.Example.csproj | 2 +- .../tests/DotEnv.Extensions.Microsoft.Config.Tests.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/Microsoft.Extensions.Config/example/DotEnv.Extensions.Microsoft.Config.Example.csproj b/plugins/Microsoft.Extensions.Config/example/DotEnv.Extensions.Microsoft.Config.Example.csproj index b7dccde..f82c712 100644 --- a/plugins/Microsoft.Extensions.Config/example/DotEnv.Extensions.Microsoft.Config.Example.csproj +++ b/plugins/Microsoft.Extensions.Config/example/DotEnv.Extensions.Microsoft.Config.Example.csproj @@ -5,7 +5,7 @@ - + diff --git a/plugins/Microsoft.Extensions.Config/tests/DotEnv.Extensions.Microsoft.Config.Tests.csproj b/plugins/Microsoft.Extensions.Config/tests/DotEnv.Extensions.Microsoft.Config.Tests.csproj index d6db9fd..318e2af 100644 --- a/plugins/Microsoft.Extensions.Config/tests/DotEnv.Extensions.Microsoft.Config.Tests.csproj +++ b/plugins/Microsoft.Extensions.Config/tests/DotEnv.Extensions.Microsoft.Config.Tests.csproj @@ -16,7 +16,7 @@ - + From 0bd9d5d0d1ebaee4ef52d862ddf4cc92e56a7f14 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 16:34:21 -0500 Subject: [PATCH 20/27] chore: Update docfx.json --- docs/docfx.json | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/docfx.json b/docs/docfx.json index 6a0db58..0280fb5 100644 --- a/docs/docfx.json +++ b/docs/docfx.json @@ -13,6 +13,12 @@ "files": [ "plugins/Microsoft.Extensions.DI/src/**.csproj" ] + }, + { + "src": "..", + "files": [ + "plugins/Microsoft.Extensions.Config/src/**.csproj" + ] } ], "dest": "api", From 34c637a8ae15b89db18c24fde50c65c184e0f626 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 16:35:24 -0500 Subject: [PATCH 21/27] chore: Update project name --- src/DotEnv.Core.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/DotEnv.Core.csproj b/src/DotEnv.Core.csproj index 222db0b..5a38e87 100644 --- a/src/DotEnv.Core.csproj +++ b/src/DotEnv.Core.csproj @@ -32,7 +32,7 @@ - + From f662dd2fe837d867740bf2a8eb0b5a2c04c7b939 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 17:52:31 -0500 Subject: [PATCH 22/27] docs: Add exception tag --- .../src/EnvConfigurationExtensions.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/plugins/Microsoft.Extensions.Config/src/EnvConfigurationExtensions.cs b/plugins/Microsoft.Extensions.Config/src/EnvConfigurationExtensions.cs index ae1861c..e4f0507 100644 --- a/plugins/Microsoft.Extensions.Config/src/EnvConfigurationExtensions.cs +++ b/plugins/Microsoft.Extensions.Config/src/EnvConfigurationExtensions.cs @@ -17,7 +17,10 @@ public static class EnvConfigurationExtensions /// The path to the file. /// The . /// - /// , or is null. + /// is null. + /// + /// + /// If the is null, empty or consists only of white-space characters. /// public static IConfigurationBuilder AddEnvFile(this IConfigurationBuilder builder, string path) => builder.AddEnvFile(path, optional: false); @@ -30,7 +33,10 @@ public static IConfigurationBuilder AddEnvFile(this IConfigurationBuilder builde /// Whether the file is optional. /// The . /// - /// , or is null. + /// is null. + /// + /// + /// If the is null, empty or consists only of white-space characters. /// public static IConfigurationBuilder AddEnvFile( this IConfigurationBuilder builder, From a5667e47c75e450bd3aa84944d329af240bbf207 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 17:56:25 -0500 Subject: [PATCH 23/27] refactor: Add null checks in the extension method --- .../src/EnvConfigurationExtensions.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/plugins/Microsoft.Extensions.Config/src/EnvConfigurationExtensions.cs b/plugins/Microsoft.Extensions.Config/src/EnvConfigurationExtensions.cs index e4f0507..b95590f 100644 --- a/plugins/Microsoft.Extensions.Config/src/EnvConfigurationExtensions.cs +++ b/plugins/Microsoft.Extensions.Config/src/EnvConfigurationExtensions.cs @@ -58,8 +58,15 @@ public static IConfigurationBuilder AddEnvFile( /// The to add to. /// Configures the source. /// The . + /// + /// , or is null. + /// public static IConfigurationBuilder AddEnvFile( this IConfigurationBuilder builder, Action configureSource) - => builder.Add(configureSource); + { + ThrowHelper.ThrowIfNull(builder, nameof(builder)); + ThrowHelper.ThrowIfNull(configureSource, nameof(configureSource)); + return builder.Add(configureSource); + } } From df20d73bab29c6c3128998471938a44daa250e90 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 18:11:40 -0500 Subject: [PATCH 24/27] Update README.md --- README.md | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cc436f0..6a3b79f 100644 --- a/README.md +++ b/README.md @@ -13,9 +13,12 @@ [![DotEnv.Core.Props](https://img.shields.io/nuget/vpre/DotEnv.Core.Props?label=DotEnv.Core.Props%20-%20nuget&color=red)](https://www.nuget.org/packages/DotEnv.Core.Props) [![downloads](https://img.shields.io/nuget/dt/DotEnv.Core.Props?color=yellow)](https://www.nuget.org/packages/DotEnv.Core.Props) -[![Dotenv.Microsoft.DI](https://img.shields.io/nuget/vpre/Dotenv.Extensions.Microsoft.DependencyInjection?label=Dotenv.Extensions.Microsoft.DependencyInjection%20-%20nuget&color=red)](https://www.nuget.org/packages/Dotenv.Extensions.Microsoft.DependencyInjection) +[![Dotenv.Microsoft.DI](https://img.shields.io/nuget/vpre/Dotenv.Extensions.Microsoft.DependencyInjection?label=DotEnv.Extensions.Microsoft.DependencyInjection%20-%20nuget&color=red)](https://www.nuget.org/packages/Dotenv.Extensions.Microsoft.DependencyInjection) [![downloads](https://img.shields.io/nuget/dt/Dotenv.Extensions.Microsoft.DependencyInjection?color=yellow)](https://www.nuget.org/packages/Dotenv.Extensions.Microsoft.DependencyInjection) +[![Dotenv.Microsoft.Config](https://img.shields.io/nuget/vpre/Dotenv.Extensions.Microsoft.Configuration?label=DotEnv.Extensions.Microsoft.Configuration%20-%20nuget&color=red)](https://www.nuget.org/packages/Dotenv.Extensions.Microsoft.Configuration) +[![downloads](https://img.shields.io/nuget/dt/Dotenv.Extensions.Microsoft.Configuration?color=yellow)](https://www.nuget.org/packages/Dotenv.Extensions.Microsoft.Configuration) + **DotEnv.Core** is a class library for read and parsing .env files in .NET Core and also provides a mechanism to retrieve the value of an environment variable in a simple and easy way. The advantage of using this library is that you do not need to set the environment variable from the operating system shell (**dotenv** sets environment variables from a .env file). @@ -38,6 +41,8 @@ The advantage of using this library is that you do not need to set the environme * [Using DotEnv in ASP.NET Core](#using-dotenv-in-aspnet-core) - [Copying .env file to the publish directory](#copying-env-file-to-the-publish-directory) - [Extensions](#extensions) + - [Integration with Microsoft.Extensions.DependencyInjection](#integration-with-microsoftextensionsdependencyinjection) + - [Integration with Microsoft.Extensions.Configuration](#integration-with-microsoftextensionsconfiguration) - [File Format](#file-format) * [Comments](#comments) * [Interpolating variables](#interpolating-variables) @@ -263,9 +268,25 @@ If you do not want to use the previous package, add the following to your `.cspr ## Extensions -- [Dotenv.Extensions.Microsoft.DependencyInjection](https://mrdave1999.github.io/dotenv.core/extensions/microsoft_extensions_DI.html) +### Integration with Microsoft.Extensions.DependencyInjection + +Install the package with this command: +```sh +dotnet add package Dotenv.Extensions.Microsoft.DependencyInjection +``` +This package adds extension methods for [Microsoft.Extensions.DependencyInjection](https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection), which can be accessed from the `IServiceCollection` interface. + +For more information, see the documentation [here](https://mrdave1999.github.io/dotenv.core/extensions/microsoft_extensions_DI.html). + +### Integration with Microsoft.Extensions.Configuration + +Install the package with this command: +```sh +dotnet add package Dotenv.Extensions.Microsoft.Configuration +``` +This package adds extension methods for [Microsoft.Extensions.Configuration](https://www.nuget.org/packages/Microsoft.Extensions.Configuration), which can be accessed from the `IConfigurationBuilder` interface. - - This package adds extension methods for [Microsoft.Extensions.DependencyInjection](https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection), which can be accessed from the `IServiceCollection` interface. +For more information, see the documentation [here](https://mrdave1999.github.io/dotenv.core/extensions/microsoft_extensions_config.html). ## File Format From 4889c0ddd43a1c40cb646f5eb338fb0a0338fd1d Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Sun, 23 Jun 2024 18:11:57 -0500 Subject: [PATCH 25/27] docs: Update home page --- docs/index.md | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/docs/index.md b/docs/index.md index 817e735..a678aa8 100644 --- a/docs/index.md +++ b/docs/index.md @@ -11,9 +11,12 @@ [![DotEnv.Core.Props](https://img.shields.io/nuget/vpre/DotEnv.Core.Props?label=DotEnv.Core.Props%20-%20nuget&color=red)](https://www.nuget.org/packages/DotEnv.Core.Props) [![downloads](https://img.shields.io/nuget/dt/DotEnv.Core.Props?color=yellow)](https://www.nuget.org/packages/DotEnv.Core.Props) -[![Dotenv.Microsoft.DI](https://img.shields.io/nuget/vpre/Dotenv.Extensions.Microsoft.DependencyInjection?label=Dotenv.Extensions.Microsoft.DependencyInjection%20-%20nuget&color=red)](https://www.nuget.org/packages/Dotenv.Extensions.Microsoft.DependencyInjection) +[![Dotenv.Microsoft.DI](https://img.shields.io/nuget/vpre/Dotenv.Extensions.Microsoft.DependencyInjection?label=DotEnv.Extensions.Microsoft.DependencyInjection%20-%20nuget&color=red)](https://www.nuget.org/packages/Dotenv.Extensions.Microsoft.DependencyInjection) [![downloads](https://img.shields.io/nuget/dt/Dotenv.Extensions.Microsoft.DependencyInjection?color=yellow)](https://www.nuget.org/packages/Dotenv.Extensions.Microsoft.DependencyInjection) +[![Dotenv.Microsoft.Config](https://img.shields.io/nuget/vpre/Dotenv.Extensions.Microsoft.Configuration?label=DotEnv.Extensions.Microsoft.Configuration%20-%20nuget&color=red)](https://www.nuget.org/packages/Dotenv.Extensions.Microsoft.Configuration) +[![downloads](https://img.shields.io/nuget/dt/Dotenv.Extensions.Microsoft.Configuration?color=yellow)](https://www.nuget.org/packages/Dotenv.Extensions.Microsoft.Configuration) + **DotEnv.Core** is a class library for read and parsing .env files in .NET Core and also provides a mechanism to retrieve the value of an environment variable in a simple and easy way. The advantage of using this library is that you do not need to set the environment variable from the operating system shell (**dotenv** sets environment variables from a .env file). @@ -114,12 +117,6 @@ KEY="first line\nsecond line" VAR='first line\nsecond line' ``` -## Extensions - -- [Dotenv.Extensions.Microsoft.DependencyInjection](https://mrdave1999.github.io/dotenv.core/extensions/microsoft_extensions_DI.html) - - - This package adds extension methods for [Microsoft.Extensions.DependencyInjection](https://www.nuget.org/packages/Microsoft.Extensions.DependencyInjection), which can be accessed from the `IServiceCollection` interface. - ## Frequently Answered Questions ### Can I use an `.env file` in a production environment? From 1c5f336db0610d1db30ea95b048cf9198d3989ea Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Mon, 24 Jun 2024 18:52:45 -0500 Subject: [PATCH 26/27] Include exception message when path is invalid --- src/Common/Resources/ExceptionMessages.Designer.cs | 9 +++++++++ src/Common/Resources/ExceptionMessages.es.resx | 3 +++ src/Common/Resources/ExceptionMessages.resx | 3 +++ 3 files changed, 15 insertions(+) diff --git a/src/Common/Resources/ExceptionMessages.Designer.cs b/src/Common/Resources/ExceptionMessages.Designer.cs index 44429d2..f97f7bb 100644 --- a/src/Common/Resources/ExceptionMessages.Designer.cs +++ b/src/Common/Resources/ExceptionMessages.Designer.cs @@ -186,6 +186,15 @@ internal static string ParserDefaultMessage { } } + /// + /// Looks up a localized string similar to The 'path' cannot be null, empty string or consist only of white-space characters.. + /// + internal static string PathIsInvalid { + get { + return ResourceManager.GetString("PathIsInvalid", resourceCulture); + } + } + /// /// Looks up a localized string similar to The '{0}.{1}' property does not match any configuration key.. /// diff --git a/src/Common/Resources/ExceptionMessages.es.resx b/src/Common/Resources/ExceptionMessages.es.resx index a27419c..b926163 100644 --- a/src/Common/Resources/ExceptionMessages.es.resx +++ b/src/Common/Resources/ExceptionMessages.es.resx @@ -159,6 +159,9 @@ Se ha producido un error al analizar una fuente de datos con pares clave-valor. + + 'Path' no puede ser null, una cadena vacía o estar formada sólo por caracteres de espacio en blanco. + La propiedad '{0}.{1}' no coincide con ninguna clave de configuración. diff --git a/src/Common/Resources/ExceptionMessages.resx b/src/Common/Resources/ExceptionMessages.resx index 7fd4995..61c17f5 100644 --- a/src/Common/Resources/ExceptionMessages.resx +++ b/src/Common/Resources/ExceptionMessages.resx @@ -159,6 +159,9 @@ An error occurred while parsing a data source with key-value pairs. + + The 'path' cannot be null, empty string or consist only of white-space characters. + The '{0}.{1}' property does not match any configuration key. From 0be0f15d49a8b1aad9fb0c30fd777875e943c263 Mon Sep 17 00:00:00 2001 From: MrDave1999 Date: Mon, 24 Jun 2024 18:57:25 -0500 Subject: [PATCH 27/27] fix: Check if the configuration source has an invalid path --- .../src/EnvConfigurationSource.cs | 11 ++++++++++- .../tests/EnvConfigurationExtensionsTests.cs | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/plugins/Microsoft.Extensions.Config/src/EnvConfigurationSource.cs b/plugins/Microsoft.Extensions.Config/src/EnvConfigurationSource.cs index 0a177fc..4f9983b 100644 --- a/plugins/Microsoft.Extensions.Config/src/EnvConfigurationSource.cs +++ b/plugins/Microsoft.Extensions.Config/src/EnvConfigurationSource.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Text; +using DotEnv.Core; namespace Microsoft.Extensions.Configuration; @@ -28,6 +29,14 @@ public class EnvConfigurationSource : IConfigurationSource /// /// The . /// An + /// + /// is null, empty or consists only of white-space characters. + /// public IConfigurationProvider Build(IConfigurationBuilder builder) - => new EnvConfigurationProvider(this); + { + if (string.IsNullOrWhiteSpace(Path)) + throw new InvalidOperationException(ExceptionMessages.PathIsInvalid); + + return new EnvConfigurationProvider(this); + } } diff --git a/plugins/Microsoft.Extensions.Config/tests/EnvConfigurationExtensionsTests.cs b/plugins/Microsoft.Extensions.Config/tests/EnvConfigurationExtensionsTests.cs index bfeae97..9b4f41e 100644 --- a/plugins/Microsoft.Extensions.Config/tests/EnvConfigurationExtensionsTests.cs +++ b/plugins/Microsoft.Extensions.Config/tests/EnvConfigurationExtensionsTests.cs @@ -79,4 +79,21 @@ public void Build_WhenPathIsValid_ShouldLoadKeyValuePairsFromValidEnvFile(string section["user"].Should().Be("user123"); section["password"].Should().Be("1234"); } + + [TestMethod] + [DataRow("")] + [DataRow(" ")] + [DataRow(null)] + public void Build_WhenConfigurationSourceHasInvalidPath_ShouldThrowInvalidOperationException(string path) + { + // Arrange + var builder = new ConfigurationBuilder() + .AddEnvFile((EnvConfigurationSource source) => source.Path = path); + + // Act + Action act = () => builder.Build(); + + // Assert + act.Should().Throw(); + } }