diff --git a/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/.bash_profile b/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/.bash_profile new file mode 100644 index 000000000..960640755 --- /dev/null +++ b/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/.bash_profile @@ -0,0 +1,3 @@ +FNM_DIR=$HOME/.fnm +PATH=$FNM_DIR:$PATH +eval "$(fnm env --use-on-cd --shell bash)" diff --git a/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/.gitignore b/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/.gitignore new file mode 100644 index 000000000..3c3629e64 --- /dev/null +++ b/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/install-fnm.sh b/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/install-fnm.sh new file mode 100755 index 000000000..175118d22 --- /dev/null +++ b/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/install-fnm.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +# invoker env variables are not loaded yet +export HOME="$(dirname "$0")" + +export FNM_DIR="$HOME/.fnm"; +mkdir "$FNM_DIR" + +curl -fsSL https://fnm.vercel.app/install | bash -s -- --install-dir "$FNM_DIR" --skip-shell --force-install \ No newline at end of file diff --git a/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/invoker.properties b/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/invoker.properties new file mode 100644 index 000000000..286ff832e --- /dev/null +++ b/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/invoker.properties @@ -0,0 +1,5 @@ +invoker.buildResult=failure + +invoker.environmentVariables.HOME=${basedir}/target/it/node-version-manager/fnm/with-no-version-file +invoker.environmentVariables.FNM_DIR=${basedir}/target/it/node-version-manager/fnm/with-no-version-file/.fnm +invoker.environmentVariables.XDG_DATA_HOME=${basedir}/target/it/node-version-manager/fnm/with-no-version-file \ No newline at end of file diff --git a/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/package-lock.json b/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/package-lock.json new file mode 100644 index 000000000..1a8b7d458 --- /dev/null +++ b/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/package-lock.json @@ -0,0 +1,27 @@ +{ + "name": "example", + "version": "0.0.1", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "example", + "version": "0.0.1", + "dependencies": { + "classnames": "^2.3.2" + } + }, + "node_modules/classnames": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/classnames/-/classnames-2.3.2.tgz", + "integrity": "sha512-CSbhY4cFEJRe6/GQzIk5qXZ4Jeg5pcsP7b5peFSDpffpe1cqjASH/n9UTjBwOp6XpMSTwQ8Za2K5V02ueA7Tmw==" + } + }, + "dependencies": { + "classnames": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/classnames/-/classnames-2.3.2.tgz", + "integrity": "sha512-CSbhY4cFEJRe6/GQzIk5qXZ4Jeg5pcsP7b5peFSDpffpe1cqjASH/n9UTjBwOp6XpMSTwQ8Za2K5V02ueA7Tmw==" + } + } +} diff --git a/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/package.json b/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/package.json new file mode 100644 index 000000000..e36bdbfca --- /dev/null +++ b/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/package.json @@ -0,0 +1,10 @@ +{ + "name": "example", + "version": "0.0.1", + "dependencies": { + "classnames": "^2.3.2" + }, + "scripts": { + "prebuild": "npm install" + } +} diff --git a/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/pom.xml b/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/pom.xml new file mode 100644 index 000000000..a48ec0ea2 --- /dev/null +++ b/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/pom.xml @@ -0,0 +1,44 @@ + + + 4.0.0 + + com.github.eirslett + fnm-with-npm + 0 + pom + + + + + com.github.eirslett + frontend-maven-plugin + + @project.version@ + + + ${basedir} + + + + + install node + + install-node-and-npm + + + + npm ci + + npm + + + + ci + + + + + + + + diff --git a/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/prebuild.groovy b/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/prebuild.groovy new file mode 100644 index 000000000..6b13e1495 --- /dev/null +++ b/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/prebuild.groovy @@ -0,0 +1,4 @@ +// TODO share installation as test utils https://maven.apache.org/plugins/maven-invoker-plugin/integration-test-mojo.html#addTestClassPath +def p = "bash $basedir/install-fnm.sh".execute() +p.waitFor() +println p.text \ No newline at end of file diff --git a/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/verify.groovy b/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/verify.groovy new file mode 100644 index 000000000..6b402848b --- /dev/null +++ b/frontend-maven-plugin/src/it/node-version-manager/fnm/with-no-version-file/verify.groovy @@ -0,0 +1,5 @@ +import org.codehaus.plexus.util.FileUtils + +String buildLog = FileUtils.fileRead(new File(basedir, 'build.log')) +assert buildLog.contains('Installing node with FNM') : 'Node has been installed with a different version manager' +assert buildLog.contains('error: Can\'t find version in dotfiles. Please provide a version manually to the command.') : 'Node installation hasn\'t failed with a descriptive message' diff --git a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/ShellExecutor.java b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/ShellExecutor.java index babcb4134..054ff078e 100644 --- a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/ShellExecutor.java +++ b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/ShellExecutor.java @@ -6,16 +6,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.BufferedReader; import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; public class ShellExecutor { @@ -26,21 +20,13 @@ public ShellExecutor(InstallConfig config) { this.config = config; } - public String execute(List command) { - List profiledShellCommand = getShellCommand(command); - - ProcessExecutor executor = new ProcessExecutor( - config.getWorkingDirectory(), - Collections.emptyList(), - profiledShellCommand, - config.getPlatform(), - Collections.emptyMap()); - + public String executeAndCatchErrors(List command) { ByteArrayOutputStream stdout = new ByteArrayOutputStream(); ByteArrayOutputStream stderr = new ByteArrayOutputStream(); + List profiledShellCommand = getShellCommand(command); try { - int exitValue = executor.execute(logger, stdout, stderr); + int exitValue = execute(profiledShellCommand, stdout, stderr); if (exitValue != 0) { logger.debug("Command finished with error exit code {}, error output `{}`", exitValue, parseOutput(stderr)); } @@ -53,6 +39,44 @@ public String execute(List command) { return output; } + public String executeOrFail(List command) { + ByteArrayOutputStream stdout = new ByteArrayOutputStream(); + ByteArrayOutputStream stderr = new ByteArrayOutputStream(); + List profiledShellCommand = getShellCommand(command); + + boolean hasExecutionFailed = false; + try { + int exitValue = execute(profiledShellCommand, stdout, stderr); + if (exitValue != 0) { + hasExecutionFailed = true; + } + } catch (ProcessExecutionException e) { + hasExecutionFailed = true; + } + + if (hasExecutionFailed) { + String commandText = String.join(" ", profiledShellCommand); + throw new RuntimeException(String.format("Execution of `%s` has failed" + + "\nstdout: `%s`" + + "\nstderr: `%s`", commandText, parseOutput(stdout), parseOutput(stderr))); + } else { + String output = parseOutput(stdout); + logger.debug("Command output: `{}`", output); + return output; + } + } + + private int execute(List command, ByteArrayOutputStream stdout, ByteArrayOutputStream stderr) throws ProcessExecutionException { + ProcessExecutor executor = new ProcessExecutor( + config.getWorkingDirectory(), + Collections.emptyList(), + command, + config.getPlatform(), + Collections.emptyMap()); + + return executor.execute(logger, stdout, stderr); + } + private List getShellCommand(List command) { List profiledShellCommand = new ArrayList<>(); profiledShellCommand.add(getCurrentShell()); diff --git a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/AsdfClient.java b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/AsdfClient.java index cc93faf04..5d125d499 100644 --- a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/AsdfClient.java +++ b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/AsdfClient.java @@ -20,7 +20,7 @@ public AsdfClient(ShellExecutor shellExecutor) { @Override public boolean isInstalled() { - String version = shellExecutor.execute(Arrays.asList( + String version = shellExecutor.executeAndCatchErrors(Arrays.asList( EXECUTABLE, "--version" )); @@ -30,17 +30,17 @@ public boolean isInstalled() { @Override public void installNode() { - shellExecutor.execute(Arrays.asList( + shellExecutor.executeOrFail(Arrays.asList( EXECUTABLE, "plugin", "add", "nodejs" )); - shellExecutor.execute(Arrays.asList( + shellExecutor.executeOrFail(Arrays.asList( EXECUTABLE, "install", "nodejs" )); } @Override public File getNodeExecutable() { - return new File(shellExecutor.execute(Arrays.asList( + return new File(shellExecutor.executeOrFail(Arrays.asList( EXECUTABLE, "which", "node" ))); } diff --git a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/FnmClient.java b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/FnmClient.java index 835ed8902..e730e4a1c 100644 --- a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/FnmClient.java +++ b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/FnmClient.java @@ -21,7 +21,7 @@ public FnmClient(ShellExecutor shellExecutor) { @Override public boolean isInstalled() { - String version = cleanOutput(shellExecutor.execute(Arrays.asList( + String version = cleanOutput(shellExecutor.executeAndCatchErrors(Arrays.asList( EXECUTABLE, "--version" ))); @@ -30,15 +30,15 @@ public boolean isInstalled() { @Override public void installNode() { - shellExecutor.execute(Arrays.asList( + shellExecutor.executeOrFail(Arrays.asList( EXECUTABLE, "install" )); } @Override public File getNodeExecutable() { - String currentNodeVersion = cleanOutput(shellExecutor.execute(Arrays.asList(EXECUTABLE, "current"))); - String fnmDir = cleanOutput(shellExecutor.execute(Arrays.asList("echo", "$FNM_DIR"))); + String currentNodeVersion = cleanOutput(shellExecutor.executeOrFail(Arrays.asList(EXECUTABLE, "current"))); + String fnmDir = cleanOutput(shellExecutor.executeOrFail(Arrays.asList("echo", "$FNM_DIR"))); return Paths.get(fnmDir, "node-versions", currentNodeVersion, "installation", "bin", "node").toFile(); } diff --git a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/MiseClient.java b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/MiseClient.java index 70657f508..d23c34605 100644 --- a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/MiseClient.java +++ b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/MiseClient.java @@ -7,7 +7,6 @@ import java.io.File; import java.nio.file.Paths; import java.util.Arrays; -import java.util.stream.Collectors; public class MiseClient implements VersionManagerClient { final Logger logger = LoggerFactory.getLogger(getClass()); @@ -21,7 +20,7 @@ public MiseClient(ShellExecutor shellExecutor) { @Override public boolean isInstalled() { - String version = shellExecutor.execute(Arrays.asList( + String version = shellExecutor.executeAndCatchErrors(Arrays.asList( EXECUTABLE, "--version" )); @@ -30,14 +29,14 @@ public boolean isInstalled() { @Override public void installNode() { - shellExecutor.execute(Arrays.asList( + shellExecutor.executeOrFail(Arrays.asList( EXECUTABLE, "install", "node" )); } @Override public File getNodeExecutable() { - return new File(shellExecutor.execute(Arrays.asList( + return new File(shellExecutor.executeOrFail(Arrays.asList( EXECUTABLE, "which", "node" ))); } diff --git a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/NvmClient.java b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/NvmClient.java index c487718dd..ac20ac9c9 100644 --- a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/NvmClient.java +++ b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/NvmClient.java @@ -19,7 +19,7 @@ public NvmClient(ShellExecutor shellExecutor) { @Override public boolean isInstalled() { - String version = shellExecutor.execute(Arrays.asList( + String version = shellExecutor.executeAndCatchErrors(Arrays.asList( EXECUTABLE, "--version" )); @@ -28,14 +28,14 @@ public boolean isInstalled() { @Override public void installNode() { - shellExecutor.execute(Arrays.asList( + shellExecutor.executeOrFail(Arrays.asList( EXECUTABLE, "install" )); } @Override public File getNodeExecutable() { - return new File(shellExecutor.execute(Arrays.asList( + return new File(shellExecutor.executeOrFail(Arrays.asList( EXECUTABLE, "which", "node" ))); } diff --git a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/NvsClient.java b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/NvsClient.java index 531dd4c98..5b89f1026 100644 --- a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/NvsClient.java +++ b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/NvsClient.java @@ -19,7 +19,7 @@ public NvsClient(ShellExecutor shellExecutor) { @Override public boolean isInstalled() { - String version = shellExecutor.execute(Arrays.asList( + String version = shellExecutor.executeAndCatchErrors(Arrays.asList( EXECUTABLE, "--version" )); @@ -28,14 +28,14 @@ public boolean isInstalled() { @Override public void installNode() { - shellExecutor.execute(Arrays.asList( + shellExecutor.executeOrFail(Arrays.asList( EXECUTABLE, "add" )); } @Override public File getNodeExecutable() { - return new File(shellExecutor.execute(Arrays.asList( + return new File(shellExecutor.executeOrFail(Arrays.asList( EXECUTABLE, "which", "node" ))); }