Skip to content

Commit

Permalink
Merge pull request #530 from kescherCode/master
Browse files Browse the repository at this point in the history
Add options to Mapster.Tool to generate files with #nullable directives
  • Loading branch information
andrerav authored Jan 29, 2023
2 parents 7d8a172 + 79669a2 commit 54f075f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/ExpressionTranslator/ExpressionTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1896,7 +1896,7 @@ public override string ToString()
}

// NOTE: type alias cannot solve all name conflicted case, user should use PrintFullTypeName
// keep logic here for compatability
// keep logic here for compatibility
if (_typeNames != null)
{
var names = _typeNames
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 @@ -25,6 +25,9 @@ public class ExtensionOptions
[Option('s', "skipExisting", Required = false, HelpText = "Set true to skip generating already existing files")]
public bool SkipExistingFiles { get; set; }

[Option('N', "nullableDirective", Required = false, HelpText = "Set true to add \"#nullable enable\" to the top of generated extension files")]
public bool GenerateNullableDirective { 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 @@ -25,6 +25,9 @@ public class MapperOptions
[Option('s', "skipExisting", Required = false, HelpText = "Set true to skip generating already existing files")]
public bool SkipExistingFiles { get; set; }

[Option('N', "nullableDirective", Required = false, HelpText = "Set true to add \"#nullable enable\" to the top of generated mapper files")]
public bool GenerateNullableDirective { get; set; }

[Usage(ApplicationAlias = "dotnet mapster mapper")]
public static IEnumerable<Example> Examples =>
new List<Example>
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 @@ -28,6 +28,9 @@ public class ModelOptions
[Option('s', "skipExisting", Required = false, HelpText = "Set true to skip generating already existing files")]
public bool SkipExistingFiles { get; set; }

[Option('N', "nullableDirective", Required = false, HelpText = "Set true to add \"#nullable enable\" to the top of generated model files")]
public bool GenerateNullableDirective { get; set; }

[Usage(ApplicationAlias = "dotnet mapster model")]
public static IEnumerable<Example> Examples =>
new List<Example>
Expand Down
12 changes: 9 additions & 3 deletions src/Mapster.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ private static void GenerateMappers(MapperOptions opt)
}
}

var code = translator.ToString();
var code = opt.GenerateNullableDirective ?
$"#nullable enable{Environment.NewLine}{translator}" :
translator.ToString();
WriteFile(code, path);
}
}
Expand Down Expand Up @@ -266,7 +268,9 @@ private static void CreateModel(ModelOptions opt, Type type, AdaptAttributeBuild
});
}

var code = translator.ToString();
var code = opt.GenerateNullableDirective ?
$"#nullable enable{Environment.NewLine}{translator}" :
translator.ToString();
WriteFile(code, path);

static Type getPropType(MemberInfo mem)
Expand Down Expand Up @@ -481,7 +485,9 @@ private static void GenerateExtensions(ExtensionOptions opt)
GenerateExtensionMethods(mapType, config, tuple, translator, type, mapperAttr.IsHelperClass);
}

var code = translator.ToString();
var code = opt.GenerateNullableDirective ?
$"#nullable enable{Environment.NewLine}{translator}" :
translator.ToString();
WriteFile(code, path);
}
}
Expand Down

0 comments on commit 54f075f

Please sign in to comment.