-
Notifications
You must be signed in to change notification settings - Fork 15
Add option to configure via semverGit {} block #28
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
base: master
Are you sure you want to change the base?
Changes from 5 commits
a8186d5
cdd4858
9679d20
ea69b45
deeb241
15d3511
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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>"<count>.g<sha><dirty>-SNAPSHOT"</code>. | ||
| * </p> | ||
| * | ||
| * <p> | ||
| * Pattern may include the following placeholders: | ||
| * </p> | ||
| * | ||
| * <ul> | ||
| * <li><code><count></code>: Number of commits (including this) since the last version tag.</li> | ||
| * <li><code><sha></code>: Abbreviated ID of the current commit.</li> | ||
| * <li><code><dirty></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><dirty></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><dirty></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 Project project; | ||
| private final SemverGitPluginExtension extension; | ||
| private String version = null; | ||
|
|
||
| DeferredVersion(Project project, SemverGitPluginExtension extension) { | ||
| this.project = project; | ||
| 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.project.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> { | ||
|
|
||
|
|
@@ -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, extension) | ||
|
|
||
| project.tasks.register('showVersion') { | ||
| group = 'Help' | ||
| description = 'Show the project version' | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.