Lua: include script feature toolset
This commit is contained in:
parent
c3fc6cfe7d
commit
b7f26a3892
|
@ -0,0 +1,5 @@
|
||||||
|
package com.rusefi.ui.lua;
|
||||||
|
|
||||||
|
interface ContentSource {
|
||||||
|
String getContent(String name);
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.rusefi.ui.lua;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
public class LuaIncludeSyntax {
|
||||||
|
static String getScriptName(String lua) {
|
||||||
|
Pattern p = Pattern.compile(".*--\\s*scriptname\\s*([\\S]+)", Pattern.MULTILINE);
|
||||||
|
Matcher m = p.matcher(lua);
|
||||||
|
if (m.find())
|
||||||
|
return m.group(1);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
static String reloadScript(String lua, ContentSource source) {
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
Pattern startPattern = Pattern.compile(".*--\\s*include\\s*([\\S]+)", Pattern.MULTILINE);
|
||||||
|
Pattern endPattern = Pattern.compile(".*--\\s*endinclude", Pattern.MULTILINE);
|
||||||
|
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
|
||||||
|
Matcher startMatcher = startPattern.matcher(lua);
|
||||||
|
Matcher endMatcher = endPattern.matcher(lua);
|
||||||
|
while (true) {
|
||||||
|
if (!startMatcher.find(index)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
int endOfIncludeStartIndex = startMatcher.end();
|
||||||
|
|
||||||
|
result.append(lua.substring(index, endOfIncludeStartIndex));
|
||||||
|
result.append("\n");
|
||||||
|
|
||||||
|
String includeName = startMatcher.group(1);
|
||||||
|
result.append(source.getContent(includeName));
|
||||||
|
|
||||||
|
if (!endMatcher.find(endOfIncludeStartIndex))
|
||||||
|
throw new IllegalStateException("No end for " + includeName);
|
||||||
|
|
||||||
|
index = endMatcher.end();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
result.append(lua.substring(index));
|
||||||
|
|
||||||
|
return result.toString();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
package com.rusefi.ui.lua;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class LuaImportsTest {
|
||||||
|
@Test
|
||||||
|
public void testScriptName() {
|
||||||
|
assertEquals(null, LuaIncludeSyntax.getScriptName(""));
|
||||||
|
assertEquals("X", LuaIncludeSyntax.getScriptName("-- scriptname X "));
|
||||||
|
assertEquals("X2", LuaIncludeSyntax.getScriptName("-- scriptname X2\n"));
|
||||||
|
assertEquals("X3", LuaIncludeSyntax.getScriptName("sdad\n -- scriptname X3\n"));
|
||||||
|
assertEquals("folder/X3", LuaIncludeSyntax.getScriptName("sdad\n -- scriptname folder/X3\n"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReloadTwoScripts() {
|
||||||
|
|
||||||
|
String lua = "before\n" +
|
||||||
|
"-- include functions.lua\n" +
|
||||||
|
"functionOldBody\n" +
|
||||||
|
"-- endinclude\n" +
|
||||||
|
"between\n" +
|
||||||
|
"--include constants.lua \n" +
|
||||||
|
"oldConstants\n" +
|
||||||
|
"-- endinclude\n" +
|
||||||
|
"after";
|
||||||
|
|
||||||
|
ContentSource source = name -> {
|
||||||
|
if (name.equals("functions.lua"))
|
||||||
|
return "newFunction";
|
||||||
|
if (name.equals("constants.lua"))
|
||||||
|
return "newConstant";
|
||||||
|
throw new IllegalStateException();
|
||||||
|
};
|
||||||
|
|
||||||
|
String newLua = LuaIncludeSyntax.reloadScript(lua, source);
|
||||||
|
|
||||||
|
assertEquals("before\n" +
|
||||||
|
"-- include functions.lua\n" +
|
||||||
|
"newFunction\n" +
|
||||||
|
"between\n" +
|
||||||
|
"--include constants.lua\n" +
|
||||||
|
"newConstant\n" +
|
||||||
|
"after", newLua);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue