Added Utils class.

This commit is contained in:
Erik C. Thauvin 2019-04-10 14:12:56 -07:00
parent 16d1a06571
commit 7bf291ac26
5 changed files with 79 additions and 64 deletions

View file

@ -58,7 +58,7 @@ open class SemverIncrementBuildMetaTask @Inject constructor(
version.buildMeta = buildMeta
project.version = version.semver
logger.lifecycle("Version: ${project.version}")
SemverPlugin.saveProperties(config, version)
Utils.saveProperties(config, version)
}
}
}

View file

@ -56,6 +56,6 @@ open class SemverIncrementTask @Inject constructor(
isPatch = type == SemverConfig.DEFAULT_PATCH_KEY)
project.version = version.semver
logger.lifecycle("Version: ${project.version}")
SemverPlugin.saveProperties(config, version)
Utils.saveProperties(config, version)
}
}

View file

@ -37,7 +37,6 @@ import org.gradle.api.Project
import org.gradle.util.GradleVersion
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.util.Properties
class SemverPlugin : Plugin<Project> {
@ -45,47 +44,6 @@ class SemverPlugin : Plugin<Project> {
private var version = Version()
private lateinit var config: SemverConfig
companion object {
fun saveProperties(config: SemverConfig, version: Version) {
val propsFile = File(config.properties)
SortedProperties().apply {
propsFile.apply {
if (canRead() && isFile) {
FileInputStream(this).reader().use { load(it) }
}
}
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 ||
containsKey(config.buildMetaPrefixKey))
put(config.buildMetaPrefixKey, version.buildMetaPrefix)
if (version.preReleasePrefix != Version.DEFAULT_PRERELEASE_PREFIX ||
containsKey(config.preReleasePrefixKey))
put(config.preReleasePrefixKey, version.preReleasePrefix)
if (version.separator != Version.DEFAULT_SEPARATOR || containsKey(config.separatorKey))
put(config.separatorKey, version.separator)
propsFile.apply {
if (!exists()) {
// Need to create the file as canWrite() will not work unless the file exists
createNewFile()
}
if (canWrite()) {
FileOutputStream(this).writer().use {
store(it, "Generated by the Semver Plugin for Gradle")
}
} else {
throw GradleException("Unable to write version to: `$absoluteFile`")
}
}
}
}
}
override fun apply(project: Project) {
if (GradleVersion.current() < GradleVersion.version("4.8.1")) {
throw GradleException("The $simpleName plugin requires Gradle version 4.8.1 or greater.")
@ -118,15 +76,15 @@ class SemverPlugin : Plugin<Project> {
val requiredProps = setOf(config.majorKey, config.minorKey, config.patchKey,
config.preReleaseKey, config.buildMetaKey)
hasReqProps = stringPropertyNames().containsAll(requiredProps) && !hasEnv(requiredProps)
hasReqProps = stringPropertyNames().containsAll(requiredProps) && !Utils.hasEnv(requiredProps)
version.major = loadProperty(this, config.majorKey, Version.DEFAULT_MAJOR)
version.minor = loadProperty(this, config.minorKey, Version.DEFAULT_MINOR)
version.patch = loadProperty(this, config.patchKey, Version.DEFAULT_PATCH)
version.preRelease = loadProperty(this, config.preReleaseKey, Version.DEFAULT_EMPTY)
version.major = Utils.loadProperty(this, config.majorKey, Version.DEFAULT_MAJOR)
version.minor = Utils.loadProperty(this, config.minorKey, Version.DEFAULT_MINOR)
version.patch = Utils.loadProperty(this, config.patchKey, Version.DEFAULT_PATCH)
version.preRelease = Utils.loadProperty(this, config.preReleaseKey, Version.DEFAULT_EMPTY)
version.preReleasePrefix =
getProperty(config.preReleasePrefixKey, Version.DEFAULT_PRERELEASE_PREFIX)
version.buildMeta = loadProperty(this, config.buildMetaKey, Version.DEFAULT_EMPTY)
version.buildMeta = Utils.loadProperty(this, config.buildMetaKey, Version.DEFAULT_EMPTY)
version.buildMetaPrefix =
getProperty(config.buildMetaPrefixKey, Version.DEFAULT_BUILDMETA_PREFIX)
version.separator = getProperty(config.separatorKey, Version.DEFAULT_SEPARATOR)
@ -144,19 +102,8 @@ class SemverPlugin : Plugin<Project> {
if (!hasReqProps || !isFile) {
// If first time running and there is no props file, and the required version properties are missing,
// then version props would never have been saved before
saveProperties(config, version)
Utils.saveProperties(config, version)
}
}
}
private fun hasEnv(keys: Set<String>): Boolean {
keys.forEach {
if (System.getProperties().containsKey(it)) return true
}
return false
}
private fun loadProperty(props: Properties, key: String, default: String): String {
return System.getProperty(key, props.getProperty(key, default))
}
}

View file

@ -0,0 +1,66 @@
package net.thauvin.erik.gradle.semver
import org.gradle.api.GradleException
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.util.Properties
/**
* The <code>Utils</code> class.
*
* @author <a href="https://erik.thauvin.net/" target="_blank">Erik C. Thauvin</a>
* @created 2019-04-10
* @since 1.0
*/
object Utils {
fun hasEnv(keys: Set<String>): Boolean {
keys.forEach {
if (System.getProperties().containsKey(it)) return true
}
return false
}
fun loadProperty(props: Properties, key: String, default: String): String {
return System.getProperty(key, props.getProperty(key, default))
}
fun saveProperties(config: SemverConfig, version: Version) {
val propsFile = File(config.properties)
SortedProperties().apply {
propsFile.apply {
if (canRead() && isFile) {
FileInputStream(this).reader().use { load(it) }
}
}
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 ||
containsKey(config.buildMetaPrefixKey))
put(config.buildMetaPrefixKey, version.buildMetaPrefix)
if (version.preReleasePrefix != Version.DEFAULT_PRERELEASE_PREFIX ||
containsKey(config.preReleasePrefixKey))
put(config.preReleasePrefixKey, version.preReleasePrefix)
if (version.separator != Version.DEFAULT_SEPARATOR || containsKey(config.separatorKey))
put(config.separatorKey, version.separator)
propsFile.apply {
if (!exists()) {
// Need to create the file as canWrite() will not work unless the file exists
createNewFile()
}
if (canWrite()) {
FileOutputStream(this).writer().use {
store(it, "Generated by the Semver Plugin for Gradle")
}
} else {
throw GradleException("Unable to write version to: `$absoluteFile`")
}
}
}
}
}

View file

@ -47,11 +47,13 @@ object SemverPluginSpec : Spek({
val config by memoized { SemverConfig() }
val configFile = File("test.properties")
config.properties = configFile.name
before {
config.properties = configFile.name
}
describe("test save properties") {
it("should save properties") {
SemverPlugin.saveProperties(config, version)
Utils.saveProperties(config, version)
assertTrue(configFile.exists())
}
}