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

Add option to configure via semverGit {} block #28

Open
wants to merge 6 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
3 changes: 0 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ group = 'com.cinnober.gradle'

test {
useJUnitPlatform()
retry {
maxRetries = 5
}
deepy marked this conversation as resolved.
Show resolved Hide resolved
}

// -- Maven Central --
Expand Down
42 changes: 33 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ Version numbers must follow [Semantic Versioning 2.0.0](http://semver.org/spec/v
In your `build.gradle` file:

plugins {
id "com.cinnober.gradle.semver-git" version "2.2.0" apply false
id "com.cinnober.gradle.semver-git" version "3.1.0"
}

semverGit {
// All of the following are optional:
nextVersion = "major", "minor" (default), "patch" or e.g. "3.0.0-rc2"
snapshotSuffix = "SNAPSHOT" (default) or a pattern, e.g. "<count>.g<sha><dirty>-SNAPSHOT"
dirtyMarker = "-dirty" (default) replaces <dirty> in snapshotSuffix
gitDescribeArgs = '--match *[0-9].[0-9]*.[0-9]*' (default) or other arguments for git describe.
prefix = 'v' if your tags are named like v3.0.0
}
// optionally: ext.nextVersion = "major", "minor" (default), "patch" or e.g. "3.0.0-rc2"
// optionally: ext.snapshotSuffix = "SNAPSHOT" (default) or a pattern, e.g. "<count>.g<sha><dirty>-SNAPSHOT"
// optionally: ext.dirtyMarker = "-dirty" (default) replaces <dirty> in snapshotSuffix
// optionally: ext.gitDescribeArgs = '--match *[0-9].[0-9]*.[0-9]*' (default) or other arguments for git describe.
// optionally: ext.semverPrefix = 'v' if your tags are named like v3.0.0
apply plugin: 'com.cinnober.gradle.semver-git'

Note: Use `apply false` combined with a manual apply if you want to change `nextVersion` and `snapshotSuffix`.

Then everything should just work. To create a release, create an
annotated git tag, e.g.:
Expand All @@ -41,6 +42,29 @@ For example the snapshot suffix pattern is `<count>.g<sha>-SNAPSHOT`
which will generate the version `1.1.0-5.g5242341-SNAPSHOT`, which is
a unique snapshot version.


### Legacy config ###

Plugin versions 3.0.0 and earlier used a different config method,
which required setting the configuration before applying the plugin:

plugins {
id "com.cinnober.gradle.semver-git" version "2.2.0" apply false
}
// optionally: ext.nextVersion = "major", "minor" (default), "patch" or e.g. "3.0.0-rc2"
// optionally: ext.snapshotSuffix = "SNAPSHOT" (default) or a pattern, e.g. "<count>.g<sha><dirty>-SNAPSHOT"
// optionally: ext.dirtyMarker = "-dirty" (default) replaces <dirty> in snapshotSuffix
// optionally: ext.gitDescribeArgs = '--match *[0-9].[0-9]*.[0-9]*' (default) or other arguments for git describe.
// optionally: ext.semverPrefix = 'v' if your tags are named like v3.0.0
apply plugin: 'com.cinnober.gradle.semver-git'

Note: This configuration method requires `apply false` combined with a manual apply.

Both legacy config and the newer `semverGit {}` config block may be
used simultaneously. Any non-null setting defined using both methods
will use the value from the `semverGit {}` block.


### Release ###

Versions are stored as annotated tags in git. [Semantic versioning](http://semver.org) is used.
Expand Down
130 changes: 118 additions & 12 deletions src/main/groovy/com/cinnober/gradle/semver_git/SemverGitPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,110 @@

package com.cinnober.gradle.semver_git

import org.gradle.api.Project
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.provider.Property

interface SemverGitPluginExtension {
/**
* One of <code>"minor"</code> (default) or <code>"major"</code> to bump the respective SemVer part,
* or a literal value such as <code>"3.0.0-rc2"</code> to use as the next version.
*/
Property<String> getNextVersion()

/**
* Pattern for the suffix to append when the current commit is not version tagged.
*
* <p>
* Example: <code>"&lt;count&gt;.g&lt;sha&gt;&lt;dirty&gt;-SNAPSHOT"</code>.
* </p>
*
* <p>
* Pattern may include the following placeholders:
* </p>
*
* <ul>
* <li><code>&lt;count&gt;</code>: Number of commits (including this) since the last version tag.</li>
* <li><code>&lt;sha&gt;</code>: Abbreviated ID of the current commit.</li>
* <li><code>&lt;dirty&gt;</code>: The value of {@link #getDirtyMarker() dirtyMarker} if there are unstaged changes,
* otherwise empty. Note that untracked files do not count.</li>
* </ul>
*
* <p>
* Default: <code>"SNAPSHOT"</code>
* </p>
*/
Property<String> getSnapshotSuffix()

/**
* The value to substitute for <code>&lt;dirty&gt;</code> in {@link #getSnapshotSuffix() snapshotSuffix}
* if there are unstaged changes.
*
* <p>
* Note that this has no effect if {@link #getSnapshotSuffix() snapshotSuffix}
* does not include a <code>&lt;dirty&gt;</code> placeholder.
* </p>
*
* <p>
* Default: <code>"-dirty"</code>
* </p>
*/
Property<String> getDirtyMarker()

/**
* Additional arguments for the <code>git describe</code> subprocess.
*
* <p>
* Default: <code>"--match *[0-9].[0-9]*.[0-9]*"</code>
* </p>
*/
Property<String> getGitDescribeArgs()

/**
* A prefix that may be stripped from tag names to create a version number.
*
* <p>
* For example: if your tags are named like <code>v3.0.0</code>, set this to <code>"v"</code>.
* </p>
*
* <p>
* Default: null
* </p>
*/
Property<String> getPrefix()
}

class DeferredVersion {
private final File projectDir;
private final SemverGitPluginExtension extension;
private String version = null;

DeferredVersion(File projectDir, SemverGitPluginExtension extension) {
this.projectDir = projectDir;
this.extension = extension;
}

private synchronized String evaluate() {
if (version == null) {
version = SemverGitPlugin.getGitVersion(
this.extension.nextVersion.getOrNull(),
this.extension.snapshotSuffix.getOrNull(),
this.extension.dirtyMarker.getOrNull(),
this.extension.gitDescribeArgs.getOrNull(),
this.extension.prefix.getOrNull(),
this.projectDir
)
}
return version;
}

@Override
// Gradle uses the toString() of the version object,
// see: https://docs.gradle.org/current/javadoc/org/gradle/api/Project.html#getVersion--
public String toString() {
return evaluate();
}
}

class SemverGitPlugin implements Plugin<Project> {

Expand Down Expand Up @@ -113,27 +215,31 @@ class SemverGitPlugin implements Plugin<Project> {
}

void apply(Project project) {
def nextVersion = "minor"
def snapshotSuffix = "SNAPSHOT"
def dirtyMarker = "-dirty"
def gitDescribeArgs = '--match *[0-9].[0-9]*.[0-9]*'
String semverPrefix = null
def extension = project.extensions.create("semverGit", SemverGitPluginExtension)
extension.nextVersion.convention("minor").finalizeValueOnRead()
extension.snapshotSuffix.convention("SNAPSHOT").finalizeValueOnRead()
extension.dirtyMarker.convention("-dirty").finalizeValueOnRead()
extension.gitDescribeArgs.convention('--match *[0-9].[0-9]*.[0-9]*').finalizeValueOnRead()
extension.prefix.convention(null).finalizeValueOnRead()

if (project.ext.properties.containsKey("nextVersion")) {
nextVersion = project.ext.nextVersion
extension.nextVersion.set(project.ext.nextVersion)
}
if (project.ext.properties.containsKey("snapshotSuffix")) {
snapshotSuffix = project.ext.snapshotSuffix
extension.snapshotSuffix.set(project.ext.snapshotSuffix)
}
if (project.ext.properties.containsKey("gitDescribeArgs")) {
gitDescribeArgs = project.ext.gitDescribeArgs
extension.gitDescribeArgs.set(project.ext.gitDescribeArgs)
}
if (project.ext.properties.containsKey("dirtyMarker")) {
dirtyMarker = project.ext.dirtyMarker
extension.dirtyMarker.set(project.ext.dirtyMarker)
}
if (project.ext.properties.containsKey("semverPrefix")) {
semverPrefix = project.ext.semverPrefix
extension.prefix.set(project.ext.semverPrefix)
}
project.version = getGitVersion(nextVersion, snapshotSuffix, dirtyMarker, gitDescribeArgs, semverPrefix, project.projectDir)

project.version = new DeferredVersion(project.projectDir, extension)

project.tasks.register('showVersion') {
group = 'Help'
description = 'Show the project version'
Expand Down
Loading