Skip to content

Commit

Permalink
add version fallback to git version tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
LCLPYT committed Nov 11, 2023
1 parent b0e5ab5 commit 29aaf85
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/main/java/work/lclpnet/build/ext/BuildUtilsExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public interface BuildUtilsExtension {

Property<String> getVersionPattern();

Property<String> getFallbackVersion();

/**
* Loads properties from a source, such as a file or filename.
* The properties are loaded into this extension.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ public class BuildUtilsExtensionImpl implements BuildUtilsExtension {

private final Project project;
private final Property<String> versionPattern;
private final Property<String> fallbackVersion;
private final Properties properties = new Properties();
private GitVersionResolver gitVersionResolver = null;
private volatile String latestTag = null;

public BuildUtilsExtensionImpl(Project project) {
this.project = project;
this.versionPattern = project.getObjects().property(String.class).convention(GitVersionResolver.VERSION_PATTERN);
this.fallbackVersion = project.getObjects().property(String.class).convention("0.1.0-SNAPSHOT");
}

@Override
Expand Down Expand Up @@ -101,6 +103,11 @@ public Property<String> getVersionPattern() {
return versionPattern;
}

@Override
public Property<String> getFallbackVersion() {
return fallbackVersion;
}

private String fetchLatestTag() {
if (latestTag != null) {
return latestTag;
Expand All @@ -112,7 +119,7 @@ private String fetchLatestTag() {
}

if (gitVersionResolver == null) {
gitVersionResolver = new GitVersionResolver(project.getLogger());
gitVersionResolver = new GitVersionResolver(project.getLogger(), fallbackVersion.get());
}

File pwd = project.getProjectDir();
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/work/lclpnet/build/util/GitVersionResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ public class GitVersionResolver {

public static final String VERSION_PATTERN = "^[0-9]+\\.[0-9]+\\.[0-9]+(?:[-+][a-zA-Z0-9.]+)?$";
private final Logger logger;
private final String fallbackVersion;

public GitVersionResolver(Logger logger) {
public GitVersionResolver(Logger logger, String fallbackVersion) {
this.logger = logger;
this.fallbackVersion = fallbackVersion;
}

/**
Expand Down Expand Up @@ -42,7 +44,8 @@ public String getGitVersion(File pwd) {

return result.stdout.trim().split("\\r?\\n")[0];
} catch (Exception ex) {
throw new IllegalStateException("Could not determine version", ex);
logger.warn("Could not determine version from latest git tag. Fallback to {}", fallbackVersion, ex);
return fallbackVersion;
}
}
}

0 comments on commit 29aaf85

Please sign in to comment.