extracting a few methods

This commit is contained in:
rusefillc 2022-04-15 21:10:20 -04:00
parent d872c8ef98
commit 841b6469c2
1 changed files with 68 additions and 51 deletions

View File

@ -10,9 +10,20 @@ import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class UsagesReader {
private final static String header = "// generated by gen_live_documentation.sh / UsagesReader.java\n";
private final StringBuilder enumContent = new StringBuilder(header +
"#pragma once\n" +
"\n" +
"typedef enum {\n");
private final StringBuilder fragmentsContent = new StringBuilder(
header +
"#include \"pch.h\"\n" +
"#include \"tunerstudio.h\"\n");
public static void main(String[] args) throws IOException {
if (args.length != 1) {
System.err.println("One parameter expected: name of live data yaml input file");
@ -22,43 +33,12 @@ public class UsagesReader {
Yaml yaml = new Yaml();
Map<String, Object> data = yaml.load(new FileReader(yamlFileName));
String header = "// generated by gen_live_documentation.sh / UsagesReader.java\n";
StringBuilder enumContent = new StringBuilder(header +
"#pragma once\n" +
"\n" +
"typedef enum {\n");
LinkedHashMap<?, ?> liveDocs = (LinkedHashMap) data.get("Usages");
StringBuilder fragmentsContent = new StringBuilder(
header +
"#include \"pch.h\"\n" +
"#include \"tunerstudio.h\"\n" +
"static FragmentEntry fragments[" + liveDocs.size() + "];\n\n")
;
fragmentsContent.append("int getFragmentsCount() {\n" +
"\treturn " + liveDocs.size() + ";\n" +
"}\n" +
"\n" +
"FragmentEntry *getFragments() {\n" +
"\treturn fragments;\n" +
"}\n\n" +
"void initFragments() {\n");
int index = 0;
for (Map.Entry entry : liveDocs.entrySet()) {
String name = (String) entry.getKey();
System.out.println(" " + name);
System.out.println(" " + entry.getValue());
List elements = (List) entry.getValue();
EntryHandler handler = new EntryHandler() {
@Override
public void onEntry(String name, List elements) {
String javaName = (String) elements.get(0);
String folder = (String) elements.get(1);
String withCDefines = "false";
String prepend = "";
for (int i = 2; i < elements.size(); i++) {
@ -91,6 +71,42 @@ public class UsagesReader {
ConfigDefinition.KEY_C_DESTINATION,
folder + File.separator + name + "_generated.h"
});
}
};
UsagesReader usagesReader = new UsagesReader();
usagesReader.handleYaml(data, handler);
usagesReader.writeFiles();
}
interface EntryHandler {
void onEntry(String name, List elements);
}
private void handleYaml(Map<String, Object> data, EntryHandler handler) throws IOException {
LinkedHashMap<?, ?> liveDocs = (LinkedHashMap) data.get("Usages");
fragmentsContent.append("static FragmentEntry fragments[" + liveDocs.size() + "];\n\n");
fragmentsContent.append("int getFragmentsCount() {\n" +
"\treturn " + liveDocs.size() + ";\n" +
"}\n" +
"\n" +
"FragmentEntry *getFragments() {\n" +
"\treturn fragments;\n" +
"}\n\n" +
"void initFragments() {\n");
int index = 0;
for (Map.Entry entry : liveDocs.entrySet()) {
String name = (String) entry.getKey();
System.out.println(" " + name);
System.out.println(" " + entry.getValue());
List elements = (List) entry.getValue();
handler.onEntry(name, elements);
String enumName = "LDS_" + name;
String type = name + "_s"; // convention
@ -107,13 +123,14 @@ public class UsagesReader {
.append("));\n");
}
enumContent.append("} live_data_e;\n");
fragmentsContent.append("};\n");
}
private void writeFiles() throws IOException {
try (FileWriter fw = new FileWriter("console/binary/generated/live_data_ids.h")) {
fw.write(enumContent.toString());
}
fragmentsContent.append("};\n");
try (FileWriter fw = new FileWriter("console/binary/generated/live_data_fragments.cpp")) {
fw.write(fragmentsContent.toString());
}