aboutsummaryrefslogtreecommitdiff
path: root/Timeline.ErrorCodes.CodeGenerator
diff options
context:
space:
mode:
Diffstat (limited to 'Timeline.ErrorCodes.CodeGenerator')
-rw-r--r--Timeline.ErrorCodes.CodeGenerator/Program.cs68
-rw-r--r--Timeline.ErrorCodes.CodeGenerator/Timeline.ErrorCodes.CodeGenerator.csproj16
-rw-r--r--Timeline.ErrorCodes.CodeGenerator/packages.lock.json24
3 files changed, 108 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;
+ }
+ }
+}
diff --git a/Timeline.ErrorCodes.CodeGenerator/Timeline.ErrorCodes.CodeGenerator.csproj b/Timeline.ErrorCodes.CodeGenerator/Timeline.ErrorCodes.CodeGenerator.csproj
new file mode 100644
index 00000000..c8eb97f3
--- /dev/null
+++ b/Timeline.ErrorCodes.CodeGenerator/Timeline.ErrorCodes.CodeGenerator.csproj
@@ -0,0 +1,16 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <OutputType>Exe</OutputType>
+ <TargetFramework>netcoreapp3.1</TargetFramework>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="TextCopy" Version="4.2.0" />
+ </ItemGroup>
+
+ <ItemGroup>
+ <ProjectReference Include="..\Timeline.ErrorCodes\Timeline.ErrorCodes.csproj" />
+ </ItemGroup>
+
+</Project>
diff --git a/Timeline.ErrorCodes.CodeGenerator/packages.lock.json b/Timeline.ErrorCodes.CodeGenerator/packages.lock.json
new file mode 100644
index 00000000..69cfee1e
--- /dev/null
+++ b/Timeline.ErrorCodes.CodeGenerator/packages.lock.json
@@ -0,0 +1,24 @@
+{
+ "version": 1,
+ "dependencies": {
+ ".NETCoreApp,Version=v3.1": {
+ "TextCopy": {
+ "type": "Direct",
+ "requested": "[4.2.0, )",
+ "resolved": "4.2.0",
+ "contentHash": "NY2UAFIjBJj+3aABP5tyO6ooEdkJxIGtwRNqvMQKLmyIeZiyGvM4XYbkKNntyQlhyFhhfBww05C3D/0DdimfaQ==",
+ "dependencies": {
+ "Microsoft.Extensions.DependencyInjection.Abstractions": "3.1.4"
+ }
+ },
+ "Microsoft.Extensions.DependencyInjection.Abstractions": {
+ "type": "Transitive",
+ "resolved": "3.1.4",
+ "contentHash": "AceHamXNKDMDwIoZqEoApLp8s3935wSC3VXrPaRWa0wWOaEcYdDlo1nWQ1zLiezoDmpJzV7FqDm53E0Ty/hEMg=="
+ },
+ "timeline.errorcodes": {
+ "type": "Project"
+ }
+ }
+ }
+} \ No newline at end of file