From e30e005d0929f31f958d8e95c5929a4c4b56b48f Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Mon, 23 Jul 2018 16:16:08 +0200 Subject: [PATCH] Create basic structure for generated code --- .gitignore | 6 ++ .../github/lonamiwebs/overgram/Overgram.java | 17 ++++- .../overgram/generator/Generator.java | 69 +++++++++++++++++++ .../lonamiwebs/overgram/parser/TLObject.java | 4 ++ .../lonamiwebs/overgram/tl/TLObject.java | 5 ++ .../lonamiwebs/overgram/tl/TLRequest.java | 4 ++ 6 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 src/main/java/io/github/lonamiwebs/overgram/generator/Generator.java create mode 100644 src/main/java/io/github/lonamiwebs/overgram/tl/TLObject.java create mode 100644 src/main/java/io/github/lonamiwebs/overgram/tl/TLRequest.java diff --git a/.gitignore b/.gitignore index 51fc733..e664777 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,9 @@ +# Built objects out/ build/ .gradle/ + +# Generated code +tl/Abstract.java +tl/Types.java +tl/Functions.java diff --git a/src/main/java/io/github/lonamiwebs/overgram/Overgram.java b/src/main/java/io/github/lonamiwebs/overgram/Overgram.java index cd3400e..d340186 100644 --- a/src/main/java/io/github/lonamiwebs/overgram/Overgram.java +++ b/src/main/java/io/github/lonamiwebs/overgram/Overgram.java @@ -1,11 +1,26 @@ package io.github.lonamiwebs.overgram; +import io.github.lonamiwebs.overgram.generator.Generator; +import io.github.lonamiwebs.overgram.parser.TLObject; import io.github.lonamiwebs.overgram.parser.TLReader; import java.io.File; +import java.io.IOException; +import java.util.List; +import java.util.Map; public class Overgram { public static void main(final String... args) { - TLReader.readTlObjects(new File("telegram_api.tl")); + final Map> objects = TLReader.readTlObjects(new File("telegram_api.tl")); + try { + Generator.generateJava( + objects.get("types"), + objects.get("functions"), + new File("Abstract.java"), + new File("Types.java"), + new File("Functions.java") + ); + } catch (IOException ignored) { + } } } diff --git a/src/main/java/io/github/lonamiwebs/overgram/generator/Generator.java b/src/main/java/io/github/lonamiwebs/overgram/generator/Generator.java new file mode 100644 index 0000000..e16b2d6 --- /dev/null +++ b/src/main/java/io/github/lonamiwebs/overgram/generator/Generator.java @@ -0,0 +1,69 @@ +package io.github.lonamiwebs.overgram.generator; + +import io.github.lonamiwebs.overgram.parser.TLObject; + +import java.io.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Generator { + public static void generateJava( + final List types, final List functions, + final File abstractsFile, final File typesFile, final File functionsFile) throws IOException { + makeParents(abstractsFile, typesFile, functionsFile); + try (final Writer writer = new BufferedWriter(new FileWriter(typesFile))) { + writeCode(writer, "Types", "TLObject", types); + } + try (final Writer writer = new BufferedWriter(new FileWriter(functionsFile))) { + writeCode(writer, "Functions", "TLRequest", functions); + } + } + + private static void writeCode( + final Writer writer, final String className, final String extendsName, + final List objects) throws IOException { + writer.write("package io.github.lonamiwebs.overgram.tl;\n"); + writer.write("public class "); + writer.write(className); + writer.write(" {\n"); + for (Map.Entry> nsObjects : byNamespace(objects).entrySet()) { + if (!nsObjects.getKey().isEmpty()) { + writer.write("public static class "); + writer.write(TLObject.toCamelCase(nsObjects.getKey())); + writer.write(" {\n"); + } + for (TLObject object : nsObjects.getValue()) { + writer.write("public static class "); + writer.write(TLObject.toCamelCase(object.name)); + writer.write(" extends "); + writer.write(extendsName); + writer.write(" {\n"); + writer.write("}\n"); + } + if (!nsObjects.getKey().isEmpty()) { + writer.write("}\n"); + } + } + } + + private static Map> byNamespace(final List objects) { + final Map> result = new HashMap<>(); + for (final TLObject object : objects) { + result.computeIfAbsent(object.namespace, k -> new ArrayList<>()).add(object); + } + return result; + } + + private static void makeParents(final File... files) throws IOException { + for (File file : files) { + file = file.getAbsoluteFile().getParentFile(); + if (!file.isDirectory()) { + if (!file.mkdirs()) { + throw new IOException("Failed to create " + file); + } + } + } + } +} diff --git a/src/main/java/io/github/lonamiwebs/overgram/parser/TLObject.java b/src/main/java/io/github/lonamiwebs/overgram/parser/TLObject.java index 472794e..65007c2 100644 --- a/src/main/java/io/github/lonamiwebs/overgram/parser/TLObject.java +++ b/src/main/java/io/github/lonamiwebs/overgram/parser/TLObject.java @@ -28,6 +28,10 @@ public class TLObject { } } + public static String toCamelCase(final String string) { + return string.substring(0, 1).toUpperCase() + string.substring(1); + } + public static TLObject fromString(String string) { String[] tmp; String name; diff --git a/src/main/java/io/github/lonamiwebs/overgram/tl/TLObject.java b/src/main/java/io/github/lonamiwebs/overgram/tl/TLObject.java new file mode 100644 index 0000000..2b6850c --- /dev/null +++ b/src/main/java/io/github/lonamiwebs/overgram/tl/TLObject.java @@ -0,0 +1,5 @@ +package io.github.lonamiwebs.overgram.tl; + +public abstract class TLObject { + +} diff --git a/src/main/java/io/github/lonamiwebs/overgram/tl/TLRequest.java b/src/main/java/io/github/lonamiwebs/overgram/tl/TLRequest.java new file mode 100644 index 0000000..2a9249c --- /dev/null +++ b/src/main/java/io/github/lonamiwebs/overgram/tl/TLRequest.java @@ -0,0 +1,4 @@ +package io.github.lonamiwebs.overgram.tl; + +public abstract class TLRequest extends TLObject { +}