First commit.

This commit is contained in:
Erik C. Thauvin 2018-06-29 20:21:27 -07:00
commit 17c072b192
39 changed files with 1817 additions and 0 deletions

View file

@ -0,0 +1,72 @@
/*
* SemverConfig.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
open class SemverConfig {
companion object {
const val DEFAULT_KEY_PREFIX = "version."
const val DEFAULT_PROPERTIES = "${DEFAULT_KEY_PREFIX}properties"
const val DEFAULT_MAJOR_KEY = "${DEFAULT_KEY_PREFIX}major"
const val DEFAULT_MINOR_KEY = "${DEFAULT_KEY_PREFIX}minor"
const val DEFAULT_PATCH_KEY = "${DEFAULT_KEY_PREFIX}patch"
const val DEFAULT_PRERELEASE_KEY = "${DEFAULT_KEY_PREFIX}prerelease"
const val DEFAULT_PRERELEASE_PREFIX_KEY = "${DEFAULT_KEY_PREFIX}prerelease.prefix"
const val DEFAULT_BUILDMETA_KEY = "${DEFAULT_KEY_PREFIX}buildmeta"
const val DEFAULT_BUILDMETA_PREFIX_KEY = "${DEFAULT_KEY_PREFIX}buildmeta.prefix"
const val DEFAULT_SEPARATOR = "${DEFAULT_KEY_PREFIX}separator"
}
var properties = DEFAULT_PROPERTIES
var majorKey = DEFAULT_MAJOR_KEY
var minorKey = DEFAULT_MINOR_KEY
var patchKey = DEFAULT_PATCH_KEY
var preReleaseKey = DEFAULT_PRERELEASE_KEY
var preReleasePrefixKey = DEFAULT_PRERELEASE_PREFIX_KEY
var buildMetaKey = DEFAULT_BUILDMETA_KEY
var buildMetaPrefixKey = DEFAULT_BUILDMETA_PREFIX_KEY
var separatorKey = DEFAULT_SEPARATOR
@Suppress("unused")
fun setKeysPrefix(keyPrefix: String) {
if (keyPrefix.isNotBlank()) {
majorKey = keyPrefix + majorKey.removePrefix(DEFAULT_KEY_PREFIX)
minorKey = keyPrefix + minorKey.removePrefix(DEFAULT_KEY_PREFIX)
patchKey = keyPrefix + patchKey.removePrefix(DEFAULT_KEY_PREFIX)
preReleaseKey = keyPrefix + preReleaseKey.removePrefix(DEFAULT_KEY_PREFIX)
preReleasePrefixKey = keyPrefix + preReleasePrefixKey.removePrefix(DEFAULT_KEY_PREFIX)
buildMetaKey = keyPrefix + buildMetaKey.removePrefix(DEFAULT_KEY_PREFIX)
buildMetaPrefixKey = keyPrefix + buildMetaPrefixKey.removePrefix(DEFAULT_KEY_PREFIX)
separatorKey = keyPrefix + separatorKey.removePrefix(DEFAULT_KEY_PREFIX)
}
}
}

View file

@ -0,0 +1,62 @@
/*
* SemverIncrementTask.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 net.thauvin.erik.gradle.semver.SemverPlugin.Types
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
open class SemverIncrementTask : DefaultTask() {
init {
group = "version"
}
@Input
lateinit var config: SemverConfig
@Input
lateinit var version: Version
@Input
lateinit var type: Types
@Suppress("unused")
@TaskAction
fun increment() {
version.increment(isMajor = type == Types.MAJOR, isMinor = type == Types.MINOR, isPatch = type == Types.PATCH)
project.version = version.semver
logger.warn("Version: ${project.version}")
SemverPlugin.saveProperties(config, version)
}
}

View file

@ -0,0 +1,124 @@
/*
* SemverPlugin.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.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.util.*
class SemverPlugin : Plugin<Project> {
private var version = Version()
private lateinit var config: SemverConfig
enum class Types {
MAJOR, MINOR, PATCH
}
companion object {
fun saveProperties(config: SemverConfig, version: Version) {
Properties().apply {
put(config.majorKey, version.major)
put(config.minorKey, version.minor)
put(config.patchKey, version.patch)
put(config.preReleaseKey, version.preRelease)
put(config.buildMetaKey, version.buildMeta)
if (version.buildMetaPrefix != Version.DEFAULT_BUILDMETA_PREFIX)
put(config.buildMetaPrefixKey, version.buildMetaPrefix)
if (version.preReleasePrefix != Version.DEFAULT_PRERELEASE_PREFIX)
put(config.preReleasePrefixKey, version.preReleasePrefix)
if (version.separator != Version.DEFAULT_SEPARATOR)
put(config.separatorKey, version.separator)
FileOutputStream(config.properties).use { output ->
store(output, "Generated by the Semver Plugin for Gradle")
}
}
}
}
override fun apply(project: Project?) {
project!!.afterEvaluate(this::afterEvaluate)
config = project.extensions.create("semver", SemverConfig::class.java)
project.tasks.create("incrementMajor", SemverIncrementTask::class.java) {
description = "Increments Major version number."
config = this@SemverPlugin.config
version = this@SemverPlugin.version
type = Types.MAJOR
}
project.tasks.create("incrementMinor", SemverIncrementTask::class.java) {
description = "Increments Minor version number."
config = this@SemverPlugin.config
version = this@SemverPlugin.version
type = Types.MINOR
}
project.tasks.create("incrementPatch", SemverIncrementTask::class.java) {
description = "Increments Patch version number."
config = this@SemverPlugin.config
version = this@SemverPlugin.version
type = Types.PATCH
}
}
private fun afterEvaluate(project: Project) {
if (project.version != "unspecified") {
project.logger.warn("Please specify the version in ${config.properties} and remove it from ${project.buildFile.name}")
}
File(config.properties).apply {
if (canRead()) {
FileInputStream(this).use { fis ->
Properties().apply {
load(fis)
version.major = getProperty(config.majorKey, Version.DEFAULT_MAJOR)
version.minor = getProperty(config.minorKey, Version.DEFAULT_MINOR)
version.patch = getProperty(config.patchKey, Version.DEFAULT_PATCH)
version.preRelease = getProperty(config.preReleaseKey, Version.DEFAULT_EMPTY)
version.preReleasePrefix = getProperty(config.preReleasePrefixKey, Version.DEFAULT_PRERELEASE_PREFIX)
version.buildMeta = getProperty(config.buildMetaKey, Version.DEFAULT_EMPTY)
version.buildMetaPrefix = getProperty(config.buildMetaPrefixKey, Version.DEFAULT_BUILDMETA_PREFIX)
version.separator = getProperty(config.separatorKey, Version.DEFAULT_SEPARATOR)
project.version = version.semver
}
}
} else if (exists()) {
throw GradleException("Unable to read version from ${config.properties}")
}
saveProperties(config, version)
}
}
}

View file

@ -0,0 +1,64 @@
/*
* Version.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
class Version {
companion object {
const val DEFAULT_MAJOR: String = "1"
const val DEFAULT_MINOR: String = "0"
const val DEFAULT_PATCH: String = "0"
const val DEFAULT_EMPTY: String = ""
const val DEFAULT_PRERELEASE_PREFIX = "-"
const val DEFAULT_BUILDMETA_PREFIX = "+"
const val DEFAULT_SEPARATOR = "."
}
var major = DEFAULT_MAJOR
var minor = DEFAULT_MINOR
var patch = DEFAULT_PATCH
var preRelease = DEFAULT_EMPTY
var preReleasePrefix = DEFAULT_PRERELEASE_PREFIX
var buildMeta = DEFAULT_EMPTY
var buildMetaPrefix = DEFAULT_BUILDMETA_PREFIX
var separator = DEFAULT_SEPARATOR
val semver: String
get()= "$major$separator$minor$separator$patch" +
(if (preRelease.isNotEmpty()) "$preReleasePrefix$preRelease" else "") +
(if (buildMeta.isNotEmpty()) "$buildMetaPrefix$buildMeta" else "")
fun increment(isMajor: Boolean = false, isMinor: Boolean = false, isPatch: Boolean = false) {
if (isMajor) major = (major.toInt() + 1).toString()
if (isMinor) minor = (minor.toInt() + 1).toString()
if (isPatch) patch = (patch.toInt() + 1).toString()
}
}

View file

@ -0,0 +1,95 @@
/*
* SemverConfigSpec.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.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.given
import org.jetbrains.spek.api.dsl.it
import org.jetbrains.spek.api.dsl.on
import kotlin.test.assertTrue
@Suppress("unused")
object SemverConfigSpec : Spek({
describe("config test") {
given("a config") {
val config = SemverConfig()
val vars = listOf(
config.properties,
config.majorKey,
config.minorKey,
config.patchKey,
config.preReleaseKey,
config.preReleasePrefixKey,
config.buildMetaKey,
config.buildMetaPrefixKey,
config.separatorKey
)
val defaults = listOf(
SemverConfig.DEFAULT_PROPERTIES,
SemverConfig.DEFAULT_MAJOR_KEY,
SemverConfig.DEFAULT_MINOR_KEY,
SemverConfig.DEFAULT_PATCH_KEY,
SemverConfig.DEFAULT_PRERELEASE_KEY,
SemverConfig.DEFAULT_PRERELEASE_PREFIX_KEY,
SemverConfig.DEFAULT_BUILDMETA_KEY,
SemverConfig.DEFAULT_BUILDMETA_PREFIX_KEY,
SemverConfig.DEFAULT_SEPARATOR
)
on("defaults") {
defaults.forEachIndexed { i, d ->
it("should be the same: ${vars[i]}, $d") {
assertTrue(vars[i] == d)
}
}
}
on("set keys to test.xxx") {
config.setKeysPrefix("test.")
val keys = listOf(
config.majorKey,
config.minorKey,
config.patchKey,
config.preReleaseKey,
config.preReleasePrefixKey,
config.buildMetaKey,
config.buildMetaPrefixKey,
config.separatorKey)
keys.forEach { k ->
it("should all start with test.xxx: $k") {
assertTrue(k.startsWith("test."))
}
}
}
}
}
})

View file

@ -0,0 +1,110 @@
/*
* SemverVersionSpec.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.jetbrains.spek.api.Spek
import org.jetbrains.spek.api.dsl.describe
import org.jetbrains.spek.api.dsl.given
import org.jetbrains.spek.api.dsl.it
import org.jetbrains.spek.api.dsl.on
import kotlin.test.assertEquals
@Suppress("unused")
object SemverVersionSpec : Spek({
describe("version test") {
given("a version") {
val version = Version()
on("increment major") {
version.increment(isMajor = true)
it("should return 2.0.0") {
assertEquals("2.0.0", version.semver)
}
}
on("increment minor") {
version.increment(isMinor = true)
it("should return 2.1.0") {
assertEquals("2.1.0", version.semver)
}
}
on("increment patch") {
version.increment(isPatch = true)
it("should return 2.1.1") {
assertEquals("2.1.1", version.semver)
}
}
on("increment all") {
version.increment(true, true, true)
it("should rerturn 3.2.2") {
assertEquals("3.2.2", version.semver)
}
}
on("reset version") {
version.major = "1"
version.minor = "0"
version.patch = "0"
it("should return 1.0.0") {
assertEquals("1.0.0", version.semver)
}
}
on("add prerelease") {
version.preRelease = "beta"
it("should return 1.0.0-beta") {
assertEquals("1.0.0-beta", version.semver)
}
}
on("add metadata") {
version.buildMeta = "007"
it("should return 1.0.0-beta+007") {
assertEquals("1.0.0-beta+007", version.semver)
}
}
on("change prerelease prefix") {
version.preReleasePrefix = "--"
it("should return 1.0.0--beta+007") {
assertEquals("1.0.0--beta+007", version.semver)
}
}
on("change prerelease prefix") {
version.buildMetaPrefix = "++"
it("should return 1.0.0--beta+=007") {
assertEquals("1.0.0--beta++007", version.semver)
}
}
on("change sperator") {
version.separator = "-"
it("should return 1-0-0--beta+007") {
assertEquals("1-0-0--beta+007", version.semver)
}
}
}
}
})