Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for 'useMaxVersion' jgitver paramter #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ build/
.gradle/
.settings/
.okhttpcache/
.idea/
*.iml
src/test/integration/test/.git/
src/test/integration/test/build/
src/test/integration/test/.gradle/
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ jgitver {
useDistance true/false
useGitCommitID true/false
gitCommitIDLength integer
useMaxVersion true/false
maxVersionSearchDepth integer
nonQualifierBranches string (comma separated list of branches)
policy { repeatable closure
pattern string (regexp with capturing group)
Expand All @@ -113,6 +115,8 @@ If you do not provide such a configuration (or fill only partial configuration)
- _useGitCommitTimestamp_: `false`
- _useDistance_: `true`
- _useGitCommitId_: `false`
- _userMaxVersion_: `false`
- _maxVersionSearchDepth_: 1000
- _nonQualifierBranches_: `'master'`
- _regexVersionTag_: `'Java regexp pattern'`
- if non set or null then [jgitver](https://github.com/jgitver/jgitver) default applies
Expand Down Expand Up @@ -196,7 +200,7 @@ Since `0.4.1` it now possible to provide externally the branch information via a
````gradle
buildscript {
repositories {
mavenLocal()
mavenLocal()
}
dependencies {
classpath "fr.brouillard.oss.gradle:gradle-jgitver-plugin:0.3.2"
Expand All @@ -208,7 +212,7 @@ Since `0.4.1` it now possible to provide externally the branch information via a
````gradle
buildscript {
repositories {
mavenLocal()
mavenLocal()
}
dependencies {
classpath "fr.brouillard.oss.gradle:gradle-jgitver-plugin:0.3.2"
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ buildscript {

plugins {
id "com.gradle.plugin-publish" version "0.9.7"
id "fr.brouillard.oss.gradle.jgitver" version "0.4.1"
id "fr.brouillard.oss.gradle.jgitver" version "0.5.0"
}

apply plugin: 'java'
Expand Down Expand Up @@ -41,7 +41,7 @@ pluginBundle {

dependencies {
compile gradleApi()
compile "fr.brouillard.oss:jgitver:0.6.1"
compile "fr.brouillard.oss:jgitver:0.8.0"
}

task changelog(type: se.bjurr.gitchangelog.plugin.gradle.GitChangelogTask) {
Expand Down
Empty file modified gradlew
100644 → 100755
Empty file.
96 changes: 46 additions & 50 deletions src/main/java/fr/brouillard/oss/gradle/plugins/JGitverPlugin.java
Original file line number Diff line number Diff line change
@@ -1,74 +1,70 @@
package fr.brouillard.oss.gradle.plugins;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

import fr.brouillard.oss.jgitver.BranchingPolicy;
import org.gradle.api.Action;
import fr.brouillard.oss.jgitver.GitVersionCalculator;
import fr.brouillard.oss.jgitver.metadata.Metadatas;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.tooling.BuildException;

import fr.brouillard.oss.jgitver.GitVersionCalculator;
import fr.brouillard.oss.jgitver.metadata.Metadatas;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

public class JGitverPlugin implements Plugin<Project> {
@Override
public void apply(Project project) {
project.getExtensions().create("jgitver", JGitverPluginExtension.class, project);
project.getTasks().create("version", JGitverVersionTask.class);

project.afterEvaluate(new Action<Project>() {
@Override
public void execute(Project evaluatedProject) {
JGitverPluginExtension jgitverConfiguration = project.getExtensions().findByType(JGitverPluginExtension.class);
project.afterEvaluate(evaluatedProject -> {
JGitverPluginExtension jgitverConfiguration = project.getExtensions().findByType(JGitverPluginExtension.class);

GitVersionCalculator versionCalculator = GitVersionCalculator.location(project.getRootDir())
.setMavenLike(jgitverConfiguration.mavenLike)
.setAutoIncrementPatch(jgitverConfiguration.autoIncrementPatch)
.setUseDistance(jgitverConfiguration.useDistance)
.setUseDirty(jgitverConfiguration.useDirty)
.setUseGitCommitTimestamp(jgitverConfiguration.useGitCommitTimestamp)
.setUseGitCommitId(jgitverConfiguration.useGitCommitID)
.setGitCommitIdLength(jgitverConfiguration.gitCommitIDLength)
.setNonQualifierBranches(jgitverConfiguration.nonQualifierBranches);
GitVersionCalculator versionCalculator = GitVersionCalculator.location(project.getRootDir())
.setMavenLike(jgitverConfiguration.mavenLike)
.setAutoIncrementPatch(jgitverConfiguration.autoIncrementPatch)
.setUseDistance(jgitverConfiguration.useDistance)
.setUseDirty(jgitverConfiguration.useDirty)
.setUseGitCommitTimestamp(jgitverConfiguration.useGitCommitTimestamp)
.setUseGitCommitId(jgitverConfiguration.useGitCommitID)
.setGitCommitIdLength(jgitverConfiguration.gitCommitIDLength)
.setUseMaxVersion(jgitverConfiguration.useMaxVersion)
.setMaxVersionSearchDepth(jgitverConfiguration.maxVersionSearchDepth)
.setNonQualifierBranches(jgitverConfiguration.nonQualifierBranches);

if (!jgitverConfiguration.policies.isEmpty()) {
List<BranchingPolicy> branchingPolicies = jgitverConfiguration.policies.stream()
.map(JGitverPlugin::toBranchingPolicy)
.collect(Collectors.toList());
versionCalculator.setQualifierBranchingPolicies(branchingPolicies);
}
if (!jgitverConfiguration.policies.isEmpty()) {
List<BranchingPolicy> branchingPolicies = jgitverConfiguration.policies.stream()
.map(JGitverPlugin::toBranchingPolicy)
.collect(Collectors.toList());
versionCalculator.setQualifierBranchingPolicies(branchingPolicies);
}

if (jgitverConfiguration.regexVersionTag != null) {
versionCalculator = versionCalculator.setFindTagVersionPattern(jgitverConfiguration.regexVersionTag);
}
if (jgitverConfiguration.regexVersionTag != null) {
versionCalculator = versionCalculator.setFindTagVersionPattern(jgitverConfiguration.regexVersionTag);
}

String gitCalculatedVersion = versionCalculator.getVersion();
String gitCalculatedVersion = versionCalculator.getVersion();

boolean isDirty = versionCalculator
.meta(Metadatas.DIRTY)
.map(Boolean::parseBoolean)
.orElse(Boolean.FALSE);

if (jgitverConfiguration.useDirty && jgitverConfiguration.failIfDirty && isDirty) {
IllegalStateException cause = new IllegalStateException("jgitver detected a dirty state, project is configured to fail");
throw new BuildException("jgitver stopped the build for a git dirty state", cause);
}

project.setVersion(gitCalculatedVersion);
project.getAllprojects().forEach(subproject -> subproject.setVersion(gitCalculatedVersion));
boolean isDirty = versionCalculator
.meta(Metadatas.DIRTY)
.map(Boolean::parseBoolean)
.orElse(Boolean.FALSE);

for (Metadatas metadata: Metadatas.values()) {
versionCalculator.meta(metadata).ifPresent(metadataValue -> {
project.getExtensions().getExtraProperties().set(metadata.name().toLowerCase(Locale.ENGLISH), metadataValue);
project.getAllprojects().forEach(
subproject -> subproject.getExtensions().getExtraProperties().set(metadata.name().toLowerCase(Locale.ENGLISH), metadataValue));
});
};
if (jgitverConfiguration.useDirty && jgitverConfiguration.failIfDirty && isDirty) {
IllegalStateException cause = new IllegalStateException("jgitver detected a dirty state, project is configured to fail");
throw new BuildException("jgitver stopped the build for a git dirty state", cause);
}

project.setVersion(gitCalculatedVersion);
project.getAllprojects().forEach(subproject -> subproject.setVersion(gitCalculatedVersion));

for (Metadatas metadata: Metadatas.values()) {
versionCalculator.meta(metadata).ifPresent(metadataValue -> {
project.getExtensions().getExtraProperties().set(metadata.name().toLowerCase(Locale.ENGLISH), metadataValue);
project.getAllprojects().forEach(
subproject -> subproject.getExtensions().getExtraProperties().set(metadata.name().toLowerCase(Locale.ENGLISH), metadataValue));
});
};
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package fr.brouillard.oss.gradle.plugins;

import groovy.lang.Closure;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Project;

import javax.inject.Inject;
Expand All @@ -17,9 +16,12 @@ public class JGitverPluginExtension {
public Boolean useGitCommitTimestamp = Boolean.FALSE;
public Boolean useGitCommitID = Boolean.FALSE;
public int gitCommitIDLength = 8;
public Boolean useMaxVersion = Boolean.FALSE;
public int maxVersionSearchDepth = 1000;
public String nonQualifierBranches = "master";
public String regexVersionTag = null;
public List<JGitverPluginExtensionBranchPolicy> policies;

private Project project;

@Inject
Expand All @@ -39,7 +41,7 @@ public void autoIncrementPatch(boolean autoIncrementPatch) {
public void useDistance(boolean useDistance) {
this.useDistance = useDistance;
}

public void useDirty(boolean useDirty) {
this.useDirty = useDirty;
}
Expand All @@ -64,6 +66,14 @@ public void gitCommitIDLength(int gitCommitIDLength) {
this.gitCommitIDLength = gitCommitIDLength;
}

public void useMaxVersion(Boolean useMaxVersion) {
this.useMaxVersion = useMaxVersion;
}

public void maxVersionSearchDepth(int maxVersionSearchDepth) {
this.maxVersionSearchDepth = maxVersionSearchDepth;
}

public void nonQualifierBranches(String nonQualifierBranches) {
this.nonQualifierBranches = nonQualifierBranches;
}
Expand Down