Improved file visitors

This commit is contained in:
Renaud Pawlak 2017-08-02 09:43:01 +02:00
parent e608bbdc74
commit c3a057cf37
2 changed files with 108 additions and 40 deletions

View File

@ -7,49 +7,55 @@ import org.cincheo.filevisitor.ProcessUtil;
public class GitCommitPush implements FileVisitor {
int count = 0;
int errorCount = 0;
int count = 0;
int errorCount = 0;
@Override
public void onBegin() {
System.out.println("=== Committing projects === ");
}
private String commitMessage;
@Override
public boolean visit(File file) {
try {
// System.out.println("visiting: " + file);
if (file.isDirectory() && new File(file, ".git").exists()) {
System.out.println();
System.out.println(file + ": committing git project...");
ProcessUtil.runCommand("git", file, false, (out) -> {
System.out.println(out);
}, (p1) -> {
count++;
System.out.println(file + ": pushing git project...");
ProcessUtil.runCommand("git", file, false, (out) -> {
System.out.println(out);
}, (p2) -> {
}, () -> {
System.out.println("ERROR: cannot push project!");
errorCount++;
}, "push", "origin", "master");
}, () -> {
System.out.println("ERROR: cannot commit project!");
errorCount++;
}, "commit", "-m", "publishing");
}
return true;
} catch (Exception e) {
throw new RuntimeException(e);
public GitCommitPush(String commitMessage) {
this.commitMessage = commitMessage;
}
}
@Override
public boolean onEnd() {
System.out.println("=== " + count + " git project(s) updated successfully, " + errorCount + " error(s) ===");
System.out.println();
return errorCount == 0;
}
@Override
public void onBegin() {
System.out.println("=== Committing projects === ");
}
@Override
public boolean visit(File file) {
try {
// System.out.println("visiting: " + file);
if (file.isDirectory() && new File(file, ".git").exists()) {
System.out.println();
System.out.println(file + ": committing git project...");
ProcessUtil.runCommand("git", file, false, (out) -> {
System.out.println(out);
}, (p1) -> {
count++;
System.out.println(file + ": pushing git project...");
ProcessUtil.runCommand("git", file, false, (out) -> {
System.out.println(out);
}, (p2) -> {
}, () -> {
System.out.println("ERROR: cannot push project!");
errorCount++;
}, "push", "origin", "master");
}, () -> {
System.out.println("ERROR: cannot commit project!");
errorCount++;
}, "commit", "-m", commitMessage);
}
return true;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public boolean onEnd() {
System.out.println("=== " + count + " git project(s) updated successfully, " + errorCount + " error(s) ===");
System.out.println();
return errorCount == 0;
}
}

View File

@ -0,0 +1,62 @@
package org.jsweet.publisher.visitor;
import java.io.File;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.cincheo.filevisitor.FileVisitor;
import org.cincheo.filevisitor.ProcessUtil;
public class PomFixer implements FileVisitor {
private String targetExpression;
private String replacementExpression;
public PomFixer(String targetExpression, String replacementExpression) {
this.targetExpression = targetExpression;
this.replacementExpression = replacementExpression;
}
int count = 0;
@Override
public void onBegin() {
System.out.println("=== Running pom fixer === ");
System.out.println("- target: " + targetExpression);
System.out.println("- replacement: " + replacementExpression);
}
@Override
public boolean visit(File file) {
try {
// System.out.println("visiting: " + file);
if (file.isFile() && file.getName().equals("pom.xml")) {
String content = FileUtils.readFileToString(file);
if (Pattern.compile(targetExpression).matcher(content).find()) {
System.out.println(file + ": updating pom.xml");
content = content.replaceAll(targetExpression, replacementExpression);
FileUtils.writeStringToFile(file, content);
count++;
}
ProcessUtil.runCommand("git", file.getParentFile(), false, (out) -> {
System.out.println(out);
}, (p1) -> {
}, () -> {
System.out.println("ERROR: cannot add file");
}, "add", file.toString());
}
return true;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public boolean onEnd() {
System.out.println("=== Updated " + count + " poms ===");
System.out.println();
return true;
}
}