Recursive properties string-replace

This commit is contained in:
Cristian Maglie 2012-01-13 14:10:34 +01:00
parent aa3d6cd2ca
commit 0dd5309bc0
2 changed files with 33 additions and 18 deletions

View File

@ -53,6 +53,8 @@ public class Compiler implements MessageConsumer {
private String buildPath;
private String toolsPath;
private String corePath;
private String variantPath;
private String primaryClassName;
private String board;
private List<File> objectFiles;
@ -134,7 +136,7 @@ public class Compiler implements MessageConsumer {
String corePath = coreFolder.getAbsolutePath();
String variant = boardPreferences.get("build.variant");
String variantPath = null;
variantPath = null;
if (variant != null) {
File variantFolder;
if (!variant.contains(":")) {
@ -172,7 +174,7 @@ public class Compiler implements MessageConsumer {
// 3. compile the core, outputting .o files to <buildPath> and then
// collecting them into the core.a library file.
sketch.setCompilingProgress(50);
compileCore(corePath, variant, variantPath);
compileCore(variant);
// 4. link it all together into the .elf file
sketch.setCompilingProgress(60);
@ -481,7 +483,7 @@ public class Compiler implements MessageConsumer {
try {
String cmd = prefs.get("recipe.S.o.pattern");
return StringReplacer.formatAndSplit(cmd, dict);
return StringReplacer.formatAndSplit(cmd, dict, true);
} catch (Exception e) {
throw new RunnerException(e);
}
@ -501,7 +503,7 @@ public class Compiler implements MessageConsumer {
String cmd = prefs.get("recipe.c.o.pattern");
try {
return StringReplacer.formatAndSplit(cmd, dict);
return StringReplacer.formatAndSplit(cmd, dict, true);
} catch (Exception e) {
throw new RunnerException(e);
}
@ -521,7 +523,7 @@ public class Compiler implements MessageConsumer {
String cmd = prefs.get("recipe.cpp.o.pattern");
try {
return StringReplacer.formatAndSplit(cmd, dict);
return StringReplacer.formatAndSplit(cmd, dict, true);
} catch (Exception e) {
throw new RunnerException(e);
}
@ -588,7 +590,7 @@ public class Compiler implements MessageConsumer {
// 3. compile the core, outputting .o files to <buildPath> and then
// collecting them into the core.a library file.
void compileCore(String corePath, String variant, String variantPath)
void compileCore(String variant)
throws RunnerException {
List<String> includePaths = new ArrayList<String>();
@ -611,7 +613,7 @@ public class Compiler implements MessageConsumer {
String[] cmdArray;
try {
String cmd = prefs.get("recipe.ar.pattern");
cmdArray = StringReplacer.formatAndSplit(cmd, dict);
cmdArray = StringReplacer.formatAndSplit(cmd, dict, true);
} catch (Exception e) {
throw new RunnerException(e);
}
@ -646,11 +648,12 @@ public class Compiler implements MessageConsumer {
dict.put("object_files", objectFileList);
dict.put("ide_version", "" + Base.REVISION);
dict.put("core_path", corePath);
dict.put("variant_path", variantPath + File.separator);
String[] cmdArray;
try {
String cmd = prefs.get("recipe.c.combine.pattern");
cmdArray = StringReplacer.formatAndSplit(cmd, dict);
cmdArray = StringReplacer.formatAndSplit(cmd, dict, true);
} catch (Exception e) {
throw new RunnerException(e);
}
@ -668,7 +671,7 @@ public class Compiler implements MessageConsumer {
String[] cmdArray;
try {
String cmd = prefs.get("recipe.objcopy.eep.pattern");
cmdArray = StringReplacer.formatAndSplit(cmd, dict);
cmdArray = StringReplacer.formatAndSplit(cmd, dict, true);
} catch (Exception e) {
throw new RunnerException(e);
}
@ -686,7 +689,7 @@ public class Compiler implements MessageConsumer {
String[] cmdArray;
try {
String cmd = prefs.get("recipe.objcopy.hex.pattern");
cmdArray = StringReplacer.formatAndSplit(cmd, dict);
cmdArray = StringReplacer.formatAndSplit(cmd, dict, true);
} catch (Exception e) {
throw new RunnerException(e);
}

View File

@ -29,17 +29,28 @@ import java.util.Map;
public class StringReplacer {
public static String[] formatAndSplit(String src, Map<String, String> dict)
throws Exception {
// Do a replace with dictionary
src = StringReplacer.replaceFromMapping(src, dict);
public static String[] formatAndSplit(String src, Map<String, String> dict,
boolean recursive) throws Exception {
String res;
// Recursive replace with a max depth of 10 levels.
for (int i=0; i<10; i++) {
// Do a replace with dictionary
res = StringReplacer.replaceFromMapping(src, dict);
if (!recursive)
break;
if (res.equals(src))
break;
src = res;
}
// Split the resulting string in arguments
return quotedSplit(src, '"', false);
}
public static String[] quotedSplit(String src, char escapeChar,
boolean acceptEmptyArguments) throws Exception {
boolean acceptEmptyArguments)
throws Exception {
String quote = "" + escapeChar;
List<String> res = new ArrayList<String>();
String escapedArg = null;
@ -68,8 +79,8 @@ public class StringReplacer {
escaping = false;
}
if (escaping)
throw new Exception("Invalid quoting: no closing '" + escapeChar
+ "' char found.");
throw new Exception("Invalid quoting: no closing '" + escapeChar +
"' char found.");
return res.toArray(new String[0]);
}
@ -78,7 +89,8 @@ public class StringReplacer {
}
public static String replaceFromMapping(String src, Map<String, String> map,
String leftDelimiter, String rightDelimiter) {
String leftDelimiter,
String rightDelimiter) {
for (String k : map.keySet()) {
String keyword = leftDelimiter + k + rightDelimiter;
src = src.replace(keyword, map.get(k));