Skip to content

Commit 36b0702

Browse files
committed
fix #328 generate dynamic outputs & namespaces
1 parent 3012c14 commit 36b0702

File tree

6 files changed

+42
-8
lines changed

6 files changed

+42
-8
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": "8.0.13",
6+
"version": "8.1.0",
77
"commands": [
88
"dotnet-mapster"
99
]

src/Mapster.Tool/ExtensionOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public class ExtensionOptions
1919
[Option('p', "printFullTypeName", Required = false, HelpText = "Set true to print full type name")]
2020
public bool PrintFullTypeName { get; set; }
2121

22+
[Option('b', "baseNamespace", Required = false, HelpText = "Provide base namespace to generate nested output & namespace")]
23+
public string? BaseNamespace { get; set; }
24+
2225
[Usage(ApplicationAlias = "dotnet mapster extension")]
2326
public static IEnumerable<Example> Examples =>
2427
new List<Example>

src/Mapster.Tool/MapperOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public class MapperOptions
1818

1919
[Option('p', "printFullTypeName", Required = false, HelpText = "Set true to print full type name")]
2020
public bool PrintFullTypeName { get; set; }
21+
22+
[Option('b', "baseNamespace", Required = false, HelpText = "Provide base namespace to generate nested output & namespace")]
23+
public string? BaseNamespace { get; set; }
2124

2225
[Usage(ApplicationAlias = "dotnet mapster mapper")]
2326
public static IEnumerable<Example> Examples =>

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>8.0.13</Version>
18+
<Version>8.1.0</Version>
1919
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2020
<Copyright>Copyright (c) 2020 Chaowlert Chaisrichalermpol</Copyright>
2121
<PackageIcon>icon.png</PackageIcon>

src/Mapster.Tool/ModelOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public class ModelOptions
2121

2222
[Option('r', "isRecordType", Required = false, HelpText = "Generate record type")]
2323
public bool IsRecordType { get; set; }
24+
25+
[Option('b', "baseNamespace", Required = false, HelpText = "Provide base namespace to generate nested output & namespace")]
26+
public string? BaseNamespace { get; set; }
2427

2528
[Usage(ApplicationAlias = "dotnet mapster model")]
2629
public static IEnumerable<Example> Examples =>

src/Mapster.Tool/Program.cs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,28 @@ static void Main(string[] args)
2020
.WithParsed<ExtensionOptions>(GenerateExtensions);
2121
}
2222

23+
private static string? GetSegments(string? ns, string? baseNs)
24+
{
25+
if (ns == null || string.IsNullOrEmpty(baseNs) || baseNs == ns)
26+
return null;
27+
return ns.StartsWith(baseNs + ".") ? ns.Substring(baseNs.Length + 1) : ns;
28+
}
29+
30+
private static string? CreateNamespace(string? ns, string? segment, string? typeNs)
31+
{
32+
if (ns == null)
33+
return typeNs;
34+
return segment == null ? ns : $"{ns}.{segment}";
35+
}
36+
37+
private static string GetOutput(string baseOutput, string? segment, string typeName)
38+
{
39+
var fullBasePath = Path.GetFullPath(baseOutput);
40+
return segment == null
41+
? Path.Combine(fullBasePath, typeName + ".g.cs")
42+
: Path.Combine(fullBasePath, segment.Replace('.', '/'), typeName + ".g.cs");
43+
}
44+
2345
private static void WriteFile(string code, string path)
2446
{
2547
var dir = Path.GetDirectoryName(path);
@@ -52,10 +74,11 @@ private static void GenerateMappers(MapperOptions opt)
5274

5375
Console.WriteLine($"Processing: {type.FullName}");
5476

77+
var segments = GetSegments(type.Namespace, opt.BaseNamespace);
5578
var definitions = new TypeDefinitions
5679
{
5780
Implements = new[] {type},
58-
Namespace = opt.Namespace ?? type.Namespace,
81+
Namespace = CreateNamespace(opt.Namespace, segments, type.Namespace),
5982
TypeName = attr.Name ?? GetImplName(type.Name),
6083
IsInternal = attr.IsInternal,
6184
PrintFullTypeName = opt.PrintFullTypeName,
@@ -103,7 +126,7 @@ private static void GenerateMappers(MapperOptions opt)
103126
}
104127

105128
var code = translator.ToString();
106-
var path = Path.Combine(Path.GetFullPath(opt.Output), definitions.TypeName + ".g.cs");
129+
var path = GetOutput(opt.Output, segments, definitions.TypeName);
107130
WriteFile(code, path);
108131
}
109132
}
@@ -156,10 +179,11 @@ private static void GenerateModels(ModelOptions opt)
156179
}
157180
private static void CreateModel(ModelOptions opt, Type type, AdaptAttributeBuilder builder)
158181
{
182+
var segments = GetSegments(type.Namespace, opt.BaseNamespace);
159183
var attr = builder.Attribute;
160184
var definitions = new TypeDefinitions
161185
{
162-
Namespace = opt.Namespace ?? type.Namespace,
186+
Namespace = CreateNamespace(opt.Namespace, segments, type.Namespace),
163187
TypeName = attr.Name!.Replace("[name]", type.Name),
164188
PrintFullTypeName = opt.PrintFullTypeName,
165189
IsRecordType = opt.IsRecordType,
@@ -228,7 +252,7 @@ private static void CreateModel(ModelOptions opt, Type type, AdaptAttributeBuild
228252
}
229253

230254
var code = translator.ToString();
231-
var path = Path.Combine(Path.GetFullPath(opt.Output), definitions.TypeName + ".g.cs");
255+
var path = GetOutput(opt.Output, segments, definitions.TypeName);
232256
WriteFile(code, path);
233257

234258
static Type getPropType(MemberInfo mem)
@@ -395,10 +419,11 @@ private static void GenerateExtensions(ExtensionOptions opt)
395419

396420
Console.WriteLine($"Processing: {type.FullName}");
397421

422+
var segments = GetSegments(type.Namespace, opt.BaseNamespace);
398423
var definitions = new TypeDefinitions
399424
{
400425
IsStatic = true,
401-
Namespace = opt.Namespace ?? type.Namespace,
426+
Namespace = CreateNamespace(opt.Namespace, segments, type.Namespace),
402427
TypeName = mapperAttr.Name.Replace("[name]", type.Name),
403428
IsInternal = mapperAttr.IsInternal,
404429
PrintFullTypeName = opt.PrintFullTypeName,
@@ -435,7 +460,7 @@ private static void GenerateExtensions(ExtensionOptions opt)
435460
}
436461

437462
var code = translator.ToString();
438-
var path = Path.Combine(Path.GetFullPath(opt.Output), definitions.TypeName + ".g.cs");
463+
var path = GetOutput(opt.Output, segments, definitions.TypeName);
439464
WriteFile(code, path);
440465
}
441466
}

0 commit comments

Comments
 (0)