Create basic structure for generated code
This commit is contained in:
parent
d554b69c82
commit
e30e005d09
|
@ -1,3 +1,9 @@
|
|||
# Built objects
|
||||
out/
|
||||
build/
|
||||
.gradle/
|
||||
|
||||
# Generated code
|
||||
tl/Abstract.java
|
||||
tl/Types.java
|
||||
tl/Functions.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<String, List<TLObject>> 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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<TLObject> types, final List<TLObject> 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<TLObject> 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<String, List<TLObject>> 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<String, List<TLObject>> byNamespace(final List<TLObject> objects) {
|
||||
final Map<String, List<TLObject>> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
package io.github.lonamiwebs.overgram.tl;
|
||||
|
||||
public abstract class TLObject {
|
||||
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
package io.github.lonamiwebs.overgram.tl;
|
||||
|
||||
public abstract class TLRequest extends TLObject {
|
||||
}
|
Loading…
Reference in New Issue