Skip to content

Commit

Permalink
Merge pull request #176 from MrDave1999/feat/issue-136
Browse files Browse the repository at this point in the history
feat: Add support for Microsoft.Extensions.Configuration
  • Loading branch information
MrDave1999 authored Jun 25, 2024
2 parents 4452363 + 0be0f15 commit fd4f77e
Show file tree
Hide file tree
Showing 27 changed files with 524 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/dotnetcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="7.0.5" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="6.0.0" />
</ItemGroup>
</Project>
23 changes: 23 additions & 0 deletions DotEnv.Core.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ 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("{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("{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("{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
Directory.Build.props = Directory.Build.props
Expand Down Expand Up @@ -53,6 +61,18 @@ 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
{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
Expand All @@ -61,6 +81,9 @@ 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}
{91AA172D-AE1E-445E-B3A1-5841C856E659} = {72F0723F-912D-42F3-AA80-928F7509DD94}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DADE2FE0-EA2C-41E8-B91E-2A5F19EAAD87}
Expand Down
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions docs/docfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
"files": [
"plugins/Microsoft.Extensions.DI/src/**.csproj"
]
},
{
"src": "..",
"files": [
"plugins/Microsoft.Extensions.Config/src/**.csproj"
]
}
],
"dest": "api",
Expand Down
4 changes: 2 additions & 2 deletions docs/extensions/microsoft_extensions_DI.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
55 changes: 55 additions & 0 deletions docs/extensions/microsoft_extensions_config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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 [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

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.
4 changes: 3 additions & 1 deletion docs/extensions/toc.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
- name: Microsoft.Extensions.DI
href: microsoft_extensions_DI.md
href: microsoft_extensions_DI.md
- name: Microsoft.Extensions.Configuration
href: microsoft_extensions_config.md
11 changes: 4 additions & 7 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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?
Expand Down
9 changes: 9 additions & 0 deletions plugins/Microsoft.Extensions.Config/example/.env
Original file line number Diff line number Diff line change
@@ -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##$
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\src\DotEnv.Extensions.Microsoft.Configuration.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="DotEnv.Core.Props" />
</ItemGroup>

</Project>
21 changes: 21 additions & 0 deletions plugins/Microsoft.Extensions.Config/example/Program.cs
Original file line number Diff line number Diff line change
@@ -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"]);
1 change: 1 addition & 0 deletions plugins/Microsoft.Extensions.Config/example/config.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BASE_URL=http://localhost:3000/
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>Microsoft.Extensions.Configuration</RootNamespace>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<PackageId>Dotenv.Extensions.Microsoft.Configuration</PackageId>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Authors>David Román Amariles</Authors>
<Copyright>David Román Amariles</Copyright>
<PackageProjectUrl>https://mrdave1999.github.io/dotenv.core</PackageProjectUrl>
<PackageIcon>dotenv-icon-nuget.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/MrDave1999/dotenv.core</RepositoryUrl>
<PackageTags>dotenv, env, variables, environment, configuration, envfile</PackageTags>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
<Description>Integrates DotEnv.Core package with Microsoft.Extensions.Configuration</Description>
<Version>1.0.0</Version>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<NoWarn>1591</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\DotEnv.Core.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\..\dotenv-icon-nuget.png" Pack="True" PackagePath="" />
<None Include="..\..\..\docs\extensions\microsoft_extensions_config.md" Pack="True" PackagePath="README.md" />
<None Include="..\..\..\LICENSE" Pack="True" PackagePath="" />
</ItemGroup>

</Project>
Loading

0 comments on commit fd4f77e

Please sign in to comment.