Skip to content

Commit

Permalink
fix MapsterMapper#328 generate dynamic outputs & namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
chaowlert committed Apr 10, 2021
1 parent 3012c14 commit 36b0702
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 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": "8.0.13",
"version": "8.1.0",
"commands": [
"dotnet-mapster"
]
Expand Down
3 changes: 3 additions & 0 deletions src/Mapster.Tool/ExtensionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class ExtensionOptions
[Option('p', "printFullTypeName", Required = false, HelpText = "Set true to print full type name")]
public bool PrintFullTypeName { get; set; }

[Option('b', "baseNamespace", Required = false, HelpText = "Provide base namespace to generate nested output & namespace")]
public string? BaseNamespace { get; set; }

[Usage(ApplicationAlias = "dotnet mapster extension")]
public static IEnumerable<Example> Examples =>
new List<Example>
Expand Down
3 changes: 3 additions & 0 deletions src/Mapster.Tool/MapperOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ public class MapperOptions

[Option('p', "printFullTypeName", Required = false, HelpText = "Set true to print full type name")]
public bool PrintFullTypeName { get; set; }

[Option('b', "baseNamespace", Required = false, HelpText = "Provide base namespace to generate nested output & namespace")]
public string? BaseNamespace { get; set; }

[Usage(ApplicationAlias = "dotnet mapster mapper")]
public static IEnumerable<Example> Examples =>
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>8.0.13</Version>
<Version>8.1.0</Version>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Copyright>Copyright (c) 2020 Chaowlert Chaisrichalermpol</Copyright>
<PackageIcon>icon.png</PackageIcon>
Expand Down
3 changes: 3 additions & 0 deletions src/Mapster.Tool/ModelOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class ModelOptions

[Option('r', "isRecordType", Required = false, HelpText = "Generate record type")]
public bool IsRecordType { get; set; }

[Option('b', "baseNamespace", Required = false, HelpText = "Provide base namespace to generate nested output & namespace")]
public string? BaseNamespace { get; set; }

[Usage(ApplicationAlias = "dotnet mapster model")]
public static IEnumerable<Example> Examples =>
Expand Down
37 changes: 31 additions & 6 deletions src/Mapster.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,28 @@ static void Main(string[] args)
.WithParsed<ExtensionOptions>(GenerateExtensions);
}

private static string? GetSegments(string? ns, string? baseNs)
{
if (ns == null || string.IsNullOrEmpty(baseNs) || baseNs == ns)
return null;
return ns.StartsWith(baseNs + ".") ? ns.Substring(baseNs.Length + 1) : ns;
}

private static string? CreateNamespace(string? ns, string? segment, string? typeNs)
{
if (ns == null)
return typeNs;
return segment == null ? ns : $"{ns}.{segment}";
}

private static string GetOutput(string baseOutput, string? segment, string typeName)
{
var fullBasePath = Path.GetFullPath(baseOutput);
return segment == null
? Path.Combine(fullBasePath, typeName + ".g.cs")
: Path.Combine(fullBasePath, segment.Replace('.', '/'), typeName + ".g.cs");
}

private static void WriteFile(string code, string path)
{
var dir = Path.GetDirectoryName(path);
Expand Down Expand Up @@ -52,10 +74,11 @@ private static void GenerateMappers(MapperOptions opt)

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

var segments = GetSegments(type.Namespace, opt.BaseNamespace);
var definitions = new TypeDefinitions
{
Implements = new[] {type},
Namespace = opt.Namespace ?? type.Namespace,
Namespace = CreateNamespace(opt.Namespace, segments, type.Namespace),
TypeName = attr.Name ?? GetImplName(type.Name),
IsInternal = attr.IsInternal,
PrintFullTypeName = opt.PrintFullTypeName,
Expand Down Expand Up @@ -103,7 +126,7 @@ private static void GenerateMappers(MapperOptions opt)
}

var code = translator.ToString();
var path = Path.Combine(Path.GetFullPath(opt.Output), definitions.TypeName + ".g.cs");
var path = GetOutput(opt.Output, segments, definitions.TypeName);
WriteFile(code, path);
}
}
Expand Down Expand Up @@ -156,10 +179,11 @@ private static void GenerateModels(ModelOptions opt)
}
private static void CreateModel(ModelOptions opt, Type type, AdaptAttributeBuilder builder)
{
var segments = GetSegments(type.Namespace, opt.BaseNamespace);
var attr = builder.Attribute;
var definitions = new TypeDefinitions
{
Namespace = opt.Namespace ?? type.Namespace,
Namespace = CreateNamespace(opt.Namespace, segments, type.Namespace),
TypeName = attr.Name!.Replace("[name]", type.Name),
PrintFullTypeName = opt.PrintFullTypeName,
IsRecordType = opt.IsRecordType,
Expand Down Expand Up @@ -228,7 +252,7 @@ private static void CreateModel(ModelOptions opt, Type type, AdaptAttributeBuild
}

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

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

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

var segments = GetSegments(type.Namespace, opt.BaseNamespace);
var definitions = new TypeDefinitions
{
IsStatic = true,
Namespace = opt.Namespace ?? type.Namespace,
Namespace = CreateNamespace(opt.Namespace, segments, type.Namespace),
TypeName = mapperAttr.Name.Replace("[name]", type.Name),
IsInternal = mapperAttr.IsInternal,
PrintFullTypeName = opt.PrintFullTypeName,
Expand Down Expand Up @@ -435,7 +460,7 @@ private static void GenerateExtensions(ExtensionOptions opt)
}

var code = translator.ToString();
var path = Path.Combine(Path.GetFullPath(opt.Output), definitions.TypeName + ".g.cs");
var path = GetOutput(opt.Output, segments, definitions.TypeName);
WriteFile(code, path);
}
}
Expand Down

0 comments on commit 36b0702

Please sign in to comment.