Skip to content
Browse files

fixed quotes arround commandline

  • Loading branch information...
1 parent 5f12ba3 commit 0b9453baa3c355bde1cd66a13a918e6b5daac56b Parris, A - SPLXL committed Dec 18, 2012
View
109 src/main/java/com/sirrapa/maven/plugins/nodejs/NodeJsMojo.java
@@ -11,6 +11,7 @@
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
+import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.cli.CommandLineException;
import org.codehaus.plexus.util.cli.CommandLineUtils;
import org.codehaus.plexus.util.cli.Commandline;
@@ -20,7 +21,7 @@
* Goal which runs Nodejs.
*/
@Mojo(name = "compile", requiresDependencyResolution = ResolutionScope.COMPILE,
- defaultPhase = LifecyclePhase.PROCESS_SOURCES, threadSafe = true)
+ defaultPhase = LifecyclePhase.PROCESS_SOURCES, threadSafe = true)
public class NodeJsMojo extends AbstractDependencyMojo {
@@ -79,10 +80,10 @@
public static final String MAC_FAMILY = "mac";
public static final String[][] OS_CLASSIFIER_FAMILIES = new String[][]{
- {WINDOWS_FAMILY, CLASSIFIER_WINDOWS_FAMILY},
- {LINUX_FAMILY, CLASSIFIER_LINUX_FAMILY},
- {SOLARIS_FAMILY, CLASSIFIER_UNIX_FAMILY},
- {MAC_FAMILY, CLASSIFIER_MAC_FAMILY}};
+ {WINDOWS_FAMILY, CLASSIFIER_WINDOWS_FAMILY},
+ {LINUX_FAMILY, CLASSIFIER_LINUX_FAMILY},
+ {SOLARIS_FAMILY, CLASSIFIER_UNIX_FAMILY},
+ {MAC_FAMILY, CLASSIFIER_MAC_FAMILY}};
/**
@@ -101,23 +102,21 @@ public void execute() throws MojoExecutionException {
executeNodeJs();
}
- } catch (ArtifactResolutionException e) {
+ }
+ catch (ArtifactResolutionException e) {
throw new MojoExecutionException("Unable to locate artifact: '" + nodeJsArtifactString + "' in any repository.", e);
- } catch (ArtifactNotFoundException e) {
+ }
+ catch (ArtifactNotFoundException e) {
throw new MojoExecutionException("Unable to locate artifact: '" + nodeJsArtifactString + "' in any repository.", e);
- } catch (CommandLineException e) {
+ }
+ catch (CommandLineException e) {
throw new MojoExecutionException("Command execution failed.", e);
- } catch (NodeJsPluginException e) {
+ }
+ catch (NodeJsPluginException e) {
throw new MojoExecutionException("", e);
}
}
- protected int executeCommandLine(Commandline commandLine, StreamConsumer stream1, StreamConsumer stream2)
- throws CommandLineException {
- getLog().info("Executing command: " + commandLine.toString());
- return CommandLineUtils.executeCommandLine(commandLine, stream1, stream2);
- }
-
/**
* Determine the right command executable for the given osFamily
*
@@ -129,38 +128,76 @@ protected String getNodeJsExecutable(String osFamily) {
StringBuilder sb = new StringBuilder(nodejsDirectory.getAbsolutePath());
if (osFamily.toLowerCase().startsWith(CLASSIFIER_WINDOWS_FAMILY)) {
sb.append(File.separator).append("node.exe");
- } else {
+ }
+ else {
sb.append(File.separator).append("bin").append(File.separator).append("node");
}
System.out.println("Determined executable for osFamily: " + sb.toString());
return sb.toString();
}
- protected void executeNodeJs() throws MojoExecutionException, CommandLineException {
- // create a commandline and set the executable to nodejs
+ /**
+ * Executes the given commandline
+ * @param commandLine
+ * @return
+ * @throws CommandLineException
+ */
+ protected void executeCommandLine(Commandline commandLine)
+ throws CommandLineException, MojoExecutionException {
+ getLog().info("Executing command: " + commandLine.toString());
+ CommandLineUtils.StringStreamConsumer systemErr = new CommandLineUtils.StringStreamConsumer();
+ CommandLineUtils.StringStreamConsumer systemOut = new CommandLineUtils.StringStreamConsumer();
+
+ int exitCode = CommandLineUtils.executeCommandLine(commandLine, systemOut, systemErr);
+ String output = StringUtils.isEmpty(systemOut.getOutput()) ? null : '\n' + systemOut.getOutput().trim();
+ if (exitCode != 0) {
+ if (StringUtils.isNotEmpty(output)) {
+ getLog().info(output);
+ }
+ throw new MojoExecutionException("Result of " + commandLine + " execution is: '" + exitCode + "'.");
+ }
+ }
+
+ /**
+ * Create an CommandLine for the given executable
+ * @param workDir
+ * @param executable
+ * @param args
+ * @return
+ */
+ protected Commandline getCommandLine(File workDir, String executable, String... args) {
Commandline commandLine = new Commandline();
+ if (workDir != null) {
+ if (!workDir.exists()) {
+ workDir.mkdirs();
+ }
+ commandLine.setWorkingDirectory(workDir);
+ }
+
+ commandLine.getShell().setQuotedExecutableEnabled(false);
+ commandLine.setExecutable(executable);
- if (!workingDirectory.exists()) {
- workingDirectory.mkdirs();
+ if (args != null) {
+ commandLine.getShell().setQuotedArgumentsEnabled(false);
+ commandLine.addArguments(args);
}
- commandLine.setWorkingDirectory(workingDirectory);
- StreamConsumer consumer = new StreamConsumer() {
- public void consumeLine(String line) {
- getLog().info(line);
- }
- };
+ System.out.println("commandLine = " + commandLine);
+
+ return commandLine;
+ }
+ /**
+ * Execute nodejs for every module in #modules.
+ * @throws MojoExecutionException
+ * @throws CommandLineException
+ */
+ protected void executeNodeJs() throws MojoExecutionException, CommandLineException {
String executable = getNodeJsExecutable(nodejsClassifier) + " %s";
for (String module : modules) {
- commandLine.setExecutable(String.format(executable, module));
-
- System.out.println("commandLine = " + commandLine);
- int result = executeCommandLine(commandLine, consumer, consumer);
-
- if (result != 0) {
- throw new MojoExecutionException("Result of " + commandLine + " execution is: '" + result + "'.");
- }
+ // get a commandline with the nodejs executable
+ Commandline commandLine = getCommandLine(workingDirectory, String.format(executable, module), null);
+ executeCommandLine(commandLine);
}
}
@@ -180,7 +217,7 @@ protected void unpackNodeJs(Artifact nodeJsArtifact, File outputDirectory) throw
* Resolve the nodejs artifact from the repos #remoteRepositories and #localRepository.
*
* @return The desired Artifact file if resolved
- * @throws ArtifactNotFoundException if artifact was not resolved
+ * @throws ArtifactNotFoundException if artifact was not resolved
* @throws ArtifactResolutionException if there went something wrong while resolving
*/
protected Artifact resolveNodeJsArtifact() throws ArtifactNotFoundException, ArtifactResolutionException {
@@ -200,7 +237,7 @@ protected Artifact resolveNodeJsArtifact() throws ArtifactNotFoundException, Art
* @throws NodeJsPluginException if the configuration is invalid
*/
protected void initializeModulesConfiguration()
- throws NodeJsPluginException {
+ throws NodeJsPluginException {
nodejsClassifier = getOSClassifier();
if (modules == null) {
throw new NodeJsPluginException("No NodeJs modules configured.");
View
30 src/test/java/com/sirrapa/maven/plugins/nodejs/NodeJsMojoTest.java
@@ -16,6 +16,7 @@
import org.apache.maven.plugin.testing.stubs.StubArtifactResolver;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.util.FileUtils;
+import org.codehaus.plexus.util.cli.Commandline;
public class NodeJsMojoTest extends AbstractMojoTestCase {
NodeJsMojo mojo;
@@ -168,19 +169,30 @@ public void testGetNodeJsExecutable() throws Exception {
System.out.println("nodejsEx = " + nodejsEx);
if (NodeJsMojo.WINDOWS_FAMILY.endsWith(family[0])) {
- assertTrue(String.format("The nodejs executable for %s family is invalid", family), nodejsEx.endsWith("/nodejs/node.exe"));
+ assertTrue(String.format("The nodejs executable for %s family is invalid", family), nodejsEx.endsWith(File.separator+"nodejs"+File.separator+"node.exe"));
} else {
- assertTrue(String.format("The nodejs executable for %s family is invalid", family), nodejsEx.endsWith("/nodejs/bin/node"));
+ assertTrue(String.format("The nodejs executable for %s family is invalid", family), nodejsEx.endsWith(File.separator+"nodejs"+File.separator+"bin"+File.separator+"node"));
}
}
}
-// public void testExecuteNodeJs() throws Exception {
-// loadMojo("plugin-config.xml");
-// mojo.initializeModulesConfiguration();
-// setVariableValueToObject(mojo, "nodejsDirectory", new File(System.getProperty("java.io.tmpdir") + "nodejs/"));
-// setVariableValueToObject(mojo, "workingDirectory", new File(System.getProperty("java.io.tmpdir") + "work/"));
-// mojo.executeNodeJs();
-// }
+ public void testGetCommandLine() throws Exception {
+ loadMojo("plugin-config.xml");
+ mojo.initializeModulesConfiguration();
+ setVariableValueToObject(mojo, "nodejsDirectory", new File(System.getProperty("java.io.tmpdir") + "nodejs/"));
+ String executable = mojo.getNodeJsExecutable("win");
+ assertNotNull("executable is null", executable);
+
+ Commandline cmd = mojo.getCommandLine(new File(workDir),executable,"-o foobar");
+ assertNotNull("commandline si null", cmd);
+ String shellCmd = cmd.getShell().getShellCommand();
+ String shellArgs = "";
+ for (String arg:cmd.getShell().getShellArgs()) {
+ shellArgs+= " ";
+ shellArgs+= arg;
+ }
+ String expected = "%s%s \"%s -o foobar\"";
+ assertEquals("The commandline string is invalid", String.format(expected,shellCmd,shellArgs,executable), cmd.toString());
+ }
}

0 comments on commit 0b9453b

Please sign in to comment.
Something went wrong with that request. Please try again.