Impletemented Gradle project directory-aware getPropertiesFile() function for all loading/saving.
This commit is contained in:
Erik C. Thauvin 2019-05-24 09:52:34 -07:00
parent ffda5e3ad4
commit fa23243ff5
5 changed files with 78 additions and 44 deletions

View file

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

View file

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

View file

@ -36,7 +36,6 @@ import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.util.GradleVersion
import java.io.File
class SemverPlugin : Plugin<Project> {
private val simpleName = SemverPlugin::class.simpleName
@ -59,11 +58,7 @@ class SemverPlugin : Plugin<Project> {
}
private fun afterEvaluate(project: Project) {
val propsFile = if (File(config.properties).isAbsolute) {
File(config.properties)
} else {
File("${project.projectDir}${File.separator}${config.properties}")
}
val propsFile = Utils.getPropertiesFile(project.projectDir, config.properties)
if (project.version != "unspecified") {
project.logger.warn(
@ -93,7 +88,7 @@ class SemverPlugin : Plugin<Project> {
if (!hasReqProps || !isFile) {
project.logger.info("[$simpleName] Saving version properties to `$absoluteFile`.")
Utils.saveProperties(config, version)
Utils.saveProperties(project.projectDir, config, version)
}
}
}

View file

@ -66,6 +66,14 @@ object Utils {
return true
}
fun getPropertiesFile(projectDir: File, propsFile: String): File {
return if (File(propsFile).isAbsolute) {
File(propsFile)
} else {
File(projectDir, propsFile)
}
}
fun loadProperties(file: File): Properties {
var isNew = false
val props = Properties()
@ -197,9 +205,10 @@ object Utils {
return true
}
fun saveProperties(config: SemverConfig, version: Version) {
val propsFile = File(config.properties)
fun saveProperties(projectDir: File, config: SemverConfig, version: Version) {
val propsFile = getPropertiesFile(projectDir, config.properties)
SortedProperties().apply {
try {
propsFile.apply {
if (canReadFile()) {
FileInputStream(this).reader().use { load(it) }
@ -230,9 +239,12 @@ object Utils {
store(it, "Generated by the Semver Plugin for Gradle")
}
} else {
throw GradleException("Unable to write version to: `$absoluteFile`")
throw GradleException()
}
}
} catch (e: Exception) {
throw GradleException("Unable to write version to: `${propsFile.absoluteFile}`")
}
}
}
}

View file

@ -48,12 +48,13 @@ object UtilsSpec : Spek({
val version = Version()
val config = SemverConfig()
val propsFile = File("test.properties")
val projectDir = File("./")
lateinit var props: Properties
Scenario("Save/Load Properties") {
When("saving the property") {
config.properties = propsFile.name
Utils.saveProperties(config, version)
Utils.saveProperties(projectDir, config, version)
}
Then("properties file should exists and be readable") {
@ -115,7 +116,7 @@ object UtilsSpec : Spek({
}
When("saving properties") {
Utils.saveProperties(config, version)
Utils.saveProperties(projectDir, config, version)
}
lateinit var newProps: Properties
@ -196,10 +197,36 @@ object UtilsSpec : Spek({
Then("saving the locked properties file") {
assertFailsWith<GradleException> {
Utils.saveProperties(config, version)
Utils.saveProperties(projectDir, config, version)
}
propsLocked.delete()
}
}
Scenario("Save/Load Properties in foo") {
lateinit var fooDir: File
lateinit var fooProps: File
When("saving the foo property") {
fooDir = File("foo")
fooDir.mkdir()
fooProps = File(fooDir, propsFile.name)
config.properties = fooProps.absolutePath
Utils.saveProperties(projectDir, config, version)
}
Then("foo properties file should exists and be readable") {
assertEquals(fooProps.canReadFile(), fooProps.canRead() && fooProps.isFile)
}
When("loading the foo properties file") {
props = Utils.loadProperties(fooProps)
fooProps.delete()
fooDir.delete()
}
Then("version in foo properties should be the same") {
assertEquals(props.getProperty(config.semverKey), version.semver)
}
}
}
})