forked from devonfw/IDEasy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessContext.java
More file actions
186 lines (157 loc) · 6.57 KB
/
ProcessContext.java
File metadata and controls
186 lines (157 loc) · 6.57 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
package com.devonfw.tools.ide.process;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import com.devonfw.tools.ide.log.IdeSubLogger;
/**
* Wrapper for {@link ProcessBuilder} to simplify its usage and avoid common mistakes and pitfalls.
*/
public interface ProcessContext extends EnvironmentContext {
/**
* @param handling the desired {@link ProcessErrorHandling}.
* @return this {@link ProcessContext} for fluent API calls.
*/
ProcessContext errorHandling(ProcessErrorHandling handling);
/**
* @param directory the {@link Path} to the working directory from where to execute the command.
* @return this {@link ProcessContext} for fluent API calls.
*/
ProcessContext directory(Path directory);
/**
* Sets the executable command to be {@link #run()}.
*
* @param executable the {@link Path} to the command to be executed by {@link #run()}. Depending on your operating system and the extension of the
* 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,
* etc.
* @return this {@link ProcessContext} for fluent API calls.
*/
ProcessContext executable(Path executable);
/**
* Sets the executable command to be {@link #run()}.
*
* @param executable the command to be executed by {@link #run()}.
* @return this {@link ProcessContext} for fluent API calls.
* @see #executable(Path)
*/
default ProcessContext executable(String executable) {
return executable(Path.of(executable));
}
/**
* Adds a single argument for {@link #executable(Path) command}.
*
* @param arg the next argument for {@link #executable(Path) command} to be added.
* @return this {@link ProcessContext} for fluent API calls.
*/
ProcessContext addArg(String arg);
/**
* Adds a single argument for {@link #executable(Path) command}.
*
* @param arg the next argument for {@link #executable(Path) command} to be added.
* @return this {@link ProcessContext} for fluent API calls.
*/
default ProcessContext addArg(Object arg) {
Objects.requireNonNull(arg);
return addArg(arg.toString());
}
/**
* Adds the given arguments for {@link #executable(Path) command}. E.g. for {@link #executable(Path) command} "mvn" the arguments "clean" and "install" may be
* 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
* install -Pmyprofile -X" would be run.
*
* @param args the arguments for {@link #executable(Path) command} to be added.
* @return this {@link ProcessContext} for fluent API calls.
*/
default ProcessContext addArgs(String... args) {
for (String arg : args) {
addArg(arg);
}
return this;
}
/**
* Adds the given arguments for {@link #executable(Path) command} as arbitrary Java objects. It will add the {@link Object#toString() string representation}
* of these arguments to the command.
*
* @param args the arguments for {@link #executable(Path) command} to be added.
* @return this {@link ProcessContext} for fluent API calls.
*/
default ProcessContext addArgs(Object... args) {
for (Object arg : args) {
addArg(arg);
}
return this;
}
/**
* Adds the given arguments for {@link #executable(Path) command} as arbitrary Java objects. It will add the {@link Object#toString() string representation}
* of these arguments to the command.
*
* @param args the {@link List} of arguments for {@link #executable(Path) command} to be added.
* @return this {@link ProcessContext} for fluent API calls.
*/
default ProcessContext addArgs(List<?> args) {
for (Object arg : args) {
addArg(arg);
}
return this;
}
@Override
ProcessContext withEnvVar(String key, String value);
@Override
ProcessContext withPathEntry(Path path);
/**
* 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.
*
* @return the exit code. Will be {@link ProcessResult#SUCCESS} on successful completion of the {@link Process}.
*/
default int run() {
return run(ProcessMode.DEFAULT).getExitCode();
}
/**
* Runs the given {@code executable} with the given {@code arguments}.
*
* @param executable the executable program.
* @param arguments the program arguments.
* @throws IllegalStateException if the command failed.
*/
default void run(String executable, String... arguments) {
executable(executable).addArgs(arguments).errorHandling(ProcessErrorHandling.THROW_ERR).run();
}
/**
* Runs the given {@code executable} with the given {@code arguments} and returns the expected single line from its
* {@link ProcessResult#getOut() standard output}.
*
* @param executable the executable program.
* @param arguments the program arguments.
* @return the single line printed from the command.
* @throws IllegalStateException if the command failed or did not print a single line as expected.
*/
default String runAndGetSingleOutput(String executable, String... arguments) {
return runAndGetSingleOutput(null, executable, arguments);
}
/**
* Runs the given {@code executable} with the given {@code arguments} and returns the expected single line from its
* {@link ProcessResult#getOut() standard output}.
*
* @param logger the {@link IdeSubLogger} used to log errors instead of throwing an exception.
* @param executable the executable program.
* @param arguments the program arguments.
* @return the single line printed from the command.
* @throws IllegalStateException if the command did not print a single line as expected.
*/
default String runAndGetSingleOutput(IdeSubLogger logger, String executable, String... arguments) {
executable(executable).addArgs(arguments);
if (logger == null) {
errorHandling(ProcessErrorHandling.THROW_ERR);
}
ProcessResult result = run(ProcessMode.DEFAULT_CAPTURE);
return result.getSingleOutput(logger);
}
/**
* 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.
*
* @param processMode {@link ProcessMode}
* @return the {@link ProcessResult}.
*/
ProcessResult run(ProcessMode processMode);
}