Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add options to Mapster.Tool to generate files with #nullable directives #530

Merged
merged 2 commits into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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