public static Statement switchArg(int arg, Map<String, Statement> valueToActions) { return new SwitchArg(arg, valueToActions); }
public void testSwitchArgWindows() { assertEquals( new SwitchArg(1, ImmutableMap.of("0", interpret("echo hello zero{lf}"), "1", interpret("echo hello one{lf}"))).render(OsFamily.WINDOWS), "if not \"%1\" == \"0\" if not \"%1\" == \"1\" (\r\n set EXCEPTION=bad argument: %1 not in 0 1\r\n goto abort\r\n)\r\ngoto CASE_%1\r\n:CASE_0\r\n echo hello zero\r\n GOTO END_SWITCH\r\n:CASE_1\r\n echo hello one\r\n GOTO END_SWITCH\r\n:END_SWITCH\r\n"); } }
public String render(OsFamily family) { StringBuilder switchClause = new StringBuilder(); addArgValidation(switchClause, family); switchClause.append(Utils.replaceTokens(OS_TO_SWITCH_PATTERN.get(family), ImmutableMap.of("arg", arg + ""))); for (Entry<String, Statement> entry : valueToActions.entrySet()) { StringBuilder actionBuilder = new StringBuilder(); boolean shouldIndent = true; boolean inRunScript = false; boolean inCreateFile = false; for (String line : Splitter.on(ShellToken.LF.to(family)).split(entry.getValue().render(family))) { if (shouldIndent) actionBuilder.append(INDENT); actionBuilder.append(line).append(ShellToken.LF.to(family)); if (line.indexOf(CreateRunScript.DELIMITER) != -1) { inRunScript = inRunScript ? false : true; } if (line.indexOf(AppendFile.DELIMITER) != -1) { inCreateFile = inCreateFile ? false : true; } shouldIndent = !inCreateFile && !inRunScript; } actionBuilder.delete(actionBuilder.lastIndexOf(ShellToken.LF.to(family)), actionBuilder.length()); switchClause.append(Utils.replaceTokens(OS_TO_CASE_PATTERN.get(family), ImmutableMap.of("value", entry .getKey(), "action", actionBuilder.toString()))); } switchClause.append(OS_TO_END_SWITCH_PATTERN.get(family)); return switchClause.toString(); }
public void testSwitchArgWindows() { assertEquals( new SwitchArg(1, ImmutableMap.of("0", interpret("echo hello zero{lf}"), "1", interpret("echo hello one{lf}"))).render(OsFamily.WINDOWS), "if not \"%1\" == \"0\" if not \"%1\" == \"1\" (\r\n set EXCEPTION=bad argument: %1 not in 0 1\r\n goto abort\r\n)\r\ngoto CASE_%1\r\n:CASE_0\r\n echo hello zero\r\n GOTO END_SWITCH\r\n:CASE_1\r\n echo hello one\r\n GOTO END_SWITCH\r\n:END_SWITCH\r\n"); } }
public String render(OsFamily family) { StringBuilder switchClause = new StringBuilder(); addArgValidation(switchClause, family); switchClause.append(Utils.replaceTokens(OS_TO_SWITCH_PATTERN.get(family), ImmutableMap.of("arg", arg + ""))); for (Entry<String, Statement> entry : valueToActions.entrySet()) { StringBuilder actionBuilder = new StringBuilder(); boolean shouldIndent = true; boolean inRunScript = false; boolean inCreateFile = false; for (String line : Splitter.on(ShellToken.LF.to(family)).split(entry.getValue().render(family))) { if (shouldIndent) actionBuilder.append(INDENT); actionBuilder.append(line).append(ShellToken.LF.to(family)); if (line.indexOf(CreateRunScript.DELIMITER) != -1) { inRunScript = inRunScript ? false : true; } if (line.indexOf(AppendFile.DELIMITER) != -1) { inCreateFile = inCreateFile ? false : true; } shouldIndent = !inCreateFile && !inRunScript; } actionBuilder.delete(actionBuilder.lastIndexOf(ShellToken.LF.to(family)), actionBuilder.length()); switchClause.append(Utils.replaceTokens(OS_TO_CASE_PATTERN.get(family), ImmutableMap.of("value", entry .getKey(), "action", actionBuilder.toString()))); } switchClause.append(OS_TO_END_SWITCH_PATTERN.get(family)); return switchClause.toString(); }
public static Statement switchArg(int arg, Map<String, Statement> valueToActions) { return new SwitchArg(arg, valueToActions); }
public void testSwitchArgUNIX() { assertEquals(new SwitchArg(1, ImmutableMap.of("0", newStatementList(appendFile( "{tmp}{fs}{uid}{fs}scripttest{fs}temp.txt", ImmutableList.of("hello world")), interpret("echo hello zero{lf}")), "1", interpret("echo hello one{lf}"))).render(OsFamily.UNIX), "case $1 in\n"+ "0)\n"+ " cat >> /tmp/$USER/scripttest/temp.txt <<-'END_OF_JCLOUDS_FILE'\n"+ "\thello world\n"+ "END_OF_JCLOUDS_FILE\n"+ " echo hello zero\n"+ " ;;\n"+ "1)\n"+ " echo hello one\n"+ " ;;\n"+ "esac\n"); }
public String render(OsFamily family) { StringBuilder switchClause = new StringBuilder(); addArgValidation(switchClause, family); switchClause.append(Utils.replaceTokens(OS_TO_SWITCH_PATTERN.get(family), ImmutableMap.of("arg", arg + ""))); for (Entry<String, Statement> entry : valueToActions.entrySet()) { StringBuilder actionBuilder = new StringBuilder(); boolean shouldIndent = true; boolean inRunScript = false; boolean inCreateFile = false; for (String line : Splitter.on(ShellToken.LF.to(family)).split(entry.getValue().render(family))) { if (shouldIndent) actionBuilder.append(INDENT); actionBuilder.append(line).append(ShellToken.LF.to(family)); if (line.indexOf(CreateRunScript.DELIMITER) != -1) { inRunScript = inRunScript ? false : true; } if (line.indexOf(AppendFile.DELIMITER) != -1) { inCreateFile = inCreateFile ? false : true; } shouldIndent = !inCreateFile && !inRunScript; } actionBuilder.delete(actionBuilder.lastIndexOf(ShellToken.LF.to(family)), actionBuilder.length()); switchClause.append(Utils.replaceTokens(OS_TO_CASE_PATTERN.get(family), ImmutableMap.of("value", entry .getKey(), "action", actionBuilder.toString()))); } switchClause.append(OS_TO_END_SWITCH_PATTERN.get(family)); return switchClause.toString(); }
public static Statement switchArg(int arg, Map<String, Statement> valueToActions) { return new SwitchArg(arg, valueToActions); }
public void testSwitchArgUNIX() { assertEquals(new SwitchArg(1, ImmutableMap.of("0", newStatementList(appendFile( "{tmp}{fs}{uid}{fs}scripttest{fs}temp.txt", ImmutableList.of("hello world")), interpret("echo hello zero{lf}")), "1", interpret("echo hello one{lf}"))).render(OsFamily.UNIX), "case $1 in\n" + "0)\n" + " cat >> /tmp/$USER/scripttest/temp.txt <<-'END_OF_JCLOUDS_FILE'\n" + "\thello world\n" + "END_OF_JCLOUDS_FILE\n" + " echo hello zero\n" + " ;;\n" + "1)\n" + " echo hello one\n" + " ;;\n" + "esac\n"); }
public String render(OsFamily family) { StringBuilder switchClause = new StringBuilder(); addArgValidation(switchClause, family); switchClause.append(Utils.replaceTokens(OS_TO_SWITCH_PATTERN.get(family), ImmutableMap.of("arg", arg + ""))); for (Entry<String, Statement> entry : valueToActions.entrySet()) { StringBuilder actionBuilder = new StringBuilder(); boolean shouldIndent = true; boolean inRunScript = false; boolean inCreateFile = false; for (String line : Splitter.on(ShellToken.LF.to(family)).split(entry.getValue().render(family))) { if (shouldIndent) actionBuilder.append(INDENT); actionBuilder.append(line).append(ShellToken.LF.to(family)); if (line.indexOf(CreateRunScript.DELIMITER) != -1) { inRunScript = inRunScript ? false : true; } if (line.indexOf(AppendFile.DELIMITER) != -1) { inCreateFile = inCreateFile ? false : true; } shouldIndent = !inCreateFile && !inRunScript; } actionBuilder.delete(actionBuilder.lastIndexOf(ShellToken.LF.to(family)), actionBuilder.length()); switchClause.append(Utils.replaceTokens(OS_TO_CASE_PATTERN.get(family), ImmutableMap.of("value", entry .getKey(), "action", actionBuilder.toString()))); } switchClause.append(OS_TO_END_SWITCH_PATTERN.get(family)); return switchClause.toString(); }
public static Statement switchArg(int arg, Map<String, Statement> valueToActions) { return new SwitchArg(arg, valueToActions); }
public String render(OsFamily family) { StringBuilder switchClause = new StringBuilder(); addArgValidation(switchClause, family); switchClause.append(Utils.replaceTokens(OS_TO_SWITCH_PATTERN.get(family), ImmutableMap.of("arg", arg + ""))); for (Entry<String, Statement> entry : valueToActions.entrySet()) { StringBuilder actionBuilder = new StringBuilder(); boolean shouldIndent = true; boolean inRunScript = false; boolean inCreateFile = false; for (String line : Splitter.on(ShellToken.LF.to(family)).split(entry.getValue().render(family))) { if (shouldIndent) actionBuilder.append(INDENT); actionBuilder.append(line).append(ShellToken.LF.to(family)); if (line.indexOf(CreateRunScript.DELIMITER) != -1) { inRunScript = inRunScript ? false : true; } if (line.indexOf(AppendFile.DELIMITER) != -1) { inCreateFile = inCreateFile ? false : true; } shouldIndent = !inCreateFile && !inRunScript; } actionBuilder.delete(actionBuilder.lastIndexOf(ShellToken.LF.to(family)), actionBuilder.length()); switchClause.append(Utils.replaceTokens(OS_TO_CASE_PATTERN.get(family), ImmutableMap.of("value", entry .getKey(), "action", actionBuilder.toString()))); } switchClause.append(OS_TO_END_SWITCH_PATTERN.get(family)); return switchClause.toString(); }
public static Statement switchArg(int arg, Map<String, Statement> valueToActions) { return new SwitchArg(arg, valueToActions); }
public String render(OsFamily family) { StringBuilder switchClause = new StringBuilder(); addArgValidation(switchClause, family); switchClause.append(Utils.replaceTokens(OS_TO_SWITCH_PATTERN.get(family), ImmutableMap.of("arg", arg + ""))); for (Entry<String, Statement> entry : valueToActions.entrySet()) { StringBuilder actionBuilder = new StringBuilder(); boolean shouldIndent = true; boolean inRunScript = false; boolean inCreateFile = false; for (String line : Splitter.on(ShellToken.LF.to(family)).split(entry.getValue().render(family))) { if (shouldIndent) actionBuilder.append(INDENT); actionBuilder.append(line).append(ShellToken.LF.to(family)); if (line.indexOf(CreateRunScript.DELIMITER) != -1) { inRunScript = inRunScript ? false : true; } if (line.indexOf(AppendFile.DELIMITER) != -1) { inCreateFile = inCreateFile ? false : true; } shouldIndent = !inCreateFile && !inRunScript; } actionBuilder.delete(actionBuilder.lastIndexOf(ShellToken.LF.to(family)), actionBuilder.length()); switchClause.append(Utils.replaceTokens(OS_TO_CASE_PATTERN.get(family), ImmutableMap.of("value", entry .getKey(), "action", actionBuilder.toString()))); } switchClause.append(OS_TO_END_SWITCH_PATTERN.get(family)); return switchClause.toString(); }
public static Statement switchArg(int arg, Map<String, Statement> valueToActions) { return new SwitchArg(arg, valueToActions); }
@Test public void testSwitchOn() { ScriptBuilder builder = new ScriptBuilder(); builder.addStatement(switchArg(1, ImmutableMap.of("start", interpret("echo started{lf}"), "stop", interpret("echo stopped{lf}")))); assertEquals(builder.statements, ImmutableList.of(new SwitchArg(1, ImmutableMap.of("start", interpret("echo started{lf}"), "stop", interpret("echo stopped{lf}"))))); }
@Test public void testSwitchOn() { ScriptBuilder builder = new ScriptBuilder(); builder.addStatement(switchArg(1, ImmutableMap.of("start", interpret("echo started{lf}"), "stop", interpret("echo stopped{lf}")))); assertEquals(builder.statements, ImmutableList.of(new SwitchArg(1, ImmutableMap.of("start", interpret("echo started{lf}"), "stop", interpret("echo stopped{lf}"))))); }