Skip to content

Commit fba14ad

Browse files
StephenMolloyHongGit
authored andcommitted
Actually implement the non-lazy version of KeyPerFile.GetValue (#29)
* Oops. I missed a few steps when fleshing out a non-greedy KeyPerFile.GetValue(). * Adding README messages to pre-existing builders to warn about possible loss of config options. * Project file got missed in last commit somehow. * Rewording readmes.
1 parent d1707ef commit fba14ad

File tree

11 files changed

+150
-6
lines changed

11 files changed

+150
-6
lines changed

samples/SampleWebApp/Web.config

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<!-- <add name="Environment" mode="Expand" type="Microsoft.Configuration.ConfigurationBuilders.EnvironmentConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Environment, Version=1.0.0.0, Culture=neutral"/> -->
1515
<add name="Secrets" userSecretsFile="~/App_Data/secrets.xml" mode="Greedy" type="Microsoft.Configuration.ConfigurationBuilders.UserSecretsConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.UserSecrets, Version=1.0.0.0, Culture=neutral" />
1616
<add name="SimpleJson" jsonFile="~/App_Data/settings.json" ignoreMissingFile="true" type="Microsoft.Configuration.ConfigurationBuilders.SimpleJsonConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.Json, Version=1.0.0.0, Culture=neutral" />
17-
<add name="KeyPerFile" directoryPath="~/../KeyPerFileSampleRoot" mode="Greedy" keyDelimiter="--" type="Microsoft.Configuration.ConfigurationBuilders.KeyPerFileConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.KeyPerFile, Version=1.0.0.0, Culture=neutral" />
17+
<add name="KeyPerFile" directoryPath="~/../KeyPerFileSampleRoot" mode="Strict" keyDelimiter="--" type="Microsoft.Configuration.ConfigurationBuilders.KeyPerFileConfigBuilder, Microsoft.Configuration.ConfigurationBuilders.KeyPerFile, Version=1.0.0.0, Culture=neutral" />
1818
</builders>
1919
</configBuilders>
2020

@@ -32,6 +32,10 @@
3232
<add key="jsonConnectionString1" value="WILL be replaced by 'SimpleJson' in 'Flat' jsonMode." />
3333
<add key="jsonConnectionString2" value="WILL NOT be replaced by 'SimpleJson' in 'Sectional' jsonMode." />
3434
<add key="jsonCustomSetting1" value="Will be replaced by 'SimpleJson' with prefix='customSettings:' and stripPrefix='true'." />
35+
<add key="SecretFromFile1" value="This will be replaced by KeyPerFile." />
36+
<add key="SECRETFROMFILE2" value="As will this one." />
37+
<add key="SubFeature--FeatureSecretA" value="This will be replaced by KeyPerFile if keyDelimiter is set to '--'." />
38+
<add key="ignore.secretfromfile3" value="This value should be left alone." />
3539
</appSettings>
3640

3741
<connectionStrings configBuilders="SimpleJson">

src/KeyPerFile/KeyPerFileConfigBuilder.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,19 @@ public override ICollection<KeyValuePair<string, string>> GetAllValues(string pr
8888
/// <returns>The value corresponding to the given 'key' or null if no value is found.</returns>
8989
public override string GetValue(string key)
9090
{
91-
if (_allValues != null && _allValues.TryGetValue(key, out string val))
92-
return val;
91+
string filename = key;
92+
if (!String.IsNullOrEmpty(KeyDelimiter))
93+
filename = filename.Replace(KeyDelimiter, Path.DirectorySeparatorChar.ToString());
9394

94-
return null;
95+
if (!String.IsNullOrWhiteSpace(IgnorePrefix))
96+
{
97+
foreach (var pathPart in filename.Split(new char[] { Path.DirectorySeparatorChar })) {
98+
if (pathPart.StartsWith(IgnorePrefix, StringComparison.OrdinalIgnoreCase))
99+
return null;
100+
}
101+
}
102+
103+
return ReadValueFromFile(Path.Combine(DirectoryPath, filename));
95104
}
96105

97106
private IDictionary<string, string> ReadAllValues(string root, string prefix, IDictionary<string, string> values)
@@ -107,7 +116,7 @@ private IDictionary<string, string> ReadAllValues(string root, string prefix, ID
107116
{
108117
foreach (var sub in di.EnumerateDirectories())
109118
{
110-
if (!String.IsNullOrWhiteSpace(IgnorePrefix) && sub.Name.StartsWith(IgnorePrefix))
119+
if (!String.IsNullOrWhiteSpace(IgnorePrefix) && sub.Name.StartsWith(IgnorePrefix, StringComparison.OrdinalIgnoreCase))
111120
continue;
112121

113122
ReadAllValues(sub.FullName, sub.Name + KeyDelimiter, values);
@@ -116,7 +125,7 @@ private IDictionary<string, string> ReadAllValues(string root, string prefix, ID
116125

117126
foreach (var file in di.EnumerateFiles())
118127
{
119-
if (!String.IsNullOrWhiteSpace(IgnorePrefix) && file.Name.StartsWith(IgnorePrefix))
128+
if (!String.IsNullOrWhiteSpace(IgnorePrefix) && file.Name.StartsWith(IgnorePrefix, StringComparison.OrdinalIgnoreCase))
120129
continue;
121130

122131
string key = prefix + file.Name;

src/packages/ConfigurationBuilders.Azure.nupkg/Microsoft.Configuration.ConfigurationBuilders.Azure.nuproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
<NuGetContent Include="..\ConfigurationBuilders.Base.nupkg\shared\tools\Net471\pp\*.ps1">
4141
<Destination>tools\Net471\</Destination>
4242
</NuGetContent>
43+
<NuGetContent Include="README.txt">
44+
<Destination>README.txt</Destination>
45+
</NuGetContent>
4346
</ItemGroup>
4447
<Import Project="$(RepositoryRoot)Tools\NuGetProj.targets"/>
4548
</Project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
########################################################################################################################
2+
## ##
3+
## Microsoft.Configuration.ConfigurationBuilders.Azure ##
4+
## ##
5+
########################################################################################################################
6+
7+
WARNING: Double check config builder attributes in your configuraiton files. Some attributes may have been changed
8+
when upgrading this package.
9+
10+
If this is the first time you are installing this configuration builder into your
11+
project, feel free to click the 'X' on this tab to close and continue on.
12+
13+
However, if you are updating this package from a version earlier than 1.0.2... you may want to double check the
14+
declarations for your config builders, because we might have lost changes you made to the default declarations
15+
that were created during the original install.
16+
17+
The upgrade mechanism for nuget is actually just Uninstall/Install, and prior to 1.0.2, this package would delete
18+
the config builder declaration that it created upon install. Makes sense. But if you made any changes to that
19+
declaration without changing the name of it, we didn't bother to make note of that. So the 'install' phase of
20+
updating will declare the default config builder with the default parameters again. (If you did change the name
21+
to something other than the default - AzureKeyVault - then we did not delete your declaration when uninstalling.)
22+
23+
Starting in version 1.0.2, we have a way to stash old declarations aside and restore them upon install. Sorry for the
24+
inconvenience. There should be no such troubles with future upgrades.

src/packages/ConfigurationBuilders.Environment.nupkg/Microsoft.Configuration.ConfigurationBuilders.Environment.nuproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
<NuGetContent Include="..\ConfigurationBuilders.Base.nupkg\shared\tools\Net471\pp\*.ps1">
4141
<Destination>tools\Net471\</Destination>
4242
</NuGetContent>
43+
<NuGetContent Include="README.txt">
44+
<Destination>README.txt</Destination>
45+
</NuGetContent>
4346
</ItemGroup>
4447
<Import Project="$(RepositoryRoot)Tools\NuGetProj.targets"/>
4548
</Project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
########################################################################################################################
2+
## ##
3+
## Microsoft.Configuration.ConfigurationBuilders.Environment ##
4+
## ##
5+
########################################################################################################################
6+
7+
WARNING: Double check config builder attributes in your configuraiton files. Some attributes may have been changed
8+
when upgrading this package.
9+
10+
If this is the first time you are installing this configuration builder into your
11+
project, feel free to click the 'X' on this tab to close and continue on.
12+
13+
However, if you are updating this package from a version earlier than 1.0.2... you may want to double check the
14+
declarations for your config builders, because we might have lost changes you made to the default declarations
15+
that were created during the original install.
16+
17+
The upgrade mechanism for nuget is actually just Uninstall/Install, and prior to 1.0.2, this package would delete
18+
the config builder declaration that it created upon install. Makes sense. But if you made any changes to that
19+
declaration without changing the name of it, we didn't bother to make note of that. So the 'install' phase of
20+
updating will declare the default config builder with the default parameters again. (If you did change the name
21+
to something other than the default - Environment - then we did not delete your declaration when uninstalling.)
22+
23+
Starting in version 1.0.2, we have a way to stash old declarations aside and restore them upon install. Sorry for the
24+
inconvenience. There should be no such troubles with future upgrades.

src/packages/ConfigurationBuilders.Json.nupkg/Microsoft.Configuration.ConfigurationBuilders.Json.nuproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
<NuGetContent Include="..\ConfigurationBuilders.Base.nupkg\shared\tools\Net471\pp\*.ps1">
4141
<Destination>tools\Net471\</Destination>
4242
</NuGetContent>
43+
<NuGetContent Include="README.txt">
44+
<Destination>README.txt</Destination>
45+
</NuGetContent>
4346
</ItemGroup>
4447
<Import Project="$(RepositoryRoot)Tools\NuGetProj.targets"/>
4548
</Project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
########################################################################################################################
2+
## ##
3+
## Microsoft.Configuration.ConfigurationBuilders.Json ##
4+
## ##
5+
########################################################################################################################
6+
7+
WARNING: Double check config builder attributes in your configuraiton files. Some attributes may have been changed
8+
when upgrading this package.
9+
10+
If this is the first time you are installing this configuration builder into your
11+
project, feel free to click the 'X' on this tab to close and continue on.
12+
13+
However, if you are updating this package from a version earlier than 1.0.2... you may want to double check the
14+
declarations for your config builders, because we might have lost changes you made to the default declarations
15+
that were created during the original install.
16+
17+
The upgrade mechanism for nuget is actually just Uninstall/Install, and prior to 1.0.2, this package would delete
18+
the config builder declaration that it created upon install. Makes sense. But if you made any changes to that
19+
declaration without changing the name of it, we didn't bother to make note of that. So the 'install' phase of
20+
updating will declare the default config builder with the default parameters again. (If you did change the name
21+
to something other than the default - SimpleJson - then we did not delete your declaration when uninstalling.)
22+
23+
Starting in version 1.0.2, we have a way to stash old declarations aside and restore them upon install. Sorry for the
24+
inconvenience. There should be no such troubles with future upgrades.

src/packages/ConfigurationBuilders.UserSecrets.nupkg/Microsoft.Configuration.ConfigurationBuilders.UserSecrets.nuproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
<NuGetContent Include="..\ConfigurationBuilders.Base.nupkg\shared\tools\Net471\pp\*.ps1">
4141
<Destination>tools\Net471\</Destination>
4242
</NuGetContent>
43+
<NuGetContent Include="README.txt">
44+
<Destination>README.txt</Destination>
45+
</NuGetContent>
4346
</ItemGroup>
4447
<Import Project="$(RepositoryRoot)Tools\NuGetProj.targets"/>
4548
</Project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
########################################################################################################################
2+
## ##
3+
## Microsoft.Configuration.ConfigurationBuilders.UserSecrets ##
4+
## ##
5+
########################################################################################################################
6+
7+
WARNING: Double check config builder attributes in your configuraiton files. Some attributes may have been changed
8+
when upgrading this package.
9+
10+
If this is the first time you are installing this configuration builder into your
11+
project, feel free to click the 'X' on this tab to close and continue on.
12+
13+
However, if you are updating this package from a version earlier than 1.0.2... you may want to double check the
14+
declarations for your config builders, because we might have lost changes you made to the default declarations
15+
that were created during the original install.
16+
17+
The upgrade mechanism for nuget is actually just Uninstall/Install, and prior to 1.0.2, this package would delete
18+
the config builder declaration that it created upon install. Makes sense. But if you made any changes to that
19+
declaration without changing the name of it, we didn't bother to make note of that. So the 'install' phase of
20+
updating will declare the default config builder with the default parameters again. (If you did change the name
21+
to something other than the default - Secrets - then we did not delete your declaration when uninstalling.)
22+
23+
Starting in version 1.0.2, we have a way to stash old declarations aside and restore them upon install. Sorry for the
24+
inconvenience. There should be no such troubles with future upgrades.
25+
26+
27+
########################################################################################################################
28+
29+
30+
In addition to the upgrade trouble mentioned above, we have also made changes to the UserSecrets package specifically.
31+
We no longer create an empty secrets file, nor do we add a link under 'Properties' to the empty secrets file that we
32+
no longer create. And we do not install any .targets/.props files to magically read $(UserSecretsId) from project
33+
settings at compile-time.
34+
35+
Why? Because Visual Studio will handle these things for you. Similar to the ASP.Net Core User Secrets experience, VS
36+
will have a contextual "Manage User Secrets..." option for your Web Forms projects. Using that will install this
37+
package, create the empty secrets file, and open it for editing. So you will use that menu item instead of seeing
38+
a project item for secrets.
39+
40+
Also, the first and final word on UserSecretsId is what is set in the builder declaration. No more cumbersome
41+
attempts at using project properties in configuration at runtime. Now, what you see (in *.config) is what you get. :)

src/packages/packages.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@
6363
<None Include="ConfigurationBuilders.Json.nupkg\tools\Net471\Install.ps1" />
6464
<None Include="ConfigurationBuilders.Json.nupkg\tools\Net471\Uninstall.ps1" />
6565
</ItemGroup>
66+
<ItemGroup>
67+
<Content Include="ConfigurationBuilders.Azure.nupkg\README.txt" />
68+
<Content Include="ConfigurationBuilders.Environment.nupkg\README.txt" />
69+
<Content Include="ConfigurationBuilders.Json.nupkg\README.txt" />
70+
<Content Include="ConfigurationBuilders.UserSecrets.nupkg\README.txt" />
71+
</ItemGroup>
6672
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6773
<Target Name="Build">
6874
<MSBuild Projects="@(NuGetProject)" Targets="Build" />

0 commit comments

Comments
 (0)