Added increment build metadata task.

This commit is contained in:
Erik C. Thauvin 2018-07-13 15:19:05 -07:00
parent edae9cc660
commit a4e47cef0b
3 changed files with 119 additions and 1 deletions

View file

@ -31,6 +31,8 @@ If you need to change the properties file or name of the properties keys to matc
## Increment Version Tasks
### incrementMajor, incrementMinor and incrementPatch
The `incrementMajor`, `incrementMinor` and `incrementPatch` tasks are available to automatically increment their respective and reset lower counterpart version numbers.
- `incrementMajor` will increment the `major` and set the `minor` and `patch` versions to `0`.
@ -53,6 +55,52 @@ someTask {
- __Examples__: [Java](https://github.com/ethauvin/semver-gradle/tree/master/examples/java), [Kotlin](https://github.com/ethauvin/semver-gradle/tree/master/examples/kotlin)
### incrementBuildMeta
The `incrementBuildMeta` task is available to set the build metadata version to a custom calculated value.
#### Examples:
To set the build metadata to a custom formatted date:
```gradle
incrementBuildMeta {
doFirst {
buildMeta = new Date().format("yyyyMMddHHmmss")
}
}
```
```bash
./gradlew incrementBuildMeta
...
> Task :incrementBuildMeta
Version: 1.0.16-beta+20180713143416
```
Or to set the build metadata to a custom formatted number with increment:
```gradle
incrementBuildMeta {
doFirst {
buildMeta = sprintf("%03d", (buildMeta as Integer) + 1)
}
}
```
```bash
./gradlew incrementBuildMeta
...
> Task :incrementBuildMeta
Version: 1.0.16-beta+002
```
## Configuration
### Version File Properties
@ -165,4 +213,4 @@ test.buildmeta=
If you'd like to incorporate the version number data into your source code, please have a look at the [__Semantic Version Annotation Processor__](https://github.com/ethauvin/semver).
There are also full [examples](https://github.com/ethauvin/semver-gradle/tree/master/examples/annotation-processor) in both [Java](https://github.com/ethauvin/semver-gradle/tree/master/examples/annotation-processor/java) and [Kotlin](https://github.com/ethauvin/semver-gradle/tree/master/examples/annotation-processor) showing how to use both the plugin and annotation processor concurrently.
There are also full [examples](https://github.com/ethauvin/semver-gradle/tree/master/examples/annotation-processor) in both [Java](https://github.com/ethauvin/semver-gradle/tree/master/examples/annotation-processor/java) and [Kotlin](https://github.com/ethauvin/semver-gradle/tree/master/examples/annotation-processor) showing how to use both the plugin and annotation processor concurrently.

View file

@ -0,0 +1,64 @@
/*
* SemverIncrementBuildmetaTask.kt
*
* Copyright (c) 2018, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of this project nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.thauvin.erik.gradle.semver
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.CacheableTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
import javax.inject.Inject
@CacheableTask
open class SemverIncrementBuildMetaTask @Inject constructor(
private val config: SemverConfig,
private val version: Version
) : DefaultTask() {
init {
group = "version"
description = "Increments Build Metadata version number."
}
@Suppress("MemberVisibilityCanBePrivate")
@Input
var buildMeta: String = ""
@Suppress("unused")
@TaskAction
fun increment() {
if (version.buildMeta != buildMeta) {
version.buildMeta = buildMeta
project.version = version.semver
logger.lifecycle("Version: ${project.version}")
SemverPlugin.saveProperties(config, version)
}
}
}

View file

@ -77,6 +77,7 @@ class SemverPlugin : Plugin<Project> {
create("incrementMajor", SemverIncrementTask::class.java, config, version, SemverConfig.DEFAULT_MAJOR_KEY)
create("incrementMinor", SemverIncrementTask::class.java, config, version, SemverConfig.DEFAULT_MINOR_KEY)
create("incrementPatch", SemverIncrementTask::class.java, config, version, SemverConfig.DEFAULT_PATCH_KEY)
create("incrementBuildMeta", SemverIncrementBuildMetaTask::class.java, config, version)
}
}
@ -93,6 +94,7 @@ class SemverPlugin : Plugin<Project> {
FileInputStream(this).reader().use { reader ->
Properties().apply {
load(reader)
version.major = getProperty(config.majorKey, Version.DEFAULT_MAJOR)
version.minor = getProperty(config.minorKey, Version.DEFAULT_MINOR)
version.patch = getProperty(config.patchKey, Version.DEFAULT_PATCH)
@ -103,6 +105,10 @@ class SemverPlugin : Plugin<Project> {
version.buildMetaPrefix =
getProperty(config.buildMetaPrefixKey, Version.DEFAULT_BUILDMETA_PREFIX)
version.separator = getProperty(config.separatorKey, Version.DEFAULT_SEPARATOR)
project.tasks.withType(SemverIncrementBuildMetaTask::class.java) {
buildMeta = version.buildMeta
}
}
}
} else if (exists()) {