Skip to content

Commit

Permalink
Merge pull request #53 from stoerr/feature/versionfile
Browse files Browse the repository at this point in the history
Add version files for version markers:
Since some file formats (e.g. JSON) are braindead enough that they don't allow comments, we add a strategy -wvf to write a .version file: the version for foo.json is read and written from / to foo.json.version .
  • Loading branch information
stoerr authored Sep 20, 2024
2 parents b75d689 + 72f360a commit a1d96cf
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <marker> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <marker> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> inputVersions;

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.stoerr.ai.aigenpipeline.framework.task;

import java.io.IOException;
import static net.stoerr.ai.aigenpipeline.framework.task.AIVersionMarker.FILESUFFIX_VERSION;

import java.io.File;

import javax.annotation.Nonnull;

Expand All @@ -21,7 +23,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);
}

Expand All @@ -42,7 +44,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));
}

Expand All @@ -54,10 +56,18 @@ public AIVersionMarker getRecordedVersionMarker(@Nonnull AIInOut output) {
} catch (RuntimeException e) {
return null;
}
if (content == null) {
return null;
}
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.find(content);
if (aiVersionMarker == null) { // here is really something wrong.
throw new IllegalStateException("Could not find version marker in " + versionFile);
}
}
/* if (aiVersionMarker == null) {
throw new IllegalStateException("Could not find version marker in " + output);
} probably invalid heuristic. */
Expand Down Expand Up @@ -104,4 +114,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() + FILESUFFIX_VERSION);
AIInOut.of(versionFile).write(versionComment);
}

@Override
public AIVersionMarker getRecordedVersionMarker(@Nonnull AIInOut output) {
return WITHVERSION.getRecordedVersionMarker(output);
}

@Override
public String toString() {
return "WritingStrategy.WITHVERSIONFILE";
}
};

}
1 change: 1 addition & 0 deletions src/site/markdown/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <marker> 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.
Expand Down

0 comments on commit a1d96cf

Please sign in to comment.