diff --git a/src/Mapster.Tool/ExtensionOptions.cs b/src/Mapster.Tool/ExtensionOptions.cs index 333987f1..5242b192 100644 --- a/src/Mapster.Tool/ExtensionOptions.cs +++ b/src/Mapster.Tool/ExtensionOptions.cs @@ -22,6 +22,9 @@ public class ExtensionOptions [Option('b', "baseNamespace", Required = false, HelpText = "Provide base namespace to generate nested output & namespace")] public string? BaseNamespace { get; set; } + [Option('s', "skipExisting", Required = false, HelpText = "Set true to skip generating already existing files")] + public bool SkipExistingFiles { get; set; } + [Usage(ApplicationAlias = "dotnet mapster extension")] public static IEnumerable Examples => new List diff --git a/src/Mapster.Tool/MapperOptions.cs b/src/Mapster.Tool/MapperOptions.cs index 534cf69c..689c0c7d 100644 --- a/src/Mapster.Tool/MapperOptions.cs +++ b/src/Mapster.Tool/MapperOptions.cs @@ -22,6 +22,9 @@ public class MapperOptions [Option('b', "baseNamespace", Required = false, HelpText = "Provide base namespace to generate nested output & namespace")] public string? BaseNamespace { get; set; } + [Option('s', "skipExisting", Required = false, HelpText = "Set true to skip generating already existing files")] + public bool SkipExistingFiles { get; set; } + [Usage(ApplicationAlias = "dotnet mapster mapper")] public static IEnumerable Examples => new List diff --git a/src/Mapster.Tool/ModelOptions.cs b/src/Mapster.Tool/ModelOptions.cs index 7548475e..47cf667f 100644 --- a/src/Mapster.Tool/ModelOptions.cs +++ b/src/Mapster.Tool/ModelOptions.cs @@ -25,6 +25,9 @@ public class ModelOptions [Option('b', "baseNamespace", Required = false, HelpText = "Provide base namespace to generate nested output & namespace")] public string? BaseNamespace { get; set; } + [Option('s', "skipExisting", Required = false, HelpText = "Set true to skip generating already existing files")] + public bool SkipExistingFiles { get; set; } + [Usage(ApplicationAlias = "dotnet mapster model")] public static IEnumerable Examples => new List diff --git a/src/Mapster.Tool/Program.cs b/src/Mapster.Tool/Program.cs index 482a78cb..03fa643d 100644 --- a/src/Mapster.Tool/Program.cs +++ b/src/Mapster.Tool/Program.cs @@ -83,6 +83,14 @@ private static void GenerateMappers(MapperOptions opt) IsInternal = attr.IsInternal, PrintFullTypeName = opt.PrintFullTypeName, }; + + var path = GetOutput(opt.Output, segments, definitions.TypeName); + if (opt.SkipExistingFiles && File.Exists(path)) + { + Console.WriteLine($"Skipped: {type.FullName}. Mapper {definitions.TypeName} already exists."); + continue; + } + var translator = new ExpressionTranslator(definitions); var interfaces = type.GetAllInterfaces(); foreach (var @interface in interfaces) @@ -126,7 +134,6 @@ private static void GenerateMappers(MapperOptions opt) } var code = translator.ToString(); - var path = GetOutput(opt.Output, segments, definitions.TypeName); WriteFile(code, path); } } @@ -189,6 +196,14 @@ private static void CreateModel(ModelOptions opt, Type type, AdaptAttributeBuild IsRecordType = opt.IsRecordType, NullableContext = GetTypeNullableContext(type), }; + + var path = GetOutput(opt.Output, segments, definitions.TypeName); + if (opt.SkipExistingFiles && File.Exists(path)) + { + Console.WriteLine($"Skipped: {type.FullName}. Model {definitions.TypeName} already exists."); + return; + } + var translator = new ExpressionTranslator(definitions); var isAdaptTo = attr is AdaptToAttribute; var isTwoWays = attr is AdaptTwoWaysAttribute; @@ -252,7 +267,6 @@ private static void CreateModel(ModelOptions opt, Type type, AdaptAttributeBuild } var code = translator.ToString(); - var path = GetOutput(opt.Output, segments, definitions.TypeName); WriteFile(code, path); static Type getPropType(MemberInfo mem) @@ -428,6 +442,14 @@ private static void GenerateExtensions(ExtensionOptions opt) IsInternal = mapperAttr.IsInternal, PrintFullTypeName = opt.PrintFullTypeName, }; + + var path = GetOutput(opt.Output, segments, definitions.TypeName); + if (opt.SkipExistingFiles && File.Exists(path)) + { + Console.WriteLine($"Skipped: {type.FullName}. Extension class {definitions.TypeName} already exists."); + continue; + } + var translator = new ExpressionTranslator(definitions); foreach (var builder in builders) @@ -460,7 +482,6 @@ private static void GenerateExtensions(ExtensionOptions opt) } var code = translator.ToString(); - var path = GetOutput(opt.Output, segments, definitions.TypeName); WriteFile(code, path); } }