• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 14975564558

12 May 2025 02:56PM UTC coverage: 67.679% (+0.03%) from 67.649%
14975564558

push

github

web-flow
#1058: fixed missing JAVA_HOME env var (#1100)

3101 of 4990 branches covered (62.14%)

Branch coverage included in aggregate %.

7974 of 11374 relevant lines covered (70.11%)

3.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

30.0
cli/src/main/java/com/devonfw/tools/ide/process/ProcessContext.java
1
package com.devonfw.tools.ide.process;
2

3
import java.nio.file.Path;
4
import java.util.List;
5
import java.util.Objects;
6

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

9
/**
10
 * Wrapper for {@link ProcessBuilder} to simplify its usage and avoid common mistakes and pitfalls.
11
 */
12
public interface ProcessContext extends EnvironmentContext {
13

14
  /**
15
   * @param handling the desired {@link ProcessErrorHandling}.
16
   * @return this {@link ProcessContext} for fluent API calls.
17
   */
18
  ProcessContext errorHandling(ProcessErrorHandling handling);
19

20
  /**
21
   * @param directory the {@link Path} to the working directory from where to execute the command.
22
   * @return this {@link ProcessContext} for fluent API calls.
23
   */
24
  ProcessContext directory(Path directory);
25

26
  /**
27
   * Sets the executable command to be {@link #run()}.
28
   *
29
   * @param executable the {@link Path} to the command to be executed by {@link #run()}. Depending on your operating system and the extension of the
30
   *     executable or OS specific conventions. So e.g. a *.cmd or *.bat file will be called via CMD shell on windows while a *.sh file will be called via Bash,
31
   *     etc.
32
   * @return this {@link ProcessContext} for fluent API calls.
33
   */
34
  ProcessContext executable(Path executable);
35

36
  /**
37
   * Sets the executable command to be {@link #run()}.
38
   *
39
   * @param executable the command to be executed by {@link #run()}.
40
   * @return this {@link ProcessContext} for fluent API calls.
41
   * @see #executable(Path)
42
   */
43
  default ProcessContext executable(String executable) {
44

45
    return executable(Path.of(executable));
7✔
46
  }
47

48
  /**
49
   * Adds a single argument for {@link #executable(Path) command}.
50
   *
51
   * @param arg the next argument for {@link #executable(Path) command} to be added.
52
   * @return this {@link ProcessContext} for fluent API calls.
53
   */
54
  ProcessContext addArg(String arg);
55

56
  /**
57
   * Adds a single argument for {@link #executable(Path) command}.
58
   *
59
   * @param arg the next argument for {@link #executable(Path) command} to be added.
60
   * @return this {@link ProcessContext} for fluent API calls.
61
   */
62
  default ProcessContext addArg(Object arg) {
63

64
    Objects.requireNonNull(arg);
3✔
65
    return addArg(arg.toString());
5✔
66
  }
67

68
  /**
69
   * Adds the given arguments for {@link #executable(Path) command}. E.g. for {@link #executable(Path) command} "mvn" the arguments "clean" and "install" may be
70
   * added here to run "mvn clean install". If this method would be called again with "-Pmyprofile" and "-X" before {@link #run()} gets called then "mvn clean
71
   * install -Pmyprofile -X" would be run.
72
   *
73
   * @param args the arguments for {@link #executable(Path) command} to be added.
74
   * @return this {@link ProcessContext} for fluent API calls.
75
   */
76
  default ProcessContext addArgs(String... args) {
77

78
    for (String arg : args) {
16✔
79
      addArg(arg);
4✔
80
    }
81
    return this;
2✔
82
  }
83

84
  /**
85
   * Adds the given arguments for {@link #executable(Path) command} as arbitrary Java objects. It will add the {@link Object#toString() string representation}
86
   * of these arguments to the command.
87
   *
88
   * @param args the arguments for {@link #executable(Path) command} to be added.
89
   * @return this {@link ProcessContext} for fluent API calls.
90
   */
91
  default ProcessContext addArgs(Object... args) {
92

93
    for (Object arg : args) {
×
94
      addArg(arg);
×
95
    }
96
    return this;
×
97
  }
98

99
  /**
100
   * Adds the given arguments for {@link #executable(Path) command} as arbitrary Java objects. It will add the {@link Object#toString() string representation}
101
   * of these arguments to the command.
102
   *
103
   * @param args the {@link List} of arguments for {@link #executable(Path) command} to be added.
104
   * @return this {@link ProcessContext} for fluent API calls.
105
   */
106
  default ProcessContext addArgs(List<?> args) {
107

108
    for (Object arg : args) {
×
109
      addArg(arg);
×
110
    }
×
111
    return this;
×
112
  }
113

114
  @Override
115
  ProcessContext withEnvVar(String key, String value);
116

117
  @Override
118
  ProcessContext withPathEntry(Path path);
119

120
  /**
121
   * Runs the previously configured {@link #executable(Path) command} with the configured {@link #addArgs(String...) arguments}. Will reset the
122
   * {@link #addArgs(String...) arguments} but not the {@link #executable(Path) command} for sub-sequent calls.
123
   *
124
   * @return the exit code. Will be {@link ProcessResult#SUCCESS} on successful completion of the {@link Process}.
125
   */
126
  default int run() {
127

128
    return run(ProcessMode.DEFAULT).getExitCode();
5✔
129
  }
130

131
  /**
132
   * Runs the given {@code executable} with the given {@code arguments}.
133
   *
134
   * @param executable the executable program.
135
   * @param arguments the program arguments.
136
   * @throws IllegalStateException if the command failed.
137
   */
138
  default void run(String executable, String... arguments) {
139

140
    executable(executable).addArgs(arguments).errorHandling(ProcessErrorHandling.THROW_ERR).run();
×
141
  }
×
142

143
  /**
144
   * Runs the given {@code executable} with the given {@code arguments} and returns the expected single line from its
145
   * {@link ProcessResult#getOut() standard output}.
146
   *
147
   * @param executable the executable program.
148
   * @param arguments the program arguments.
149
   * @return the single line printed from the command.
150
   * @throws IllegalStateException if the command failed or did not print a single line as expected.
151
   */
152
  default String runAndGetSingleOutput(String executable, String... arguments) {
153

154
    return runAndGetSingleOutput(null, executable, arguments);
×
155
  }
156

157
  /**
158
   * Runs the given {@code executable} with the given {@code arguments} and returns the expected single line from its
159
   * {@link ProcessResult#getOut() standard output}.
160
   *
161
   * @param logger the {@link IdeSubLogger} used to log errors instead of throwing an exception.
162
   * @param executable the executable program.
163
   * @param arguments the program arguments.
164
   * @return the single line printed from the command.
165
   * @throws IllegalStateException if the command did not print a single line as expected.
166
   */
167
  default String runAndGetSingleOutput(IdeSubLogger logger, String executable, String... arguments) {
168

169
    executable(executable).addArgs(arguments);
×
170
    if (logger == null) {
×
171
      errorHandling(ProcessErrorHandling.THROW_ERR);
×
172
    }
173
    ProcessResult result = run(ProcessMode.DEFAULT_CAPTURE);
×
174
    return result.getSingleOutput(logger);
×
175
  }
176

177
  /**
178
   * Runs the previously configured {@link #executable(Path) command} with the configured {@link #addArgs(String...) arguments}. Will reset the
179
   * {@link #addArgs(String...) arguments} but not the {@link #executable(Path) command} for sub-sequent calls.
180
   *
181
   * @param processMode {@link ProcessMode}
182
   * @return the {@link ProcessResult}.
183
   */
184
  ProcessResult run(ProcessMode processMode);
185

186
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc