Skip to content

Commit

Permalink
Added version extension properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
ethauvin committed Jul 25, 2019
1 parent 17b5cea commit e1891c4
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 10 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

### 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

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).
Expand Down
43 changes: 40 additions & 3 deletions src/main/kotlin/net/thauvin/erik/gradle/semver/SemverConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@

package net.thauvin.erik.gradle.semver

open class SemverConfig {
import javax.inject.Inject

open class SemverConfig @Inject constructor(
private val semVersion: Version
) {
companion object {
const val DEFAULT_KEYS_PREFIX = "version."
const val DEFAULT_PROPERTIES = "${DEFAULT_KEYS_PREFIX}properties"
Expand All @@ -48,6 +52,8 @@ open class SemverConfig {
}

var properties = DEFAULT_PROPERTIES

// Keys
var semverKey = DEFAULT_SEMVER_KEY
get() = "$keysPrefix$field"
var majorKey = DEFAULT_MAJOR_KEY
Expand All @@ -68,10 +74,32 @@ open class SemverConfig {
get() = "$keysPrefix$field"
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 {
return "SemverConfig(" +
"properties='$properties', " +
"semver='$semverKey', " +
"semverKey='$semverKey', " +
"majorKey='$majorKey', " +
"minorKey='$minorKey', " +
"patchKey='$patchKey', " +
Expand All @@ -80,7 +108,16 @@ open class SemverConfig {
"buildMetaKey='$buildMetaKey', " +
"buildMetaPrefixKey='$buildMetaPrefixKey', " +
"separator='$separatorKey', " +
"keysPrefix='$keysPrefix')" +
"keysPrefix='$keysPrefix', " +
"semver='$semver', " +
"major='$major', " +
"minor='$minor', " +
"patch='$patch', " +
"preRelease='$preRelease', " +
"buildMeta='$buildMeta', " +
"preReleasePrefix='$preReleasePrefix', " +
"buildMetaPrefix='$buildMetaPrefix', " +
"separator='$separator'" +
')'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ open class SemverIncrementTask @Inject constructor(
version.increment(
isMajor = type == SemverConfig.DEFAULT_MAJOR_KEY,
isMinor = type == SemverConfig.DEFAULT_MINOR_KEY,
isPatch = type == SemverConfig.DEFAULT_PATCH_KEY)
isPatch = type == SemverConfig.DEFAULT_PATCH_KEY
)
project.version = version.semver
logger.lifecycle("Version: ${project.version}")
Utils.saveProperties(project.projectDir, config, version)
Expand Down
14 changes: 9 additions & 5 deletions src/main/kotlin/net/thauvin/erik/gradle/semver/SemverPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class SemverPlugin : Plugin<Project> {
if (GradleVersion.current() < GradleVersion.version("4.8.1")) {
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.tasks.apply {
Expand All @@ -62,18 +62,22 @@ class SemverPlugin : Plugin<Project> {

if (project.version != "unspecified") {
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 {
val isNew = !exists()

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 requiredProps = setOf(config.semverKey, config.majorKey, config.minorKey, config.patchKey,
config.preReleaseKey, config.buildMetaKey)
val requiredProps = setOf(
config.semverKey, config.majorKey, config.minorKey, config.patchKey,
config.preReleaseKey, config.buildMetaKey
)
val hasReqProps = !isNew && props.stringPropertyNames().containsAll(requiredProps) &&
Utils.isNotSystemProperty(requiredProps)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import kotlin.test.assertTrue
object SemverConfigSpec : Spek({
Feature("SemverConfig") {
Scenario("Testing configs") {
val config by memoized { SemverConfig() }
val config by memoized { SemverConfig(Version()) }
val vars = listOf(
config.semverKey,
config.majorKey,
Expand Down

0 comments on commit e1891c4

Please sign in to comment.