Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates for loading settings from only the current assembly's attributes #146

Merged
merged 6 commits into from Dec 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions AutoDI.Build/ProcessAssemblyTask.Settings.cs
Expand Up @@ -4,12 +4,12 @@ namespace AutoDI.Build
{
public partial class ProcessAssemblyTask
{
private Settings LoadSettings(TypeResolver typeResolver)
private Settings LoadSettings()
{
Settings settings;
try
{
settings = Settings.Load(typeResolver);
settings = Settings.Load(ModuleDefinition);
}
catch (SettingsParseException e)
{
Expand Down
2 changes: 1 addition & 1 deletion AutoDI.Build/ProcessAssemblyTask.cs
Expand Up @@ -27,7 +27,7 @@ protected override bool WeaveAssembly()

var typeResolver = new TypeResolver(ModuleDefinition, ModuleDefinition.AssemblyResolver, Logger);

Settings settings = LoadSettings(typeResolver);
Settings settings = LoadSettings();
if (settings == null) return false;

ICollection<TypeDefinition> allTypes = typeResolver.GetAllTypes(settings, out AssemblyDefinition autoDIAssembly);
Expand Down
113 changes: 11 additions & 102 deletions AutoDI.Build/Settings.cs
Expand Up @@ -8,11 +8,10 @@ namespace AutoDI.Build
{
internal class Settings
{
public static Settings Load(TypeResolver typeResolver)
public static Settings Load(ModuleDefinition module)
{
var settings = new Settings();
foreach (CustomAttribute attribute in typeResolver.GetAllModules()
.SelectMany(m => m.Assembly.CustomAttributes))
foreach (CustomAttribute attribute in module.Assembly.CustomAttributes)
{
if (attribute.AttributeType.IsType<SettingsAttribute>())
{
Expand All @@ -23,27 +22,26 @@ public static Settings Load(TypeResolver typeResolver)
switch (property.Name)
{
case nameof(SettingsAttribute.AutoInit):
settings.AutoInit = (bool) property.Argument.Value;
settings.AutoInit = (bool)property.Argument.Value;
break;
case nameof(SettingsAttribute.Behavior):
settings.Behavior = (Behaviors) property.Argument.Value;
settings.Behavior = (Behaviors)property.Argument.Value;
break;
case nameof(SettingsAttribute.DebugLogLevel):
settings.DebugLogLevel = (DebugLogLevel) property.Argument.Value;
settings.DebugLogLevel = (DebugLogLevel)property.Argument.Value;
break;
case nameof(SettingsAttribute.GenerateRegistrations):
settings.GenerateRegistrations = (bool) property.Argument.Value;
settings.GenerateRegistrations = (bool)property.Argument.Value;
break;
case nameof(SettingsAttribute.DebugCodeGeneration):
settings.DebugCodeGeneration = (CodeLanguage) property.Argument.Value;
settings.DebugCodeGeneration = (CodeLanguage)property.Argument.Value;
break;
}
}
}
}
else if (attribute.AttributeType.IsType<MapAttribute>())

{
{
Lifetime? lifetime = null;
string targetTypePattern = null;
string sourceTypePattern = null;
Expand Down Expand Up @@ -89,7 +87,7 @@ void SetFromName(string name, CustomAttributeArgument argument)
case nameof(MapAttribute.SourceTypePattern):
if (argument.Type.IsType<string>())
{
sourceTypePattern = (string) argument.Value;
sourceTypePattern = (string)argument.Value;
}
else if (argument.Type.IsType<Type>())
{
Expand All @@ -99,7 +97,7 @@ void SetFromName(string name, CustomAttributeArgument argument)
case nameof(MapAttribute.TargetTypePattern):
if (argument.Type.IsType<string>())
{
targetTypePattern = (string) argument.Value;
targetTypePattern = (string)argument.Value;
}
else if (argument.Type.IsType<Type>())
{
Expand All @@ -109,7 +107,7 @@ void SetFromName(string name, CustomAttributeArgument argument)
case nameof(MapAttribute.Force):
if (argument.Type.IsType<bool>())
{
force = (bool) argument.Value;
force = (bool)argument.Value;
}
break;
}
Expand Down Expand Up @@ -287,94 +285,5 @@ public override string ToString()

return sb.ToString();
}

//public static Settings Parse(Settings settings, XElement rootElement)
//{
// if (rootElement == null) return settings;
//
// ParseAttributes(rootElement,
// Attrib.OptionalBool(nameof(AutoInit), x => settings.AutoInit = x),
// Attrib.OptionalBool(nameof(GenerateRegistrations), x => settings.GenerateRegistrations = x),
// Attrib.OptionalEnum<DebugLogLevel>(nameof(DebugLogLevel), x => settings.DebugLogLevel = x),
// Attrib.OptionalBool(nameof(DebugExceptions), x => settings.DebugExceptions = x),
// Attrib.Create(nameof(Behavior), x => settings.Behavior = x, (string x, out Behaviors behavior) =>
// {
// behavior = Behaviors.None;
//
// if (string.IsNullOrWhiteSpace(x))
// return false;
//
// foreach (string value in x.Split(','))
// {
// if (Enum.TryParse(value, out Behaviors @enum))
// behavior |= @enum;
// else
// return false;
// }
// return true;
// }, false),
// Attrib.OptionalEnum<CodeLanguage>(nameof(DebugCodeGeneration), x => settings.DebugCodeGeneration = x));
//
// foreach (XElement element in rootElement.DescendantNodes().OfType<XElement>())
// {
// if (element.Name.LocalName.Equals("Assembly", StringComparison.OrdinalIgnoreCase))
// {
// string assemblyName = "";
// ParseAttributes(element, Attrib.RequiredString("Name", x => assemblyName = x));
// settings.Assemblies.Add(new MatchAssembly(assemblyName));
// }
// else if (element.Name.LocalName.Equals("Type", StringComparison.OrdinalIgnoreCase))
// {
// string typePattern = "";
// Lifetime lifetime = DefaultLifetime;
// ParseAttributes(element, Attrib.RequiredString("Name", x => typePattern = x),
// Attrib.RequiredEnum<Lifetime>("Lifetime", x => lifetime = x));
// settings.Types.Add(new MatchType(typePattern, lifetime));
// }
// else if (element.Name.LocalName.Equals("Map", StringComparison.OrdinalIgnoreCase))
// {
// string from = "";//GetRequiredString(element, "From");
// string to = "";//GetRequiredString(element, "To");
// bool force = false;
// Lifetime? lifetime = null;
// ParseAttributes(element, Attrib.RequiredString("From", x => from = x),
// Attrib.RequiredString("To", x => to = x),
// Attrib.OptionalBool("Force", x => force = x),
// Attrib.OptionalEnum<Lifetime>("Lifetime", x => lifetime = x));
//
// settings.Maps.Add(new Map(from, to, force, lifetime));
// }
// else
// {
// throw new SettingsParseException($"'{element.Name.LocalName}' is not a valid child node of AutoDI");
// }
// }
//
// return settings;
//
// void ParseAttributes(XElement element, params IAttribute[] attributes)
// {
// Dictionary<string, IAttribute> attributesByName =
// attributes.ToDictionary(x => x.Name, StringComparer.OrdinalIgnoreCase);
//
// foreach (XAttribute attribute in element.Attributes())
// {
// if (attributesByName.TryGetValue(attribute.Name.LocalName, out IAttribute attrib))
// {
// attrib.Set(attribute.Value);
// attributesByName.Remove(attribute.Name.LocalName);
// }
// else
// {
// throw new SettingsParseException($"'{attribute.Name.LocalName}' is not a valid attribute for {element.Name.LocalName}");
// }
// }
//
// foreach (IAttribute attribute in attributesByName.Values.Where(x => x.IsRequired))
// {
// throw new SettingsParseException($"'{element.Name.LocalName}' requires a value for '{attribute.Name}'");
// }
// }
//}
}
}
4 changes: 2 additions & 2 deletions AutoDI.Build/TypeResolver.cs
Expand Up @@ -36,9 +36,9 @@ public ICollection<TypeDefinition> GetAllTypes(Settings settings, out AssemblyDe
continue;
}
bool isMainModule = ReferenceEquals(module, _module);
bool useAutoDiAssebmlies = settings.Behavior.HasFlag(Behaviors.IncludeDependentAutoDIAssemblies);
bool useAutoDiAssemblies = settings.Behavior.HasFlag(Behaviors.IncludeDependentAutoDIAssemblies);
bool matchesAssembly = settings.Assemblies.Any(a => a.Matches(module.Assembly));
if (isMainModule || useAutoDiAssebmlies || matchesAssembly)
if (isMainModule || useAutoDiAssemblies || matchesAssembly)
{
//Check if it references AutoDI. If it doesn't we will skip
//We also always process the main module since the weaver was directly added to it
Expand Down
17 changes: 8 additions & 9 deletions AutoDI.Generator/GeneratorTask.cs
Expand Up @@ -28,20 +28,19 @@ public ITaskItem[] GeneratedCodeFiles

public override bool Execute()
{
var assemblyResolver = new DefaultAssemblyResolver();
assemblyResolver.AddSearchDirectory(Path.GetDirectoryName(OutputPath));

var compiledAssembly = AssemblyDefinition.ReadAssembly(OutputPath);

var settings = Settings.Load(compiledAssembly.MainModule);

var logger = new TaskLogger(this);
if (settings.GenerateRegistrations)
{
var assemblyResolver = new DefaultAssemblyResolver();
assemblyResolver.AddSearchDirectory(Path.GetDirectoryName(OutputPath));

var typeResolver = new TypeResolver(compiledAssembly.MainModule, assemblyResolver, logger);
var logger = new TaskLogger(this) { DebugLogLevel = settings.DebugLogLevel };

var settings = Settings.Load(typeResolver);
logger.DebugLogLevel = settings.DebugLogLevel;
var typeResolver = new TypeResolver(compiledAssembly.MainModule, assemblyResolver, logger);

if (settings.GenerateRegistrations)
{
ICollection<TypeDefinition> allTypes =
typeResolver.GetAllTypes(settings, out AssemblyDefinition _);
Mapping mapping = Mapping.GetMapping(settings, allTypes, logger);
Expand Down