Issue 126: begin/end script tags

git-svn-id: http://jclouds.googlecode.com/svn/trunk@2352 3d8758e0-26b5-11de-8745-db77d3ebf521
This commit is contained in:
adrian.f.cole 2009-11-30 18:22:32 +00:00
parent 3516c2928a
commit 61582b0a0a
6 changed files with 30 additions and 29 deletions

View File

@ -115,8 +115,7 @@ public class ScriptBuilder {
public String build(final OsFamily osFamily) {
final Map<String, String> tokenValueMap = ShellToken.tokenValueMap(osFamily);
StringBuilder builder = new StringBuilder();
builder.append(ShellToken.SHEBANG.to(osFamily));
builder.append(Utils.writeScriptInit(osFamily));
builder.append(ShellToken.BEGIN_SCRIPT.to(osFamily));
builder.append(Utils.writeUnsetVariables(Lists.newArrayList(Iterables.transform(
variablesToUnset, new Function<String, String>() {
@Override
@ -140,6 +139,7 @@ public class ScriptBuilder {
for (Entry<String, Map<String, String>> entry : switchExec.entrySet()) {
builder.append(Utils.writeSwitch(entry.getKey(), entry.getValue(), osFamily));
}
builder.append(ShellToken.END_SCRIPT.to(osFamily));
return builder.toString();
}
}

View File

@ -58,7 +58,7 @@ public enum ShellToken {
/**
* End the function. exits successfully and closes the code block.
*/
FNCE, BEGIN_FUNCTIONS, END_FUNCTIONS, EXPORT, LF, SH, SOURCE, REM, RETURN, ARGS, VARSTART, VAREND, SHEBANG, LIBRARY_PATH_VARIABLE;
FNCE, BEGIN_SCRIPT, END_SCRIPT, BEGIN_FUNCTIONS, END_FUNCTIONS, EXPORT, LF, SH, SOURCE, REM, RETURN, ARGS, VARSTART, VAREND, LIBRARY_PATH_VARIABLE;
private static final Map<OsFamily, Map<String, String>> familyToTokenValueMap = new MapMaker()
.makeComputingMap(new Function<OsFamily, Map<String, String>>() {
@ -139,6 +139,20 @@ public enum ShellToken {
case UNIX:
return "";
}
case BEGIN_SCRIPT:
switch (family) {
case WINDOWS:
return "@echo off\r\n";
case UNIX:
return "#!/bin/bash\nset +u\nshopt -s xpg_echo\nshopt -s expand_aliases\n";
}
case END_SCRIPT:
switch (family) {
case WINDOWS:
return "exit /b 0\r\n";
case UNIX:
return "set -u\nreturn 0\n";
}
case EXPORT:
switch (family) {
case WINDOWS:
@ -209,14 +223,6 @@ public enum ShellToken {
case UNIX:
return "";
}
case SHEBANG:
switch (family) {
case WINDOWS:
return "@echo off\r\n";
case UNIX:
return "#!/bin/bash\n";
}
default:
throw new UnsupportedOperationException("token " + this + " not configured");
}

View File

@ -214,16 +214,6 @@ public class Utils {
return OS_TO_ZERO_PATH.get(family);
}
public static final Map<OsFamily, String> OS_TO_SCRIPT_INIT = ImmutableMap.of(OsFamily.UNIX,
"set +u\nshopt -s xpg_echo\nshopt -s expand_aliases\n", OsFamily.WINDOWS, "");
/**
* sets up shell options needed for script execution
*/
public static String writeScriptInit(OsFamily family) {
return OS_TO_SCRIPT_INIT.get(family);
}
public static final Map<OsFamily, String> OS_TO_SWITCH_PATTERN = ImmutableMap.of(OsFamily.UNIX,
"case ${variable} in\n", OsFamily.WINDOWS, "goto CASE%{variable}\r\n");

View File

@ -43,9 +43,11 @@ public class ShellTokenTest {
Map<String, String> expected = new ImmutableMap.Builder<String, String>().put("fs", "/").put(
"ps", ":").put("lf", "\n").put("sh", "bash").put("source", ".").put("rem", "#").put(
"args", "$@").put("varstart", "$").put("return", "return").put("varend", "").put(
"libraryPathVariable", "LD_LIBRARY_PATH").put("shebang", "#!/bin/bash\n").put("vq",
"\"").put("beginFunctions", "").put("endFunctions", "").put("fncl", "function ")
.put("fncr", " {\n").put("fnce", " return 0\n}\n").put("export", "export").build();
"libraryPathVariable", "LD_LIBRARY_PATH").put("beginScript",
"#!/bin/bash\nset +u\nshopt -s xpg_echo\nshopt -s expand_aliases\n").put(
"endScript", "set -u\nreturn 0\n").put("vq", "\"").put("beginFunctions", "").put(
"endFunctions", "").put("fncl", "function ").put("fncr", " {\n").put("fnce",
" return 0\n}\n").put("export", "export").build();
assertEquals(ShellToken.tokenValueMap(OsFamily.UNIX), expected);
}
@ -54,11 +56,11 @@ public class ShellTokenTest {
Map<String, String> expected = new ImmutableMap.Builder<String, String>().put("fs", "\\")
.put("ps", ";").put("lf", "\r\n").put("sh", "cmd").put("source", "@call").put("rem",
"@rem").put("args", "%*").put("varstart", "%").put("varend", "%").put(
"libraryPathVariable", "PATH").put("return", "exit /b").put("shebang",
"@echo off\r\n").put("vq", "").put("beginFunctions",
"GOTO FUNCTION_END\r\n").put("endFunctions", ":FUNCTION_END\r\n").put(
"fncl", ":").put("fncr", "\r\n").put("fnce", " exit /b 0\r\n").put(
"export", "set").build();
"libraryPathVariable", "PATH").put("return", "exit /b").put("vq", "").put(
"beginFunctions", "GOTO FUNCTION_END\r\n").put("endFunctions",
":FUNCTION_END\r\n").put("beginScript", "@echo off\r\n").put("endScript",
"exit /b 0\r\n").put("fncl", ":").put("fncr", "\r\n").put("fnce",
" exit /b 0\r\n").put("export", "set").build();
assertEquals(ShellToken.tokenValueMap(OsFamily.WINDOWS), expected);
}

View File

@ -20,3 +20,5 @@ stop)
echo stopped
;;
esac
set -u
return 0

View File

@ -19,3 +19,4 @@ goto CASE%1
echo stopped
GOTO END_SWITCH
:END_SWITCH
exit /b 0