Thanks to visit codestin.com
Credit goes to Github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .dir-locals.el
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
;;; For more information see (info "(emacs) Directory Variables")

((nil . (
(compile-command . "mvn -f ~/projects/gis/pom.xml -T 1C clean package -DskipTests; cd /home/minh/projects/test/small-git-root-module; java -jar ~/projects/gis/target/gis-2.0.0-dev.jar ")
(compile-command . "mvn -f /opt/minh/tools/gis/pom.xml -T 1C clean package -DskipTests; cd /home/minh/projects/test/small-git-root-module; java -jar /opt/minh/tools/gis/target/gis-2.0.0-dev.jar ")
(projectile-project-compilation-cmd . "mvn -T 1C clean package -DskipTests")
(projectile-project-run-cmd . "cd /home/minh/projects/test/small-git-root-module; java -jar ~/projects/gis/target/gis-2.0.0-dev.jar ")
(projectile-project-run-cmd . "cd /home/minh/projects/test/small-git-root-module; java -jar /opt/minh/tools/gis/target/gis-2.0.0-dev.jar ")
)))
50 changes: 13 additions & 37 deletions src/main/java/org/nqm/command/CommandVerticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static org.nqm.command.GitCommand.HOOKS_OPTION;
import static org.nqm.utils.GisStringUtils.isNotBlank;
import static org.nqm.utils.StdOutUtils.gitStatus;
import static org.nqm.utils.StdOutUtils.gitStatusOneLine;
import static org.nqm.utils.StdOutUtils.infof;
import static org.nqm.utils.StdOutUtils.warnln;
import java.io.BufferedReader;
Expand All @@ -17,9 +15,9 @@
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.nqm.GisException;
import org.nqm.config.GisConfig;
import org.nqm.config.GisLog;
import org.nqm.GisException;
import org.nqm.model.GisProcessDto;
import org.nqm.utils.GisProcessUtils;
import org.nqm.utils.GisStringUtils;
Expand Down Expand Up @@ -54,9 +52,9 @@ private static String[] buildCommandWithArgs(String... args) {
.toArray(String[]::new);
}

public static void execute(Path path, String... args) {
public static String execute(Path path, String... args) {
if (path == null) {
return;
return "";
}
var commandHook = extractHookCommand(args);
var commandWithArgs = buildCommandWithArgs(args);
Expand All @@ -70,14 +68,14 @@ public static void execute(Path path, String... args) {
var result = GisProcessUtils.run(path.toFile(), commandWithArgs);
if (GisStringUtils.isNotBlank(commandHook)) {
gisExecuteCommand(path, GisStringUtils.toInputStreamReader(result.output()), commandHook)
.forEach(p -> infof("%s", p.output()));
.forEach(p -> infof(p.output()));
} else if (Stream.of(gisOptions).anyMatch("--gis-no-print-modules-name"::equals)) {
safelyPrintWithoutModules(path, result);
} else if (Stream.of(gisOptions).anyMatch("--gis-concat-modules-name"::equals)) {
safelyConcatModuleNames(path, result);
} else {
safelyPrint(path, gisOptions, commandWithArgs, result);
return safelyPrintWithoutModules(path, result);
}
if (Stream.of(gisOptions).anyMatch("--gis-concat-modules-name"::equals)) {
return safelyConcatModuleNames(path, result);
}
return result.parseGitStatus(Stream.of(gisOptions).anyMatch("--gis-one-line"::equals));
} catch (IOException e) {
GisLog.debug(e);
throw new GisException(e.getMessage());
Expand All @@ -88,42 +86,20 @@ public static void execute(Path path, String... args) {
}
}

private static void safelyPrint(
Path path, String[] gisOptions, String[] commandWithArgs, GisProcessDto result) throws IOException {
var line = "";
var input = new BufferedReader(GisStringUtils.toInputStreamReader(result.output()));
var sb = new StringBuilder(infof("%s", "" + path.getFileName()));
var isOneLineOpt = Stream.of(gisOptions).anyMatch("--gis-one-line"::equals);
while (isNotBlank(line = input.readLine())) {
if (commandWithArgs[1].equals(GitCommand.GIT_STATUS)) {
sb.append(isOneLineOpt ? gitStatusOneLine(line) : gitStatus(line));
} else {
sb.append("%n %s".formatted(line));
}
}
StdOutUtils.println(sb.toString());
Optional.of(result.exitCode())
.filter(exitCode -> exitCode != 0)
.ifPresent(exitCode -> {
GisLog.debug(EXIT_WITH_CODE_MSG_FMT.formatted(exitCode));
warnln(WARN_MSG_FMT.formatted(path.getFileName()));
});
}

private static void safelyPrintWithoutModules(Path path, GisProcessDto result) throws IOException {
private static String safelyPrintWithoutModules(Path path, GisProcessDto result) throws IOException {
var input = new BufferedReader(GisStringUtils.toInputStreamReader(result.output()));
var sb = new StringBuilder();
var line = "";
while (isNotBlank(line = input.readLine())) {
sb.append("%s%n".formatted(line));
}
StdOutUtils.print(sb.toString());
Optional.of(result.exitCode())
.filter(exitCode -> exitCode != 0)
.ifPresent(exitCode -> {
GisLog.debug(EXIT_WITH_CODE_MSG_FMT.formatted(exitCode));
warnln(WARN_MSG_FMT.formatted(path.getFileName()));
});
return sb.toString();
}

private static List<GisProcessDto> gisExecuteCommand(Path path, InputStreamReader stream, String cmd)
Expand All @@ -137,7 +113,7 @@ private static List<GisProcessDto> gisExecuteCommand(Path path, InputStreamReade
return futures;
}

private static void safelyConcatModuleNames(Path path, GisProcessDto result) throws IOException {
private static String safelyConcatModuleNames(Path path, GisProcessDto result) throws IOException {
var input = new BufferedReader(GisStringUtils.toInputStreamReader(result.output()));
var sb = new StringBuilder();
var isRootModule = path.toFile()
Expand All @@ -160,12 +136,12 @@ private static void safelyConcatModuleNames(Path path, GisProcessDto result) thr
sb.append("%s/%s%n".formatted(shortPath, line));
}
}
StdOutUtils.print(sb.toString());
Optional.of(result.exitCode())
.filter(exitCode -> exitCode != 0)
.ifPresent(exitCode -> {
GisLog.debug(EXIT_WITH_CODE_MSG_FMT.formatted(exitCode));
warnln(WARN_MSG_FMT.formatted(path.getFileName()));
});
return sb.toString();
}
}
11 changes: 9 additions & 2 deletions src/main/java/org/nqm/command/GitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Optional;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.nqm.GisException;
import org.nqm.config.GisConfig;
Expand Down Expand Up @@ -48,11 +51,15 @@ void pull() throws IOException {

@Command(name = GIT_STATUS, aliases = "st", description = "Show the working trees status")
void status(@Option(names = "--one-line") boolean oneLineOpt) throws IOException {
var output = new ConcurrentLinkedQueue<String>();
if (oneLineOpt) {
forEachModuleDo(GIT_STATUS, "-sb", "--ignore-submodules", "--porcelain=v2", "--gis-one-line");
output = forEachModuleDo(GIT_STATUS, "-sb", "--ignore-submodules", "--gis-one-line");
} else {
forEachModuleDo(GIT_STATUS, "-sb", "--ignore-submodules", "--porcelain=v2");
output = forEachModuleDo(GIT_STATUS, "-sb", "--ignore-submodules");
}
StdOutUtils.println(String.join(
GisStringUtils.NEWLINE,
output.stream().collect(Collectors.toCollection(TreeSet::new))));
if (Files.exists(TMP_FILE)) {
var lastFetched = Files.readString(TMP_FILE);
if (GisStringUtils.isNotBlank(lastFetched)) {
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/org/nqm/command/Wrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.file.Path;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.function.Predicate;
Expand Down Expand Up @@ -42,13 +43,14 @@ private static File getFileMarker() {
return gitModulesFilePath;
}

public static void forEachModuleWith(Predicate<Path> pred, String... args) throws IOException {
public static ConcurrentLinkedQueue<String> forEachModuleWith(Predicate<Path> pred, String... args) throws IOException {
var output = new ConcurrentLinkedQueue<String>();
var gitModulesFilePath = getFileMarker();
var currentDir = currentDir();
try (var exe = Executors.newVirtualThreadPerTaskExecutor()) {
Optional.of(Path.of(currentDir))
.filter(pred)
.ifPresent(root -> exe.submit(() -> CommandVerticle.execute(root, args)));
.ifPresent(root -> exe.submit(() -> output.add(CommandVerticle.execute(root, args))));
Files.readAllLines(gitModulesFilePath.toPath()).stream()
.map(String::trim)
.filter(s -> s.startsWith("path"))
Expand All @@ -62,11 +64,12 @@ public static void forEachModuleWith(Predicate<Path> pred, String... args) throw
return false;
})
.filter(pred)
.forEach(dir -> exe.submit(() -> CommandVerticle.execute(dir, args)));
.forEach(dir -> exe.submit(() -> output.add(CommandVerticle.execute(dir, args))));
}
return output;
}

public static void forEachModuleDo(String... args) throws IOException {
forEachModuleWith(p -> true, args);
public static ConcurrentLinkedQueue<String> forEachModuleDo(String... args) throws IOException {
return forEachModuleWith(p -> true, args);
}
}
96 changes: 95 additions & 1 deletion src/main/java/org/nqm/model/GisProcessDto.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,98 @@
package org.nqm.model;

public record GisProcessDto(String output, int exitCode) {
import static org.nqm.utils.GisStringUtils.NEWLINE;
import static org.nqm.utils.StdOutUtils.CL_GREEN;
import static org.nqm.utils.StdOutUtils.CL_RED;
import static org.nqm.utils.StdOutUtils.RG_AHEAD_NUM;
import static org.nqm.utils.StdOutUtils.RG_BEHIND_NUM;
import static org.nqm.utils.StdOutUtils.RG_STAGED_STATUS;
import static org.nqm.utils.StdOutUtils.RG_UNSTAGED_STATUS;
import static org.nqm.utils.StdOutUtils.RG_LOCAL_BRANCH;
import static org.nqm.utils.StdOutUtils.RG_UP_STREAM_BRANCH;
import static org.nqm.utils.StdOutUtils.coloringBranch;
import static org.nqm.utils.StdOutUtils.coloringFirstRegex;
import static org.nqm.utils.StdOutUtils.coloringWord;
import static org.nqm.utils.StdOutUtils.extractFirstRegexMatched;
import static org.nqm.utils.StdOutUtils.infof;
import static org.nqm.utils.StdOutUtils.warnln;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.nqm.config.GisLog;
import org.nqm.utils.GisStringUtils;

public record GisProcessDto(String output, int exitCode, String directory) {

private static final String WARN_MSG_FMT = "Could not perform on module: '%s'";
private static final String EXIT_WITH_CODE_MSG_FMT = "exit with code: '%s'";

private void verifyExitCode() {
Optional.of(exitCode)
.filter(exitCode -> exitCode != 0)
.ifPresent(exitCode -> {
GisLog.debug(EXIT_WITH_CODE_MSG_FMT.formatted(exitCode));
warnln(WARN_MSG_FMT.formatted(directory));
});
}

public String parseGitStatus(boolean oneLiner) {
verifyExitCode();
var sb = new StringBuilder(infof(directory));
if (oneLiner) {
return sb.append(Stream.of(output.split(NEWLINE))
.map(GisProcessDto::buildStatusOneLiner)
.collect(Collectors.joining(" ")))
.toString();
}
return sb.append(NEWLINE)
.append(Stream.of(output.split(NEWLINE))
.map(GisProcessDto::buildStatusFull)
.map(" %s"::formatted)
.collect(Collectors.joining(NEWLINE)))
.toString();
}

private static String buildStatusFull(String s) {
var colored = s;
if (s.startsWith("##")) {
colored = coloringFirstRegex(colored, RG_LOCAL_BRANCH, CL_GREEN);
colored = coloringFirstRegex(colored, RG_UP_STREAM_BRANCH, CL_RED);
colored = coloringFirstRegex(colored, RG_AHEAD_NUM, CL_GREEN);
colored = coloringFirstRegex(colored, RG_BEHIND_NUM, CL_RED);
} else {
colored = coloringFirstRegex(colored, RG_UNSTAGED_STATUS, CL_RED);
colored = coloringFirstRegex(colored, RG_STAGED_STATUS, CL_GREEN);
}
return colored;
}

// TODO: ## No commits yet on master

private static String buildStatusOneLiner(String s) {
if (s.startsWith("##")) {
var coloredBranch = coloringBranch(extractFirstRegexMatched(s, RG_LOCAL_BRANCH));
var ahead = Optional.of(extractFirstRegexMatched(s, RG_AHEAD_NUM))
.filter(Predicate.not(String::isBlank))
.map(n -> coloringWord(n, CL_GREEN))
.map("ahead "::concat)
.orElse("");
var behind = Optional.of(extractFirstRegexMatched(s, RG_BEHIND_NUM))
.filter(Predicate.not(String::isBlank))
.map(n -> coloringWord(n, CL_RED))
.map("behind "::concat)
.orElse("");
var coloredAheadBehind = Stream.of(ahead, behind)
.filter(Predicate.not(String::isBlank))
.collect(Collectors.joining(", "));
if (GisStringUtils.isBlank(coloredAheadBehind)) {
return " " + coloredBranch;
}
return " %s [%s]".formatted(coloredBranch, coloredAheadBehind);
}
if (s.startsWith("R ")) {
return "'%s'".formatted(s.substring("R ".length()));
}
return Stream.of(s.split("\s")).reduce((a, b) -> b).orElse("");
}
}
24 changes: 17 additions & 7 deletions src/main/java/org/nqm/utils/GisProcessUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,31 @@ public static void isDryRunEnabled(boolean b) {
dryRunEnabled = b;
}

public static GisProcessDto run(File directory, String... commands) throws IOException, InterruptedException {
public static GisProcessDto run(File directory, String... commands)
throws IOException, InterruptedException {
var dirName = "" + directory.toPath().getFileName();
if (dryRunEnabled) {
StdOutUtils.println(String.join(" ", commands));
return new GisProcessDto("", 0);
return new GisProcessDto("", 0, dirName);
}
var p = new ProcessBuilder(commands).directory(directory).start();
return new GisProcessDto(new String(p.getInputStream().readAllBytes()), p.waitFor());
var p = new ProcessBuilder(commands)
// .inheritIO()
.directory(directory)
.start();
return new GisProcessDto(new String(p.getInputStream().readAllBytes()), p.waitFor(), dirName);
}

public static GisProcessDto quickRun(File directory, String... commands) throws IOException {
var dirName = "" + directory.toPath().getFileName();
if (dryRunEnabled) {
StdOutUtils.println(String.join(" ", commands));
return new GisProcessDto("", 0);
return new GisProcessDto("", 0, dirName);
}
var inputStream = new ProcessBuilder(commands).directory(directory).start().getInputStream();
return new GisProcessDto(new String(inputStream.readAllBytes()), 0);
var inputStream = new ProcessBuilder(commands)
// .inheritIO()
.directory(directory)
.start()
.getInputStream();
return new GisProcessDto(new String(inputStream.readAllBytes()), 0, dirName);
}
}
2 changes: 2 additions & 0 deletions src/main/java/org/nqm/utils/GisStringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class GisStringUtils {

private GisStringUtils() {}

public static final String NEWLINE = "%n".formatted();

public static boolean isNotBlank(String s) {
return s != null && !s.isBlank();
}
Expand Down
Loading