refactoring - mostly reducing complexity of code flow, end goal is making class testable

This commit is contained in:
rusefillc 2023-02-11 14:42:20 -05:00
parent 6e4028f2c3
commit a0b812f47d
2 changed files with 17 additions and 12 deletions

View File

@ -176,6 +176,7 @@ public class ConfigDefinition {
new TriggerWheelTSLogic().execute(triggersInputFolder, state.getVariableRegistry()); new TriggerWheelTSLogic().execute(triggersInputFolder, state.getVariableRegistry());
if (pinoutLogic != null) { if (pinoutLogic != null) {
pinoutLogic.readFiles();
pinoutLogic.registerBoardSpecificPinNames(state.getVariableRegistry(), state, parseState); pinoutLogic.registerBoardSpecificPinNames(state.getVariableRegistry(), state, parseState);
} }

View File

@ -30,11 +30,13 @@ public class PinoutLogic {
private final ArrayList<PinState> globalList = new ArrayList<>(); private final ArrayList<PinState> globalList = new ArrayList<>();
private final Map</*id*/String, /*tsName*/String> tsNameById = new TreeMap<>(); private final Map</*id*/String, /*tsName*/String> tsNameById = new TreeMap<>();
private final StringBuilder header = new StringBuilder("//DO NOT EDIT MANUALLY, let automation work hard.\n\n"); private final StringBuilder header = new StringBuilder("//DO NOT EDIT MANUALLY, let automation work hard.\n\n");
private final List<String> inputFiles;
public PinoutLogic(String boardName, File[] boardYamlFiles) { public PinoutLogic(String boardName, File[] boardYamlFiles) {
log.info(boardName + ": " + Arrays.toString(boardYamlFiles)); log.info(boardName + ": " + Arrays.toString(boardYamlFiles));
this.boardName = boardName; this.boardName = boardName;
this.boardYamlFiles = boardYamlFiles; this.boardYamlFiles = boardYamlFiles;
inputFiles = getInputFilesList(boardName, boardYamlFiles);
} }
private static Map.Entry<String, Value> find(EnumsReader.EnumState enumList, String id) { private static Map.Entry<String, Value> find(EnumsReader.EnumState enumList, String id) {
@ -120,10 +122,6 @@ public class PinoutLogic {
} }
String keyValueForm = VariableRegistry.getHumanSortedTsKeyValueString(pinMap); String keyValueForm = VariableRegistry.getHumanSortedTsKeyValueString(pinMap);
return new EnumPair(keyValueForm, simpleForm.toString()); return new EnumPair(keyValueForm, simpleForm.toString());
// String shorterForm = smartForm.length() < simpleForm.length() ? smartForm.toString() : simpleForm.toString();
//
// return new EnumPair(shorterForm, simpleForm.toString());
} }
private static void appendCommaIfNeeded(StringBuilder sb) { private static void appendCommaIfNeeded(StringBuilder sb) {
@ -143,19 +141,19 @@ public class PinoutLogic {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private void readMetaInfo(File yamlFile) throws IOException { private void readMetaInfo(String yamlName, Reader reader) {
Yaml yaml = new Yaml(); Yaml yaml = new Yaml();
Map<String, Object> yamlData = yaml.load(new FileReader(yamlFile)); Map<String, Object> yamlData = yaml.load(reader);
if (yamlData == null) { if (yamlData == null) {
SystemOut.println("Null yaml for " + yamlFile); SystemOut.println("Null yaml for " + yamlName);
return; return;
} }
List<Map<String, Object>> data = (List<Map<String, Object>>) yamlData.get("pins"); List<Map<String, Object>> data = (List<Map<String, Object>>) yamlData.get("pins");
if (data == null) { if (data == null) {
SystemOut.println("Null yaml for " + yamlFile); SystemOut.println("Null yaml for " + yamlName);
return; return;
} }
log.info("Got from " + yamlFile + ": " + data); log.info("Got from " + yamlName + ": " + data);
Objects.requireNonNull(data, "data"); Objects.requireNonNull(data, "data");
for (Map<String, Object> pin : data) { for (Map<String, Object> pin : data) {
Object pinId = pin.get("id"); Object pinId = pin.get("id");
@ -211,7 +209,6 @@ public class PinoutLogic {
} }
public void registerBoardSpecificPinNames(VariableRegistry registry, ReaderStateImpl state, DefinitionsState parseState) throws IOException { public void registerBoardSpecificPinNames(VariableRegistry registry, ReaderStateImpl state, DefinitionsState parseState) throws IOException {
readFiles();
registerPins(boardName, globalList, registry, state, parseState); registerPins(boardName, globalList, registry, state, parseState);
try (FileWriter getTsNameByIdFile = new FileWriter(boardName + PinoutLogic.CONNECTORS + File.separator + "generated_ts_name_by_pin.cpp")) { try (FileWriter getTsNameByIdFile = new FileWriter(boardName + PinoutLogic.CONNECTORS + File.separator + "generated_ts_name_by_pin.cpp")) {
@ -235,15 +232,22 @@ public class PinoutLogic {
} }
} }
private void readFiles() throws IOException { public void readFiles() throws IOException {
for (File yamlFile : boardYamlFiles) { for (File yamlFile : boardYamlFiles) {
header.append("// auto-generated by PinoutLogic.java based on " + yamlFile + "\n"); header.append("// auto-generated by PinoutLogic.java based on " + yamlFile + "\n");
readMetaInfo(yamlFile); readMetaInfo(yamlFile.toString(), new FileReader(yamlFile));
} }
log.info("Got from " + boardYamlFiles.length + " file(s): " + this); log.info("Got from " + boardYamlFiles.length + " file(s): " + this);
} }
/**
* @return list of affected files so that we can generate total unique ID
*/
public List<String> getInputFiles() { public List<String> getInputFiles() {
return inputFiles;
}
private static List<String> getInputFilesList(String boardName, File[] boardYamlFiles) {
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
for (File yamlFile : boardYamlFiles) { for (File yamlFile : boardYamlFiles) {
result.add(boardName + PinoutLogic.CONNECTORS + File.separator + yamlFile.getName()); result.add(boardName + PinoutLogic.CONNECTORS + File.separator + yamlFile.getName());