Added version extension properties.
This commit is contained in:
parent
17b5ceaaf6
commit
e1891c47af
5 changed files with 78 additions and 10 deletions
26
README.md
26
README.md
|
@ -244,6 +244,32 @@ test.semver=1.0.0
|
||||||
|
|
||||||
- __Examples__: [Java](https://github.com/ethauvin/semver-gradle/tree/master/examples/java), [Kotlin](https://github.com/ethauvin/semver-gradle/tree/master/examples/kotlin)
|
- __Examples__: [Java](https://github.com/ethauvin/semver-gradle/tree/master/examples/java), [Kotlin](https://github.com/ethauvin/semver-gradle/tree/master/examples/kotlin)
|
||||||
|
|
||||||
|
### Semver Extension Properties
|
||||||
|
|
||||||
|
The values stored in the version properties file can individually be accessed using the `semver` extension, for example:
|
||||||
|
|
||||||
|
```gradle
|
||||||
|
fooTask {
|
||||||
|
println "Build: $semver.buildMeta"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The semver extension properties are:
|
||||||
|
|
||||||
|
Property | Description
|
||||||
|
:-------------------------|:----------------------------
|
||||||
|
`semver.semver` | The full semantic version.
|
||||||
|
`semver.version` | Same as `semver.semver`.
|
||||||
|
`semver.major` | The major version.
|
||||||
|
`semver.minor` | The minor version.
|
||||||
|
`semver.patch` | The patch version.
|
||||||
|
`semver.preRelease` | The pre-release version
|
||||||
|
`semver.preReleasePrefix` | The pre-release prefix
|
||||||
|
`semver.buildMeta` | The build metatdata version
|
||||||
|
`semver.buildMetaPrefix` | The build metadata prefix
|
||||||
|
`semver.separator` | The version separator.
|
||||||
|
|
||||||
|
|
||||||
## Source Code Generation
|
## Source Code Generation
|
||||||
|
|
||||||
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).
|
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).
|
||||||
|
|
|
@ -32,7 +32,11 @@
|
||||||
|
|
||||||
package net.thauvin.erik.gradle.semver
|
package net.thauvin.erik.gradle.semver
|
||||||
|
|
||||||
open class SemverConfig {
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
open class SemverConfig @Inject constructor(
|
||||||
|
private val semVersion: Version
|
||||||
|
) {
|
||||||
companion object {
|
companion object {
|
||||||
const val DEFAULT_KEYS_PREFIX = "version."
|
const val DEFAULT_KEYS_PREFIX = "version."
|
||||||
const val DEFAULT_PROPERTIES = "${DEFAULT_KEYS_PREFIX}properties"
|
const val DEFAULT_PROPERTIES = "${DEFAULT_KEYS_PREFIX}properties"
|
||||||
|
@ -48,6 +52,8 @@ open class SemverConfig {
|
||||||
}
|
}
|
||||||
|
|
||||||
var properties = DEFAULT_PROPERTIES
|
var properties = DEFAULT_PROPERTIES
|
||||||
|
|
||||||
|
// Keys
|
||||||
var semverKey = DEFAULT_SEMVER_KEY
|
var semverKey = DEFAULT_SEMVER_KEY
|
||||||
get() = "$keysPrefix$field"
|
get() = "$keysPrefix$field"
|
||||||
var majorKey = DEFAULT_MAJOR_KEY
|
var majorKey = DEFAULT_MAJOR_KEY
|
||||||
|
@ -68,10 +74,32 @@ open class SemverConfig {
|
||||||
get() = "$keysPrefix$field"
|
get() = "$keysPrefix$field"
|
||||||
var keysPrefix = DEFAULT_KEYS_PREFIX
|
var keysPrefix = DEFAULT_KEYS_PREFIX
|
||||||
|
|
||||||
|
// Properties
|
||||||
|
val semver: String
|
||||||
|
get() = semVersion.semver
|
||||||
|
val version: String
|
||||||
|
get() = semVersion.semver
|
||||||
|
val major: Int
|
||||||
|
get() = semVersion.major
|
||||||
|
val minor: Int
|
||||||
|
get() = semVersion.minor
|
||||||
|
val patch: Int
|
||||||
|
get() = semVersion.patch
|
||||||
|
val preRelease: String
|
||||||
|
get() = semVersion.preRelease
|
||||||
|
val buildMeta: String
|
||||||
|
get() = semVersion.buildMeta
|
||||||
|
val preReleasePrefix: String
|
||||||
|
get() = semVersion.preReleasePrefix
|
||||||
|
val buildMetaPrefix: String
|
||||||
|
get() = semVersion.buildMetaPrefix
|
||||||
|
val separator: String
|
||||||
|
get() = semVersion.separator
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return "SemverConfig(" +
|
return "SemverConfig(" +
|
||||||
"properties='$properties', " +
|
"properties='$properties', " +
|
||||||
"semver='$semverKey', " +
|
"semverKey='$semverKey', " +
|
||||||
"majorKey='$majorKey', " +
|
"majorKey='$majorKey', " +
|
||||||
"minorKey='$minorKey', " +
|
"minorKey='$minorKey', " +
|
||||||
"patchKey='$patchKey', " +
|
"patchKey='$patchKey', " +
|
||||||
|
@ -80,7 +108,16 @@ open class SemverConfig {
|
||||||
"buildMetaKey='$buildMetaKey', " +
|
"buildMetaKey='$buildMetaKey', " +
|
||||||
"buildMetaPrefixKey='$buildMetaPrefixKey', " +
|
"buildMetaPrefixKey='$buildMetaPrefixKey', " +
|
||||||
"separator='$separatorKey', " +
|
"separator='$separatorKey', " +
|
||||||
"keysPrefix='$keysPrefix')" +
|
"keysPrefix='$keysPrefix', " +
|
||||||
|
"semver='$semver', " +
|
||||||
|
"major='$major', " +
|
||||||
|
"minor='$minor', " +
|
||||||
|
"patch='$patch', " +
|
||||||
|
"preRelease='$preRelease', " +
|
||||||
|
"buildMeta='$buildMeta', " +
|
||||||
|
"preReleasePrefix='$preReleasePrefix', " +
|
||||||
|
"buildMetaPrefix='$buildMetaPrefix', " +
|
||||||
|
"separator='$separator'" +
|
||||||
')'
|
')'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,7 +54,8 @@ open class SemverIncrementTask @Inject constructor(
|
||||||
version.increment(
|
version.increment(
|
||||||
isMajor = type == SemverConfig.DEFAULT_MAJOR_KEY,
|
isMajor = type == SemverConfig.DEFAULT_MAJOR_KEY,
|
||||||
isMinor = type == SemverConfig.DEFAULT_MINOR_KEY,
|
isMinor = type == SemverConfig.DEFAULT_MINOR_KEY,
|
||||||
isPatch = type == SemverConfig.DEFAULT_PATCH_KEY)
|
isPatch = type == SemverConfig.DEFAULT_PATCH_KEY
|
||||||
|
)
|
||||||
project.version = version.semver
|
project.version = version.semver
|
||||||
logger.lifecycle("Version: ${project.version}")
|
logger.lifecycle("Version: ${project.version}")
|
||||||
Utils.saveProperties(project.projectDir, config, version)
|
Utils.saveProperties(project.projectDir, config, version)
|
||||||
|
|
|
@ -46,7 +46,7 @@ class SemverPlugin : Plugin<Project> {
|
||||||
if (GradleVersion.current() < GradleVersion.version("4.8.1")) {
|
if (GradleVersion.current() < GradleVersion.version("4.8.1")) {
|
||||||
throw GradleException("The $simpleName plugin requires Gradle version 4.8.1 or greater.")
|
throw GradleException("The $simpleName plugin requires Gradle version 4.8.1 or greater.")
|
||||||
}
|
}
|
||||||
config = project.extensions.create("semver", SemverConfig::class.java)
|
config = project.extensions.create("semver", SemverConfig::class.java, version)
|
||||||
project.afterEvaluate(this::afterEvaluate)
|
project.afterEvaluate(this::afterEvaluate)
|
||||||
|
|
||||||
project.tasks.apply {
|
project.tasks.apply {
|
||||||
|
@ -62,18 +62,22 @@ class SemverPlugin : Plugin<Project> {
|
||||||
|
|
||||||
if (project.version != "unspecified") {
|
if (project.version != "unspecified") {
|
||||||
project.logger.warn(
|
project.logger.warn(
|
||||||
"Please specify the version in ${propsFile.name} and remove it from ${project.buildFile.name}")
|
"Please specify the version in ${propsFile.name} and remove it from ${project.buildFile.name}"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
propsFile.apply {
|
propsFile.apply {
|
||||||
val isNew = !exists()
|
val isNew = !exists()
|
||||||
|
|
||||||
project.logger.info(
|
project.logger.info(
|
||||||
"[$simpleName] Attempting to read properties from: `$absoluteFile`. [exists: $isNew, isFile: $isFile, canRead: ${propsFile.canRead()}]")
|
"[$simpleName] Attempting to read properties from: `$absoluteFile`. [exists: $isNew, isFile: $isFile, canRead: ${canRead()}]"
|
||||||
|
)
|
||||||
|
|
||||||
val props = Utils.loadProperties(this)
|
val props = Utils.loadProperties(this)
|
||||||
val requiredProps = setOf(config.semverKey, config.majorKey, config.minorKey, config.patchKey,
|
val requiredProps = setOf(
|
||||||
config.preReleaseKey, config.buildMetaKey)
|
config.semverKey, config.majorKey, config.minorKey, config.patchKey,
|
||||||
|
config.preReleaseKey, config.buildMetaKey
|
||||||
|
)
|
||||||
val hasReqProps = !isNew && props.stringPropertyNames().containsAll(requiredProps) &&
|
val hasReqProps = !isNew && props.stringPropertyNames().containsAll(requiredProps) &&
|
||||||
Utils.isNotSystemProperty(requiredProps)
|
Utils.isNotSystemProperty(requiredProps)
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ import kotlin.test.assertTrue
|
||||||
object SemverConfigSpec : Spek({
|
object SemverConfigSpec : Spek({
|
||||||
Feature("SemverConfig") {
|
Feature("SemverConfig") {
|
||||||
Scenario("Testing configs") {
|
Scenario("Testing configs") {
|
||||||
val config by memoized { SemverConfig() }
|
val config by memoized { SemverConfig(Version()) }
|
||||||
val vars = listOf(
|
val vars = listOf(
|
||||||
config.semverKey,
|
config.semverKey,
|
||||||
config.majorKey,
|
config.majorKey,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue