Skip to content

Commit

Permalink
feat: add projectVersionPattern
Browse files Browse the repository at this point in the history
  • Loading branch information
qoomon committed Jun 30, 2022
1 parent 093d65c commit 99ca62e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 8 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
# Changelog

## 6.2.0

##### Features
- add config option for `projectVersionPattern` to use special parts of the project version as placeholders e.g. `${version.environment}`
- add placeholder `${version.core}` the core version component of `${version}` e.g. '1.2.3'
- `${version.release}` is marked as deprecated


## 6.1.6

##### Fixes
- handle lightweight tags
- set `${version.minor.next}` placeholders properly


## 6.1.5

##### Fixes
Expand Down
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ e.g `${dirty:-SNAPSHOT}` resolves to `-SNAPSHOT` instead of `-DIRTY`
<br><br>
- `${version}` `version` set in `build.gradle` e.g. '1.0.0-SNAPSHOT'
- `${version.core}` the core version component of `${version}` e.g. '1.2.3'
- `${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'
Expand All @@ -184,8 +185,27 @@ e.g `${dirty:-SNAPSHOT}` resolves to `-SNAPSHOT` instead of `-DIRTY`
- `${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>
- Project Version Pattern Groups
- Content of regex groups in `projectVersionPattern` can be addressed like this:
- `${version.GROUP_NAME}`
- `${version.GROUP_INDEX}`
- Named Group Example
**groovy**
```groovy
projectVersionPattern = '^.+-(?<environment>.+)-SNAPSHOT$'
branch('main') {
version = '${version.environment}-SNAPSHOT'
}
```
**kotlin**
```kotlin
projectVersionPattern = '^.+-(?<environment>.+)-SNAPSHOT\$'
branch("main") {
version = "\${version.environment}-SNAPSHOT"
}
```
<br>
- `${ref}` `${ref.slug}` ref name (branch or tag name or commit hash)
- Ref Pattern Groups
Expand Down Expand Up @@ -225,6 +245,7 @@ e.g `${dirty:-SNAPSHOT}` resolves to `-SNAPSHOT` instead of `-DIRTY`
- `${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.core}` the core version component of `${describe.tag.version}` e.g. '1.2.3'
- `${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'
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
}

group 'me.qoomon'
version '6.1.6'
version '6.2.0'
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ protected ObjectFactory getObjectFactory() {

public Boolean disable = false;

public String describeTagPattern = MATCH_ALL.pattern();
public String projectVersionPattern = null;

public Pattern projectVersionPattern() {
return projectVersionPattern != null
? Pattern.compile(projectVersionPattern)
: null;
}

public String describeTagPattern = null;

public Boolean updateGradleProperties;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
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.toList;
import static me.qoomon.gitversioning.commons.GitRefType.*;
import static me.qoomon.gitversioning.commons.StringUtil.*;
Expand All @@ -47,7 +48,7 @@ public abstract class GitVersioningPluginExtension {

public static final Logger LOGGER = LoggerFactory.getLogger(GitVersioningPluginExtension.class);

private static final Pattern VERSION_PATTERN = Pattern.compile("(:?(?<major>\\d+)(:?\\.(?<minor>\\d+)(:?\\.(?<patch>\\d+))?)?(:?-(?<label>.*))?)?");
private static final Pattern VERSION_PATTERN = Pattern.compile("(:?(?<core>(?<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";
Expand All @@ -60,6 +61,8 @@ public abstract class GitVersioningPluginExtension {

private final Project project;

private GitVersioningPluginConfig config;

public GitVersionDetails gitVersionDetails;

public Map<String, Supplier<String>> globalFormatPlaceholderMap;
Expand All @@ -75,8 +78,12 @@ public void apply(Action<GitVersioningPluginConfig> action) throws IOException {
}

public void apply(GitVersioningPluginConfig config) throws IOException {
this.config = config;
normalizeConfig(config);
apply();
}

private void apply() throws IOException {
// check if extension is disabled by command option
final String commandOptionDisable = getCommandOption(OPTION_NAME_DISABLE);
if (commandOptionDisable != null) {
Expand Down Expand Up @@ -459,6 +466,8 @@ private Map<String, Supplier<String>> generateFormatPlaceholderMap(String projec
return matcher;
});

placeholderMap.put("version.core", Lazy.by(() -> notNullOrDefault(versionComponents.get().group("core"), "0.0.0")));

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

Expand All @@ -474,8 +483,19 @@ private Map<String, Supplier<String>> generateFormatPlaceholderMap(String projec
return !label.isEmpty() ? "-" + label : "";
}));

final Lazy<String> versionRelease = Lazy.by(() -> projectVersion.replaceFirst("-.*$", ""));
placeholderMap.put("version.release", versionRelease);

// deprecated
placeholderMap.put("version.release", Lazy.by(() -> projectVersion.replaceFirst("-.*$", "")));

final Pattern projectVersionPattern = config.projectVersionPattern();
if (projectVersionPattern != null) {
// ref pattern groups
for (Entry<String, String> patternGroup : patternGroupValues(projectVersionPattern, projectVersion).entrySet()) {
final String groupName = patternGroup.getKey();
final String value = patternGroup.getValue() != null ? patternGroup.getValue() : "";
placeholderMap.put("version." + groupName, () -> value);
}
}

return placeholderMap;
}
Expand Down Expand Up @@ -547,6 +567,8 @@ private Map<String, Supplier<String>> generateGlobalFormatPlaceholderMap(GitSitu
return matcher;
});

placeholderMap.put("describe.tag.version.core", Lazy.by(() -> notNullOrDefault(descriptionTagVersionComponents.get().group("core"), "0")));

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

Expand Down Expand Up @@ -615,7 +637,7 @@ private void normalizeConfig(GitVersioningPluginConfig config) {
}

private String getCommandOption(final String name) {
String value = System.getProperty(name);
String value = System.getProperty(name);
if (value == null) {
String plainName = name.replaceFirst("^versioning\\.", "");
String environmentVariableName = "VERSIONING_"
Expand Down

0 comments on commit 99ca62e

Please sign in to comment.