From 13d37f554d8ed0965324f48a17387c3e96beb31b Mon Sep 17 00:00:00 2001 From: "adrian.f.cole" Date: Sun, 29 Nov 2009 23:53:28 +0000 Subject: [PATCH] Issue 126: added environment setting script builder git-svn-id: http://jclouds.googlecode.com/svn/trunk@2345 3d8758e0-26b5-11de-8745-db77d3ebf521 --- .../org/jclouds/initbuilder/EnvBuilder.java | 84 +++++++++++++++++++ .../initbuilder/domain/ShellToken.java | 9 +- .../org/jclouds/initbuilder/util/Utils.java | 5 ++ .../jclouds/initbuilder/EnvBuilderTest.java | 59 +++++++++++++ .../initbuilder/domain/ShellTokenTest.java | 7 +- initbuilder/src/test/resources/test_env.bash | 10 +++ initbuilder/src/test/resources/test_env.cmd | 10 +++ .../src/test/resources/test_script_funcs.cmd | 27 ++++++ 8 files changed, 207 insertions(+), 4 deletions(-) create mode 100644 initbuilder/src/main/java/org/jclouds/initbuilder/EnvBuilder.java create mode 100644 initbuilder/src/test/java/org/jclouds/initbuilder/EnvBuilderTest.java create mode 100644 initbuilder/src/test/resources/test_env.bash create mode 100644 initbuilder/src/test/resources/test_env.cmd create mode 100644 initbuilder/src/test/resources/test_script_funcs.cmd diff --git a/initbuilder/src/main/java/org/jclouds/initbuilder/EnvBuilder.java b/initbuilder/src/main/java/org/jclouds/initbuilder/EnvBuilder.java new file mode 100644 index 0000000000..ac5298963d --- /dev/null +++ b/initbuilder/src/main/java/org/jclouds/initbuilder/EnvBuilder.java @@ -0,0 +1,84 @@ +/** + * + * Copyright (C) 2009 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + */ +package org.jclouds.initbuilder; + +import static com.google.common.base.Preconditions.checkNotNull; + +import java.util.Map; + +import org.jclouds.initbuilder.domain.OsFamily; +import org.jclouds.initbuilder.domain.ShellToken; +import org.jclouds.initbuilder.util.Utils; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Maps; + +/** + * Creates an environment file + * + * @author Adrian Cole + */ +public class EnvBuilder { + + @VisibleForTesting + Map variables = Maps.newHashMap(); + + /** + * Exports a variable inside the script + */ + public EnvBuilder export(String name, String value) { + variables.put(checkNotNull(name, "name"), checkNotNull(value, "value")); + return this; + } + + /** + * builds the environment file, by adding the following + *
    + *
  1. example usage
  2. + *
  3. variable exports
  4. + *
  5. return statement
  6. + *
+ * + * @param osFamily + * whether to write a cmd or bash script. + */ + public String build(final OsFamily osFamily) { + StringBuilder builder = new StringBuilder(); + builder.append(Utils.writeComment(" Env file: please do not confuse people by making this executable", osFamily)); + builder.append(Utils.writeComment("", osFamily)); + builder.append(Utils.writeComment(" Example usage to set a variable", osFamily)); + builder.append(Utils.writeComment("", osFamily)); + builder.append(Utils.writeComment(" " + + Utils.writeVariableExporters(ImmutableMap.of("mavenOpts", "-Xms64m -Xmx128m"), + osFamily), osFamily)); + builder.append(Utils.writeVariableExporters(variables, osFamily)); + builder.append(ShellToken.LF.to(osFamily)); + builder.append(Utils.writeComment( + " Please retain this statement so that the script can be validated", osFamily)); + builder.append(ShellToken.RETURN.to(osFamily)).append(" 0") + .append(ShellToken.LF.to(osFamily)); + return builder.toString(); + } +} \ No newline at end of file diff --git a/initbuilder/src/main/java/org/jclouds/initbuilder/domain/ShellToken.java b/initbuilder/src/main/java/org/jclouds/initbuilder/domain/ShellToken.java index 6006ea9a3c..bbc6a7e012 100644 --- a/initbuilder/src/main/java/org/jclouds/initbuilder/domain/ShellToken.java +++ b/initbuilder/src/main/java/org/jclouds/initbuilder/domain/ShellToken.java @@ -39,7 +39,7 @@ import com.google.common.collect.Maps; */ public enum ShellToken { - FS, PS, LF, SH, SOURCE, REM, ARGS, VARSTART, VAREND, SHEBANG, LIBRARY_PATH_VARIABLE; + FS, PS, LF, SH, SOURCE, REM, RETURN, ARGS, VARSTART, VAREND, SHEBANG, LIBRARY_PATH_VARIABLE; private static final Map> familyToTokenValueMap = new MapMaker() .makeComputingMap(new Function>() { @@ -77,6 +77,13 @@ public enum ShellToken { case UNIX: return ":"; } + case RETURN: + switch (family) { + case WINDOWS: + return "exit /b"; + case UNIX: + return "return"; + } case LF: switch (family) { case WINDOWS: diff --git a/initbuilder/src/main/java/org/jclouds/initbuilder/util/Utils.java b/initbuilder/src/main/java/org/jclouds/initbuilder/util/Utils.java index 0c9066a044..256db96cb2 100644 --- a/initbuilder/src/main/java/org/jclouds/initbuilder/util/Utils.java +++ b/initbuilder/src/main/java/org/jclouds/initbuilder/util/Utils.java @@ -30,6 +30,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.jclouds.initbuilder.domain.OsFamily; +import org.jclouds.initbuilder.domain.ShellToken; import com.google.common.base.CaseFormat; import com.google.common.base.Function; @@ -227,4 +228,8 @@ public class Utils { switchClause.append(OS_TO_END_SWITCH_PATTERN.get(family)); return switchClause.toString(); } + + public static String writeComment(String comment, OsFamily family) { + return String.format("%s%s%s", ShellToken.REM.to(family), comment, ShellToken.LF.to(family)); + } } diff --git a/initbuilder/src/test/java/org/jclouds/initbuilder/EnvBuilderTest.java b/initbuilder/src/test/java/org/jclouds/initbuilder/EnvBuilderTest.java new file mode 100644 index 0000000000..11990b8056 --- /dev/null +++ b/initbuilder/src/test/java/org/jclouds/initbuilder/EnvBuilderTest.java @@ -0,0 +1,59 @@ +package org.jclouds.initbuilder; + +import static org.testng.Assert.assertEquals; + +import java.io.IOException; +import java.net.MalformedURLException; + +import org.jclouds.initbuilder.domain.OsFamily; +import org.jclouds.initbuilder.domain.ShellToken; +import org.testng.annotations.Test; + +import com.google.common.base.Charsets; +import com.google.common.collect.ImmutableMap; +import com.google.common.io.CharStreams; +import com.google.common.io.Resources; + +/** + * Tests possible uses of EnvBuilder + * + * @author Adrian Cole + */ +public class EnvBuilderTest { + + EnvBuilder testScriptBuilder = new EnvBuilder().export("javaHome", + "/apps/jdk1.6"); + + @Test + public void testBuildSimpleWindows() throws MalformedURLException, IOException { + assertEquals(testScriptBuilder.build(OsFamily.WINDOWS), CharStreams.toString(Resources + .newReaderSupplier(Resources.getResource("test_env." + + ShellToken.SH.to(OsFamily.WINDOWS)), Charsets.UTF_8))); + } + + @Test + public void testBuildSimpleUNIX() throws MalformedURLException, IOException { + assertEquals(testScriptBuilder.build(OsFamily.UNIX), CharStreams.toString(Resources + .newReaderSupplier(Resources.getResource("test_env." + + ShellToken.SH.to(OsFamily.UNIX)), Charsets.UTF_8))); + } + + @Test + public void testExport() { + EnvBuilder builder = new EnvBuilder(); + builder.export("javaHome", "/apps/jdk1.6"); + assertEquals(builder.variables, ImmutableMap.of("javaHome", "/apps/jdk1.6")); + } + + @Test + public void testNoExport() { + EnvBuilder builder = new EnvBuilder(); + assertEquals(builder.variables.size(), 0); + } + + @Test(expectedExceptions = NullPointerException.class) + public void testExportNPE() { + new EnvBuilder().export(null, null); + } + +} diff --git a/initbuilder/src/test/java/org/jclouds/initbuilder/domain/ShellTokenTest.java b/initbuilder/src/test/java/org/jclouds/initbuilder/domain/ShellTokenTest.java index b7a1237830..45491aa02e 100644 --- a/initbuilder/src/test/java/org/jclouds/initbuilder/domain/ShellTokenTest.java +++ b/initbuilder/src/test/java/org/jclouds/initbuilder/domain/ShellTokenTest.java @@ -40,8 +40,8 @@ public class ShellTokenTest { public void testTokenValueMapUNIX() { Map expected = new ImmutableMap.Builder().put("fs", "/").put( "ps", ":").put("lf", "\n").put("sh", "bash").put("source", ".").put("rem", "#").put( - "args", "$@").put("varstart", "$").put("varend", "").put("libraryPathVariable", - "LD_LIBRARY_PATH").put("shebang", "#!/bin/bash\n").build(); + "args", "$@").put("varstart", "$").put("return", "return").put("varend", "").put( + "libraryPathVariable", "LD_LIBRARY_PATH").put("shebang", "#!/bin/bash\n").build(); assertEquals(ShellToken.tokenValueMap(OsFamily.UNIX), expected); } @@ -50,7 +50,8 @@ public class ShellTokenTest { Map expected = new ImmutableMap.Builder().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("shebang", "@echo off\r\n").build(); + "libraryPathVariable", "PATH").put("return", "exit /b").put("shebang", + "@echo off\r\n").build(); assertEquals(ShellToken.tokenValueMap(OsFamily.WINDOWS), expected); } diff --git a/initbuilder/src/test/resources/test_env.bash b/initbuilder/src/test/resources/test_env.bash new file mode 100644 index 0000000000..ee21abec23 --- /dev/null +++ b/initbuilder/src/test/resources/test_env.bash @@ -0,0 +1,10 @@ +# Env file: please do not confuse people by making this executable +# +# Example usage to set a variable +# +# export MAVEN_OPTS="-Xms64m -Xmx128m" + +export JAVA_HOME="/apps/jdk1.6" + +# Please retain this statement so that the script can be validated +return 0 diff --git a/initbuilder/src/test/resources/test_env.cmd b/initbuilder/src/test/resources/test_env.cmd new file mode 100644 index 0000000000..1284b87c51 --- /dev/null +++ b/initbuilder/src/test/resources/test_env.cmd @@ -0,0 +1,10 @@ +@rem Env file: please do not confuse people by making this executable +@rem +@rem Example usage to set a variable +@rem +@rem set MAVEN_OPTS=-Xms64m -Xmx128m + +set JAVA_HOME=/apps/jdk1.6 + +@rem Please retain this statement so that the script can be validated +exit /b 0 diff --git a/initbuilder/src/test/resources/test_script_funcs.cmd b/initbuilder/src/test/resources/test_script_funcs.cmd new file mode 100644 index 0000000000..bac705f36d --- /dev/null +++ b/initbuilder/src/test/resources/test_script_funcs.cmd @@ -0,0 +1,27 @@ +@echo off + +goto END_FUNCTIONS +:ABORT_SUB + echo Aborting: %EXCEPTION%. + exit /b 1 + +:SOURCE_ENV + set ENV_FILE=%1 + shift + if not defined ENV_FILE ( + set EXCEPTION=Internal error. Called SOURCE_ENV with no file param + exit /b 1 + ) + call %SETTINGS_FILE% + if errorlevel 1 ( + set EXCEPTION=Please end your '%SETTINGS_FILE%' file with the command 'exit /b 0' to enable this script to detect syntax errors. + exit /b 1 + ) + exit /b 0 + +:END_FUNCTIONS + +if exist "%APPENV_SETTINGS_FILE%" ( + call :SOURCE_SF "%APPENV_SETTINGS_FILE%" + if errorlevel 1 goto ABORT_SUB +)