mirror of https://github.com/dotnet/tye.git
Browse Source
This removes the need to copy-paste code that reads a specific directory in Program.cs. Updated docs/samples and tests. We need a workaround in tests to be able to use a P2P for the library. What I did here is the same trick we do in Razor. - Force everyone to use an MSBuild variable to locate the library - Set that variable in a Directory.Build.props for the normal build - Drop a special Directory.Build.props for testing Updates a bunch of test code to use new helper methods for copying stuff, so we can correctly do this.pull/290/head
25 changed files with 280 additions and 175 deletions
@ -0,0 +1,11 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netstandard2.0</TargetFramework> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="Microsoft.Extensions.Configuration.KeyPerFile" Version="3.1.3" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,53 @@ |
|||||
|
// Licensed to the .NET Foundation under one or more agreements.
|
||||
|
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
|
// See the LICENSE file in the project root for more information.
|
||||
|
|
||||
|
using System; |
||||
|
using System.IO; |
||||
|
using System.Runtime.InteropServices; |
||||
|
using Microsoft.Extensions.FileProviders; |
||||
|
|
||||
|
namespace Microsoft.Extensions.Configuration |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Contains extension methods for adding Tye's secrets to <see cref="IConfiguration" />.
|
||||
|
/// </summary>
|
||||
|
public static class TyeSecretsConfigurationBuilderExtensions |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Adds Tye's secrets to <see cref="IConfiguration" />.
|
||||
|
/// </summary>
|
||||
|
/// <param name="builder">The <see cref="IConfigurationBuilder" />.</param>
|
||||
|
/// <param name="configure">A delegate for additional configuration.</param>
|
||||
|
/// <returns>The <see cref="IConfigurationBuilder" />.</returns>
|
||||
|
/// <remarks>
|
||||
|
/// The environment variable <c>TYE_SECRETS_PATH</c> is used to populate the directory used by secrets.
|
||||
|
/// When the environment variable is specified, and the specified directory exists, then the value of
|
||||
|
/// <see cref="TyeSecretsConfigurationSource.FileProvider" /> will be non-null.
|
||||
|
/// </remarks>
|
||||
|
public static IConfigurationBuilder AddTyeSecrets(this IConfigurationBuilder builder, Action<TyeSecretsConfigurationSource>? configure = null) |
||||
|
{ |
||||
|
TyeSecretsConfigurationSource source; |
||||
|
var secretsDirectory = Environment.GetEnvironmentVariable(TyeSecretsConfigurationSource.TyeSecretsPathEnvironmentVariable); |
||||
|
if (secretsDirectory == null || !Directory.Exists(secretsDirectory)) |
||||
|
{ |
||||
|
source = new TyeSecretsConfigurationSource() |
||||
|
{ |
||||
|
FileProvider = null, |
||||
|
}; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
source = new TyeSecretsConfigurationSource() |
||||
|
{ |
||||
|
FileProvider = new PhysicalFileProvider(secretsDirectory), |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
configure?.Invoke(source); |
||||
|
builder.Add(source); |
||||
|
|
||||
|
return builder; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,37 @@ |
|||||
|
// Licensed to the .NET Foundation under one or more agreements.
|
||||
|
// The .NET Foundation licenses this file to you under the MIT license.
|
||||
|
// See the LICENSE file in the project root for more information.
|
||||
|
|
||||
|
using Microsoft.Extensions.Configuration.KeyPerFile; |
||||
|
using Microsoft.Extensions.FileProviders; |
||||
|
|
||||
|
namespace Microsoft.Extensions.Configuration |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// An <see cref="IConfigurationSource" /> implementation for Tye's secrets.
|
||||
|
/// </summary>
|
||||
|
public sealed class TyeSecretsConfigurationSource : IConfigurationSource |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// The environment variable used to configure the path where Tye looks for secrets.
|
||||
|
/// </summary>
|
||||
|
public static readonly string TyeSecretsPathEnvironmentVariable = "TYE_SECRETS_PATH"; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets or sets the <see cref="IFileProvider" /> used by the configuration source.
|
||||
|
/// </summary>
|
||||
|
public IFileProvider? FileProvider { get; set; } |
||||
|
|
||||
|
public IConfigurationProvider Build(IConfigurationBuilder builder) |
||||
|
{ |
||||
|
var source = new KeyPerFileConfigurationSource() |
||||
|
{ |
||||
|
FileProvider = FileProvider, |
||||
|
Optional = true, |
||||
|
}; |
||||
|
|
||||
|
return source.Build(builder); |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,8 @@ |
|||||
|
<Project> |
||||
|
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory)..\, Directory.Build.props))\Directory.Build.props" /> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<!-- Used for P2Ps in our samples. The tests know how to set this value so that references work. --> |
||||
|
<TyeLibrariesPath>$(MSBuildThisFileDirectory)..\src\</TyeLibrariesPath> |
||||
|
</PropertyGroup> |
||||
|
</Project> |
||||
Loading…
Reference in new issue