Skip to content

Commit

Permalink
fix MapsterMapper#306 add types from scanning assembly
Browse files Browse the repository at this point in the history
  • Loading branch information
chaowlert committed Feb 24, 2021
1 parent 510d252 commit c21b773
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"mapster.tool": {
"version": "7.1.3",
"version": "8.0.0",
"commands": [
"dotnet-mapster"
]
Expand Down
2 changes: 1 addition & 1 deletion src/Mapster.Core/Mapster.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<AssemblyOriginatorKeyFile>Mapster.Core.snk</AssemblyOriginatorKeyFile>
<PackageIcon>icon.png</PackageIcon>
<PackageIconUrl>https://cloud.githubusercontent.com/assets/5763993/26522718/d16f3e42-4330-11e7-9b78-f8c7402624e7.png</PackageIconUrl>
<Version>1.1.3</Version>
<Version>1.1.5</Version>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<RootNamespace>Mapster</RootNamespace>
Expand Down
2 changes: 1 addition & 1 deletion src/Mapster.Tool/Mapster.Tool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>Mapster.Tool.snk</AssemblyOriginatorKeyFile>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>7.1.4</Version>
<Version>8.0.0</Version>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Copyright>Copyright (c) 2020 Chaowlert Chaisrichalermpol</Copyright>
<PackageIcon>icon.png</PackageIcon>
Expand Down
32 changes: 25 additions & 7 deletions src/Mapster.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ static void Main(string[] args)

private static void WriteFile(string code, string path)
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
var dir = Path.GetDirectoryName(path);
if (dir != null)
Directory.CreateDirectory(dir);
if (File.Exists(path))
{
var old = File.ReadAllText(path);
Expand Down Expand Up @@ -120,7 +122,15 @@ private static void GenerateModels(ModelOptions opt)
var codeGenConfig = new CodeGenerationConfig();
codeGenConfig.Scan(assembly);

foreach (var type in assembly.GetTypes())
var types = assembly.GetTypes().ToHashSet();
foreach (var builder in codeGenConfig.AdaptAttributeBuilders)
{
foreach (var setting in builder.TypeSettings)
{
types.Add(setting.Key);
}
}
foreach (var type in types)
{
var builders = type.GetAdaptAttributeBuilders(codeGenConfig)
.Where(it => !string.IsNullOrEmpty(it.Attribute.Name) && it.Attribute.Name != "[name]")
Expand Down Expand Up @@ -261,7 +271,7 @@ private static Type GetPropertyType(MemberInfo member, Type propType, Type attrT
return mockType;
}

private static Type? GetFromType(Type type, BaseAdaptAttribute attr, Type[] types)
private static Type? GetFromType(Type type, BaseAdaptAttribute attr, HashSet<Type> types)
{
if (!(attr is AdaptFromAttribute) && !(attr is AdaptTwoWaysAttribute))
return null;
Expand All @@ -270,13 +280,13 @@ private static Type GetPropertyType(MemberInfo member, Type propType, Type attrT
if (fromType == null && attr.Name != null)
{
var name = attr.Name.Replace("[name]", type.Name);
fromType = Array.Find(types, it => it.Name == name);
fromType = types.FirstOrDefault(it => it.Name == name);
}

return fromType;
}

private static Type? GetToType(Type type, BaseAdaptAttribute attr, Type[] types)
private static Type? GetToType(Type type, BaseAdaptAttribute attr, HashSet<Type> types)
{
if (!(attr is AdaptToAttribute))
return null;
Expand All @@ -285,7 +295,7 @@ private static Type GetPropertyType(MemberInfo member, Type propType, Type attrT
if (toType == null && attr.Name != null)
{
var name = attr.Name.Replace("[name]", type.Name);
toType = Array.Find(types, it => it.Name == name);
toType = types.FirstOrDefault(it => it.Name == name);
}

return toType;
Expand Down Expand Up @@ -322,7 +332,15 @@ private static void GenerateExtensions(ExtensionOptions opt)
var codeGenConfig = new CodeGenerationConfig();
codeGenConfig.Scan(assembly);

var types = assembly.GetTypes();
var assemblies = new HashSet<Assembly> {assembly};
foreach (var builder in codeGenConfig.AdaptAttributeBuilders)
{
foreach (var setting in builder.TypeSettings)
{
assemblies.Add(setting.Key.Assembly);
}
}
var types = assemblies.SelectMany(it => it.GetTypes()).ToHashSet();
var configDict = new Dictionary<BaseAdaptAttribute, TypeAdapterConfig>();
foreach (var builder in codeGenConfig.AdaptAttributeBuilders)
{
Expand Down

0 comments on commit c21b773

Please sign in to comment.