rusefi/java_tools/configuration_definition/src/main/java/com/rusefi/output/TSProjectConsumer.java

168 lines
6.3 KiB
Java
Raw Normal View History

2019-05-18 14:10:28 -07:00
package com.rusefi.output;
import com.rusefi.*;
2023-07-02 21:56:19 -07:00
import com.rusefi.util.LazyFileImpl;
2020-02-14 19:44:59 -08:00
import com.rusefi.util.Output;
import com.rusefi.util.SystemOut;
2024-03-18 06:51:18 -07:00
import org.jetbrains.annotations.NotNull;
2018-12-19 21:11:47 -08:00
2018-12-19 21:27:49 -08:00
import java.io.*;
2021-11-26 20:48:23 -08:00
import static com.rusefi.util.IoUtils.CHARSET;
2018-12-19 21:11:47 -08:00
2022-03-13 08:34:41 -07:00
/**
* [Constants]
*/
2018-12-19 21:11:47 -08:00
public class TSProjectConsumer implements ConfigurationConsumer {
2018-12-19 21:27:49 -08:00
private static final String CONFIG_DEFINITION_START = "CONFIG_DEFINITION_START";
private static final String CONFIG_DEFINITION_END = "CONFIG_DEFINITION_END";
2019-06-01 14:51:46 -07:00
private static final String TS_CONDITION = "@@if_";
2020-04-29 19:34:51 -07:00
public static final String SETTING_CONTEXT_HELP_END = "SettingContextHelpEnd";
public static final String SETTING_CONTEXT_HELP = "SettingContextHelp";
2024-03-18 07:26:19 -07:00
public static final String TS_DROP_TEMPLATE_COMMENTS = "ts_drop_template_comments";
2018-12-19 21:27:49 -08:00
private final String tsPath;
2023-01-06 08:55:59 -08:00
private final ReaderStateImpl state;
2024-03-18 07:26:19 -07:00
private boolean dropComments;
2018-12-19 22:01:59 -08:00
private int totalTsSize;
2021-11-26 20:48:23 -08:00
private final TsOutput tsOutput;
2018-12-19 21:11:47 -08:00
2023-01-06 08:55:59 -08:00
public TSProjectConsumer(String tsPath, ReaderStateImpl state) {
2018-12-19 21:27:49 -08:00
this.tsPath = tsPath;
tsOutput = new TsOutput(true);
2018-12-19 21:27:49 -08:00
this.state = state;
}
// also known as TS tooltips
2023-01-02 10:20:58 -08:00
public String getSettingContextHelpForUnitTest() {
2021-11-26 20:48:23 -08:00
return tsOutput.getSettingContextHelp();
2019-05-29 21:18:41 -07:00
}
protected void writeTunerStudioFile(String inputFile, String fieldsSection) throws IOException {
TsFileContent tsContent = readTsTemplateInputFile(inputFile);
SystemOut.println("Got " + tsContent.getPrefix().length() + "/" + tsContent.getPostfix().length() + " of " + inputFile);
2018-12-19 21:27:49 -08:00
2019-06-01 15:27:34 -07:00
// File.getPath() would eliminate potential separator at the end of the path
String fileName = getTsFileOutputName(new File(ConfigDefinitionRootOutputFolder.getValue() + inputFile).getPath());
2023-07-02 21:56:19 -07:00
Output tsHeader = new LazyFileImpl(fileName);
2020-02-14 19:44:59 -08:00
writeContent(fieldsSection, tsContent, tsHeader);
}
protected void writeContent(String fieldsSection, TsFileContent tsContent, Output tsHeader) throws IOException {
2018-12-19 21:27:49 -08:00
tsHeader.write(tsContent.getPrefix());
tsHeader.write("; " + CONFIG_DEFINITION_START + ToolUtil.EOL);
2024-03-18 07:26:19 -07:00
if (!dropComments) {
tsHeader.write("; this section " + state.getHeader() + ToolUtil.EOL + ToolUtil.EOL);
}
tsHeader.write("pageSize = " + totalTsSize + ToolUtil.EOL);
tsHeader.write("page = 1" + ToolUtil.EOL);
2018-12-19 21:27:49 -08:00
tsHeader.write(fieldsSection);
2021-11-26 20:48:23 -08:00
if (tsOutput.getSettingContextHelp().length() > 0) {
tsHeader.write("[" + SETTING_CONTEXT_HELP + "]" + ToolUtil.EOL);
2023-01-02 10:20:58 -08:00
tsHeader.write(tsOutput.getSettingContextHelp() + ToolUtil.EOL + ToolUtil.EOL);
tsHeader.write("; " + SETTING_CONTEXT_HELP_END + ToolUtil.EOL);
2018-12-19 21:27:49 -08:00
}
tsHeader.write("; " + CONFIG_DEFINITION_END + ToolUtil.EOL);
2018-12-19 21:27:49 -08:00
tsHeader.write(tsContent.getPostfix());
tsHeader.close();
}
2021-04-29 21:22:40 -07:00
/**
* rusefi.input has all the content of the future .ini file with the exception of data page
* TODO: start generating [outputs] section as well
*/
private TsFileContent readTsTemplateInputFile(String fileName) throws IOException {
2024-03-18 06:51:18 -07:00
FileInputStream in = new FileInputStream(fileName);
return getTsFileContent(in);
}
@NotNull
public TsFileContent getTsFileContent(InputStream in) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(in, CHARSET));
2018-12-19 21:27:49 -08:00
StringBuilder prefix = new StringBuilder();
StringBuilder postfix = new StringBuilder();
2024-03-18 07:26:19 -07:00
dropComments = Boolean.valueOf(state.getVariableRegistry().get(TS_DROP_TEMPLATE_COMMENTS));
2018-12-19 21:27:49 -08:00
boolean isBeforeStartTag = true;
boolean isAfterEndTag = false;
String line;
while ((line = r.readLine()) != null) {
if (line.contains(CONFIG_DEFINITION_START)) {
isBeforeStartTag = false;
continue;
}
if (line.contains(CONFIG_DEFINITION_END)) {
isAfterEndTag = true;
continue;
}
2024-03-18 07:26:19 -07:00
if (line.trim().startsWith(";") && dropComments)
2024-03-18 06:51:18 -07:00
continue;
2018-12-19 21:27:49 -08:00
2019-06-01 14:51:46 -07:00
if (line.contains(TS_CONDITION)) {
String token = getToken(line);
2023-01-06 07:42:44 -08:00
String strValue = state.getVariableRegistry().get(token);
2021-04-29 21:22:40 -07:00
boolean value = Boolean.parseBoolean(strValue);
2019-06-01 14:51:46 -07:00
if (!value)
continue; // skipping this line
line = removeToken(line);
}
2023-01-06 07:42:44 -08:00
line = state.getVariableRegistry().applyVariables(line);
2018-12-19 21:27:49 -08:00
if (isBeforeStartTag)
prefix.append(line + ToolUtil.EOL);
2018-12-19 21:27:49 -08:00
if (isAfterEndTag)
2023-01-06 07:42:44 -08:00
postfix.append(state.getVariableRegistry().applyVariables(line) + ToolUtil.EOL);
2018-12-19 21:27:49 -08:00
}
r.close();
return new TsFileContent(prefix.toString(), postfix.toString());
2018-12-19 21:11:47 -08:00
}
2019-06-01 15:27:34 -07:00
public static String removeToken(String line) {
2019-06-01 14:51:46 -07:00
int index = line.indexOf(TS_CONDITION);
String token = getToken(line);
int afterTokenIndex = index + TS_CONDITION.length() + token.length();
if (afterTokenIndex < line.length())
afterTokenIndex++; // skipping one whitestace after token
line = line.substring(0, index) + line.substring(afterTokenIndex);
return line;
}
2019-06-01 15:27:34 -07:00
public static String getToken(String line) {
2019-06-01 14:51:46 -07:00
int index = line.indexOf(TS_CONDITION) + TS_CONDITION.length();
String token = "";
while (index < line.length() && !Character.isWhitespace(line.charAt(index))) {
token += line.charAt(index);
index++;
}
return token;
}
2022-04-17 10:12:58 -07:00
private String getTsFileOutputName(String tsPath) {
2023-01-06 07:42:44 -08:00
return tsPath + File.separator + state.getTsFileOutputName();
}
2018-12-19 21:11:47 -08:00
@Override
public void endFile() throws IOException {
2022-04-17 11:18:30 -07:00
writeTunerStudioFile(tsPath, getContent());
2018-12-19 21:11:47 -08:00
}
@Override
public void handleEndStruct(ReaderState readerState, ConfigStructure structure) {
2023-01-06 07:54:29 -08:00
state.getVariableRegistry().register(structure.getName() + "_size", structure.getTotalSize());
totalTsSize = tsOutput.run(readerState, structure, 0, "", "");
2022-04-17 11:28:32 -07:00
2023-01-06 07:52:09 -08:00
if (state.isStackEmpty()) {
2023-01-06 07:42:44 -08:00
state.getVariableRegistry().register("TOTAL_CONFIG_SIZE", totalTsSize);
2018-12-19 21:27:49 -08:00
}
2018-12-19 21:11:47 -08:00
}
2022-04-17 11:18:30 -07:00
public String getContent() {
return tsOutput.getContent();
}
2018-12-19 21:11:47 -08:00
}