refactoring
This commit is contained in:
parent
fca9f998ea
commit
c64e52c940
Binary file not shown.
|
@ -228,8 +228,7 @@ public class ConfigDefinition {
|
||||||
state.destinations.add(new GaugeConsumer(tsOutputsDestination + File.separator + "generated/gauges.ini", state));
|
state.destinations.add(new GaugeConsumer(tsOutputsDestination + File.separator + "generated/gauges.ini", state));
|
||||||
}
|
}
|
||||||
if (tsInputFileFolder != null) {
|
if (tsInputFileFolder != null) {
|
||||||
CharArrayWriter tsWriter = new CharArrayWriter();
|
state.destinations.add(new TSProjectConsumer(tsInputFileFolder, state));
|
||||||
state.destinations.add(new TSProjectConsumer(tsWriter, tsInputFileFolder, state));
|
|
||||||
|
|
||||||
VariableRegistry tmpRegistry = new VariableRegistry();
|
VariableRegistry tmpRegistry = new VariableRegistry();
|
||||||
// store the CRC32 as a built-in variable
|
// store the CRC32 as a built-in variable
|
||||||
|
|
|
@ -11,7 +11,6 @@ public class OutputsSectionConsumer implements ConfigurationConsumer {
|
||||||
private final String tsOutputsSectionFileName;
|
private final String tsOutputsSectionFileName;
|
||||||
private final TsOutput tsOutput;
|
private final TsOutput tsOutput;
|
||||||
private final ReaderState state;
|
private final ReaderState state;
|
||||||
private final StringBuilder tsWriter = new StringBuilder();
|
|
||||||
|
|
||||||
public OutputsSectionConsumer(String tsOutputsSectionFileName, ReaderState state) {
|
public OutputsSectionConsumer(String tsOutputsSectionFileName, ReaderState state) {
|
||||||
this.tsOutputsSectionFileName = tsOutputsSectionFileName;
|
this.tsOutputsSectionFileName = tsOutputsSectionFileName;
|
||||||
|
@ -20,7 +19,7 @@ public class OutputsSectionConsumer implements ConfigurationConsumer {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getContent() {
|
public String getContent() {
|
||||||
return tsWriter.toString();
|
return tsOutput.getContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -28,11 +27,11 @@ public class OutputsSectionConsumer implements ConfigurationConsumer {
|
||||||
System.out.println("handleEndStruct");
|
System.out.println("handleEndStruct");
|
||||||
|
|
||||||
if (state.stack.isEmpty()) {
|
if (state.stack.isEmpty()) {
|
||||||
tsOutput.writeFields(structure, "", tsWriter, 0);
|
tsOutput.writeFields(structure, "", 0);
|
||||||
|
|
||||||
if (tsOutputsSectionFileName != null) {
|
if (tsOutputsSectionFileName != null) {
|
||||||
FileWriter fos = new FileWriter(tsOutputsSectionFileName);
|
FileWriter fos = new FileWriter(tsOutputsSectionFileName);
|
||||||
fos.write(tsWriter.toString());
|
fos.write(tsOutput.getContent());
|
||||||
fos.close();
|
fos.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,14 +21,12 @@ public class TSProjectConsumer implements ConfigurationConsumer {
|
||||||
public static final String SETTING_CONTEXT_HELP_END = "SettingContextHelpEnd";
|
public static final String SETTING_CONTEXT_HELP_END = "SettingContextHelpEnd";
|
||||||
public static final String SETTING_CONTEXT_HELP = "SettingContextHelp";
|
public static final String SETTING_CONTEXT_HELP = "SettingContextHelp";
|
||||||
|
|
||||||
private final CharArrayWriter tsWriter;
|
|
||||||
private final String tsPath;
|
private final String tsPath;
|
||||||
private final ReaderState state;
|
private final ReaderState state;
|
||||||
private int totalTsSize;
|
private int totalTsSize;
|
||||||
private final TsOutput tsOutput;
|
private final TsOutput tsOutput;
|
||||||
|
|
||||||
public TSProjectConsumer(CharArrayWriter tsWriter, String tsPath, ReaderState state) {
|
public TSProjectConsumer(String tsPath, ReaderState state) {
|
||||||
this.tsWriter = tsWriter;
|
|
||||||
this.tsPath = tsPath;
|
this.tsPath = tsPath;
|
||||||
tsOutput = new TsOutput(state, true);
|
tsOutput = new TsOutput(state, true);
|
||||||
this.state = state;
|
this.state = state;
|
||||||
|
@ -142,16 +140,19 @@ public class TSProjectConsumer implements ConfigurationConsumer {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void endFile() throws IOException {
|
public void endFile() throws IOException {
|
||||||
writeTunerStudioFile(tsPath, tsWriter.toString());
|
writeTunerStudioFile(tsPath, getContent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleEndStruct(ReaderState readerState, ConfigStructure structure) throws IOException {
|
public void handleEndStruct(ReaderState readerState, ConfigStructure structure) throws IOException {
|
||||||
state.variableRegistry.register(structure.name + "_size", structure.getTotalSize());
|
state.variableRegistry.register(structure.name + "_size", structure.getTotalSize());
|
||||||
if (state.stack.isEmpty()) {
|
if (state.stack.isEmpty()) {
|
||||||
totalTsSize = tsOutput.writeFields(structure, "", tsWriter, 0);
|
totalTsSize = tsOutput.writeFields(structure, "", 0);
|
||||||
tsWriter.write("; total TS size = " + totalTsSize + EOL);
|
|
||||||
state.variableRegistry.register("TOTAL_CONFIG_SIZE", totalTsSize);
|
state.variableRegistry.register("TOTAL_CONFIG_SIZE", totalTsSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getContent() {
|
||||||
|
return tsOutput.getContent();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,21 +13,27 @@ import static com.rusefi.ToolUtil.EOL;
|
||||||
* Same code is used to generate [Constants] and [OutputChannels] bodies, with just one flag controlling the minor
|
* Same code is used to generate [Constants] and [OutputChannels] bodies, with just one flag controlling the minor
|
||||||
* difference in behaviours
|
* difference in behaviours
|
||||||
*/
|
*/
|
||||||
|
@SuppressWarnings({"StringConcatenationInsideStringBufferAppend", "DanglingJavadoc"})
|
||||||
public class TsOutput {
|
public class TsOutput {
|
||||||
private final StringBuilder settingContextHelp = new StringBuilder();
|
private final StringBuilder settingContextHelp = new StringBuilder();
|
||||||
private final ReaderState state;
|
private final ReaderState state;
|
||||||
private final boolean isConstantsSection;
|
private final boolean isConstantsSection;
|
||||||
|
private final StringBuilder tsHeader = new StringBuilder();
|
||||||
|
|
||||||
public TsOutput(ReaderState state, boolean longForm) {
|
public TsOutput(ReaderState state, boolean longForm) {
|
||||||
this.state = state;
|
this.state = state;
|
||||||
this.isConstantsSection = longForm;
|
this.isConstantsSection = longForm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getContent() {
|
||||||
|
return tsHeader.toString();
|
||||||
|
}
|
||||||
|
|
||||||
public StringBuilder getSettingContextHelp() {
|
public StringBuilder getSettingContextHelp() {
|
||||||
return settingContextHelp;
|
return settingContextHelp;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int writeOneField(FieldIterator it, String prefix, Appendable tsHeader, int tsPosition) throws IOException {
|
private int writeOneField(FieldIterator it, String prefix, int tsPosition) throws IOException {
|
||||||
ConfigField configField = it.cf;
|
ConfigField configField = it.cf;
|
||||||
ConfigField next = it.next;
|
ConfigField next = it.next;
|
||||||
int bitIndex = it.bitState.get();
|
int bitIndex = it.bitState.get();
|
||||||
|
@ -47,7 +53,7 @@ public class TsOutput {
|
||||||
|
|
||||||
if (cs != null) {
|
if (cs != null) {
|
||||||
String extraPrefix = cs.withPrefix ? configField.getName() + "_" : "";
|
String extraPrefix = cs.withPrefix ? configField.getName() + "_" : "";
|
||||||
return writeFields(cs, prefix + extraPrefix, tsHeader, tsPosition);
|
return writeFields(cs, prefix + extraPrefix, tsPosition);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (configField.isBit()) {
|
if (configField.isBit()) {
|
||||||
|
@ -98,7 +104,7 @@ public class TsOutput {
|
||||||
} else {
|
} else {
|
||||||
tsHeader.append("x");
|
tsHeader.append("x");
|
||||||
}
|
}
|
||||||
tsHeader.append(Integer.toString(size));
|
tsHeader.append(size);
|
||||||
}
|
}
|
||||||
tsHeader.append("], " + handleTsInfo(configField.getTsInfo(), 1));
|
tsHeader.append("], " + handleTsInfo(configField.getTsInfo(), 1));
|
||||||
|
|
||||||
|
@ -109,15 +115,19 @@ public class TsOutput {
|
||||||
return tsPosition;
|
return tsPosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int writeFields(ConfigStructure configStructure, String prefix, Appendable tsHeader, int tsPosition) throws IOException {
|
protected int writeFields(ConfigStructure configStructure, String prefix, int tsPosition) throws IOException {
|
||||||
FieldIterator iterator = new FieldIterator(configStructure.tsFields);
|
FieldIterator iterator = new FieldIterator(configStructure.tsFields);
|
||||||
for (int i = 0; i < configStructure.tsFields.size(); i++) {
|
for (int i = 0; i < configStructure.tsFields.size(); i++) {
|
||||||
iterator.start(i);
|
iterator.start(i);
|
||||||
|
|
||||||
tsPosition = writeOneField(iterator, prefix, tsHeader, tsPosition);
|
tsPosition = writeOneField(iterator, prefix, tsPosition);
|
||||||
|
|
||||||
iterator.end();
|
iterator.end();
|
||||||
}
|
}
|
||||||
|
if (prefix.isEmpty()) {
|
||||||
|
// empty prefix means top level
|
||||||
|
tsHeader.append("; total TS size = " + tsPosition + EOL);
|
||||||
|
}
|
||||||
return tsPosition;
|
return tsPosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,7 @@ public class BitParsingTest {
|
||||||
|
|
||||||
StringWriter sw = new StringWriter();
|
StringWriter sw = new StringWriter();
|
||||||
|
|
||||||
TSProjectConsumer javaFieldsConsumer = new TSProjectConsumer(new CharArrayWriter(), "", state) {
|
TSProjectConsumer javaFieldsConsumer = new TSProjectConsumer("", state) {
|
||||||
@Override
|
@Override
|
||||||
protected void writeTunerStudioFile(String tsPath, String fieldsSection) throws IOException {
|
protected void writeTunerStudioFile(String tsPath, String fieldsSection) throws IOException {
|
||||||
writeContent(fieldsSection, new TsFileContent("", ""), createOutput(sw));
|
writeContent(fieldsSection, new TsFileContent("", ""), createOutput(sw));
|
||||||
|
|
|
@ -10,10 +10,7 @@ import com.rusefi.output.JavaFieldsConsumer;
|
||||||
import com.rusefi.output.TSProjectConsumer;
|
import com.rusefi.output.TSProjectConsumer;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.CharArrayWriter;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.Collections;
|
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
@ -58,12 +55,11 @@ public class ConfigFieldParserTest {
|
||||||
"end_struct\n";
|
"end_struct\n";
|
||||||
ReaderState state = new ReaderState();
|
ReaderState state = new ReaderState();
|
||||||
|
|
||||||
CharArrayWriter writer = new CharArrayWriter();
|
TestTSProjectConsumer tsProjectConsumer = new TestTSProjectConsumer("", state);
|
||||||
TestTSProjectConsumer tsProjectConsumer = new TestTSProjectConsumer(writer, "", state);
|
|
||||||
state.readBufferedReader(test, tsProjectConsumer);
|
state.readBufferedReader(test, tsProjectConsumer);
|
||||||
assertEquals("afr_type = scalar, F32, 0, \"ms\", 1, 0, 0, 3000, 0\n" +
|
assertEquals("afr_type = scalar, F32, 0, \"ms\", 1, 0, 0, 3000, 0\n" +
|
||||||
"afr_typet = scalar, F32, 4, \"ms\", 1, 0, 0, 3000, 0\n" +
|
"afr_typet = scalar, F32, 4, \"ms\", 1, 0, 0, 3000, 0\n" +
|
||||||
"; total TS size = 8\n", new String(writer.toCharArray()));
|
"; total TS size = 8\n", tsProjectConsumer.getContent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = IllegalStateException.class)
|
@Test(expected = IllegalStateException.class)
|
||||||
|
@ -89,11 +85,10 @@ public class ConfigFieldParserTest {
|
||||||
"end_struct\n";
|
"end_struct\n";
|
||||||
ReaderState state = new ReaderState();
|
ReaderState state = new ReaderState();
|
||||||
|
|
||||||
CharArrayWriter writer = new CharArrayWriter();
|
TestTSProjectConsumer tsProjectConsumer = new TestTSProjectConsumer("", state);
|
||||||
TestTSProjectConsumer tsProjectConsumer = new TestTSProjectConsumer(writer, "", state);
|
|
||||||
state.readBufferedReader(test, (tsProjectConsumer));
|
state.readBufferedReader(test, (tsProjectConsumer));
|
||||||
assertEquals("afr_type = bits, S32, 0, [0:1], \"BPSX\", \"Innovate\", \"14Point7\", \"INVALID\"\n" +
|
assertEquals("afr_type = bits, S32, 0, [0:1], \"BPSX\", \"Innovate\", \"14Point7\", \"INVALID\"\n" +
|
||||||
"; total TS size = 4\n", new String(writer.toCharArray()));
|
"; total TS size = 4\n", tsProjectConsumer.getContent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -162,16 +157,15 @@ public class ConfigFieldParserTest {
|
||||||
"end_struct\n" +
|
"end_struct\n" +
|
||||||
"";
|
"";
|
||||||
|
|
||||||
CharArrayWriter writer = new CharArrayWriter();
|
|
||||||
ReaderState state = new ReaderState();
|
ReaderState state = new ReaderState();
|
||||||
TSProjectConsumer tsProjectConsumer = new TestTSProjectConsumer(writer, "", state);
|
TSProjectConsumer tsProjectConsumer = new TestTSProjectConsumer("", state);
|
||||||
|
|
||||||
state.readBufferedReader(test, (tsProjectConsumer));
|
state.readBufferedReader(test, (tsProjectConsumer));
|
||||||
|
|
||||||
assertEquals("periodMs = scalar, S16, 0, \"ms\", 0.1, 0, 0, 3000, 0\n" +
|
assertEquals("periodMs = scalar, S16, 0, \"ms\", 0.1, 0, 0, 3000, 0\n" +
|
||||||
"periodMs2 = scalar, S16, 2, \"ms\", 1, 0, 0, 3000, 0\n" +
|
"periodMs2 = scalar, S16, 2, \"ms\", 1, 0, 0, 3000, 0\n" +
|
||||||
"afrTable = array, U08, 4, [4x4],\"deg\", 0.1, 0, 0, 25.0, 1\n" +
|
"afrTable = array, U08, 4, [4x4],\"deg\", 0.1, 0, 0, 25.0, 1\n" +
|
||||||
"; total TS size = 20\n", new String(writer.toCharArray()));
|
"; total TS size = 20\n", tsProjectConsumer.getContent());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -376,8 +370,7 @@ public class ConfigFieldParserTest {
|
||||||
"end_struct\n";
|
"end_struct\n";
|
||||||
ReaderState state = new ReaderState();
|
ReaderState state = new ReaderState();
|
||||||
|
|
||||||
CharArrayWriter writer = new CharArrayWriter();
|
TestTSProjectConsumer tsProjectConsumer = new TestTSProjectConsumer("", state);
|
||||||
TestTSProjectConsumer tsProjectConsumer = new TestTSProjectConsumer(writer, "", state);
|
|
||||||
state.readBufferedReader(test, (tsProjectConsumer));
|
state.readBufferedReader(test, (tsProjectConsumer));
|
||||||
assertEquals("pid_afr_type = scalar, F32, 0, \"ms\", 1, 0, 0, 3000, 0\n" +
|
assertEquals("pid_afr_type = scalar, F32, 0, \"ms\", 1, 0, 0, 3000, 0\n" +
|
||||||
"pid_afr_typet = scalar, F32, 4, \"ms\", 1, 0, 0, 3000, 0\n" +
|
"pid_afr_typet = scalar, F32, 4, \"ms\", 1, 0, 0, 3000, 0\n" +
|
||||||
|
@ -413,7 +406,7 @@ public class ConfigFieldParserTest {
|
||||||
"pid_unusedBit_4_29 = bits, U32, 8, [29:29], \"false\", \"true\"\n" +
|
"pid_unusedBit_4_29 = bits, U32, 8, [29:29], \"false\", \"true\"\n" +
|
||||||
"pid_unusedBit_4_30 = bits, U32, 8, [30:30], \"false\", \"true\"\n" +
|
"pid_unusedBit_4_30 = bits, U32, 8, [30:30], \"false\", \"true\"\n" +
|
||||||
"pid_unusedBit_4_31 = bits, U32, 8, [31:31], \"false\", \"true\"\n" +
|
"pid_unusedBit_4_31 = bits, U32, 8, [31:31], \"false\", \"true\"\n" +
|
||||||
"; total TS size = 12\n", new String(writer.toCharArray()));
|
"; total TS size = 12\n", tsProjectConsumer.getContent());
|
||||||
assertEquals(
|
assertEquals(
|
||||||
"\tpid_afr_type = \"PID dTime\"\n" +
|
"\tpid_afr_type = \"PID dTime\"\n" +
|
||||||
"\tpid_afr_typet = \"PID dTime\"\n" +
|
"\tpid_afr_typet = \"PID dTime\"\n" +
|
||||||
|
|
|
@ -58,7 +58,8 @@ public class OutputsTest {
|
||||||
"unusedBit_4_29 = bits, U32, 5, [29:29]\n" +
|
"unusedBit_4_29 = bits, U32, 5, [29:29]\n" +
|
||||||
"unusedBit_4_30 = bits, U32, 5, [30:30]\n" +
|
"unusedBit_4_30 = bits, U32, 5, [30:30]\n" +
|
||||||
"unusedBit_4_31 = bits, U32, 5, [31:31]\n" +
|
"unusedBit_4_31 = bits, U32, 5, [31:31]\n" +
|
||||||
"alignmentFill_at_9 = array, U08, 9, [3], \"units\", 1, 0\n", new String(tsProjectConsumer.getContent()));
|
"alignmentFill_at_9 = array, U08, 9, [3], \"units\", 1, 0\n" +
|
||||||
|
"; total TS size = 12\n", tsProjectConsumer.getContent());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,6 @@ import org.junit.Test;
|
||||||
|
|
||||||
import java.io.CharArrayWriter;
|
import java.io.CharArrayWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
@ -41,9 +40,8 @@ public class TSProjectConsumerTest {
|
||||||
"end_struct\n" +
|
"end_struct\n" +
|
||||||
"";
|
"";
|
||||||
|
|
||||||
CharArrayWriter writer = new CharArrayWriter();
|
|
||||||
ReaderState state = new ReaderState();
|
ReaderState state = new ReaderState();
|
||||||
TSProjectConsumer tsProjectConsumer = new TestTSProjectConsumer(writer, "", state);
|
TSProjectConsumer tsProjectConsumer = new TestTSProjectConsumer("", state);
|
||||||
JavaFieldsConsumer javaFieldsConsumer = new TestJavaFieldsConsumer(state);
|
JavaFieldsConsumer javaFieldsConsumer = new TestJavaFieldsConsumer(state);
|
||||||
|
|
||||||
|
|
||||||
|
@ -62,7 +60,7 @@ public class TSProjectConsumerTest {
|
||||||
"afrTable = array, U08, 2, [4x4],\"deg\", 0.1, 0, 0, 25.0, 1\n" +
|
"afrTable = array, U08, 2, [4x4],\"deg\", 0.1, 0, 0, 25.0, 1\n" +
|
||||||
"#endif\n" +
|
"#endif\n" +
|
||||||
"periodMs = scalar, S16, 18, \"ms\", 0.1, 0, 0, 3000, 0\n" +
|
"periodMs = scalar, S16, 18, \"ms\", 0.1, 0, 0, 3000, 0\n" +
|
||||||
"; total TS size = 20\n", new String(writer.toCharArray()));
|
"; total TS size = 20\n", tsProjectConsumer.getContent());
|
||||||
|
|
||||||
assertEquals("\tpublic static final Field PERIODMS2 = Field.create(\"PERIODMS2\", 0, FieldType.INT16).setScale(1.0);\n" +
|
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 AFRTABLE = Field.create(\"AFRTABLE\", 2, FieldType.INT).setScale(1.0);\n" +
|
||||||
|
|
|
@ -6,8 +6,8 @@ import com.rusefi.output.TSProjectConsumer;
|
||||||
import java.io.CharArrayWriter;
|
import java.io.CharArrayWriter;
|
||||||
|
|
||||||
public class TestTSProjectConsumer extends TSProjectConsumer {
|
public class TestTSProjectConsumer extends TSProjectConsumer {
|
||||||
public TestTSProjectConsumer(CharArrayWriter writer, String tsPath, ReaderState state) {
|
public TestTSProjectConsumer(String tsPath, ReaderState state) {
|
||||||
super(writer, tsPath, state);
|
super(tsPath, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue