From ac769e656b122ff569c3f1534701b71e00fed586 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 27 Oct 2020 19:21:35 +0800 Subject: Split front and back end. --- .../Timeline.ErrorCodes.CodeGenerator/Program.cs | 77 ++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 BackEnd/Timeline.ErrorCodes.CodeGenerator/Program.cs (limited to 'BackEnd/Timeline.ErrorCodes.CodeGenerator/Program.cs') diff --git a/BackEnd/Timeline.ErrorCodes.CodeGenerator/Program.cs b/BackEnd/Timeline.ErrorCodes.CodeGenerator/Program.cs new file mode 100644 index 00000000..84ab5908 --- /dev/null +++ b/BackEnd/Timeline.ErrorCodes.CodeGenerator/Program.cs @@ -0,0 +1,77 @@ +using System; +using System.Linq; +using System.Reflection; +using System.Text; + +namespace Timeline.ErrorCodes.CodeGenerator +{ + class Program + { + static void Main(string[] args) + { + 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.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.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); + } + + depth -= 1; + + code.AppendLine($"{Indent(depth)}}}"); + code.AppendLine(); + } + + RecursiveAddErrorCode(typeof(Timeline.Models.Http.ErrorCodes), true); + + code.AppendLine("}"); + + var generatedCode = code.ToString(); + + Console.WriteLine(generatedCode); + + TextCopy.ClipboardService.SetText(generatedCode); + var oldColor = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("Code has copied to clipboard!"); + Console.ForegroundColor = oldColor; + } + } +} -- cgit v1.2.3