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

devonfw / IDEasy / 12808103722

16 Jan 2025 11:40AM UTC coverage: 68.152% (+0.3%) from 67.826%
12808103722

Pull #912

github

web-flow
Merge eb3a1052d into 93247a4dd
Pull Request #912: #498: resolve variables on load #691: solve sub-node merging

2709 of 4345 branches covered (62.35%)

Branch coverage included in aggregate %.

7021 of 9932 relevant lines covered (70.69%)

3.09 hits per line

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

79.73
cli/src/main/java/com/devonfw/tools/ide/process/ProcessContextImpl.java
1
package com.devonfw.tools.ide.process;
2

3
import java.io.BufferedReader;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.io.InputStreamReader;
7
import java.lang.ProcessBuilder.Redirect;
8
import java.nio.charset.StandardCharsets;
9
import java.nio.file.Files;
10
import java.nio.file.Path;
11
import java.util.ArrayList;
12
import java.util.List;
13
import java.util.Map;
14
import java.util.Objects;
15
import java.util.concurrent.CompletableFuture;
16
import java.util.concurrent.ConcurrentLinkedQueue;
17
import java.util.stream.Collectors;
18

19
import com.devonfw.tools.ide.cli.CliProcessException;
20
import com.devonfw.tools.ide.common.SystemPath;
21
import com.devonfw.tools.ide.context.IdeContext;
22
import com.devonfw.tools.ide.environment.VariableLine;
23
import com.devonfw.tools.ide.log.IdeLogLevel;
24
import com.devonfw.tools.ide.os.SystemInfoImpl;
25
import com.devonfw.tools.ide.os.WindowsPathSyntax;
26
import com.devonfw.tools.ide.util.FilenameUtil;
27
import com.devonfw.tools.ide.variable.IdeVariables;
28

29
/**
30
 * Implementation of {@link ProcessContext}.
31
 */
32
public class ProcessContextImpl implements ProcessContext {
33

34
  private static final String PREFIX_USR_BIN_ENV = "/usr/bin/env ";
35

36
  /** The owning {@link IdeContext}. */
37
  protected final IdeContext context;
38

39
  private final ProcessBuilder processBuilder;
40

41
  private final List<String> arguments;
42

43
  private Path executable;
44

45
  private String overriddenPath;
46

47
  private final List<Path> extraPathEntries;
48

49
  private ProcessErrorHandling errorHandling;
50

51
  /**
52
   * The constructor.
53
   *
54
   * @param context the owning {@link IdeContext}.
55
   */
56
  public ProcessContextImpl(IdeContext context) {
57

58
    super();
2✔
59
    this.context = context;
3✔
60
    this.processBuilder = new ProcessBuilder();
7✔
61
    this.errorHandling = ProcessErrorHandling.THROW_ERR;
3✔
62
    Map<String, String> environment = this.processBuilder.environment();
4✔
63
    for (VariableLine var : this.context.getVariables().collectExportedVariables()) {
13✔
64
      if (var.isExport()) {
3!
65
        environment.put(var.getName(), var.getValue());
7✔
66
      }
67
    }
1✔
68
    this.arguments = new ArrayList<>();
5✔
69
    this.extraPathEntries = new ArrayList<>();
5✔
70
  }
1✔
71

72
  @Override
73
  public ProcessContext errorHandling(ProcessErrorHandling handling) {
74

75
    Objects.requireNonNull(handling);
3✔
76
    this.errorHandling = handling;
3✔
77
    return this;
2✔
78
  }
79

80
  @Override
81
  public ProcessContext directory(Path directory) {
82

83
    if (directory != null) {
×
84
      this.processBuilder.directory(directory.toFile());
×
85
    } else {
86
      this.context.debug(
×
87
          "Could not set the process builder's working directory! Directory of the current java process is used.");
88
    }
89

90
    return this;
×
91
  }
92

93
  @Override
94
  public ProcessContext executable(Path command) {
95

96
    if (!this.arguments.isEmpty()) {
4!
97
      throw new IllegalStateException("Arguments already present - did you forget to call run for previous call?");
×
98
    }
99

100
    this.executable = command;
3✔
101
    return this;
2✔
102
  }
103

104
  @Override
105
  public ProcessContext addArg(String arg) {
106

107
    this.arguments.add(arg);
5✔
108
    return this;
2✔
109
  }
110

111
  @Override
112
  public ProcessContext withEnvVar(String key, String value) {
113

114
    if (IdeVariables.PATH.getName().equals(key)) {
5!
115
      this.overriddenPath = value;
×
116
    } else {
117
      this.context.trace("Setting process environment variable {}={}", key, value);
14✔
118
      this.processBuilder.environment().put(key, value);
7✔
119
    }
120
    return this;
2✔
121
  }
122

123
  @Override
124
  public ProcessContext withPathEntry(Path path) {
125

126
    this.extraPathEntries.add(path);
5✔
127
    return this;
2✔
128
  }
129

130
  @Override
131
  public ProcessResult run(ProcessMode processMode) {
132

133
    if (processMode == ProcessMode.DEFAULT) {
3✔
134
      this.processBuilder.redirectOutput(Redirect.INHERIT).redirectError(Redirect.INHERIT);
7✔
135
    }
136

137
    if (processMode == ProcessMode.DEFAULT_SILENT) {
3!
138
      this.processBuilder.redirectOutput(Redirect.DISCARD).redirectError(Redirect.DISCARD);
×
139
    }
140

141
    if (this.executable == null) {
3✔
142
      throw new IllegalStateException("Missing executable to run process!");
5✔
143
    }
144

145
    SystemPath systemPath = this.context.getPath();
4✔
146
    if ((this.overriddenPath != null) || !this.extraPathEntries.isEmpty()) {
7!
147
      systemPath = systemPath.withPath(this.overriddenPath, this.extraPathEntries);
7✔
148
    }
149
    String path = systemPath.toString();
3✔
150
    this.context.trace("Setting PATH for process execution of {} to {}", this.executable.getFileName(), path);
16✔
151
    this.executable = systemPath.findBinary(this.executable);
6✔
152
    this.processBuilder.environment().put(IdeVariables.PATH.getName(), path);
8✔
153
    List<String> args = new ArrayList<>(this.arguments.size() + 4);
9✔
154
    String interpreter = addExecutable(args);
4✔
155
    args.addAll(this.arguments);
5✔
156
    String command = createCommand();
3✔
157
    if (this.context.debug().isEnabled()) {
5!
158
      String message = createCommandMessage(interpreter, " ...");
5✔
159
      this.context.debug(message);
4✔
160
    }
161

162
    try {
163

164
      if (processMode == ProcessMode.DEFAULT_CAPTURE) {
3✔
165
        this.processBuilder.redirectOutput(Redirect.PIPE).redirectError(Redirect.PIPE);
8✔
166
      } else if (processMode.isBackground()) {
3✔
167
        modifyArgumentsOnBackgroundProcess(processMode);
3✔
168
      }
169

170
      this.processBuilder.command(args);
5✔
171

172
      ConcurrentLinkedQueue<OutputMessage> output = new ConcurrentLinkedQueue<>();
4✔
173

174
      Process process = this.processBuilder.start();
4✔
175

176
      try {
177
        if (processMode == ProcessMode.DEFAULT_CAPTURE) {
3✔
178
          CompletableFuture<Void> outFut = readInputStream(process.getInputStream(), false, output);
6✔
179
          CompletableFuture<Void> errFut = readInputStream(process.getErrorStream(), true, output);
6✔
180
          outFut.get();
3✔
181
          errFut.get();
3✔
182
        }
183

184
        int exitCode;
185

186
        if (processMode.isBackground()) {
3✔
187
          exitCode = ProcessResult.SUCCESS;
3✔
188
        } else {
189
          exitCode = process.waitFor();
3✔
190
        }
191

192
        List<OutputMessage> finalOutput = new ArrayList<>(output);
5✔
193
        ProcessResult result = new ProcessResultImpl(this.executable.getFileName().toString(), command, exitCode, finalOutput);
11✔
194

195
        performLogging(result, exitCode, interpreter);
5✔
196

197
        return result;
4✔
198
      } finally {
199
        if (!processMode.isBackground()) {
3✔
200
          process.destroy();
2✔
201
        }
202
      }
203
    } catch (CliProcessException | IllegalStateException e) {
1✔
204
      // these exceptions are thrown from performLogOnError and we do not want to wrap them (see #593)
205
      throw e;
2✔
206
    } catch (Exception e) {
1✔
207
      String msg = e.getMessage();
3✔
208
      if ((msg == null) || msg.isEmpty()) {
5!
209
        msg = e.getClass().getSimpleName();
×
210
      }
211
      throw new IllegalStateException(createCommandMessage(interpreter, " failed: " + msg), e);
10✔
212
    } finally {
213
      this.arguments.clear();
3✔
214
    }
215
  }
216

217
  /**
218
   * Asynchronously and parallel reads {@link InputStream input stream} and stores it in {@link CompletableFuture}. Inspired by: <a href=
219
   * "https://stackoverflow.com/questions/14165517/processbuilder-forwarding-stdout-and-stderr-of-started-processes-without-blocki/57483714#57483714">StackOverflow</a>
220
   *
221
   * @param is {@link InputStream}.
222
   * @param errorStream to identify if the output came from stdout or stderr
223
   * @return {@link CompletableFuture}.
224
   */
225
  private static CompletableFuture<Void> readInputStream(InputStream is, boolean errorStream, ConcurrentLinkedQueue<OutputMessage> outputMessages) {
226

227
    return CompletableFuture.supplyAsync(() -> {
6✔
228

229
      try (InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr)) {
10✔
230

231
        String line;
232
        while ((line = br.readLine()) != null) {
5✔
233
          OutputMessage outputMessage = new OutputMessage(errorStream, line);
6✔
234
          outputMessages.add(outputMessage);
4✔
235
        }
1✔
236

237
        return null;
4✔
238
      } catch (Throwable e) {
1✔
239
        throw new RuntimeException("There was a problem while executing the program", e);
6✔
240
      }
241
    });
242
  }
243

244
  private String createCommand() {
245
    String cmd = this.executable.toString();
4✔
246
    StringBuilder sb = new StringBuilder(cmd.length() + this.arguments.size() * 4);
12✔
247
    sb.append(cmd);
4✔
248
    for (String arg : this.arguments) {
11✔
249
      sb.append(' ');
4✔
250
      sb.append(arg);
4✔
251
    }
1✔
252
    return sb.toString();
3✔
253
  }
254

255
  private String createCommandMessage(String interpreter, String suffix) {
256

257
    StringBuilder sb = new StringBuilder();
4✔
258
    sb.append("Running command '");
4✔
259
    sb.append(this.executable);
5✔
260
    sb.append("'");
4✔
261
    if (interpreter != null) {
2✔
262
      sb.append(" using ");
4✔
263
      sb.append(interpreter);
4✔
264
    }
265
    int size = this.arguments.size();
4✔
266
    if (size > 0) {
2✔
267
      sb.append(" with arguments");
4✔
268
      for (int i = 0; i < size; i++) {
7✔
269
        String arg = this.arguments.get(i);
6✔
270
        sb.append(" '");
4✔
271
        sb.append(arg);
4✔
272
        sb.append("'");
4✔
273
      }
274
    }
275
    sb.append(suffix);
4✔
276
    return sb.toString();
3✔
277
  }
278

279
  private String getSheBang(Path file) {
280

281
    try (InputStream in = Files.newInputStream(file)) {
5✔
282
      // "#!/usr/bin/env bash".length() = 19
283
      byte[] buffer = new byte[32];
3✔
284
      int read = in.read(buffer);
4✔
285
      if ((read > 2) && (buffer[0] == '#') && (buffer[1] == '!')) {
13!
286
        int start = 2;
2✔
287
        int end = 2;
2✔
288
        while (end < read) {
3!
289
          byte c = buffer[end];
4✔
290
          if ((c == '\n') || (c == '\r') || (c > 127)) {
9!
291
            break;
×
292
          } else if ((end == start) && (c == ' ')) {
6!
293
            start++;
×
294
          }
295
          end++;
1✔
296
        }
1✔
297
        String sheBang = new String(buffer, start, end - start, StandardCharsets.US_ASCII).trim();
11✔
298
        if (sheBang.startsWith(PREFIX_USR_BIN_ENV)) {
4!
299
          sheBang = sheBang.substring(PREFIX_USR_BIN_ENV.length());
×
300
        }
301
        return sheBang;
4✔
302
      }
303
    } catch (IOException e) {
5!
304
      // ignore...
305
    }
×
306
    return null;
2✔
307
  }
308

309
  private String addExecutable(List<String> args) {
310

311
    String interpreter = null;
2✔
312
    String fileExtension = FilenameUtil.getExtension(this.executable.getFileName().toString());
6✔
313
    boolean isBashScript = "sh".equals(fileExtension);
4✔
314
    this.context.getFileAccess().makeExecutable(this.executable, true);
7✔
315
    if (!isBashScript) {
2✔
316
      String sheBang = getSheBang(this.executable);
5✔
317
      if (sheBang != null) {
2✔
318
        String cmd = sheBang;
2✔
319
        int lastSlash = cmd.lastIndexOf('/');
4✔
320
        if (lastSlash >= 0) {
2!
321
          cmd = cmd.substring(lastSlash + 1);
6✔
322
        }
323
        if (cmd.equals("bash")) {
4!
324
          isBashScript = true;
2✔
325
        } else {
326
          // currently we do not support other interpreters...
327
        }
328
      }
329
    }
330
    if (isBashScript) {
2✔
331
      interpreter = "bash";
2✔
332
      args.add(this.context.findBashRequired());
6✔
333
    }
334
    if ("msi".equalsIgnoreCase(fileExtension)) {
4!
335
      args.add(0, "/i");
×
336
      args.add(0, "msiexec");
×
337
    }
338
    args.add(this.executable.toString());
6✔
339
    return interpreter;
2✔
340
  }
341

342
  private void performLogging(ProcessResult result, int exitCode, String interpreter) {
343

344
    if (!result.isSuccessful() && (this.errorHandling != ProcessErrorHandling.NONE)) {
7!
345
      IdeLogLevel ideLogLevel = this.errorHandling.getLogLevel();
4✔
346
      String message = createCommandMessage(interpreter, "\nfailed with exit code " + exitCode + "!");
6✔
347

348
      context.level(ideLogLevel).log(message);
6✔
349
      result.log(ideLogLevel, context);
5✔
350

351
      if (this.errorHandling == ProcessErrorHandling.THROW_CLI) {
4!
352
        throw new CliProcessException(message, result);
×
353
      } else if (this.errorHandling == ProcessErrorHandling.THROW_ERR) {
4✔
354
        throw new IllegalStateException(message);
5✔
355
      }
356
    }
357
  }
1✔
358

359
  private void modifyArgumentsOnBackgroundProcess(ProcessMode processMode) {
360

361
    if (processMode == ProcessMode.BACKGROUND) {
3✔
362
      this.processBuilder.redirectOutput(Redirect.INHERIT).redirectError(Redirect.INHERIT);
8✔
363
    } else if (processMode == ProcessMode.BACKGROUND_SILENT) {
3!
364
      this.processBuilder.redirectOutput(Redirect.DISCARD).redirectError(Redirect.DISCARD);
8✔
365
    } else {
366
      throw new IllegalStateException("Cannot handle non background process mode!");
×
367
    }
368

369
    String bash = this.context.findBash();
4✔
370
    if (bash == null) {
2!
371
      this.context.warning(
×
372
          "Cannot start background process via bash because no bash installation was found. Hence, output will be discarded.");
373
      this.processBuilder.redirectOutput(Redirect.DISCARD).redirectError(Redirect.DISCARD);
×
374
      return;
×
375
    }
376

377
    String commandToRunInBackground = buildCommandToRunInBackground();
3✔
378

379
    this.arguments.clear();
3✔
380
    this.arguments.add(bash);
5✔
381
    this.arguments.add("-c");
5✔
382
    commandToRunInBackground += " & disown";
3✔
383
    this.arguments.add(commandToRunInBackground);
5✔
384

385
  }
1✔
386

387
  private String buildCommandToRunInBackground() {
388

389
    if (this.context.getSystemInfo().isWindows()) {
5!
390

391
      StringBuilder stringBuilder = new StringBuilder();
×
392

393
      for (String argument : this.arguments) {
×
394

395
        if (SystemInfoImpl.INSTANCE.isWindows() && SystemPath.isValidWindowsPath(argument)) {
×
396
          argument = WindowsPathSyntax.MSYS.normalize(argument);
×
397
        }
398

399
        stringBuilder.append(argument);
×
400
        stringBuilder.append(" ");
×
401
      }
×
402
      return stringBuilder.toString().trim();
×
403
    } else {
404
      return this.arguments.stream().map(Object::toString).collect(Collectors.joining(" "));
10✔
405
    }
406
  }
407
}
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