Skip to content

Commit e56811b

Browse files
committed
Add support for .env files
Loads from user home dir too.
1 parent 35ee912 commit e56811b

File tree

6 files changed

+28
-1
lines changed

6 files changed

+28
-1
lines changed

src/Samples/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
FOO=BAR

src/Samples/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{ "user", "What's Tesla stock worth today? Search X and the news for latest info." },
66
};
77

8+
// Env supports .env as well as all standard .NET configuration sources
89
var grok = new GrokClient(Env.Get("XAI_API_KEY")!, new GrokClientOptions()
910
.UseJsonConsoleLogging(new() { WrapLength = 80 }));
1011

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Weaving;
66

7-
class ConsoleInitializer
7+
class AppInitializer
88
{
99
#pragma warning disable CA2255 // The 'ModuleInitializer' attribute should not be used in libraries
1010
[ModuleInitializer]
@@ -13,5 +13,13 @@ public static void Init()
1313
{
1414
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
1515
Console.InputEncoding = Console.OutputEncoding = Encoding.UTF8;
16+
17+
// Load environment variables from .env files in current dir and above.
18+
DotNetEnv.Env.TraversePath().Load();
19+
20+
// Load environment variables from user profile directory.
21+
var userEnv = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".env");
22+
if (File.Exists(userEnv))
23+
DotNetEnv.Env.Load(userEnv);
1624
}
1725
}

src/Weaving/Weaving.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13+
<PackageReference Include="DotNetEnv" Version="3.1.1" />
1314
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.6" />
1415
<PackageReference Include="NuGetizer" Version="1.2.4" PrivateAssets="all" />
1516
</ItemGroup>

src/Weaving/Weaving.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
<ItemGroup>
3232
<AssemblyMetadata Include="MSBuildProjectName" Value="$(MSBuildProjectName)" />
33+
<None Update=".env" CopyToOutputDirectory="PreserveNewest" CopyToPublishDirectory="Never" />
3334
</ItemGroup>
3435

3536
</Project>

src/Weaving/readme.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ AnsiConsole.MarkupLine($":robot: {response.Text}");
2929
> The most useful namespaces and dependencies for developing Microsoft.Extensions.AI-
3030
> powered applications are automatically referenced and imported when using this package.
3131
32+
## Configuration / Environment Variables
33+
34+
The `Env` class provides access to the following variables/configuration automatically:
35+
36+
* `.env` files: in local and parent directories
37+
* `~/.env` file: in the user's home directory (`%userprofile%\.env` on Windows)
38+
* All default configuration sources from [App Builder](https://learn.microsoft.com/en-us/dotnet/core/extensions/generic-host?tabs=appbuilder#host-builder-settings):
39+
* Environment variables prefixed with DOTNET_.
40+
* Command-line arguments.
41+
* appsettings.json.
42+
* appsettings.{Environment}.json.
43+
* Secret Manager when the app runs in the Development environment.
44+
* Environment variables.
45+
* Command-line arguments.
46+
3247
<!-- #content -->
3348
<!-- include https://github.com/devlooped/sponsors/raw/main/footer.md -->
3449
<!-- exclude -->

0 commit comments

Comments
 (0)