Skip to content

Commit

Permalink
#9: background process (devonfw#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
MustaphaOuchen authored Mar 1, 2024
1 parent 9d89b23 commit 2df8b88
Show file tree
Hide file tree
Showing 14 changed files with 558 additions and 127 deletions.
43 changes: 34 additions & 9 deletions cli/src/main/java/com/devonfw/tools/ide/common/SystemPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import com.devonfw.tools.ide.context.IdeContext;
Expand Down Expand Up @@ -67,7 +68,8 @@ public SystemPath(String envPath, Path softwarePath, char pathSeparator, IdeCont
} else {
Path duplicate = this.tool2pathMap.putIfAbsent(tool, path);
if (duplicate != null) {
context.warning("Duplicated tool path for tool: {} at path: {} with duplicated path: {}.", tool, path, duplicate);
context.warning("Duplicated tool path for tool: {} at path: {} with duplicated path: {}.", tool, path,
duplicate);
}
}
}
Expand Down Expand Up @@ -211,22 +213,45 @@ public String toString(boolean bash) {
return sb.toString();
}

private void appendPath(Path path, StringBuilder sb, char separator, boolean bash) {
private static void appendPath(Path path, StringBuilder sb, char separator, boolean bash) {

if (sb.length() > 0) {
sb.append(separator);
}
String pathString = path.toString();
if (bash && (pathString.length() > 3) && (pathString.charAt(1) == ':')) {
char slash = pathString.charAt(2);
if ((slash == '\\') || (slash == '/')) {
char drive = Character.toLowerCase(pathString.charAt(0));
if ((drive >= 'a') && (drive <= 'z')) {
pathString = "/" + drive + pathString.substring(2).replace('\\', '/');
}
}
pathString = convertWindowsPathToUnixPath(pathString);
}
sb.append(pathString);
}

/**
* Method to convert a valid Windows path string representation to its corresponding one in Unix format.
*
* @param pathString The Windows path string to convert.
* @return The converted Unix path string.
*/
public static String convertWindowsPathToUnixPath(String pathString) {

char slash = pathString.charAt(2);
if ((slash == '\\') || (slash == '/')) {
char drive = Character.toLowerCase(pathString.charAt(0));
if ((drive >= 'a') && (drive <= 'z')) {
pathString = "/" + drive + pathString.substring(2).replace('\\', '/');
}
}
return pathString;
}

/**
* Method to validate if a given path string is a Windows path or not
*
* @param pathString The string to check if it is a Windows path string.
* @return {@code true} if it is a valid windows path string, else {@code false}.
*/
public static boolean isValidWindowsPath(String pathString) {

String windowsFilePathRegEx = "([a-zA-Z]:)?(\\\\[a-zA-Z0-9\\s_.-]+)+\\\\?";
return Pattern.matches(windowsFilePathRegEx, pathString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.devonfw.tools.ide.log.IdeSubLogger;
import com.devonfw.tools.ide.process.ProcessContext;
import com.devonfw.tools.ide.process.ProcessErrorHandling;
import com.devonfw.tools.ide.process.ProcessMode;
import com.devonfw.tools.ide.process.ProcessResult;

/**
Expand Down Expand Up @@ -96,7 +97,7 @@ public void pullOrClone(String gitRepoUrl, Path targetRepository) {
initializeProcessContext(targetRepository);
if (Files.isDirectory(targetRepository.resolve(".git"))) {
// checks for remotes
ProcessResult result = this.processContext.addArg("remote").run(true, false);
ProcessResult result = this.processContext.addArg("remote").run(ProcessMode.DEFAULT_CAPTURE);
List<String> remotes = result.getOut();
if (remotes.isEmpty()) {
String message = targetRepository
Expand Down Expand Up @@ -183,7 +184,7 @@ public void clone(GitUrl gitRepoUrl, Path targetRepository) {
this.processContext.addArg("-q");
}
this.processContext.addArgs("--recursive", parsedUrl, "--config", "core.autocrlf=false", ".");
result = this.processContext.run(true, false);
result = this.processContext.run(ProcessMode.DEFAULT_CAPTURE);
if (!result.isSuccessful()) {
this.context.warning("Git failed to clone {} into {}.", parsedUrl, targetRepository);
}
Expand All @@ -198,7 +199,7 @@ public void pull(Path targetRepository) {
initializeProcessContext(targetRepository);
ProcessResult result;
// pull from remote
result = this.processContext.addArg("--no-pager").addArg("pull").run(true, false);
result = this.processContext.addArg("--no-pager").addArg("pull").run(ProcessMode.DEFAULT_CAPTURE);

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

Map<String, String> remoteAndBranchName = new HashMap<>();
ProcessResult remoteResult = this.processContext.addArg("branch").addArg("-vv").run(true, false);
ProcessResult remoteResult = this.processContext.addArg("branch").addArg("-vv").run(ProcessMode.DEFAULT_CAPTURE);
List<String> remotes = remoteResult.getOut();
if (!remotes.isEmpty()) {
for (String remote : remotes) {
Expand Down Expand Up @@ -242,14 +243,14 @@ public void reset(Path targetRepository, String remoteName, String branchName) {
initializeProcessContext(targetRepository);
ProcessResult result;
// check for changed files
result = this.processContext.addArg("diff-index").addArg("--quiet").addArg("HEAD").run(true, false);
result = this.processContext.addArg("diff-index").addArg("--quiet").addArg("HEAD").run(ProcessMode.DEFAULT_CAPTURE);

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

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

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

if (!result.isSuccessful()) {
context.warning("Git failed to clean the repository {}.", targetRepository);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,36 +132,17 @@ default ProcessContext addArgs(List<?>... args) {
*/
default int run() {

return run(false, false).getExitCode();
return run(ProcessMode.DEFAULT).getExitCode();
}

/**
* 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 capture - {@code true} to capture standard {@link ProcessResult#getOut() out} and
* {@link ProcessResult#getErr() err} in the {@link ProcessResult}, {@code false} otherwise (to redirect out
* and err).
* @param processMode {@link ProcessMode}
* @return the {@link ProcessResult}.
*/
default ProcessResult run(boolean capture) {

return run(capture, false);
}

/**
* 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 capture - {@code true} to capture standard {@link ProcessResult#getOut() out} and
* {@link ProcessResult#getErr() err} in the {@link ProcessResult}, {@code false} otherwise (to redirect out
* and err).
* @param runInBackground {@code true}, the process of the command will be run as background process, {@code false}
* otherwise (it will be run as foreground process).
* @return the {@link ProcessResult}.
*/
ProcessResult run(boolean capture, boolean runInBackground);
ProcessResult run(ProcessMode processMode);

}
Loading

0 comments on commit 2df8b88

Please sign in to comment.