aboutsummaryrefslogtreecommitdiff
path: root/Timeline.ErrorCodes.CodeGenerator/Program.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-10-27 15:14:03 +0800
committercrupest <crupest@outlook.com>2020-10-27 15:14:03 +0800
commitfe1ca658ca5afbe7cd74303bb9b75f1e735c0762 (patch)
tree4c4f297e4141d6723d42c4f3d05c7c43816a9cbf /Timeline.ErrorCodes.CodeGenerator/Program.cs
parentc2871e2c314b840bb938a74081299b34a94d88b7 (diff)
downloadtimeline-fe1ca658ca5afbe7cd74303bb9b75f1e735c0762.tar.gz
timeline-fe1ca658ca5afbe7cd74303bb9b75f1e735c0762.tar.bz2
timeline-fe1ca658ca5afbe7cd74303bb9b75f1e735c0762.zip
refactor(back): Rename code generator.
Diffstat (limited to 'Timeline.ErrorCodes.CodeGenerator/Program.cs')
-rw-r--r--Timeline.ErrorCodes.CodeGenerator/Program.cs68
1 files changed, 68 insertions, 0 deletions
diff --git a/Timeline.ErrorCodes.CodeGenerator/Program.cs b/Timeline.ErrorCodes.CodeGenerator/Program.cs
new file mode 100644
index 00000000..d182de7c
--- /dev/null
+++ b/Timeline.ErrorCodes.CodeGenerator/Program.cs
@@ -0,0 +1,68 @@
+using System;
+using System.Linq;
+using System.Reflection;
+
+namespace Timeline.ErrorCodes.CodeGenerator
+{
+ class Program
+ {
+ static void Main(string[] args)
+ {
+ var code = "";
+
+ void RecursiveAddErrorCode(Type type, bool root)
+ {
+ code += $@"
+ public static class {(root ? "ErrorResponse" : type.Name)}
+ {{
+";
+
+ 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));
+ }}
+";
+ }
+
+ foreach (var nestedType in type.GetNestedTypes())
+ {
+ RecursiveAddErrorCode(nestedType, false);
+ }
+
+ code += @"
+ }
+";
+ }
+
+ RecursiveAddErrorCode(typeof(Timeline.Models.Http.ErrorCodes), true);
+
+ code = @"
+using static Timeline.Resources.Messages;
+
+namespace Timeline.Models.Http
+{
+$
+}
+".Replace("$", code);
+
+ Console.WriteLine(code);
+
+ TextCopy.ClipboardService.SetText(code);
+ var oldColor = Console.ForegroundColor;
+ Console.ForegroundColor = ConsoleColor.Green;
+ Console.WriteLine("Code has copied to clipboard!");
+ Console.ForegroundColor = oldColor;
+ }
+ }
+}