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

View File

@ -58,7 +58,7 @@ public enum ShellToken {
/** /**
* End the function. exits successfully and closes the code block. * 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() private static final Map<OsFamily, Map<String, String>> familyToTokenValueMap = new MapMaker()
.makeComputingMap(new Function<OsFamily, Map<String, String>>() { .makeComputingMap(new Function<OsFamily, Map<String, String>>() {
@ -139,6 +139,20 @@ public enum ShellToken {
case UNIX: case UNIX:
return ""; 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: case EXPORT:
switch (family) { switch (family) {
case WINDOWS: case WINDOWS:
@ -209,14 +223,6 @@ public enum ShellToken {
case UNIX: case UNIX:
return ""; return "";
} }
case SHEBANG:
switch (family) {
case WINDOWS:
return "@echo off\r\n";
case UNIX:
return "#!/bin/bash\n";
}
default: default:
throw new UnsupportedOperationException("token " + this + " not configured"); throw new UnsupportedOperationException("token " + this + " not configured");
} }

View File

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

View File

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

View File

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