From d5ee1833893552ba7b5042f7b911a79cc57443c6 Mon Sep 17 00:00:00 2001 From: "Dr. Hans-Peter Stoerr" Date: Tue, 17 Sep 2024 20:18:05 +0200 Subject: [PATCH 1/2] add the option -wvf for an additional file for the file version for braindead file formats like JSON that don't permit comments --- README.md | 1 + .../commandline/AIGenPipeline.java | 4 ++ .../src/main/resources/aigencmdline/usage.txt | 1 + .../framework/task/WritingStrategy.java | 39 +++++++++++++++++-- src/site/markdown/index.md | 1 + 5 files changed, 42 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index bd19cf2..eaa61ec 100644 --- a/README.md +++ b/README.md @@ -193,6 +193,7 @@ Options: -go, --gen-older Generate the output file if it does not exist or is older than any of the input files. -gv, --gen-versioncheck Generate the output file if the version of the input files has changed. (Default.) -wv, --write-version Write the output file with a version comment. (Default.) + -wvf, --write-versionfile Write the version comment to a separate file named like the output file with .version appended. -wo, --write-noversion Write the output file without a version comment. Not compatible with default -gv . -wp, --write-part Replace the lines between the first occurrence of the marker and the second occurrence. If a version marker is written, it has to be in the first of those lines and is changed there. diff --git a/aigenpipeline-commandline/src/main/java/net/stoerr/ai/aigenpipeline/commandline/AIGenPipeline.java b/aigenpipeline-commandline/src/main/java/net/stoerr/ai/aigenpipeline/commandline/AIGenPipeline.java index 1f9aed5..2011fdc 100644 --- a/aigenpipeline-commandline/src/main/java/net/stoerr/ai/aigenpipeline/commandline/AIGenPipeline.java +++ b/aigenpipeline-commandline/src/main/java/net/stoerr/ai/aigenpipeline/commandline/AIGenPipeline.java @@ -489,6 +489,10 @@ protected void parseArguments(String[] args, File dir) throws IOException { case "--write-version": writingStrategy = WritingStrategy.WITHVERSION; break; + case "-wvf": + case "--write-versionfile": + writingStrategy = WritingStrategy.WITHVERSIONFILE; + break; case "-wo": case "--write-noversion": writingStrategy = WritingStrategy.WITHOUTVERSION; diff --git a/aigenpipeline-commandline/src/main/resources/aigencmdline/usage.txt b/aigenpipeline-commandline/src/main/resources/aigencmdline/usage.txt index d67cc41..8f1e14a 100644 --- a/aigenpipeline-commandline/src/main/resources/aigencmdline/usage.txt +++ b/aigenpipeline-commandline/src/main/resources/aigencmdline/usage.txt @@ -43,6 +43,7 @@ Options: -go, --gen-older Generate the output file if it does not exist or is older than any of the input files. -gv, --gen-versioncheck Generate the output file if the version of the input files has changed. (Default.) -wv, --write-version Write the output file with a version comment. (Default.) + -wvf, --write-versionfile Write the version comment to a separate file named like the output file with .version appended. -wo, --write-noversion Write the output file without a version comment. Not compatible with default -gv . -wp, --write-part Replace the lines between the first occurrence of the marker and the second occurrence. If a version marker is written, it has to be in the first of those lines and is changed there. diff --git a/aigenpipeline-framework/src/main/java/net/stoerr/ai/aigenpipeline/framework/task/WritingStrategy.java b/aigenpipeline-framework/src/main/java/net/stoerr/ai/aigenpipeline/framework/task/WritingStrategy.java index 0d01fbc..89e33be 100644 --- a/aigenpipeline-framework/src/main/java/net/stoerr/ai/aigenpipeline/framework/task/WritingStrategy.java +++ b/aigenpipeline-framework/src/main/java/net/stoerr/ai/aigenpipeline/framework/task/WritingStrategy.java @@ -1,6 +1,6 @@ package net.stoerr.ai.aigenpipeline.framework.task; -import java.io.IOException; +import java.io.File; import javax.annotation.Nonnull; @@ -21,7 +21,7 @@ public interface WritingStrategy { */ WritingStrategy WITHOUTVERSION = new WritingStrategy() { @Override - public void write(@Nonnull AIInOut output, @Nonnull String content, @Nonnull String versionComment) { + public void write(@Nonnull AIInOut output, @Nonnull String content, @Nonnull String versionComment) { output.write(content); } @@ -42,7 +42,7 @@ public String toString() { */ WritingStrategy WITHVERSION = new WritingStrategy() { @Override - public void write(@Nonnull AIInOut output, @Nonnull String content, @Nonnull String versionComment) { + public void write(@Nonnull AIInOut output, @Nonnull String content, @Nonnull String versionComment) { output.write(embedComment(output, content, versionComment)); } @@ -55,7 +55,16 @@ public AIVersionMarker getRecordedVersionMarker(@Nonnull AIInOut output) { return null; } if (content == null) { - return null; + File versionFile = new File(output.getFile() + ".version"); + if (!versionFile.exists()) { + return null; + } + content = AIInOut.of(versionFile).read(); + AIVersionMarker aiVersionMarker = AIVersionMarker.find(content); + if (aiVersionMarker == null) { // here is really something wrong. + throw new IllegalStateException("Could not find version marker in " + versionFile); + } + return aiVersionMarker; } AIVersionMarker aiVersionMarker = AIVersionMarker.find(content); /* if (aiVersionMarker == null) { @@ -104,4 +113,26 @@ public String toString() { } }; + /** + * Writes an additional file (.version) with the version. + */ + WritingStrategy WITHVERSIONFILE = new WritingStrategy() { + @Override + public void write(@Nonnull AIInOut output, @Nonnull String content, @Nonnull String versionComment) { + output.write(content); + File versionFile = new File(output.getFile() + ".version"); + AIInOut.of(versionFile).write(versionComment); + } + + @Override + public AIVersionMarker getRecordedVersionMarker(@Nonnull AIInOut output) { + return WITHVERSION.getRecordedVersionMarker(output); + } + + @Override + public String toString() { + return "WritingStrategy.WITHVERSIONFILE"; + } + }; + } diff --git a/src/site/markdown/index.md b/src/site/markdown/index.md index eb52dda..b492efc 100644 --- a/src/site/markdown/index.md +++ b/src/site/markdown/index.md @@ -264,6 +264,7 @@ Options: -go, --gen-older Generate the output file if it does not exist or is older than any of the input files. -gv, --gen-versioncheck Generate the output file if the version of the input files has changed. (Default.) -wv, --write-version Write the output file with a version comment. (Default.) + -wvf, --write-versionfile Write the version comment to a separate file named like the output file with .version appended. -wo, --write-noversion Write the output file without a version comment. Not compatible with default -gv . -wp, --write-part Replace the lines between the first occurrence of the marker and the second occurrence. If a version marker is written, it has to be in the first of those lines and is changed there. From 72f360a4996edc7a9f7efb6b1729b9ef866d95b6 Mon Sep 17 00:00:00 2001 From: "Dr. Hans-Peter Stoerr" Date: Fri, 20 Sep 2024 22:11:18 +0200 Subject: [PATCH 2/2] fixes for version file strategy --- .../framework/task/AIVersionMarker.java | 16 +++++++++++++++- .../framework/task/WritingStrategy.java | 13 +++++++------ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/aigenpipeline-framework/src/main/java/net/stoerr/ai/aigenpipeline/framework/task/AIVersionMarker.java b/aigenpipeline-framework/src/main/java/net/stoerr/ai/aigenpipeline/framework/task/AIVersionMarker.java index e837dc3..db78511 100644 --- a/aigenpipeline-framework/src/main/java/net/stoerr/ai/aigenpipeline/framework/task/AIVersionMarker.java +++ b/aigenpipeline-framework/src/main/java/net/stoerr/ai/aigenpipeline/framework/task/AIVersionMarker.java @@ -2,7 +2,7 @@ import static java.util.Objects.requireNonNull; -import java.io.IOException; +import java.io.File; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -24,6 +24,13 @@ public class AIVersionMarker { public static final Pattern VERSION_MARKER_PATTERN = Pattern.compile("AIGenVersion\\([^)]+\\)"); + + /** + * Suffix appended to files with formats braindead enough to not allow comments so that we have to store + * the file version in an additional file. + */ + public static final String FILESUFFIX_VERSION = ".version"; + protected final String ourVersion; protected final List inputVersions; @@ -71,6 +78,13 @@ public static String determineFileVersionMarker(@Nonnull AIInOut inOut) { String content = inOut.read(); requireNonNull(content, "Could not read file " + inOut); AIVersionMarker aiVersionMarker = AIVersionMarker.find(content); + if (aiVersionMarker == null) { + File versionFile = new File(inOut.getFile() + FILESUFFIX_VERSION); + if (versionFile.exists()) { + String versionFileContent = AIInOut.of(versionFile).read(); + aiVersionMarker = AIVersionMarker.find(versionFileContent); + } + } String version; if (aiVersionMarker != null) { version = aiVersionMarker.getOurVersion(); diff --git a/aigenpipeline-framework/src/main/java/net/stoerr/ai/aigenpipeline/framework/task/WritingStrategy.java b/aigenpipeline-framework/src/main/java/net/stoerr/ai/aigenpipeline/framework/task/WritingStrategy.java index 89e33be..88f49b0 100644 --- a/aigenpipeline-framework/src/main/java/net/stoerr/ai/aigenpipeline/framework/task/WritingStrategy.java +++ b/aigenpipeline-framework/src/main/java/net/stoerr/ai/aigenpipeline/framework/task/WritingStrategy.java @@ -1,5 +1,7 @@ package net.stoerr.ai.aigenpipeline.framework.task; +import static net.stoerr.ai.aigenpipeline.framework.task.AIVersionMarker.FILESUFFIX_VERSION; + import java.io.File; import javax.annotation.Nonnull; @@ -54,19 +56,18 @@ public AIVersionMarker getRecordedVersionMarker(@Nonnull AIInOut output) { } catch (RuntimeException e) { return null; } - if (content == null) { - File versionFile = new File(output.getFile() + ".version"); + AIVersionMarker aiVersionMarker = AIVersionMarker.find(content); + if (aiVersionMarker == null) { + File versionFile = new File(output.getFile() + FILESUFFIX_VERSION); if (!versionFile.exists()) { return null; } content = AIInOut.of(versionFile).read(); - AIVersionMarker aiVersionMarker = AIVersionMarker.find(content); + aiVersionMarker = AIVersionMarker.find(content); if (aiVersionMarker == null) { // here is really something wrong. throw new IllegalStateException("Could not find version marker in " + versionFile); } - return aiVersionMarker; } - AIVersionMarker aiVersionMarker = AIVersionMarker.find(content); /* if (aiVersionMarker == null) { throw new IllegalStateException("Could not find version marker in " + output); } probably invalid heuristic. */ @@ -120,7 +121,7 @@ public String toString() { @Override public void write(@Nonnull AIInOut output, @Nonnull String content, @Nonnull String versionComment) { output.write(content); - File versionFile = new File(output.getFile() + ".version"); + File versionFile = new File(output.getFile() + FILESUFFIX_VERSION); AIInOut.of(versionFile).write(versionComment); }