blob: 84ab5908281185f71fc0c7700ddd80e06d4e2046 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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;
}
}
}
|