Skip to content

Commit

Permalink
feat: add describe.tag.version... and ...next placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
qoomon committed May 8, 2022
1 parent e8dbe3d commit 64be83c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 35 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,23 @@
# Changelog

## 8.0.0
##### Features
- migrate to java 11
- placeholder
- new
- `${version.major.next}`
- `${version.minor.next}`
- `${version.patch.next}`
- `${describe.tag.version}`
- `${describe.tag.version.major}`
- `${describe.tag.version.major.next}`
- `${describe.tag.version.minor}`
- `${describe.tag.version.minor.next}`
- `${describe.tag.version.path}`
- `${describe.tag.version.patch.next}`
- removed
- `${version.minor.prefixed}`
- `${version.patch.prefixed}`

##### BREAKING CHANGES
- drop support for java 8
Expand Down
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,16 @@ e.g `${dirty:-SNAPSHOT}` resolves to `-SNAPSHOT` instead of `-DIRTY`
<br><br>

- `${version}` `<version>` set in `pom.xml` e.g. '1.2.3-SNAPSHOT'
- `${version.major}` the major version component of `${version}` e.g. '1'
- `${version.minor}` the minor version component of `${version}` e.g. '2'
- `${version.minor.prefixed}` like `${version.minor}` with version component separator e.g. '.2'
- `${version.patch}` the patch version component of `${version}` e.g. '3'
- `${version.patch.prefixed}` like `${version.patch}` with version component separator e.g. '.3'
- `${version.label}` the version label of `${version}` e.g. 'SNAPSHOT'
- `${version.label.prefixed}` like `${version.label}` with label separator e.g. '-SNAPSHOT'
- `${version.release}` like `${version}` without version labels like `-SNAPSHOT` e.g. '1.2.3'
<br><br>
- `${version.major}` the major version component of `${version}` e.g. '1'
- `${version.major.next}` the `${version.major}` increased by 1 e.g. '2'
- `${version.minor}` the minor version component of `${version}` e.g. '2'
- `${version.minor.next}` the `${version.minor}` increased by 1 e.g. '3'
- `${version.patch}` the patch version component of `${version}` e.g. '3'
- `${version.patch.next}` the `${version.patch}` increased by 1 e.g. '4'
- `${version.label}` the version label of `${version}` e.g. 'SNAPSHOT'
- `${version.label.prefixed}` like `${version.label}` with label separator e.g. '-SNAPSHOT'
- `${version.release}` like `${version}` without version labels like `-SNAPSHOT` e.g. '1.2.3'
<br><br>

- `${ref}` `${ref.slug}` ref name (branch or tag name or commit hash)
- Ref Pattern Groups
Expand Down Expand Up @@ -190,6 +191,13 @@ e.g `${dirty:-SNAPSHOT}` resolves to `-SNAPSHOT` instead of `-DIRTY`
- `${describe}` Will resolve to `git describe` output
- `${describe.distance}` The distance count to last matching tag
- `${describe.tag}` The matching tag of `git describe`
- `${describe.tag.version}` the tag version determined by regex `\d+\.\d+\.\d+`
- `${describe.tag.version.major}` the major version component of `${describe.tag.version}` e.g. '1'
- `${describe.tag.version.major.next}` the `${describe.tag.version.major}` increased by 1 e.g. '2'
- `${describe.tag.version.minor}` the major version component of `${describe.tag.version}` e.g. '2'
- `${describe.tag.version.minor.next}` the `${describe.tag.version.minor}` increased by 1 e.g. '3'
- `${describe.tag.version.path}` the major version component of `${describe.tag.version}` e.g. '3'
- `${describe.tag.version.patch.next}` the `${describe.tag.version.patch}` increased by 1 e.g. '4'
- Describe Tag Pattern Groups
- Content of regex groups in `<describeTagPattern>` can be addressed like this:
- `${describe.tag.GROUP_NAME}` `${describe.tag.GROUP_NAME.slug}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Map.Entry;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.fasterxml.jackson.databind.MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS;
Expand All @@ -42,12 +43,14 @@
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.Comparator.comparing;
import static java.util.Objects.requireNonNullElse;
import static java.util.stream.Collectors.*;
import static me.qoomon.gitversioning.commons.GitRefType.*;
import static me.qoomon.gitversioning.commons.StringUtil.*;
import static me.qoomon.maven.gitversioning.BuildProperties.projectArtifactId;
import static me.qoomon.maven.gitversioning.GitVersioningMojo.asPlugin;
import static me.qoomon.maven.gitversioning.MavenUtil.*;
import static org.apache.commons.lang3.StringUtils.leftPad;
import static org.apache.maven.shared.utils.StringUtils.*;
import static org.apache.maven.shared.utils.logging.MessageUtils.buffer;
import static org.slf4j.LoggerFactory.getLogger;
Expand All @@ -59,6 +62,8 @@
@Singleton
public class GitVersioningModelProcessor extends DefaultModelProcessor {

private static final Pattern VERSION_PATTERN = Pattern.compile("(:?(?<major>\\d+)(:?\\.(?<minor>\\d+)(:?\\.(?<patch>\\d+))?)?(:?-(?<label>.*))?)?");

private static final String OPTION_NAME_GIT_REF = "git.ref";
private static final String OPTION_NAME_GIT_TAG = "git.tag";
private static final String OPTION_NAME_GIT_BRANCH = "git.branch";
Expand Down Expand Up @@ -698,19 +703,26 @@ private String getGitPropertyValue(String propertyFormat, String originalValue,

private Map<String, Supplier<String>> generateFormatPlaceholderMap(String projectVersion) {
final Map<String, Supplier<String>> placeholderMap = new HashMap<>(globalFormatPlaceholderMap);
final Lazy<String[]> versionComponents = Lazy.by(() -> projectVersion.replaceFirst("-.*$", "").split("\\."));
final Lazy<String> versionMajor = Lazy.by(() -> versionComponents.get().length > 0 ? versionComponents.get()[0] : "");
final Lazy<String> versionMinor = Lazy.by(() -> versionComponents.get().length > 1 ? versionComponents.get()[1] : "");
final Lazy<String> versionPatch = Lazy.by(() -> versionComponents.get().length > 1 ? versionComponents.get()[2] : "");
final Lazy<String> versionLabel = Lazy.by(() -> projectVersion.replaceFirst("^[^-]*-?", ""));

placeholderMap.put("version", Lazy.of(projectVersion));
placeholderMap.put("version.major", versionMajor);
placeholderMap.put("version.minor", versionMinor);
placeholderMap.put("version.minor.prefixed", Lazy.by(() -> "." + versionMinor.get()));
placeholderMap.put("version.patch", versionPatch);
placeholderMap.put("version.patch.prefixed", Lazy.by(() -> "." + versionPatch.get()));
placeholderMap.put("version.label", versionLabel);
placeholderMap.put("version.label.prefixed", Lazy.by(() -> "-" + versionLabel.get()));

final Lazy<Matcher> versionComponents = Lazy.by(() -> {
Matcher matcher = VERSION_PATTERN.matcher(projectVersion);
//noinspection ResultOfMethodCallIgnored
matcher.find();
return matcher;
});

placeholderMap.put("version.major", Lazy.by(() -> requireNonNullElse(versionComponents.get().group("major"), "0")));
placeholderMap.put("version.major.next", Lazy.by(() -> increaseStringNumber(placeholderMap.get("version.major").get())));

placeholderMap.put("version.minor", Lazy.by(() -> requireNonNullElse(versionComponents.get().group("minor"), "0")));
placeholderMap.put("version.major.next", Lazy.by(() -> increaseStringNumber(placeholderMap.get("version.minor").get())));

placeholderMap.put("version.patch", Lazy.by(() -> requireNonNullElse(versionComponents.get().group("patch"), "0")));
placeholderMap.put("version.patch.next", Lazy.by(() -> increaseStringNumber(placeholderMap.get("version.patch").get())));

placeholderMap.put("version.label", Lazy.by(() -> requireNonNullElse(versionComponents.get().group("label"), "")));

final Lazy<String> versionRelease = Lazy.by(() -> projectVersion.replaceFirst("-.*$", ""));
placeholderMap.put("version.release", versionRelease);
Expand Down Expand Up @@ -764,17 +776,40 @@ private Map<String, Supplier<String>> generateGlobalFormatPlaceholderMap(GitSitu
placeholderMap.put("describe", Lazy.by(() -> description.get().toString()));
final Lazy<String> descriptionTag = Lazy.by(() -> description.get().getTag());
placeholderMap.put("describe.tag", descriptionTag);
placeholderMap.put("describe.distance", Lazy.by(() -> String.valueOf(description.get().getDistance())));

// describe tag pattern groups
final Lazy<Map<String, String>> describeTagPatternValues = Lazy.by(
() -> patternGroupValues(gitSituation.getDescribeTagPattern(), descriptionTag.get()));
for (String groupName : patternGroups(gitSituation.getDescribeTagPattern())) {
Lazy<String> value = Lazy.by(() -> describeTagPatternValues.get().get(groupName));
placeholderMap.put("describe.tag." + groupName, value);
placeholderMap.put("describe.tag." + groupName + ".slug", Lazy.by(() -> slugify(value.get())));
Lazy<String> groupValue = Lazy.by(() -> describeTagPatternValues.get().get(groupName));
placeholderMap.put("describe.tag." + groupName, groupValue);
placeholderMap.put("describe.tag." + groupName + ".slug", Lazy.by(() -> slugify(groupValue.get())));
}

Supplier<String> descriptionTagVersion = placeholderMap.computeIfAbsent("describe.tag.version", key -> Lazy.by(() -> {
Matcher matcher = VERSION_PATTERN.matcher(descriptionTag.get());
return matcher.find() ? matcher.group() : "0.0.0";
}));

final Lazy<Matcher> descriptionTagVersionComponents = Lazy.by(() -> {
Matcher matcher = VERSION_PATTERN.matcher(descriptionTagVersion.get());
//noinspection ResultOfMethodCallIgnored
matcher.find();
return matcher;
});

placeholderMap.put("describe.tag.version.major", Lazy.by(() -> requireNonNullElse(descriptionTagVersionComponents.get().group("major"), "0")));
placeholderMap.put("describe.tag.version.major.next", Lazy.by(() -> increaseStringNumber(placeholderMap.get("describe.tag.version.major").get())));

placeholderMap.put("describe.tag.version.minor", Lazy.by(() -> requireNonNullElse(descriptionTagVersionComponents.get().group("minor"), "0")));
placeholderMap.put("describe.tag.version.minor.next", Lazy.by(() -> increaseStringNumber(placeholderMap.get("describe.tag.version.minor").get())));

placeholderMap.put("describe.tag.version.patch", Lazy.by(() -> requireNonNullElse(descriptionTagVersionComponents.get().group("patch"), "0")));
placeholderMap.put("describe.tag.version.patch.next", Lazy.by(() -> increaseStringNumber(placeholderMap.get("describe.tag.version.patch").get())));

placeholderMap.put("describe.tag.version.version.label", Lazy.by(() -> requireNonNullElse(descriptionTagVersionComponents.get().group("label"), "")));

placeholderMap.put("describe.distance", Lazy.by(() -> String.valueOf(description.get().getDistance())));

// command parameters e.g. mvn -Dfoo=123 will be available as ${property.foo}
for (Entry<Object, Object> property : mavenSession.getUserProperties().entrySet()) {
if (property.getValue() != null) {
Expand Down Expand Up @@ -1217,14 +1252,6 @@ private static String sectionLogHeader(String title, ModelBase model) {
return header;
}

private static String slugify(String value) {
if (value == null) {
return "";
}
return value.replace("/", "-");
}


// ---- utils ------------------------------------------------------------------------------------------------------

public static <T1, T2> void forEachPair(Collection<T1> collection1, Collection<T2> collection2, BiConsumer<T1, T2> consumer) {
Expand All @@ -1238,4 +1265,15 @@ public static <T1, T2> void forEachPair(Collection<T1> collection1, Collection<T
consumer.accept(iter1.next(), iter2.next());
}
}

private static String slugify(String value) {
if (value == null) {
return "";
}
return value.replace("/", "-");
}

private static String increaseStringNumber(String majorVersion) {
return String.format("%0" + majorVersion.length() + "d", Long.parseLong(majorVersion) + 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<ref type="branch">
<pattern>master</pattern>
<version>${describe.tag.version}-SNAPSHOT</version>
<version>${describe.tag.version.major.next}-SNAPSHOT</version>
<properties>
<foo>new_new_new</foo>
</properties>
Expand Down

0 comments on commit 64be83c

Please sign in to comment.