more refactoring - mostly reducing complexity of code flow, end goal is making class testable
This commit is contained in:
parent
a0b812f47d
commit
a294224725
|
@ -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<String> getInputFiles();
|
||||
|
||||
Writer getWriter() throws IOException;
|
||||
}
|
|
@ -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<File> 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<String> getInputFiles() {
|
||||
List<String> 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");
|
||||
}
|
||||
}
|
|
@ -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<PinState> globalList = new ArrayList<>();
|
||||
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 List<String> 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<String, Value> 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<String> getInputFiles() {
|
||||
return inputFiles;
|
||||
}
|
||||
|
||||
private static List<String> getInputFilesList(String boardName, File[] boardYamlFiles) {
|
||||
List<String> 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 +
|
||||
'}';
|
||||
|
|
Loading…
Reference in New Issue