From 0c4caaebe2480e77918d5d7df234f0edaeab74ba Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 27 Oct 2020 15:39:09 +0800 Subject: refactor(back): Refactor error codes code generator. --- Timeline.ErrorCodes.CodeGenerator/Program.cs | 65 ++++++++++++++++------------ 1 file changed, 37 insertions(+), 28 deletions(-) (limited to 'Timeline.ErrorCodes.CodeGenerator/Program.cs') diff --git a/Timeline.ErrorCodes.CodeGenerator/Program.cs b/Timeline.ErrorCodes.CodeGenerator/Program.cs index d182de7c..84ab5908 100644 --- a/Timeline.ErrorCodes.CodeGenerator/Program.cs +++ b/Timeline.ErrorCodes.CodeGenerator/Program.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using System.Reflection; +using System.Text; namespace Timeline.ErrorCodes.CodeGenerator { @@ -8,57 +9,65 @@ namespace Timeline.ErrorCodes.CodeGenerator { static void Main(string[] args) { - var code = ""; + string Indent(int n) + { + const string indent = " "; + return string.Concat(Enumerable.Repeat(indent, n)); + } + + StringBuilder code = new StringBuilder(); + + code.AppendLine("using static Timeline.Resources.Messages;"); + code.AppendLine(); + code.AppendLine("namespace Timeline.Models.Http"); + code.AppendLine("{"); + + int depth = 1; void RecursiveAddErrorCode(Type type, bool root) { - code += $@" - public static class {(root ? "ErrorResponse" : type.Name)} - {{ -"; + code.AppendLine($"{Indent(depth)}public static class {(root ? "ErrorResponse" : type.Name)}"); + code.AppendLine($"{Indent(depth)}{{"); foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) .Where(fi => fi.IsLiteral && !fi.IsInitOnly && fi.FieldType == typeof(int))) { var path = type.FullName.Replace("+", ".").Replace("Timeline.Models.Http.ErrorCodes.", "") + "." + field.Name; - code += $@" - public static CommonResponse {field.Name}(params object?[] formatArgs) - {{ - return new CommonResponse({"ErrorCodes." + path}, string.Format({path.Replace(".", "_")}, formatArgs)); - }} - - public static CommonResponse CustomMessage_{field.Name}(string message, params object?[] formatArgs) - {{ - return new CommonResponse({"ErrorCodes." + path}, string.Format(message, formatArgs)); - }} -"; + code.AppendLine($"{Indent(depth + 1)}public static CommonResponse {field.Name}(params object?[] formatArgs)"); + code.AppendLine($"{Indent(depth + 1)}{{"); + code.AppendLine($"{Indent(depth + 2)}return new CommonResponse({"ErrorCodes." + path}, string.Format({path.Replace(".", "_")}, formatArgs));"); + code.AppendLine($"{Indent(depth + 1)}}}"); + code.AppendLine(); + code.AppendLine($"{Indent(depth + 1)}public static CommonResponse CustomMessage_{field.Name}(string message, params object?[] formatArgs)"); + code.AppendLine($"{Indent(depth + 1)}{{"); + code.AppendLine($"{Indent(depth + 2)}return new CommonResponse({"ErrorCodes." + path}, string.Format(message, formatArgs));"); + code.AppendLine($"{Indent(depth + 1)}}}"); + code.AppendLine(); } + depth += 1; + foreach (var nestedType in type.GetNestedTypes()) { RecursiveAddErrorCode(nestedType, false); } - code += @" - } -"; + depth -= 1; + + code.AppendLine($"{Indent(depth)}}}"); + code.AppendLine(); } RecursiveAddErrorCode(typeof(Timeline.Models.Http.ErrorCodes), true); - code = @" -using static Timeline.Resources.Messages; + code.AppendLine("}"); -namespace Timeline.Models.Http -{ -$ -} -".Replace("$", code); + var generatedCode = code.ToString(); - Console.WriteLine(code); + Console.WriteLine(generatedCode); - TextCopy.ClipboardService.SetText(code); + TextCopy.ClipboardService.SetText(generatedCode); var oldColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Code has copied to clipboard!"); -- cgit v1.2.3