Skip to content

Commit c21b773

Browse files
committed
fix #306 add types from scanning assembly
1 parent 510d252 commit c21b773

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

.config/dotnet-tools.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"isRoot": true,
44
"tools": {
55
"mapster.tool": {
6-
"version": "7.1.3",
6+
"version": "8.0.0",
77
"commands": [
88
"dotnet-mapster"
99
]

src/Mapster.Core/Mapster.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<AssemblyOriginatorKeyFile>Mapster.Core.snk</AssemblyOriginatorKeyFile>
1616
<PackageIcon>icon.png</PackageIcon>
1717
<PackageIconUrl>https://cloud.githubusercontent.com/assets/5763993/26522718/d16f3e42-4330-11e7-9b78-f8c7402624e7.png</PackageIconUrl>
18-
<Version>1.1.3</Version>
18+
<Version>1.1.5</Version>
1919
<LangVersion>8.0</LangVersion>
2020
<Nullable>enable</Nullable>
2121
<RootNamespace>Mapster</RootNamespace>

src/Mapster.Tool/Mapster.Tool.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<SignAssembly>true</SignAssembly>
1616
<AssemblyOriginatorKeyFile>Mapster.Tool.snk</AssemblyOriginatorKeyFile>
1717
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
18-
<Version>7.1.4</Version>
18+
<Version>8.0.0</Version>
1919
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2020
<Copyright>Copyright (c) 2020 Chaowlert Chaisrichalermpol</Copyright>
2121
<PackageIcon>icon.png</PackageIcon>

src/Mapster.Tool/Program.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ static void Main(string[] args)
2222

2323
private static void WriteFile(string code, string path)
2424
{
25-
Directory.CreateDirectory(Path.GetDirectoryName(path));
25+
var dir = Path.GetDirectoryName(path);
26+
if (dir != null)
27+
Directory.CreateDirectory(dir);
2628
if (File.Exists(path))
2729
{
2830
var old = File.ReadAllText(path);
@@ -120,7 +122,15 @@ private static void GenerateModels(ModelOptions opt)
120122
var codeGenConfig = new CodeGenerationConfig();
121123
codeGenConfig.Scan(assembly);
122124

123-
foreach (var type in assembly.GetTypes())
125+
var types = assembly.GetTypes().ToHashSet();
126+
foreach (var builder in codeGenConfig.AdaptAttributeBuilders)
127+
{
128+
foreach (var setting in builder.TypeSettings)
129+
{
130+
types.Add(setting.Key);
131+
}
132+
}
133+
foreach (var type in types)
124134
{
125135
var builders = type.GetAdaptAttributeBuilders(codeGenConfig)
126136
.Where(it => !string.IsNullOrEmpty(it.Attribute.Name) && it.Attribute.Name != "[name]")
@@ -261,7 +271,7 @@ private static Type GetPropertyType(MemberInfo member, Type propType, Type attrT
261271
return mockType;
262272
}
263273

264-
private static Type? GetFromType(Type type, BaseAdaptAttribute attr, Type[] types)
274+
private static Type? GetFromType(Type type, BaseAdaptAttribute attr, HashSet<Type> types)
265275
{
266276
if (!(attr is AdaptFromAttribute) && !(attr is AdaptTwoWaysAttribute))
267277
return null;
@@ -270,13 +280,13 @@ private static Type GetPropertyType(MemberInfo member, Type propType, Type attrT
270280
if (fromType == null && attr.Name != null)
271281
{
272282
var name = attr.Name.Replace("[name]", type.Name);
273-
fromType = Array.Find(types, it => it.Name == name);
283+
fromType = types.FirstOrDefault(it => it.Name == name);
274284
}
275285

276286
return fromType;
277287
}
278288

279-
private static Type? GetToType(Type type, BaseAdaptAttribute attr, Type[] types)
289+
private static Type? GetToType(Type type, BaseAdaptAttribute attr, HashSet<Type> types)
280290
{
281291
if (!(attr is AdaptToAttribute))
282292
return null;
@@ -285,7 +295,7 @@ private static Type GetPropertyType(MemberInfo member, Type propType, Type attrT
285295
if (toType == null && attr.Name != null)
286296
{
287297
var name = attr.Name.Replace("[name]", type.Name);
288-
toType = Array.Find(types, it => it.Name == name);
298+
toType = types.FirstOrDefault(it => it.Name == name);
289299
}
290300

291301
return toType;
@@ -322,7 +332,15 @@ private static void GenerateExtensions(ExtensionOptions opt)
322332
var codeGenConfig = new CodeGenerationConfig();
323333
codeGenConfig.Scan(assembly);
324334

325-
var types = assembly.GetTypes();
335+
var assemblies = new HashSet<Assembly> {assembly};
336+
foreach (var builder in codeGenConfig.AdaptAttributeBuilders)
337+
{
338+
foreach (var setting in builder.TypeSettings)
339+
{
340+
assemblies.Add(setting.Key.Assembly);
341+
}
342+
}
343+
var types = assemblies.SelectMany(it => it.GetTypes()).ToHashSet();
326344
var configDict = new Dictionary<BaseAdaptAttribute, TypeAdapterConfig>();
327345
foreach (var builder in codeGenConfig.AdaptAttributeBuilders)
328346
{

0 commit comments

Comments
 (0)