Skip to content

Commit

Permalink
Fix provider locating
Browse files Browse the repository at this point in the history
  • Loading branch information
carlubian committed May 27, 2023
1 parent c6c8357 commit 91634ef
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 56 deletions.
31 changes: 16 additions & 15 deletions ConfigAdapter.Core/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,20 @@ public static IConfigurationProvider Migrate(IConfigurationProvider source, stri
return target;
}

//static Configuration()
//{
// var assemblies = Assembly.GetExecutingAssembly().GetReferencedAssemblies();

// foreach (var assemblyName in assemblies)
// {
// var assembly = Assembly.Load(assemblyName);
// var providers = assembly.GetTypes().Where(t => t.CustomAttributes.Any(a => a.AttributeType == typeof(ConfigurationProviderAttribute)));
// foreach (var provider in providers)
// {
// var initializer = provider.GetRuntimeMethods().FirstOrDefault(m => m.CustomAttributes.Any(a => a.AttributeType == typeof(ConfigurationInitializerAttribute)));
// initializer?.Invoke(null, null);
// }
// }
//}
static Configuration()
{
var dlls = Directory.EnumerateFiles(Directory.GetCurrentDirectory(), "ConfigAdapter*.dll");

foreach (var dll in dlls )
{
var assembly = Assembly.LoadFile(dll);

var providers = assembly.GetTypes().Where(t => t.CustomAttributes.Any(a => a.AttributeType == typeof(ConfigurationProviderAttribute)));
foreach (var provider in providers)
{
var initializer = provider.GetRuntimeMethods().FirstOrDefault(m => m.CustomAttributes.Any(a => a.AttributeType == typeof(ConfigurationInitializerAttribute)));
initializer?.Invoke(null, null);
}
}
}
}
8 changes: 0 additions & 8 deletions ConfigAdapter.Test/TestIni.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class TestIni
[TestMethod]
public void TestParseFile()
{
IniConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.ini");

var globalSetting1 = tree.Retrieve("GlobalSetting1");
Expand All @@ -28,7 +27,6 @@ public void TestParseFile()
[TestMethod]
public void TestParseSection()
{
IniConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.ini");

tree.Enumerate("Section 1").Should().NotBeNull()
Expand Down Expand Up @@ -57,7 +55,6 @@ public void TestParseSection()
[TestMethod]
public void TestParseArray()
{
IniConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.ini");

var localSetting = tree.Retrieve("Section 1:Section 1.3:LocalSetting1.3");
Expand All @@ -71,7 +68,6 @@ public void TestParseArray()
[TestMethod]
public void TestExtensionMethods()
{
IniConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.ini");

// String retrieved as string
Expand Down Expand Up @@ -99,7 +95,6 @@ public void TestExtensionMethods()
[TestMethod]
public void TestNonexistantKeys()
{
IniConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.ini");

Action act = () => tree.Enumerate("NotExists:Also missing");
Expand All @@ -118,7 +113,6 @@ public void TestNonexistantKeys()
[TestMethod]
public void TestExceptionEvents()
{
IniConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.ini");

var localSetting = tree.Retrieve("Section 1:LocalSetting1");
Expand All @@ -132,7 +126,6 @@ public void TestWritingFile()
{
// Write to new file
File.Copy("TestFile.ini", "TestFile2.ini", true);
IniConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile2.ini");

tree.Store("Global Setting 3", "Global Value 3");
Expand Down Expand Up @@ -182,7 +175,6 @@ public void TestWritingFile()
[TestMethod]
public void TestDeleteStuff()
{
IniConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.ini");

// Delete a global setting
Expand Down
8 changes: 0 additions & 8 deletions ConfigAdapter.Test/TestJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class TestJson
[TestMethod]
public void TestParseFile()
{
JsonConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.json");

var globalSetting1 = tree.Retrieve("GlobalSetting1");
Expand All @@ -28,7 +27,6 @@ public void TestParseFile()
[TestMethod]
public void TestParseSection()
{
JsonConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.json");

tree.Enumerate("Section 1").Should().NotBeNull()
Expand Down Expand Up @@ -57,7 +55,6 @@ public void TestParseSection()
[TestMethod]
public void TestParseArray()
{
JsonConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.json");

var localSetting = tree.Retrieve("Section 1:Section 1.3:LocalSetting1.3");
Expand All @@ -71,7 +68,6 @@ public void TestParseArray()
[TestMethod]
public void TestExtensionMethods()
{
JsonConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.json");

// String retrieved as string
Expand Down Expand Up @@ -99,7 +95,6 @@ public void TestExtensionMethods()
[TestMethod]
public void TestNonexistantKeys()
{
JsonConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.json");

Action act = () => tree.Enumerate("NotExists:Also missing");
Expand All @@ -118,7 +113,6 @@ public void TestNonexistantKeys()
[TestMethod]
public void TestExceptionEvents()
{
JsonConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.json");

var localSetting = tree.Retrieve("Section 1:LocalSetting1");
Expand All @@ -132,7 +126,6 @@ public void TestWritingFile()
{
// Write to new file
File.Copy("TestFile.json", "TestFile2.json", true);
JsonConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile2.json");

tree.Store("Global Setting 3", "Global Value 3");
Expand Down Expand Up @@ -182,7 +175,6 @@ public void TestWritingFile()
[TestMethod]
public void TestDeleteStuff()
{
JsonConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.json");

// Delete a global setting
Expand Down
9 changes: 0 additions & 9 deletions ConfigAdapter.Test/TestMigrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ public class TestMigrations
[TestMethod]
public void MigrateXmlToYaml()
{
XmlConfigurationProvider.RegisterProvider();
YamlConfigurationProvider.RegisterProvider();

var tree = Configuration.From("TestFile.xml");
var migration = Configuration.Migrate(tree, "Migrated.yaml");

Expand Down Expand Up @@ -57,9 +54,6 @@ public void MigrateXmlToYaml()
[TestMethod]
public void MigrateJsonToXml()
{
XmlConfigurationProvider.RegisterProvider();
JsonConfigurationProvider.RegisterProvider();

var tree = Configuration.From("TestFile.json");
var migration = Configuration.Migrate(tree, "Migrated.xml");

Expand Down Expand Up @@ -103,9 +97,6 @@ public void MigrateJsonToXml()
[TestMethod]
public void MigrateYamlToJson()
{
JsonConfigurationProvider.RegisterProvider();
YamlConfigurationProvider.RegisterProvider();

var tree = Configuration.From("TestFile.yaml");
var migration = Configuration.Migrate(tree, "Migrated.json");

Expand Down
8 changes: 0 additions & 8 deletions ConfigAdapter.Test/TestXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class TestXml
[TestMethod]
public void TestParseFile()
{
XmlConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.xml");

var globalSetting1 = tree.Retrieve("GlobalSetting1");
Expand All @@ -28,7 +27,6 @@ public void TestParseFile()
[TestMethod]
public void TestParseSection()
{
XmlConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.xml");

tree.Enumerate("Section 1").Should().NotBeNull()
Expand Down Expand Up @@ -57,7 +55,6 @@ public void TestParseSection()
[TestMethod]
public void TestParseArray()
{
XmlConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.xml");

var localSetting = tree.Retrieve("Section 1:Section 1.3:LocalSetting1.3");
Expand All @@ -71,7 +68,6 @@ public void TestParseArray()
[TestMethod]
public void TestExtensionMethods()
{
XmlConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.xml");

// String retrieved as string
Expand Down Expand Up @@ -99,7 +95,6 @@ public void TestExtensionMethods()
[TestMethod]
public void TestNonexistantKeys()
{
XmlConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.xml");

Action act = () => tree.Enumerate("NotExists:Also missing");
Expand All @@ -118,7 +113,6 @@ public void TestNonexistantKeys()
[TestMethod]
public void TestExceptionEvents()
{
XmlConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.xml");

var localSetting = tree.Retrieve("Section 1:LocalSetting1");
Expand All @@ -132,7 +126,6 @@ public void TestWritingFile()
{
// Write to new file
File.Copy("TestFile.xml", "TestFile2.xml", true);
XmlConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile2.xml");

tree.Store("Global Setting 3", "Global Value 3");
Expand Down Expand Up @@ -182,7 +175,6 @@ public void TestWritingFile()
[TestMethod]
public void TestDeleteStuff()
{
XmlConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.xml");

// Delete a global setting
Expand Down
8 changes: 0 additions & 8 deletions ConfigAdapter.Test/TestYaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class TestYaml
[TestMethod]
public void TestParseFile()
{
YamlConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.yaml");

var globalSetting1 = tree.Retrieve("GlobalSetting1");
Expand All @@ -28,7 +27,6 @@ public void TestParseFile()
[TestMethod]
public void TestParseSection()
{
YamlConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.yaml");

tree.Enumerate("Section 1").Should().NotBeNull()
Expand Down Expand Up @@ -57,7 +55,6 @@ public void TestParseSection()
[TestMethod]
public void TestParseArray()
{
YamlConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.yaml");

var localSetting = tree.Retrieve("Section 1:Section 1.3:LocalSetting 1.3");
Expand All @@ -71,7 +68,6 @@ public void TestParseArray()
[TestMethod]
public void TestExtensionMethods()
{
YamlConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.yaml");

// String retrieved as string
Expand Down Expand Up @@ -99,7 +95,6 @@ public void TestExtensionMethods()
[TestMethod]
public void TestNonexistantKeys()
{
YamlConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.yaml");

Action act = () => tree.Enumerate("NotExists:Also missing");
Expand All @@ -118,7 +113,6 @@ public void TestNonexistantKeys()
[TestMethod]
public void TestExceptionEvents()
{
YamlConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.yaml");

var localSetting = tree.Retrieve("Section 1:LocalSetting1");
Expand All @@ -132,7 +126,6 @@ public void TestWritingFile()
{
// Write to new file
File.Copy("TestFile.yaml", "TestFile2.yaml", true);
YamlConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile2.yaml");

tree.Store("Global Setting 3", "Global Value 3");
Expand Down Expand Up @@ -182,7 +175,6 @@ public void TestWritingFile()
[TestMethod]
public void TestDeleteStuff()
{
YamlConfigurationProvider.RegisterProvider();
var tree = Configuration.From("TestFile.yaml");

// Delete a global setting
Expand Down

0 comments on commit 91634ef

Please sign in to comment.