refactoring
This commit is contained in:
parent
e6095e1e56
commit
f3897cda58
|
@ -223,9 +223,9 @@ public class ConfigDefinition {
|
|||
* we have '-readfile OUTPUTS_SECTION' in one of .sh files in order to template rusefi.input
|
||||
* Same with '-readfile DATALOG_SECTION'
|
||||
*/
|
||||
state.destinations.add(new OutputsSectionConsumer(tsOutputsDestination + File.separator + "generated/output_channels.ini", state));
|
||||
state.destinations.add(new OutputsSectionConsumer(tsOutputsDestination + File.separator + "generated/output_channels.ini"));
|
||||
state.destinations.add(new DataLogConsumer(tsOutputsDestination + File.separator + "generated/data_logs.ini"));
|
||||
state.destinations.add(new GaugeConsumer(tsOutputsDestination + File.separator + "generated/gauges.ini", state));
|
||||
state.destinations.add(new GaugeConsumer(tsOutputsDestination + File.separator + "generated/gauges.ini"));
|
||||
}
|
||||
if (tsInputFileFolder != null) {
|
||||
state.destinations.add(new TSProjectConsumer(tsInputFileFolder, state));
|
||||
|
|
|
@ -7,7 +7,6 @@ import com.rusefi.VariableRegistry;
|
|||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.CharArrayWriter;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.TreeSet;
|
||||
|
@ -18,7 +17,7 @@ import static org.abego.treelayout.internal.util.java.lang.string.StringUtil.quo
|
|||
public class DataLogConsumer implements ConfigurationConsumer {
|
||||
public static final String UNUSED = "unused";
|
||||
private final String fileName;
|
||||
private final CharArrayWriter tsWriter = new CharArrayWriter();
|
||||
private final StringBuilder tsWriter = new StringBuilder();
|
||||
private final TreeSet<String> comments = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
|
||||
|
||||
public DataLogConsumer(String fileName) {
|
||||
|
@ -38,10 +37,10 @@ public class DataLogConsumer implements ConfigurationConsumer {
|
|||
writeStringToFile(fileName, tsWriter);
|
||||
}
|
||||
|
||||
private void writeStringToFile(@Nullable String fileName, CharArrayWriter writer) throws IOException {
|
||||
private void writeStringToFile(@Nullable String fileName, StringBuilder writer) throws IOException {
|
||||
if (fileName != null) {
|
||||
FileWriter fw = new FileWriter(fileName);
|
||||
fw.write(writer.toCharArray());
|
||||
fw.write(writer.toString());
|
||||
fw.close();
|
||||
}
|
||||
}
|
||||
|
@ -87,7 +86,7 @@ public class DataLogConsumer implements ConfigurationConsumer {
|
|||
return comment;
|
||||
}
|
||||
|
||||
public CharArrayWriter getTsWriter() {
|
||||
return tsWriter;
|
||||
public String getContent() {
|
||||
return tsWriter.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ public class FileJavaFieldsConsumer extends JavaFieldsConsumer {
|
|||
@Override
|
||||
public void endFile() throws IOException {
|
||||
javaFields.write(state.variableRegistry.getJavaConstants());
|
||||
javaFields.write(getJavaFieldsWriter());
|
||||
javaFields.write(getContent());
|
||||
|
||||
allFields.append("\t};" + EOL);
|
||||
javaFields.write(allFields.toString());
|
||||
|
|
|
@ -12,19 +12,17 @@ import static org.abego.treelayout.internal.util.java.lang.string.StringUtil.quo
|
|||
|
||||
public class GaugeConsumer implements ConfigurationConsumer {
|
||||
private final String fileName;
|
||||
private final ReaderState state;
|
||||
private final CharArrayWriter charArrayWriter = new CharArrayWriter();
|
||||
private final StringBuilder charArrayWriter = new StringBuilder();
|
||||
|
||||
public GaugeConsumer(String fileName, ReaderState state) {
|
||||
public GaugeConsumer(String fileName) {
|
||||
this.fileName = fileName;
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleEndStruct(ReaderState readerState, ConfigStructure structure) throws IOException {
|
||||
if (state.stack.isEmpty()) {
|
||||
PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(state, structure.tsFields, "",
|
||||
(state, configField, prefix) -> handle(configField, prefix));
|
||||
if (readerState.stack.isEmpty()) {
|
||||
PerFieldWithStructuresIterator iterator = new PerFieldWithStructuresIterator(readerState, structure.tsFields, "",
|
||||
(state, configField, prefix) -> handle(readerState, configField, prefix));
|
||||
iterator.loop();
|
||||
String content = iterator.getContent();
|
||||
charArrayWriter.append(content);
|
||||
|
@ -32,13 +30,13 @@ public class GaugeConsumer implements ConfigurationConsumer {
|
|||
|
||||
if (fileName != null) {
|
||||
FileWriter fw = new FileWriter(fileName);
|
||||
fw.write(charArrayWriter.toCharArray());
|
||||
fw.write(charArrayWriter.toString());
|
||||
fw.close();
|
||||
}
|
||||
}
|
||||
|
||||
private String handle(ConfigField configField, String prefix) {
|
||||
String comment = getComment("", configField, state.variableRegistry);
|
||||
private String handle(ReaderState readerState, ConfigField configField, String prefix) {
|
||||
String comment = getComment("", configField, readerState.variableRegistry);
|
||||
comment = ConfigField.unquote(comment);
|
||||
if (!prefix.isEmpty()) {
|
||||
comment = prefix + " " + comment;
|
||||
|
@ -60,7 +58,7 @@ public class GaugeConsumer implements ConfigurationConsumer {
|
|||
"\n";
|
||||
}
|
||||
|
||||
public CharArrayWriter getTsWriter() {
|
||||
return charArrayWriter;
|
||||
public String getContent() {
|
||||
return charArrayWriter.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@ package com.rusefi.output;
|
|||
import com.opensr5.ini.IniFileModel;
|
||||
import com.rusefi.*;
|
||||
|
||||
import java.io.CharArrayWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
@ -14,7 +13,7 @@ public abstract class JavaFieldsConsumer implements ConfigurationConsumer {
|
|||
// todo: why is this field 'static'?
|
||||
protected static final Set<String> javaEnums = new HashSet<>();
|
||||
|
||||
private final CharArrayWriter javaFieldsWriter = new CharArrayWriter();
|
||||
private final StringBuilder content = new StringBuilder();
|
||||
protected final StringBuffer allFields = new StringBuffer("\tpublic static final Field[] VALUES = {" + EOL);
|
||||
protected final ReaderState state;
|
||||
|
||||
|
@ -22,15 +21,15 @@ public abstract class JavaFieldsConsumer implements ConfigurationConsumer {
|
|||
this.state = state;
|
||||
}
|
||||
|
||||
public String getJavaFieldsWriter() {
|
||||
return javaFieldsWriter.toString();
|
||||
public String getContent() {
|
||||
return content.toString();
|
||||
}
|
||||
|
||||
private void writeJavaFieldName(String nameWithPrefix, int tsPosition, double scale) throws IOException {
|
||||
javaFieldsWriter.write("\tpublic static final Field ");
|
||||
content.append("\tpublic static final Field ");
|
||||
allFields.append("\t" + nameWithPrefix.toUpperCase() + "," + EOL);
|
||||
javaFieldsWriter.write(nameWithPrefix.toUpperCase());
|
||||
javaFieldsWriter.write(" = Field.create(\"" + nameWithPrefix.toUpperCase() + "\", "
|
||||
content.append(nameWithPrefix.toUpperCase());
|
||||
content.append(" = Field.create(\"" + nameWithPrefix.toUpperCase() + "\", "
|
||||
+ tsPosition + ", ");
|
||||
}
|
||||
|
||||
|
@ -73,20 +72,20 @@ public abstract class JavaFieldsConsumer implements ConfigurationConsumer {
|
|||
|
||||
if (configField.isBit()) {
|
||||
writeJavaFieldName(nameWithPrefix, tsPosition, 1);
|
||||
javaFieldsWriter.append("FieldType.BIT, " + bitIndex + ");" + EOL);
|
||||
content.append("FieldType.BIT, " + bitIndex + ");" + EOL);
|
||||
tsPosition += configField.getSize(next);
|
||||
return tsPosition;
|
||||
}
|
||||
|
||||
if (TypesHelper.isFloat(configField.getType())) {
|
||||
writeJavaFieldName(nameWithPrefix, tsPosition, configField.autoscaleSpecNumber());
|
||||
javaFieldsWriter.write("FieldType.FLOAT);" + EOL);
|
||||
content.append("FieldType.FLOAT);" + EOL);
|
||||
} else {
|
||||
String enumOptions = state.variableRegistry.get(configField.getType() + VariableRegistry.ENUM_SUFFIX);
|
||||
|
||||
if (enumOptions != null && !javaEnums.contains(configField.getType())) {
|
||||
javaEnums.add(configField.getType());
|
||||
javaFieldsWriter.write("\tpublic static final String[] " + configField.getType() + " = {" + enumOptions + "};" + EOL);
|
||||
content.append("\tpublic static final String[] " + configField.getType() + " = {" + enumOptions + "};" + EOL);
|
||||
}
|
||||
|
||||
|
||||
|
@ -95,14 +94,14 @@ public abstract class JavaFieldsConsumer implements ConfigurationConsumer {
|
|||
String custom = state.tsCustomLine.get(configField.getType());
|
||||
String[] tokens = custom.split(",");
|
||||
String stringSize = tokens[3].trim();
|
||||
javaFieldsWriter.write(stringSize + ", FieldType.STRING");
|
||||
content.append(stringSize + ", FieldType.STRING");
|
||||
} else {
|
||||
javaFieldsWriter.write(getJavaType(configField.getElementSize()));
|
||||
content.append(getJavaType(configField.getElementSize()));
|
||||
}
|
||||
if (enumOptions != null) {
|
||||
javaFieldsWriter.write(", " + configField.getType());
|
||||
content.append(", " + configField.getType());
|
||||
}
|
||||
javaFieldsWriter.write(")" + ".setScale(" + configField.autoscaleSpecNumber() + ")" +
|
||||
content.append(")" + ".setScale(" + configField.autoscaleSpecNumber() + ")" +
|
||||
";" + EOL);
|
||||
}
|
||||
|
||||
|
|
|
@ -10,12 +10,10 @@ import java.io.*;
|
|||
public class OutputsSectionConsumer implements ConfigurationConsumer {
|
||||
private final String tsOutputsSectionFileName;
|
||||
private final TsOutput tsOutput;
|
||||
private final ReaderState state;
|
||||
|
||||
public OutputsSectionConsumer(String tsOutputsSectionFileName, ReaderState state) {
|
||||
public OutputsSectionConsumer(String tsOutputsSectionFileName) {
|
||||
this.tsOutputsSectionFileName = tsOutputsSectionFileName;
|
||||
tsOutput = new TsOutput(state, false);
|
||||
this.state = state;
|
||||
tsOutput = new TsOutput(false);
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
|
@ -28,7 +26,7 @@ public class OutputsSectionConsumer implements ConfigurationConsumer {
|
|||
|
||||
tsOutput.run(readerState, structure, 0);
|
||||
|
||||
if (state.stack.isEmpty()) {
|
||||
if (readerState.stack.isEmpty()) {
|
||||
if (tsOutputsSectionFileName != null) {
|
||||
FileWriter fos = new FileWriter(tsOutputsSectionFileName);
|
||||
fos.write(tsOutput.getContent());
|
||||
|
|
|
@ -7,7 +7,6 @@ import com.rusefi.util.SystemOut;
|
|||
|
||||
import java.io.*;
|
||||
|
||||
import static com.rusefi.ToolUtil.EOL;
|
||||
import static com.rusefi.util.IoUtils.CHARSET;
|
||||
|
||||
/**
|
||||
|
@ -28,7 +27,7 @@ public class TSProjectConsumer implements ConfigurationConsumer {
|
|||
|
||||
public TSProjectConsumer(String tsPath, ReaderState state) {
|
||||
this.tsPath = tsPath;
|
||||
tsOutput = new TsOutput(state, true);
|
||||
tsOutput = new TsOutput(true);
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,6 @@ import com.rusefi.ReaderState;
|
|||
import com.rusefi.TypesHelper;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static com.rusefi.ToolUtil.EOL;
|
||||
|
||||
|
@ -17,12 +16,10 @@ import static com.rusefi.ToolUtil.EOL;
|
|||
@SuppressWarnings({"StringConcatenationInsideStringBufferAppend", "DanglingJavadoc"})
|
||||
public class TsOutput {
|
||||
private final StringBuilder settingContextHelp = new StringBuilder();
|
||||
private final ReaderState state;
|
||||
private final boolean isConstantsSection;
|
||||
private final StringBuilder tsHeader = new StringBuilder();
|
||||
|
||||
public TsOutput(ReaderState state, boolean longForm) {
|
||||
this.state = state;
|
||||
public TsOutput(boolean longForm) {
|
||||
this.isConstantsSection = longForm;
|
||||
}
|
||||
|
||||
|
@ -128,7 +125,7 @@ public class TsOutput {
|
|||
|
||||
private String handleTsInfo(String tsInfo, int multiplierIndex) {
|
||||
try {
|
||||
String[] fields = tsInfo.split("\\,");
|
||||
String[] fields = tsInfo.split(",");
|
||||
if (fields.length > multiplierIndex) {
|
||||
/**
|
||||
* Evaluate static math on .ini layer to simplify rusEFI java and rusEFI PHP project consumers
|
||||
|
|
|
@ -4,11 +4,7 @@ import com.rusefi.ReaderState;
|
|||
import com.rusefi.output.JavaFieldsConsumer;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
|
@ -58,6 +54,6 @@ public class ConfigFieldParserIssue1057Test {
|
|||
"\tpublic static final Field UNUSEDBIT_1_30 = Field.create(\"UNUSEDBIT_1_30\", 0, FieldType.BIT, 30);\n" +
|
||||
"\tpublic static final Field UNUSEDBIT_1_31 = Field.create(\"UNUSEDBIT_1_31\", 0, FieldType.BIT, 31);\n" +
|
||||
"\tpublic static final Field FIELDNAME = Field.create(\"FIELDNAME\", 4, FieldType.INT).setScale(1.0);\n",
|
||||
javaFieldsConsumer.getJavaFieldsWriter());
|
||||
javaFieldsConsumer.getContent());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -185,7 +185,7 @@ public class ConfigFieldParserTest {
|
|||
assertEquals("\tpublic static final Field VAR = Field.create(\"VAR\", 0, 120, FieldType.STRING).setScale(1.0);\n" +
|
||||
"\tpublic static final Field PERIODMS = Field.create(\"PERIODMS\", 120, FieldType.INT16).setScale(1.0);\n" +
|
||||
"\tpublic static final Field ALIGNMENTFILL_AT_122 = Field.create(\"ALIGNMENTFILL_AT_122\", 122, FieldType.INT8).setScale(1.0);\n",
|
||||
javaFieldsConsumer.getJavaFieldsWriter());
|
||||
javaFieldsConsumer.getContent());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -266,7 +266,7 @@ public class ConfigFieldParserTest {
|
|||
"\tpublic static final Field ETB2_PERIODMS = Field.create(\"ETB2_PERIODMS\", 18, FieldType.INT16).setScale(1.0);\n" +
|
||||
"\tpublic static final Field ETB2_MINVALUE = Field.create(\"ETB2_MINVALUE\", 20, FieldType.INT16).setScale(1.0);\n" +
|
||||
"\tpublic static final Field ETB2_ALIGNMENTFILL_AT_6 = Field.create(\"ETB2_ALIGNMENTFILL_AT_6\", 22, FieldType.INT8).setScale(1.0);\n",
|
||||
javaFieldsConsumer.getJavaFieldsWriter());
|
||||
javaFieldsConsumer.getContent());
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -282,7 +282,7 @@ public class ConfigFieldParserTest {
|
|||
JavaFieldsConsumer javaFieldsConsumer = new TestJavaFieldsConsumer(state);
|
||||
state.readBufferedReader(test, consumer, javaFieldsConsumer);
|
||||
assertEquals("\tpublic static final Field FIELD1 = Field.create(\"FIELD1\", 0, FieldType.INT).setScale(0.01);\n",
|
||||
javaFieldsConsumer.getJavaFieldsWriter());
|
||||
javaFieldsConsumer.getContent());
|
||||
assertEquals("// start of pid_s\n" +
|
||||
"struct pid_s {\n" +
|
||||
"\t/**\n" +
|
||||
|
|
|
@ -20,7 +20,7 @@ public class OutputsTest {
|
|||
"end_struct\n";
|
||||
ReaderState state = new ReaderState();
|
||||
|
||||
OutputsSectionConsumer tsProjectConsumer = new OutputsSectionConsumer(null, state);
|
||||
OutputsSectionConsumer tsProjectConsumer = new OutputsSectionConsumer(null);
|
||||
state.readBufferedReader(test, tsProjectConsumer);
|
||||
|
||||
|
||||
|
@ -73,7 +73,7 @@ public class OutputsTest {
|
|||
"end_struct\n";
|
||||
ReaderState state = new ReaderState();
|
||||
|
||||
OutputsSectionConsumer tsProjectConsumer = new OutputsSectionConsumer(null, state);
|
||||
OutputsSectionConsumer tsProjectConsumer = new OutputsSectionConsumer(null);
|
||||
state.readBufferedReader(test, (tsProjectConsumer));
|
||||
}
|
||||
|
||||
|
@ -107,7 +107,7 @@ public class OutputsTest {
|
|||
"entry = afr_typet, \"afr_typet\", int, \"%d\"\n" +
|
||||
"entry = vehicleSpeedKph, \"vehicleSpeedKph\", int, \"%d\"\n" +
|
||||
"entry = isForcedInduction, \"Does the vehicle have a turbo or supercharger?\", int, \"%d\"\n" +
|
||||
"entry = enableFan1WithAc, \"+Turn on this fan when AC is on.\", int, \"%d\"\n", new String(dataLogConsumer.getTsWriter().toCharArray()));
|
||||
"entry = enableFan1WithAc, \"+Turn on this fan when AC is on.\", int, \"%d\"\n", dataLogConsumer.getContent());
|
||||
|
||||
}
|
||||
|
||||
|
@ -280,7 +280,6 @@ public class OutputsTest {
|
|||
"\t\treturn config->enableFan1WithAc;\n" +
|
||||
"\treturn EFI_ERROR_CODE;\n" +
|
||||
"}\n", getConfigValueConsumer.getGetterForUnitTest());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -296,20 +295,20 @@ public class OutputsTest {
|
|||
|
||||
ReaderState state = new ReaderState();
|
||||
DataLogConsumer dataLogConsumer = new DataLogConsumer(null);
|
||||
GaugeConsumer gaugeConsumer = new GaugeConsumer(null, state);
|
||||
GaugeConsumer gaugeConsumer = new GaugeConsumer(null);
|
||||
state.readBufferedReader(test, dataLogConsumer, gaugeConsumer);
|
||||
assertEquals(
|
||||
"entry = alternatorStatus_iTerm, \"alternatorStatus_iTerm\", float, \"%.3f\"\n" +
|
||||
"entry = alternatorStatus_dTerm, \"alternatorStatus_dTerm\", float, \"%.3f\"\n" +
|
||||
"entry = idleStatus_iTerm, \"idleStatus_iTerm\", float, \"%.3f\"\n" +
|
||||
"entry = idleStatus_dTerm, \"idleStatus_dTerm\", float, \"%.3f\"\n",
|
||||
new String(dataLogConsumer.getTsWriter().toCharArray()));
|
||||
dataLogConsumer.getContent());
|
||||
|
||||
assertEquals("alternatorStatus_iTermGauge = alternatorStatus_iTerm,\"alternatorStatus_ iTerm\", \"v\", -10000.0,10000.0, -10000.0,10000.0, -10000.0,10000.0, 4,4\n" +
|
||||
"alternatorStatus_dTermGauge = alternatorStatus_dTerm,\"alternatorStatus_ dTerm\", \"v\", -10000.0,10000.0, -10000.0,10000.0, -10000.0,10000.0, 4,4\n" +
|
||||
"idleStatus_iTermGauge = idleStatus_iTerm,\"idleStatus_ iTerm\", \"v\", -10000.0,10000.0, -10000.0,10000.0, -10000.0,10000.0, 4,4\n" +
|
||||
"idleStatus_dTermGauge = idleStatus_dTerm,\"idleStatus_ dTerm\", \"v\", -10000.0,10000.0, -10000.0,10000.0, -10000.0,10000.0, 4,4\n",
|
||||
new String(gaugeConsumer.getTsWriter().toCharArray()));
|
||||
gaugeConsumer.getContent());
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@ import com.rusefi.output.JavaFieldsConsumer;
|
|||
import com.rusefi.output.TSProjectConsumer;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.CharArrayWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
@ -65,7 +64,7 @@ public class TSProjectConsumerTest {
|
|||
assertEquals("\tpublic static final Field PERIODMS2 = Field.create(\"PERIODMS2\", 0, FieldType.INT16).setScale(1.0);\n" +
|
||||
"\tpublic static final Field AFRTABLE = Field.create(\"AFRTABLE\", 2, FieldType.INT).setScale(1.0);\n" +
|
||||
"\tpublic static final Field PERIODMS = Field.create(\"PERIODMS\", 18, FieldType.INT16).setScale(1.0);\n",
|
||||
javaFieldsConsumer.getJavaFieldsWriter());
|
||||
javaFieldsConsumer.getContent());
|
||||
|
||||
|
||||
assertEquals("// start of pid_s\n" +
|
||||
|
|
Loading…
Reference in New Issue