Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;

import com.devonfw.tools.ide.log.IdeSubLogger;

Expand Down Expand Up @@ -117,6 +118,20 @@ default ProcessContext addArgs(List<?> args) {
@Override
ProcessContext withPathEntry(Path path);

/**
* @param exitCodeAcceptor the {@link Predicate} that {@link Predicate#test(Object) tests} which {@link ProcessResult#getExitCode() exit codes} should be
* accepted as {@link ProcessResult#isSuccessful() success}.
* @return this {@link ProcessContext} for fluent API calls.
*/
ProcessContext withExitCodeAcceptor(Predicate<Integer> exitCodeAcceptor);

/**
* @return a new child {@link ProcessContext} connected with this {@link ProcessContext} sharing the same {@link #withEnvVar(String, String) environment} and
* {@link #withPathEntry(Path) PATH} but not specific things like {@link #withExitCodeAcceptor(Predicate) exitCodeAcceptor} or
* {@link #errorHandling(ProcessErrorHandling) errorHandling}.
*/
ProcessContext createChild();

/**
* Runs the previously configured {@link #executable(Path) command} with the configured {@link #addArgs(String...) arguments}. Will reset the
* {@link #addArgs(String...) arguments} but not the {@link #executable(Path) command} for sub-sequent calls.
Expand Down Expand Up @@ -225,4 +240,5 @@ default void setOutputListener(OutputListener listener) {

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import com.devonfw.tools.ide.cli.CliProcessException;
Expand All @@ -32,6 +33,7 @@
public class ProcessContextImpl implements ProcessContext {

private static final String PREFIX_USR_BIN_ENV = "/usr/bin/env ";
private static final Predicate<Integer> EXIT_CODE_ACCEPTOR = rc -> rc == ProcessResult.SUCCESS;

/** The owning {@link IdeContext}. */
protected final IdeContext context;
Expand All @@ -50,6 +52,8 @@ public class ProcessContextImpl implements ProcessContext {

private OutputListener outputListener;

private Predicate<Integer> exitCodeAcceptor;

/**
* The constructor.
*
Expand All @@ -69,6 +73,18 @@ public ProcessContextImpl(IdeContext context) {
}
this.arguments = new ArrayList<>();
this.extraPathEntries = new ArrayList<>();
this.exitCodeAcceptor = EXIT_CODE_ACCEPTOR;
}

private ProcessContextImpl(ProcessContextImpl parent) {

super();
this.context = parent.context;
this.processBuilder = parent.processBuilder;
this.errorHandling = ProcessErrorHandling.THROW_ERR;
this.arguments = new ArrayList<>();
this.extraPathEntries = parent.extraPathEntries;
this.exitCodeAcceptor = EXIT_CODE_ACCEPTOR;
}

@Override
Expand Down Expand Up @@ -129,6 +145,19 @@ public ProcessContext withPathEntry(Path path) {
return this;
}

@Override
public ProcessContext withExitCodeAcceptor(Predicate<Integer> exitCodeAcceptor) {

this.exitCodeAcceptor = exitCodeAcceptor;
return this;
}

@Override
public ProcessContext createChild() {

return new ProcessContextImpl(this);
}

@Override
public void setOutputListener(OutputListener listener) {
this.outputListener = listener;
Expand Down Expand Up @@ -202,7 +231,8 @@ public ProcessResult run(ProcessMode processMode) {
}

List<OutputMessage> finalOutput = new ArrayList<>(output);
ProcessResult result = new ProcessResultImpl(this.executable.getFileName().toString(), command, exitCode, finalOutput);
boolean success = this.exitCodeAcceptor.test(exitCode);
ProcessResult result = new ProcessResultImpl(this.executable.getFileName().toString(), command, exitCode, success, finalOutput);

performLogging(result, exitCode, interpreter);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.devonfw.tools.ide.process;

import java.util.List;
import java.util.function.Predicate;

import com.devonfw.tools.ide.cli.CliProcessException;
import com.devonfw.tools.ide.context.IdeContext;
Expand Down Expand Up @@ -60,7 +61,9 @@ public interface ProcessResult {
int getExitCode();

/**
* @return {@code true} if the {@link #getExitCode() exit code} indicates {@link #SUCCESS}, {@code false} otherwise (an error occurred).
* @return {@code true} if the process execution was successful, {@code false} otherwise (an error occurred). By default, success means the
* {@link #getExitCode() exit code} was {@link #SUCCESS}.
* @see ProcessContext#withExitCodeAcceptor(Predicate)
*/
default boolean isSuccessful() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public class ProcessResultImpl implements ProcessResult {

private final List<OutputMessage> outputMessages;

private final boolean success;

/**
* The constructor.
*
Expand All @@ -31,11 +33,25 @@ public class ProcessResultImpl implements ProcessResult {
* @param outputMessages {@link #getOutputMessages() output Messages}.
*/
public ProcessResultImpl(String executable, String command, int exitCode, List<OutputMessage> outputMessages) {
this(executable, command, exitCode, exitCode == SUCCESS, outputMessages);
}

/**
* The constructor.
*
* @param executable the {@link #getExecutable() executable}.
* @param command the {@link #getCommand() command}.
* @param exitCode the {@link #getExitCode() exit code}.
* @param success the {@link #isSuccessful() success} flag.
* @param outputMessages {@link #getOutputMessages() output Messages}.
*/
public ProcessResultImpl(String executable, String command, int exitCode, boolean success, List<OutputMessage> outputMessages) {

super();
this.executable = executable;
this.command = command;
this.exitCode = exitCode;
this.success = success;
this.outputMessages = Objects.requireNonNullElse(outputMessages, Collections.emptyList());
}

Expand Down Expand Up @@ -124,7 +140,12 @@ public List<String> getErr() {
public List<OutputMessage> getOutputMessages() {

return this.outputMessages;
}

@Override
public boolean isSuccessful() {

return this.success;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,12 @@ public ProcessResult runPackageManager(PackageManagerRequest request, boolean sk
completeRequest(request);
ProcessContext pc = request.getProcessContext();
ToolCommandlet pm = request.getPackageManager();
if (skipInstallation) { // See Node.postInstallOnNewInstallation
return pm.runTool(pc, request.getProcessMode(), request.getArgs());
} else {
if (!skipInstallation) { // See Node.postInstallOnNewInstallation
ToolInstallRequest installRequest = new ToolInstallRequest(true);
installRequest.setProcessContext(pc);
return pm.runTool(installRequest, request.getProcessMode(), request.getArgs());
installRequest.setProcessContext(pc.createChild());
pm.install(installRequest);
}
return pm.runTool(pc, request.getProcessMode(), request.getArgs());
}

protected void completeRequest(PackageManagerRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import com.devonfw.tools.ide.common.Tag;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.process.ProcessContext;
import com.devonfw.tools.ide.process.ProcessErrorHandling;
import com.devonfw.tools.ide.process.ProcessMode;
import com.devonfw.tools.ide.process.ProcessResult;
import com.devonfw.tools.ide.tool.LocalToolCommandlet;
Expand Down Expand Up @@ -55,6 +57,9 @@ private VersionIdentifier runPackageManagerGetInstalledVersion(String npmPackage
}
PackageManagerRequest request = new PackageManagerRequest("list", npmPackage).addArg("list").addArg("-g").addArg(npmPackage).addArg("--depth=0")
.setProcessMode(ProcessMode.DEFAULT_CAPTURE);
ProcessContext pc = this.context.newProcess().errorHandling(ProcessErrorHandling.THROW_CLI)
.withExitCodeAcceptor(rc -> true); // if the tool is not installed npm list will end with exit code 1
request.setProcessContext(pc);
ProcessResult result = runPackageManager(request);
if (result.isSuccessful()) {
List<String> versions = result.getOut();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public LocalDateTime getNow() {
return this.now;
}

@Override
public ProcessContext createChild() {

return this;
}

@Override
public ProcessResult run(ProcessMode processMode) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.devonfw.tools.ide.context;

import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.process.ProcessContext;
import com.devonfw.tools.ide.process.ProcessContextImpl;
import com.devonfw.tools.ide.process.ProcessMode;
import com.devonfw.tools.ide.process.ProcessResult;
Expand All @@ -20,6 +21,12 @@ public ProcessContextTestImpl(IdeContext context) {
super(context);
}

@Override
public ProcessContext createChild() {

return this;
}

@Override
public ProcessResult run(ProcessMode processMode) {
ProcessResult result = super.run(ProcessMode.DEFAULT_CAPTURE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void testYarnInstall(WireMockRuntimeInfo wireMockRuntimeInfo) {
}

@Test
void testYarnInstallWhenNpmInstalled(WireMockRuntimeInfo wireMockRuntimeInfo) {
void testYarnInstallWhenNodeInstalled(WireMockRuntimeInfo wireMockRuntimeInfo) {

// arrange
IdeTestContext context = newContext(PROJECT_YARN, wireMockRuntimeInfo);
Expand All @@ -50,6 +50,7 @@ void testYarnInstallWhenNpmInstalled(WireMockRuntimeInfo wireMockRuntimeInfo) {
// assert
assertThat(context).logAtDebug()
.hasMessageContaining("npm' using bash with arguments 'list' '-g' 'yarn' '--depth=0'"); // since npm was installed this should be called
assertThat(context).log().hasNoMessageContaining("-- yarn@");
checkInstallation(context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if [ "$1" = "--version" ]; then
elif [ "$1" = "list" ] && [ "$2" = "-g" ]; then
echo $IDE_HOME/software/node
echo "-- $3@9.9.2"
exit
exit 1
fi
echo "npm $*"
if [ "$1" == "install" ]; then
Expand Down