aboutsummaryrefslogtreecommitdiff
path: root/ErrorResponseCodeGenerator/Program.cs
blob: 5ea8aaca07778788dcaf4e2d0fd90aaf1707a55f (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
using System;
using System.Linq;
using System.Reflection;

namespace ErrorResponseCodeGenerator
{
    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.Clipboard.SetText(code);
            var oldColor = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Code has copied to clipboard!");
            Console.ForegroundColor = oldColor;
        }
    }
}