Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit 7efb7b2

Browse files
committed
Add BasePath to IConfigurationSource Root
Decoupe the configuration from DependencyInjection. To resolve a file based configuration, the user should either give a full path to the file or give a directory path in which the configuration files will be looked for. Commonly the directory is the base application directory
1 parent 3f1249f commit 7efb7b2

File tree

22 files changed

+225
-119
lines changed

22 files changed

+225
-119
lines changed

Configuration.sln

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.Configu
2121
EndProject
2222
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.ConfigurationModel.FunctionalTests", "test\Microsoft.Framework.ConfigurationModel.FunctionalTests\Microsoft.Framework.ConfigurationModel.FunctionalTests.xproj", "{EAC77F15-F12E-496B-9184-1B1DA89BFFE9}"
2323
EndProject
24-
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.ConfigurationModel.Shared", "src\Microsoft.Framework.ConfigurationModel.Shared\Microsoft.Framework.ConfigurationModel.Shared.xproj", "{8967162D-4966-40A7-9970-395A206732AC}"
25-
EndProject
2624
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.ConfigurationModel.Interfaces", "src\Microsoft.Framework.ConfigurationModel.Interfaces\Microsoft.Framework.ConfigurationModel.Interfaces.xproj", "{3F1CB08E-9FBD-4CAE-A78A-4AC43F24FC49}"
2725
EndProject
2826
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.Framework.ConfigurationModel.Test.Common", "test\Microsoft.Framework.ConfigurationModel.Test.Common\Microsoft.Framework.ConfigurationModel.Test.Common.xproj", "{29C120E5-F682-4BFB-826B-040A594802CA}"
@@ -107,16 +105,6 @@ Global
107105
{EAC77F15-F12E-496B-9184-1B1DA89BFFE9}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
108106
{EAC77F15-F12E-496B-9184-1B1DA89BFFE9}.Release|Mixed Platforms.Build.0 = Release|Any CPU
109107
{EAC77F15-F12E-496B-9184-1B1DA89BFFE9}.Release|x86.ActiveCfg = Release|Any CPU
110-
{8967162D-4966-40A7-9970-395A206732AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
111-
{8967162D-4966-40A7-9970-395A206732AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
112-
{8967162D-4966-40A7-9970-395A206732AC}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
113-
{8967162D-4966-40A7-9970-395A206732AC}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
114-
{8967162D-4966-40A7-9970-395A206732AC}.Debug|x86.ActiveCfg = Debug|Any CPU
115-
{8967162D-4966-40A7-9970-395A206732AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
116-
{8967162D-4966-40A7-9970-395A206732AC}.Release|Any CPU.Build.0 = Release|Any CPU
117-
{8967162D-4966-40A7-9970-395A206732AC}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
118-
{8967162D-4966-40A7-9970-395A206732AC}.Release|Mixed Platforms.Build.0 = Release|Any CPU
119-
{8967162D-4966-40A7-9970-395A206732AC}.Release|x86.ActiveCfg = Release|Any CPU
120108
{3F1CB08E-9FBD-4CAE-A78A-4AC43F24FC49}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
121109
{3F1CB08E-9FBD-4CAE-A78A-4AC43F24FC49}.Debug|Any CPU.Build.0 = Debug|Any CPU
122110
{3F1CB08E-9FBD-4CAE-A78A-4AC43F24FC49}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
@@ -153,7 +141,6 @@ Global
153141
{8777C77E-CA2A-42C1-90CD-2EA9CBF28937} = {B54371FF-B920-46C8-8D55-6B19DBB43EBF}
154142
{62BD48B5-BB0C-4C2C-9C4B-04CF75CDCCF1} = {F141E2D0-F9B8-4ADB-A19A-7B6FF4CA19A1}
155143
{EAC77F15-F12E-496B-9184-1B1DA89BFFE9} = {B54371FF-B920-46C8-8D55-6B19DBB43EBF}
156-
{8967162D-4966-40A7-9970-395A206732AC} = {F141E2D0-F9B8-4ADB-A19A-7B6FF4CA19A1}
157144
{3F1CB08E-9FBD-4CAE-A78A-4AC43F24FC49} = {F141E2D0-F9B8-4ADB-A19A-7B6FF4CA19A1}
158145
{29C120E5-F682-4BFB-826B-040A594802CA} = {B54371FF-B920-46C8-8D55-6B19DBB43EBF}
159146
EndGlobalSection

src/Microsoft.Framework.ConfigurationModel.Interfaces/IConfigurationSourceRoot.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace Microsoft.Framework.ConfigurationModel
77
{
88
public interface IConfigurationSourceRoot : IConfiguration
99
{
10+
string BasePath { get; }
11+
1012
IEnumerable<IConfigurationSource> Sources { get; }
1113

1214
IConfigurationSourceRoot Add(IConfigurationSource configurationSource);

src/Microsoft.Framework.ConfigurationModel.Json/JsonConfigurationExtension.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.IO;
6+
using Microsoft.Framework.ConfigurationModel.Helper;
67
using Microsoft.Framework.ConfigurationModel.Json;
78

89
namespace Microsoft.Framework.ConfigurationModel
@@ -21,7 +22,7 @@ public static IConfigurationSourceRoot AddJsonFile(this IConfigurationSourceRoot
2122
throw new ArgumentException(Resources.Error_InvalidFilePath, "path");
2223
}
2324

24-
var fullPath = PathResolver.ResolveAppRelativePath(path);
25+
var fullPath = ConfigurationHelper.ResolveConfigurationFilePath(configuration, path);
2526

2627
if (!optional && !File.Exists(fullPath))
2728
{

src/Microsoft.Framework.ConfigurationModel.Json/JsonConfigurationSource.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Collections.Generic;
66
using System.IO;
77
using Newtonsoft.Json;
8-
98
using Resources = Microsoft.Framework.ConfigurationModel.Json.Resources;
109

1110
namespace Microsoft.Framework.ConfigurationModel
@@ -25,7 +24,7 @@ public JsonConfigurationSource(string path, bool optional)
2524
}
2625

2726
Optional = optional;
28-
Path = PathResolver.ResolveAppRelativePath(path);
27+
Path = path;
2928
}
3029

3130
public bool Optional { get; private set; }

src/Microsoft.Framework.ConfigurationModel.Json/project.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
"description": "JSON configuration source for the ConfigurationModel package.",
44
"dependencies": {
55
"Newtonsoft.Json": "6.0.6",
6-
"Microsoft.Framework.ConfigurationModel": "1.0.0-*",
7-
"Microsoft.Framework.ConfigurationModel.Shared": { "version": "1.0.0-*", "type": "build" }
6+
"Microsoft.Framework.ConfigurationModel": "1.0.0-*"
87
},
98
"frameworks": {
109
"net45": { },

src/Microsoft.Framework.ConfigurationModel.Shared/Microsoft.Framework.ConfigurationModel.Shared.xproj

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/Microsoft.Framework.ConfigurationModel.Shared/PathResolver.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/Microsoft.Framework.ConfigurationModel.Shared/project.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/Microsoft.Framework.ConfigurationModel.Xml/XmlConfigurationExtension.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
34
using System;
45
using System.IO;
6+
using Microsoft.Framework.ConfigurationModel.Helper;
57
using Microsoft.Framework.ConfigurationModel.Xml;
68

79
namespace Microsoft.Framework.ConfigurationModel
@@ -20,7 +22,7 @@ public static IConfigurationSourceRoot AddXmlFile(this IConfigurationSourceRoot
2022
throw new ArgumentException(Resources.Error_InvalidFilePath, "path");
2123
}
2224

23-
var fullPath = PathResolver.ResolveAppRelativePath(path);
25+
var fullPath = ConfigurationHelper.ResolveConfigurationFilePath(configuration, path);
2426

2527
if (!optional && !File.Exists(fullPath))
2628
{

src/Microsoft.Framework.ConfigurationModel.Xml/XmlConfigurationSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ internal XmlConfigurationSource(string path, XmlDocumentDecryptor xmlDocumentDec
4040
}
4141

4242
Optional = optional;
43-
Path = PathResolver.ResolveAppRelativePath(path);
43+
Path = path;
4444

4545
_xmlDocumentDecryptor = xmlDocumentDecryptor ?? XmlDocumentDecryptor.Instance;
4646
}

src/Microsoft.Framework.ConfigurationModel.Xml/project.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
"version": "1.0.0-*",
33
"description": "XML configuration source for the ConfigurationModel package.",
44
"dependencies": {
5-
"Microsoft.Framework.ConfigurationModel": "1.0.0-*",
6-
"Microsoft.Framework.ConfigurationModel.Shared": { "version": "1.0.0-*", "type": "build" }
5+
"Microsoft.Framework.ConfigurationModel": "1.0.0-*"
76
},
87
"frameworks": {
98
"net45": {

src/Microsoft.Framework.ConfigurationModel/Configuration.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
using Microsoft.Framework.ConfigurationModel.Internal;
54
using System;
6-
using System.Collections;
75
using System.Collections.Generic;
86
using System.Linq;
7+
using Microsoft.Framework.ConfigurationModel.Internal;
98

109
namespace Microsoft.Framework.ConfigurationModel
1110
{
@@ -14,6 +13,11 @@ public class Configuration : IConfiguration, IConfigurationSourceRoot
1413
private readonly IList<IConfigurationSource> _sources = new List<IConfigurationSource>();
1514

1615
public Configuration(params IConfigurationSource[] sources)
16+
: this(null, sources)
17+
{
18+
}
19+
20+
public Configuration(string basePath, params IConfigurationSource[] sources)
1721
{
1822
if (sources != null)
1923
{
@@ -22,6 +26,8 @@ public Configuration(params IConfigurationSource[] sources)
2226
Add(singleSource);
2327
}
2428
}
29+
30+
BasePath = basePath;
2531
}
2632

2733
public string this[string key]
@@ -44,6 +50,11 @@ public IEnumerable<IConfigurationSource> Sources
4450
}
4551
}
4652

53+
public string BasePath
54+
{
55+
get;
56+
}
57+
4758
public string Get(string key)
4859
{
4960
if (key == null) throw new ArgumentNullException("key");

src/Microsoft.Framework.ConfigurationModel/ConfigurationExtensions.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
7+
using Microsoft.Framework.ConfigurationModel.Helper;
78

89
namespace Microsoft.Framework.ConfigurationModel
910
{
@@ -16,7 +17,6 @@ public static T Get<T>(this IConfiguration configuration, string key)
1617
}
1718
#endif
1819

19-
2020
#if NET45 || DNX451 || DNXCORE50
2121
public static IConfigurationSourceRoot AddIniFile(this IConfigurationSourceRoot configuration, string path)
2222
{
@@ -30,15 +30,14 @@ public static IConfigurationSourceRoot AddIniFile(this IConfigurationSourceRoot
3030
throw new ArgumentException(Resources.Error_InvalidFilePath, "path");
3131
}
3232

33-
var fullPath = PathResolver.ResolveAppRelativePath(path);
33+
var fullPath = ConfigurationHelper.ResolveConfigurationFilePath(configuration, path);
3434

3535
if (!optional && !File.Exists(fullPath))
3636
{
3737
throw new FileNotFoundException(Resources.Error_FileNotFound, fullPath);
3838
}
3939

4040
configuration.Add(new IniFileConfigurationSource(path, optional: optional));
41-
4241
return configuration;
4342
}
4443
#endif
@@ -48,7 +47,7 @@ public static IConfigurationSourceRoot AddCommandLine(this IConfigurationSourceR
4847
configuration.Add(new CommandLineConfigurationSource(args));
4948
return configuration;
5049
}
51-
50+
5251
public static IConfigurationSourceRoot AddCommandLine(this IConfigurationSourceRoot configuration, string[] args, IDictionary<string, string> switchMappings)
5352
{
5453
configuration.Add(new CommandLineConfigurationSource(args, switchMappings));
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.IO;
6+
7+
namespace Microsoft.Framework.ConfigurationModel.Helper
8+
{
9+
public static class ConfigurationHelper
10+
{
11+
public static string ResolveConfigurationFilePath(IConfigurationSourceRoot configuration, string path)
12+
{
13+
if (!Path.IsPathRooted(path))
14+
{
15+
if (configuration.BasePath == null)
16+
{
17+
throw new InvalidOperationException(Resources.FormatError_MissingBasePath(
18+
path,
19+
typeof(IConfigurationSourceRoot).Name,
20+
nameof(configuration.BasePath)));
21+
}
22+
else
23+
{
24+
path = Path.Combine(configuration.BasePath, path);
25+
}
26+
}
27+
28+
return path;
29+
}
30+
}
31+
}

src/Microsoft.Framework.ConfigurationModel/Properties/Resources.Designer.cs

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Microsoft.Framework.ConfigurationModel/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@
138138
<data name="Error_KeyIsDuplicated" xml:space="preserve">
139139
<value>A duplicate key '{0}' was found.</value>
140140
</data>
141+
<data name="Error_MissingBasePath" xml:space="preserve">
142+
<value>Unable to resolve path '{0}'; construct this {1} with a non-null {2}.</value>
143+
</data>
141144
<data name="Error_NoCommitableSource" xml:space="preserve">
142145
<value>No registered configuration source is capable of committing changes.</value>
143146
</data>

src/Microsoft.Framework.ConfigurationModel/Sources/IniFileConfigurationSource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public IniFileConfigurationSource(string path, bool optional)
4646
}
4747

4848
Optional = optional;
49-
Path = PathResolver.ResolveAppRelativePath(path);
49+
Path = path;
5050
}
5151

5252
public bool Optional { get; private set; }

test/Microsoft.Framework.ConfigurationModel.Json.Test/JsonConfigurationSourceTest.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,7 @@ public void JsonConfiguration_Throws_On_Missing_Configuration_File()
146146
}
147147
catch (FileNotFoundException exception)
148148
{
149-
Assert.Equal(
150-
string.Format(Resources.Error_FileNotFound,
151-
Path.Combine(Directory.GetCurrentDirectory(), "NotExistingConfig.json")),
152-
exception.Message);
149+
Assert.Equal(Resources.FormatError_FileNotFound("NotExistingConfig.json"), exception.Message);
153150
throw;
154151
}
155152
});

0 commit comments

Comments
 (0)