-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
187 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,4 @@ jobs: | |
draft: false | ||
prerelease: false | ||
artifacts: "./target/*.zip" | ||
bodyFile: "./target/changelog.md" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package net.cofcool.toolbox; | ||
|
||
public enum ToolName { | ||
trelloLogseqImporter, shell, link2Tool, kindle, converts | ||
trelloLogseqImporter, shell, link2Tool, kindle, gitCommits2Log, converts | ||
} |
117 changes: 117 additions & 0 deletions
117
src/main/java/net/cofcool/toolbox/internal/GitCommitsToChangelog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package net.cofcool.toolbox.internal; | ||
|
||
import java.io.File; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayDeque; | ||
import java.util.Arrays; | ||
import java.util.Optional; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import net.cofcool.toolbox.Tool; | ||
import net.cofcool.toolbox.ToolName; | ||
import org.apache.commons.io.FileUtils; | ||
import org.apache.commons.io.IOUtils; | ||
|
||
/** | ||
* git-log PRETTY FORMATS | ||
* <code>git log --format="%d;%h;%s"</code> | ||
*/ | ||
public class GitCommitsToChangelog implements Tool { | ||
|
||
private final Tool delegate = new ShellStarter(); | ||
|
||
@Override | ||
public ToolName name() { | ||
return ToolName.gitCommits2Log; | ||
} | ||
|
||
// (HEAD -> master, origin/master, origin/HEAD);117826a;Converts: now, replace | ||
//;1788569;add debug mode | ||
//;1ee4312;update doc | ||
//;b676feb;update doc | ||
// (tag: 1.0.1);a9d3e04;add Converts | ||
@Override | ||
public void run(Args args) throws Exception { | ||
String out = args.readArg("out").orElse(new Arg("", "./target/changelog.md")).val(); | ||
var logFile = args.readArg("log"); | ||
var requiredTag = args.readArg("tag"); | ||
|
||
String commitLog; | ||
if (logFile.isPresent()) { | ||
commitLog = FileUtils.readFileToString(new File(logFile.get().val()), StandardCharsets.UTF_8); | ||
} else { | ||
String path = args.readArg("path").get().val(); | ||
|
||
String command = "git log --format=%d;%h;%s"; | ||
System.out.println("Run command: " + command); | ||
Process process = Runtime | ||
.getRuntime() | ||
.exec( | ||
command.split(" "), | ||
null, | ||
new File(path) | ||
); | ||
|
||
Process exec = process.onExit().get(); | ||
String error = IOUtils.toString(exec.getErrorStream(), StandardCharsets.UTF_8); | ||
if (error != null && !error.isEmpty()) { | ||
System.err.println(error); | ||
return; | ||
} | ||
commitLog = IOUtils.toString(exec.getInputStream(), StandardCharsets.UTF_8); | ||
} | ||
|
||
if (commitLog != null && !commitLog.isEmpty()) { | ||
var commits = new ArrayDeque<String>(); | ||
AtomicInteger tag = new AtomicInteger(0); | ||
Arrays.stream(commitLog.split("\n")) | ||
.map(a -> a.split(";")) | ||
.filter(a -> a.length > 2) | ||
.map(a -> new Commit(a[0], a[1], String.join(";", Arrays.copyOfRange(a, 2, a.length)))) | ||
.forEach(c -> { | ||
if (tag.get() == 0) { | ||
c.tag().ifPresent(t -> { | ||
if (requiredTag.isPresent() && !requiredTag.get().val().equals(t)) { | ||
return; | ||
} | ||
tag.set(1); | ||
commits.addFirst("## v" + t.trim() + "\n"); | ||
commits.add(c.toString()); | ||
}); | ||
} else if (tag.get() == 1) { | ||
if (c.tag().isPresent()) { | ||
tag.set(2); | ||
} else { | ||
commits.add(c.toString()); | ||
} | ||
} | ||
}); | ||
FileUtils.writeStringToFile(new File(out), String.join("\n", commits), StandardCharsets.UTF_8); | ||
System.out.println("Generate " + out + " ok"); | ||
} | ||
} | ||
|
||
@Override | ||
public String help() { | ||
return "[--path=./demo] [--out=demo-changelog.md] [--log=log.txt] [--tag=1.0.1]"; | ||
} | ||
|
||
private record Commit( | ||
String ref, | ||
String hash, | ||
String message | ||
) { | ||
public Optional<String> tag() { | ||
if (ref.contains("tag")) { | ||
return Arrays.stream(ref.replace("(", "").replace(")", "") | ||
.split(",")).filter(a -> a.trim().startsWith("tag")) | ||
.map(a -> a.split(":")[1].trim()).findAny(); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "* " + message + "(" + hash + ")"; | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/test/java/net/cofcool/toolbox/internal/GitCommitsToChangelogTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package net.cofcool.toolbox.internal; | ||
|
||
import net.cofcool.toolbox.Tool; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class GitCommitsToChangelogTest { | ||
|
||
@Test | ||
void runWithTag() throws Exception { | ||
new GitCommitsToChangelog().run(new Tool.Args().arg("path", Utils.getTestResourcePath("/")).arg("tag", "1.0.0").arg("out", "./target/changelog-runWithTag.md")); | ||
} | ||
|
||
@Test | ||
void run() throws Exception { | ||
new GitCommitsToChangelog().run(new Tool.Args().arg("path", Utils.getTestResourcePath("/"))); | ||
} | ||
|
||
@Test | ||
void runWithLogPath() throws Exception { | ||
new GitCommitsToChangelog().run(new Tool.Args().arg("log", Utils.getTestResourcePath("/gitCommitsToChangelogTest.txt")).arg("out", "./target/changelog-runWithLogPath.md")); | ||
} | ||
} |
4 changes: 1 addition & 3 deletions
4
src/test/java/net/cofcool/toolbox/internal/SplitKindleClippingsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
package net.cofcool.toolbox.internal; | ||
|
||
import java.net.URL; | ||
import net.cofcool.toolbox.Tool; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class SplitKindleClippingsTest { | ||
|
||
@Test | ||
void run() throws Exception { | ||
URL resource = SplitKindleClippings.class.getResource("/splitKindleClippingsTest.txt"); | ||
new SplitKindleClippings().run(new Tool.Args().arg("path", resource.toString().substring(5)).arg("out", "./target/splitKindleClippingsTest.md")); | ||
new SplitKindleClippings().run(new Tool.Args().arg("path", Utils.getTestResourcePath("/splitKindleClippingsTest.txt")).arg("out", "./target/splitKindleClippingsTest.md")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package net.cofcool.toolbox.internal; | ||
|
||
public class Utils { | ||
|
||
static String getTestResourcePath(String path) { | ||
return SplitKindleClippings.class.getResource(path).toString().substring(5); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
(HEAD -> master, tag: 1.0.4, origin/master, origin/HEAD);117826a;Converts: now, replace | ||
;1788569;add debug mode | ||
;1ee4312;update doc | ||
;b676feb;update doc | ||
(tag: 1.0.1);a9d3e04;add Converts | ||
(tag: 1.0.0);1b67da0;Update maven.yml | ||
;6474b63;Update maven.yml | ||
;97a383e;Update maven.yml | ||
;52ba1b8;Update maven.yml | ||
;2e71e27;Update maven.yml | ||
;61bfdff;Update maven.yml | ||
;6e26d48;github action | ||
;c92e596;fix action | ||
;24f7178;github action | ||
;8f989ce;fix readme | ||
;dc723bc;init |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters