diff --git a/src/main/kotlin/net/thauvin/erik/gradle/semver/SemverIncrementBuildMetaTask.kt b/src/main/kotlin/net/thauvin/erik/gradle/semver/SemverIncrementBuildMetaTask.kt index f2a58e0..e4555d0 100644 --- a/src/main/kotlin/net/thauvin/erik/gradle/semver/SemverIncrementBuildMetaTask.kt +++ b/src/main/kotlin/net/thauvin/erik/gradle/semver/SemverIncrementBuildMetaTask.kt @@ -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) } } } diff --git a/src/main/kotlin/net/thauvin/erik/gradle/semver/SemverIncrementTask.kt b/src/main/kotlin/net/thauvin/erik/gradle/semver/SemverIncrementTask.kt index 7966976..d7cb7ac 100644 --- a/src/main/kotlin/net/thauvin/erik/gradle/semver/SemverIncrementTask.kt +++ b/src/main/kotlin/net/thauvin/erik/gradle/semver/SemverIncrementTask.kt @@ -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) } } diff --git a/src/main/kotlin/net/thauvin/erik/gradle/semver/SemverPlugin.kt b/src/main/kotlin/net/thauvin/erik/gradle/semver/SemverPlugin.kt index 0adb8dc..11f6fa7 100644 --- a/src/main/kotlin/net/thauvin/erik/gradle/semver/SemverPlugin.kt +++ b/src/main/kotlin/net/thauvin/erik/gradle/semver/SemverPlugin.kt @@ -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 { @@ -45,47 +44,6 @@ class SemverPlugin : Plugin { 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 { 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 { 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): 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)) - } } diff --git a/src/main/kotlin/net/thauvin/erik/gradle/semver/Utils.kt b/src/main/kotlin/net/thauvin/erik/gradle/semver/Utils.kt new file mode 100644 index 0000000..cdbca54 --- /dev/null +++ b/src/main/kotlin/net/thauvin/erik/gradle/semver/Utils.kt @@ -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 Utils class. + * + * @author Erik C. Thauvin + * @created 2019-04-10 + * @since 1.0 + */ +object Utils { + fun hasEnv(keys: Set): 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`") + } + } + } + } +} diff --git a/src/test/kotlin/net/thauvin/erik/gradle/semver/SemverPluginSpec.kt b/src/test/kotlin/net/thauvin/erik/gradle/semver/UtilsSpec.kt similarity index 96% rename from src/test/kotlin/net/thauvin/erik/gradle/semver/SemverPluginSpec.kt rename to src/test/kotlin/net/thauvin/erik/gradle/semver/UtilsSpec.kt index d3ab508..8f882dd 100644 --- a/src/test/kotlin/net/thauvin/erik/gradle/semver/SemverPluginSpec.kt +++ b/src/test/kotlin/net/thauvin/erik/gradle/semver/UtilsSpec.kt @@ -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()) } }