From a294224725485c8fb6b079a9688865b00d6e5230 Mon Sep 17 00:00:00 2001 From: rusefillc Date: Sat, 11 Feb 2023 14:58:18 -0500 Subject: [PATCH] more refactoring - mostly reducing complexity of code flow, end goal is making class testable --- .../java/com/rusefi/pinout/BoardInputs.java | 19 ++++++ .../pinout/FileSystemBoardInputsImpl.java | 62 +++++++++++++++++++ .../java/com/rusefi/pinout/PinoutLogic.java | 46 ++++---------- 3 files changed, 93 insertions(+), 34 deletions(-) create mode 100644 java_tools/configuration_definition/src/main/java/com/rusefi/pinout/BoardInputs.java create mode 100644 java_tools/configuration_definition/src/main/java/com/rusefi/pinout/FileSystemBoardInputsImpl.java diff --git a/java_tools/configuration_definition/src/main/java/com/rusefi/pinout/BoardInputs.java b/java_tools/configuration_definition/src/main/java/com/rusefi/pinout/BoardInputs.java new file mode 100644 index 0000000000..89f6cc4ed7 --- /dev/null +++ b/java_tools/configuration_definition/src/main/java/com/rusefi/pinout/BoardInputs.java @@ -0,0 +1,19 @@ +package com.rusefi.pinout; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; +import java.util.List; + +public interface BoardInputs { + List getBoardYamlKeys(); + + Reader getReader(Object yamlKey) throws FileNotFoundException; + + String getName(); + + List getInputFiles(); + + Writer getWriter() throws IOException; +} diff --git a/java_tools/configuration_definition/src/main/java/com/rusefi/pinout/FileSystemBoardInputsImpl.java b/java_tools/configuration_definition/src/main/java/com/rusefi/pinout/FileSystemBoardInputsImpl.java new file mode 100644 index 0000000000..b179a82ebc --- /dev/null +++ b/java_tools/configuration_definition/src/main/java/com/rusefi/pinout/FileSystemBoardInputsImpl.java @@ -0,0 +1,62 @@ +package com.rusefi.pinout; + +import com.devexperts.logging.Logging; + +import java.io.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static com.devexperts.logging.Logging.getLogging; + +public class FileSystemBoardInputsImpl implements BoardInputs { + private static final Logging log = getLogging(FileSystemBoardInputsImpl.class); + private final String boardName; + private final List boardYamlFiles; + + public FileSystemBoardInputsImpl(String boardName) { + this.boardName = boardName; + String dirPath = boardName + PinoutLogic.CONNECTORS; + File dirName = new File(dirPath); + FilenameFilter filter = (f, name) -> name.endsWith(".yaml"); + File[] boardYamlFilesArray = dirName.listFiles(filter); + if (boardYamlFilesArray == null) { + log.info("No yaml files in " + dirPath); + boardYamlFiles = Collections.emptyList(); + return; + } + Arrays.sort(boardYamlFilesArray); + log.info(boardName + ": " + Arrays.toString(boardYamlFilesArray)); + boardYamlFiles = Arrays.asList(boardYamlFilesArray); + } + + @Override + public List getBoardYamlKeys() { + return boardYamlFiles; + } + + @Override + public Reader getReader(Object yamlKey) throws FileNotFoundException { + return new FileReader((File)yamlKey); + } + + @Override + public String getName() { + return boardName; + } + + @Override + public List getInputFiles() { + List result = new ArrayList<>(); + for (File yamlFile : boardYamlFiles) { + result.add(boardName + PinoutLogic.CONNECTORS + File.separator + yamlFile.getName()); + } + return result; + } + + @Override + public Writer getWriter() throws IOException { + return new FileWriter(boardName + PinoutLogic.CONNECTORS + File.separator + "generated_ts_name_by_pin.cpp"); + } +} diff --git a/java_tools/configuration_definition/src/main/java/com/rusefi/pinout/PinoutLogic.java b/java_tools/configuration_definition/src/main/java/com/rusefi/pinout/PinoutLogic.java index acccb1b1e2..a2b377b521 100644 --- a/java_tools/configuration_definition/src/main/java/com/rusefi/pinout/PinoutLogic.java +++ b/java_tools/configuration_definition/src/main/java/com/rusefi/pinout/PinoutLogic.java @@ -20,23 +20,18 @@ import static com.rusefi.output.JavaSensorsConsumer.quote; public class PinoutLogic { private static final Logging log = getLogging(PinoutLogic.class); - private static final String CONNECTORS = "/connectors"; + static final String CONNECTORS = "/connectors"; private static final String NONE = "NONE"; private static final String QUOTED_NONE = quote(NONE); public static final String QUOTED_INVALID = quote(VariableRegistry.INVALID); - private final File[] boardYamlFiles; - private final String boardName; private final ArrayList globalList = new ArrayList<>(); private final Map tsNameById = new TreeMap<>(); private final StringBuilder header = new StringBuilder("//DO NOT EDIT MANUALLY, let automation work hard.\n\n"); - private final List inputFiles; + private final BoardInputs boardInputs; - public PinoutLogic(String boardName, File[] boardYamlFiles) { - log.info(boardName + ": " + Arrays.toString(boardYamlFiles)); - this.boardName = boardName; - this.boardYamlFiles = boardYamlFiles; - inputFiles = getInputFilesList(boardName, boardYamlFiles); + public PinoutLogic(BoardInputs boardInputs) { + this.boardInputs = boardInputs; } private static Map.Entry find(EnumsReader.EnumState enumList, String id) { @@ -196,22 +191,13 @@ public class PinoutLogic { } public static PinoutLogic create(String boardName) { - String dirPath = boardName + PinoutLogic.CONNECTORS; - File dirName = new File(dirPath); - FilenameFilter filter = (f, name) -> name.endsWith(".yaml"); - File[] boardYamlFiles = dirName.listFiles(filter); - if (boardYamlFiles == null) { - log.info("No yaml files in " + dirPath); - return null; - } - Arrays.sort(boardYamlFiles); - return new PinoutLogic(boardName, boardYamlFiles); + return new PinoutLogic(new FileSystemBoardInputsImpl(boardName)); } public void registerBoardSpecificPinNames(VariableRegistry registry, ReaderStateImpl state, DefinitionsState parseState) throws IOException { - registerPins(boardName, globalList, registry, state, parseState); + registerPins(boardInputs.getName(), globalList, registry, state, parseState); - try (FileWriter getTsNameByIdFile = new FileWriter(boardName + PinoutLogic.CONNECTORS + File.separator + "generated_ts_name_by_pin.cpp")) { + try (Writer getTsNameByIdFile = boardInputs.getWriter()) { getTsNameByIdFile.append(header); getTsNameByIdFile.append("#include \"pch.h\"\n\n"); @@ -233,26 +219,18 @@ public class PinoutLogic { } public void readFiles() throws IOException { - for (File yamlFile : boardYamlFiles) { + for (Object yamlFile : boardInputs.getBoardYamlKeys()) { header.append("// auto-generated by PinoutLogic.java based on " + yamlFile + "\n"); - readMetaInfo(yamlFile.toString(), new FileReader(yamlFile)); + readMetaInfo(yamlFile.toString(), boardInputs.getReader(yamlFile)); } - log.info("Got from " + boardYamlFiles.length + " file(s): " + this); + log.info("Got from " + boardInputs.getBoardYamlKeys().size() + " file(s): " + this); } /** * @return list of affected files so that we can generate total unique ID */ public List getInputFiles() { - return inputFiles; - } - - private static List getInputFilesList(String boardName, File[] boardYamlFiles) { - List result = new ArrayList<>(); - for (File yamlFile : boardYamlFiles) { - result.add(boardName + PinoutLogic.CONNECTORS + File.separator + yamlFile.getName()); - } - return result; + return boardInputs.getInputFiles(); } private static class PinState { @@ -294,7 +272,7 @@ public class PinoutLogic { @Override public String toString() { return "PinoutLogic{" + - "boardName='" + boardName + '\'' + + "boardName='" + boardInputs.getName() + '\'' + ", globalList=" + globalList + ", tsNameById=" + tsNameById + '}';