Skip to content

Commit 2df8b88

Browse files
#9: background process (#200)
1 parent 9d89b23 commit 2df8b88

File tree

14 files changed

+558
-127
lines changed

14 files changed

+558
-127
lines changed

cli/src/main/java/com/devonfw/tools/ide/common/SystemPath.java

+34-9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Iterator;
1010
import java.util.List;
1111
import java.util.Map;
12+
import java.util.regex.Pattern;
1213
import java.util.stream.Stream;
1314

1415
import com.devonfw.tools.ide.context.IdeContext;
@@ -67,7 +68,8 @@ public SystemPath(String envPath, Path softwarePath, char pathSeparator, IdeCont
6768
} else {
6869
Path duplicate = this.tool2pathMap.putIfAbsent(tool, path);
6970
if (duplicate != null) {
70-
context.warning("Duplicated tool path for tool: {} at path: {} with duplicated path: {}.", tool, path, duplicate);
71+
context.warning("Duplicated tool path for tool: {} at path: {} with duplicated path: {}.", tool, path,
72+
duplicate);
7173
}
7274
}
7375
}
@@ -211,22 +213,45 @@ public String toString(boolean bash) {
211213
return sb.toString();
212214
}
213215

214-
private void appendPath(Path path, StringBuilder sb, char separator, boolean bash) {
216+
private static void appendPath(Path path, StringBuilder sb, char separator, boolean bash) {
215217

216218
if (sb.length() > 0) {
217219
sb.append(separator);
218220
}
219221
String pathString = path.toString();
220222
if (bash && (pathString.length() > 3) && (pathString.charAt(1) == ':')) {
221-
char slash = pathString.charAt(2);
222-
if ((slash == '\\') || (slash == '/')) {
223-
char drive = Character.toLowerCase(pathString.charAt(0));
224-
if ((drive >= 'a') && (drive <= 'z')) {
225-
pathString = "/" + drive + pathString.substring(2).replace('\\', '/');
226-
}
227-
}
223+
pathString = convertWindowsPathToUnixPath(pathString);
228224
}
229225
sb.append(pathString);
230226
}
231227

228+
/**
229+
* Method to convert a valid Windows path string representation to its corresponding one in Unix format.
230+
*
231+
* @param pathString The Windows path string to convert.
232+
* @return The converted Unix path string.
233+
*/
234+
public static String convertWindowsPathToUnixPath(String pathString) {
235+
236+
char slash = pathString.charAt(2);
237+
if ((slash == '\\') || (slash == '/')) {
238+
char drive = Character.toLowerCase(pathString.charAt(0));
239+
if ((drive >= 'a') && (drive <= 'z')) {
240+
pathString = "/" + drive + pathString.substring(2).replace('\\', '/');
241+
}
242+
}
243+
return pathString;
244+
}
245+
246+
/**
247+
* Method to validate if a given path string is a Windows path or not
248+
*
249+
* @param pathString The string to check if it is a Windows path string.
250+
* @return {@code true} if it is a valid windows path string, else {@code false}.
251+
*/
252+
public static boolean isValidWindowsPath(String pathString) {
253+
254+
String windowsFilePathRegEx = "([a-zA-Z]:)?(\\\\[a-zA-Z0-9\\s_.-]+)+\\\\?";
255+
return Pattern.matches(windowsFilePathRegEx, pathString);
256+
}
232257
}

cli/src/main/java/com/devonfw/tools/ide/context/GitContextImpl.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.devonfw.tools.ide.log.IdeSubLogger;
1818
import com.devonfw.tools.ide.process.ProcessContext;
1919
import com.devonfw.tools.ide.process.ProcessErrorHandling;
20+
import com.devonfw.tools.ide.process.ProcessMode;
2021
import com.devonfw.tools.ide.process.ProcessResult;
2122

2223
/**
@@ -96,7 +97,7 @@ public void pullOrClone(String gitRepoUrl, Path targetRepository) {
9697
initializeProcessContext(targetRepository);
9798
if (Files.isDirectory(targetRepository.resolve(".git"))) {
9899
// checks for remotes
99-
ProcessResult result = this.processContext.addArg("remote").run(true, false);
100+
ProcessResult result = this.processContext.addArg("remote").run(ProcessMode.DEFAULT_CAPTURE);
100101
List<String> remotes = result.getOut();
101102
if (remotes.isEmpty()) {
102103
String message = targetRepository
@@ -183,7 +184,7 @@ public void clone(GitUrl gitRepoUrl, Path targetRepository) {
183184
this.processContext.addArg("-q");
184185
}
185186
this.processContext.addArgs("--recursive", parsedUrl, "--config", "core.autocrlf=false", ".");
186-
result = this.processContext.run(true, false);
187+
result = this.processContext.run(ProcessMode.DEFAULT_CAPTURE);
187188
if (!result.isSuccessful()) {
188189
this.context.warning("Git failed to clone {} into {}.", parsedUrl, targetRepository);
189190
}
@@ -198,7 +199,7 @@ public void pull(Path targetRepository) {
198199
initializeProcessContext(targetRepository);
199200
ProcessResult result;
200201
// pull from remote
201-
result = this.processContext.addArg("--no-pager").addArg("pull").run(true, false);
202+
result = this.processContext.addArg("--no-pager").addArg("pull").run(ProcessMode.DEFAULT_CAPTURE);
202203

203204
if (!result.isSuccessful()) {
204205
Map<String, String> remoteAndBranchName = retrieveRemoteAndBranchName();
@@ -211,7 +212,7 @@ public void pull(Path targetRepository) {
211212
private Map<String, String> retrieveRemoteAndBranchName() {
212213

213214
Map<String, String> remoteAndBranchName = new HashMap<>();
214-
ProcessResult remoteResult = this.processContext.addArg("branch").addArg("-vv").run(true, false);
215+
ProcessResult remoteResult = this.processContext.addArg("branch").addArg("-vv").run(ProcessMode.DEFAULT_CAPTURE);
215216
List<String> remotes = remoteResult.getOut();
216217
if (!remotes.isEmpty()) {
217218
for (String remote : remotes) {
@@ -242,14 +243,14 @@ public void reset(Path targetRepository, String remoteName, String branchName) {
242243
initializeProcessContext(targetRepository);
243244
ProcessResult result;
244245
// check for changed files
245-
result = this.processContext.addArg("diff-index").addArg("--quiet").addArg("HEAD").run(true, false);
246+
result = this.processContext.addArg("diff-index").addArg("--quiet").addArg("HEAD").run(ProcessMode.DEFAULT_CAPTURE);
246247

247248
if (!result.isSuccessful()) {
248249
// reset to origin/master
249250
context.warning("Git has detected modified files -- attempting to reset {} to '{}/{}'.", targetRepository,
250251
remoteName, branchName);
251-
result = this.processContext.addArg("reset").addArg("--hard").addArg(remoteName + "/" + branchName).run(true,
252-
false);
252+
result = this.processContext.addArg("reset").addArg("--hard").addArg(remoteName + "/" + branchName)
253+
.run(ProcessMode.DEFAULT_CAPTURE);
253254

254255
if (!result.isSuccessful()) {
255256
context.warning("Git failed to reset {} to '{}/{}'.", remoteName, branchName, targetRepository);
@@ -265,12 +266,12 @@ public void cleanup(Path targetRepository) {
265266
ProcessResult result;
266267
// check for untracked files
267268
result = this.processContext.addArg("ls-files").addArg("--other").addArg("--directory").addArg("--exclude-standard")
268-
.run(true, false);
269+
.run(ProcessMode.DEFAULT_CAPTURE);
269270

270271
if (!result.getOut().isEmpty()) {
271272
// delete untracked files
272273
context.warning("Git detected untracked files in {} and is attempting a cleanup.", targetRepository);
273-
result = this.processContext.addArg("clean").addArg("-df").run(true, false);
274+
result = this.processContext.addArg("clean").addArg("-df").run(ProcessMode.DEFAULT_CAPTURE);
274275

275276
if (!result.isSuccessful()) {
276277
context.warning("Git failed to clean the repository {}.", targetRepository);

cli/src/main/java/com/devonfw/tools/ide/process/ProcessContext.java

+3-22
Original file line numberDiff line numberDiff line change
@@ -132,36 +132,17 @@ default ProcessContext addArgs(List<?>... args) {
132132
*/
133133
default int run() {
134134

135-
return run(false, false).getExitCode();
135+
return run(ProcessMode.DEFAULT).getExitCode();
136136
}
137137

138138
/**
139139
* Runs the previously configured {@link #executable(Path) command} with the configured {@link #addArgs(String...)
140140
* arguments}. Will reset the {@link #addArgs(String...) arguments} but not the {@link #executable(Path) command} for
141141
* sub-sequent calls.
142142
*
143-
* @param capture - {@code true} to capture standard {@link ProcessResult#getOut() out} and
144-
* {@link ProcessResult#getErr() err} in the {@link ProcessResult}, {@code false} otherwise (to redirect out
145-
* and err).
143+
* @param processMode {@link ProcessMode}
146144
* @return the {@link ProcessResult}.
147145
*/
148-
default ProcessResult run(boolean capture) {
149-
150-
return run(capture, false);
151-
}
152-
153-
/**
154-
* Runs the previously configured {@link #executable(Path) command} with the configured {@link #addArgs(String...)
155-
* arguments}. Will reset the {@link #addArgs(String...) arguments} but not the {@link #executable(Path) command} for
156-
* sub-sequent calls.
157-
*
158-
* @param capture - {@code true} to capture standard {@link ProcessResult#getOut() out} and
159-
* {@link ProcessResult#getErr() err} in the {@link ProcessResult}, {@code false} otherwise (to redirect out
160-
* and err).
161-
* @param runInBackground {@code true}, the process of the command will be run as background process, {@code false}
162-
* otherwise (it will be run as foreground process).
163-
* @return the {@link ProcessResult}.
164-
*/
165-
ProcessResult run(boolean capture, boolean runInBackground);
146+
ProcessResult run(ProcessMode processMode);
166147

167148
}

0 commit comments

Comments
 (0)