forked from devonfw/IDEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessContextImpl.java
More file actions
409 lines (338 loc) · 12.9 KB
/
ProcessContextImpl.java
File metadata and controls
409 lines (338 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package com.devonfw.tools.ide.process;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.ProcessBuilder.Redirect;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.stream.Collectors;
import com.devonfw.tools.ide.cli.CliProcessException;
import com.devonfw.tools.ide.common.SystemPath;
import com.devonfw.tools.ide.context.IdeContext;
import com.devonfw.tools.ide.environment.VariableLine;
import com.devonfw.tools.ide.log.IdeLogLevel;
import com.devonfw.tools.ide.os.SystemInfoImpl;
import com.devonfw.tools.ide.os.WindowsPathSyntax;
import com.devonfw.tools.ide.util.FilenameUtil;
import com.devonfw.tools.ide.variable.IdeVariables;
/**
* Implementation of {@link ProcessContext}.
*/
public class ProcessContextImpl implements ProcessContext {
private static final String PREFIX_USR_BIN_ENV = "/usr/bin/env ";
/** The owning {@link IdeContext}. */
protected final IdeContext context;
private final ProcessBuilder processBuilder;
private final List<String> arguments;
private Path executable;
private String overriddenPath;
private final List<Path> extraPathEntries;
private ProcessErrorHandling errorHandling;
/**
* The constructor.
*
* @param context the owning {@link IdeContext}.
*/
public ProcessContextImpl(IdeContext context) {
super();
this.context = context;
this.processBuilder = new ProcessBuilder();
this.errorHandling = ProcessErrorHandling.THROW_ERR;
Map<String, String> environment = this.processBuilder.environment();
for (VariableLine var : this.context.getVariables().collectExportedVariables()) {
if (var.isExport()) {
environment.put(var.getName(), var.getValue());
}
}
this.arguments = new ArrayList<>();
this.extraPathEntries = new ArrayList<>();
}
@Override
public ProcessContext errorHandling(ProcessErrorHandling handling) {
Objects.requireNonNull(handling);
this.errorHandling = handling;
return this;
}
@Override
public ProcessContext directory(Path directory) {
if (directory != null) {
this.processBuilder.directory(directory.toFile());
} else {
this.context.debug(
"Could not set the process builder's working directory! Directory of the current java process is used.");
}
return this;
}
@Override
public ProcessContext executable(Path command) {
if (!this.arguments.isEmpty()) {
throw new IllegalStateException("Arguments already present - did you forget to call run for previous call?");
}
this.executable = command;
return this;
}
@Override
public ProcessContext addArg(String arg) {
this.arguments.add(arg);
return this;
}
@Override
public ProcessContext withEnvVar(String key, String value) {
if (IdeVariables.PATH.getName().equals(key)) {
this.overriddenPath = value;
} else {
this.context.trace("Setting process environment variable {}={}", key, value);
this.processBuilder.environment().put(key, value);
}
return this;
}
@Override
public ProcessContext withPathEntry(Path path) {
this.extraPathEntries.add(path);
return this;
}
@Override
public ProcessResult run(ProcessMode processMode) {
if (processMode == ProcessMode.DEFAULT) {
this.processBuilder.redirectOutput(Redirect.INHERIT).redirectError(Redirect.INHERIT);
}
if (processMode == ProcessMode.DEFAULT_SILENT) {
this.processBuilder.redirectOutput(Redirect.DISCARD).redirectError(Redirect.DISCARD);
}
if (this.executable == null) {
throw new IllegalStateException("Missing executable to run process!");
}
SystemPath systemPath = this.context.getPath();
if ((this.overriddenPath != null) || !this.extraPathEntries.isEmpty()) {
systemPath = systemPath.withPath(this.overriddenPath, this.extraPathEntries);
}
String path = systemPath.toString();
this.context.trace("Setting PATH for process execution of {} to {}", this.executable.getFileName(), path);
this.executable = systemPath.findBinary(this.executable);
this.processBuilder.environment().put(IdeVariables.PATH.getName(), path);
List<String> args = new ArrayList<>(this.arguments.size() + 4);
String interpreter = addExecutable(args);
args.addAll(this.arguments);
String command = createCommand();
if (this.context.debug().isEnabled()) {
String message = createCommandMessage(interpreter, " ...");
this.context.debug(message);
}
try {
if (processMode == ProcessMode.DEFAULT_CAPTURE) {
this.processBuilder.redirectOutput(Redirect.PIPE).redirectError(Redirect.PIPE);
} else if (processMode.isBackground()) {
modifyArgumentsOnBackgroundProcess(processMode);
} else {
this.processBuilder.redirectInput(Redirect.INHERIT);
}
this.processBuilder.command(args);
ConcurrentLinkedQueue<OutputMessage> output = new ConcurrentLinkedQueue<>();
Process process = this.processBuilder.start();
try {
if (processMode == ProcessMode.DEFAULT_CAPTURE) {
CompletableFuture<Void> outFut = readInputStream(process.getInputStream(), false, output);
CompletableFuture<Void> errFut = readInputStream(process.getErrorStream(), true, output);
outFut.get();
errFut.get();
}
int exitCode;
if (processMode.isBackground()) {
exitCode = ProcessResult.SUCCESS;
} else {
exitCode = process.waitFor();
}
List<OutputMessage> finalOutput = new ArrayList<>(output);
ProcessResult result = new ProcessResultImpl(this.executable.getFileName().toString(), command, exitCode, finalOutput);
performLogging(result, exitCode, interpreter);
return result;
} finally {
if (!processMode.isBackground()) {
process.destroy();
}
}
} catch (CliProcessException | IllegalStateException e) {
// these exceptions are thrown from performLogOnError and we do not want to wrap them (see #593)
throw e;
} catch (Exception e) {
String msg = e.getMessage();
if ((msg == null) || msg.isEmpty()) {
msg = e.getClass().getSimpleName();
}
throw new IllegalStateException(createCommandMessage(interpreter, " failed: " + msg), e);
} finally {
this.arguments.clear();
}
}
/**
* Asynchronously and parallel reads {@link InputStream input stream} and stores it in {@link CompletableFuture}. Inspired by: <a href=
* "https://stackoverflow.com/questions/14165517/processbuilder-forwarding-stdout-and-stderr-of-started-processes-without-blocki/57483714#57483714">StackOverflow</a>
*
* @param is {@link InputStream}.
* @param errorStream to identify if the output came from stdout or stderr
* @return {@link CompletableFuture}.
*/
private static CompletableFuture<Void> readInputStream(InputStream is, boolean errorStream, ConcurrentLinkedQueue<OutputMessage> outputMessages) {
return CompletableFuture.supplyAsync(() -> {
try (InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr)) {
String line;
while ((line = br.readLine()) != null) {
OutputMessage outputMessage = new OutputMessage(errorStream, line);
outputMessages.add(outputMessage);
}
return null;
} catch (Throwable e) {
throw new RuntimeException("There was a problem while executing the program", e);
}
});
}
private String createCommand() {
String cmd = this.executable.toString();
StringBuilder sb = new StringBuilder(cmd.length() + this.arguments.size() * 4);
sb.append(cmd);
for (String arg : this.arguments) {
sb.append(' ');
sb.append(arg);
}
return sb.toString();
}
private String createCommandMessage(String interpreter, String suffix) {
StringBuilder sb = new StringBuilder();
sb.append("Running command '");
sb.append(this.executable);
sb.append("'");
if (interpreter != null) {
sb.append(" using ");
sb.append(interpreter);
}
int size = this.arguments.size();
if (size > 0) {
sb.append(" with arguments");
for (int i = 0; i < size; i++) {
String arg = this.arguments.get(i);
sb.append(" '");
sb.append(arg);
sb.append("'");
}
}
sb.append(suffix);
return sb.toString();
}
private String getSheBang(Path file) {
try (InputStream in = Files.newInputStream(file)) {
// "#!/usr/bin/env bash".length() = 19
byte[] buffer = new byte[32];
int read = in.read(buffer);
if ((read > 2) && (buffer[0] == '#') && (buffer[1] == '!')) {
int start = 2;
int end = 2;
while (end < read) {
byte c = buffer[end];
if ((c == '\n') || (c == '\r') || (c > 127)) {
break;
} else if ((end == start) && (c == ' ')) {
start++;
}
end++;
}
String sheBang = new String(buffer, start, end - start, StandardCharsets.US_ASCII).trim();
if (sheBang.startsWith(PREFIX_USR_BIN_ENV)) {
sheBang = sheBang.substring(PREFIX_USR_BIN_ENV.length());
}
return sheBang;
}
} catch (IOException e) {
// ignore...
}
return null;
}
private String addExecutable(List<String> args) {
String interpreter = null;
String fileExtension = FilenameUtil.getExtension(this.executable.getFileName().toString());
boolean isBashScript = "sh".equals(fileExtension);
this.context.getFileAccess().makeExecutable(this.executable, true);
if (!isBashScript) {
String sheBang = getSheBang(this.executable);
if (sheBang != null) {
String cmd = sheBang;
int lastSlash = cmd.lastIndexOf('/');
if (lastSlash >= 0) {
cmd = cmd.substring(lastSlash + 1);
}
if (cmd.equals("bash")) {
isBashScript = true;
} else {
// currently we do not support other interpreters...
}
}
}
if (isBashScript) {
interpreter = "bash";
args.add(this.context.findBashRequired());
}
if ("msi".equalsIgnoreCase(fileExtension)) {
args.add(0, "/i");
args.add(0, "msiexec");
}
args.add(this.executable.toString());
return interpreter;
}
private void performLogging(ProcessResult result, int exitCode, String interpreter) {
if (!result.isSuccessful() && (this.errorHandling != ProcessErrorHandling.NONE)) {
IdeLogLevel ideLogLevel = this.errorHandling.getLogLevel();
String message = createCommandMessage(interpreter, "\nfailed with exit code " + exitCode + "!");
context.level(ideLogLevel).log(message);
result.log(ideLogLevel, context);
if (this.errorHandling == ProcessErrorHandling.THROW_CLI) {
throw new CliProcessException(message, result);
} else if (this.errorHandling == ProcessErrorHandling.THROW_ERR) {
throw new IllegalStateException(message);
}
}
}
private void modifyArgumentsOnBackgroundProcess(ProcessMode processMode) {
if (processMode == ProcessMode.BACKGROUND) {
this.processBuilder.redirectOutput(Redirect.INHERIT).redirectError(Redirect.INHERIT);
} else if (processMode == ProcessMode.BACKGROUND_SILENT) {
this.processBuilder.redirectOutput(Redirect.DISCARD).redirectError(Redirect.DISCARD);
} else {
throw new IllegalStateException("Cannot handle non background process mode!");
}
String bash = this.context.findBash();
if (bash == null) {
this.context.warning(
"Cannot start background process via bash because no bash installation was found. Hence, output will be discarded.");
this.processBuilder.redirectOutput(Redirect.DISCARD).redirectError(Redirect.DISCARD);
return;
}
String commandToRunInBackground = buildCommandToRunInBackground();
this.arguments.clear();
this.arguments.add(bash);
this.arguments.add("-c");
commandToRunInBackground += " & disown";
this.arguments.add(commandToRunInBackground);
}
private String buildCommandToRunInBackground() {
if (this.context.getSystemInfo().isWindows()) {
StringBuilder stringBuilder = new StringBuilder();
for (String argument : this.arguments) {
if (SystemInfoImpl.INSTANCE.isWindows() && SystemPath.isValidWindowsPath(argument)) {
argument = WindowsPathSyntax.MSYS.normalize(argument);
}
stringBuilder.append(argument);
stringBuilder.append(" ");
}
return stringBuilder.toString().trim();
} else {
return this.arguments.stream().map(Object::toString).collect(Collectors.joining(" "));
}
}
}