Closes #6
Impletemented Gradle project directory-aware getPropertiesFile() function for all loading/saving.
This commit is contained in:
parent
ffda5e3ad4
commit
fa23243ff5
5 changed files with 78 additions and 44 deletions
|
@ -59,7 +59,7 @@ open class SemverIncrementBuildMetaTask @Inject constructor(
|
||||||
version.buildMeta = buildMeta
|
version.buildMeta = buildMeta
|
||||||
project.version = version.semver
|
project.version = version.semver
|
||||||
logger.lifecycle("Version: ${project.version}")
|
logger.lifecycle("Version: ${project.version}")
|
||||||
Utils.saveProperties(config, version)
|
Utils.saveProperties(project.projectDir, config, version)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,6 +57,6 @@ open class SemverIncrementTask @Inject constructor(
|
||||||
isPatch = type == SemverConfig.DEFAULT_PATCH_KEY)
|
isPatch = type == SemverConfig.DEFAULT_PATCH_KEY)
|
||||||
project.version = version.semver
|
project.version = version.semver
|
||||||
logger.lifecycle("Version: ${project.version}")
|
logger.lifecycle("Version: ${project.version}")
|
||||||
Utils.saveProperties(config, version)
|
Utils.saveProperties(project.projectDir, config, version)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,6 @@ import org.gradle.api.GradleException
|
||||||
import org.gradle.api.Plugin
|
import org.gradle.api.Plugin
|
||||||
import org.gradle.api.Project
|
import org.gradle.api.Project
|
||||||
import org.gradle.util.GradleVersion
|
import org.gradle.util.GradleVersion
|
||||||
import java.io.File
|
|
||||||
|
|
||||||
class SemverPlugin : Plugin<Project> {
|
class SemverPlugin : Plugin<Project> {
|
||||||
private val simpleName = SemverPlugin::class.simpleName
|
private val simpleName = SemverPlugin::class.simpleName
|
||||||
|
@ -59,11 +58,7 @@ class SemverPlugin : Plugin<Project> {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun afterEvaluate(project: Project) {
|
private fun afterEvaluate(project: Project) {
|
||||||
val propsFile = if (File(config.properties).isAbsolute) {
|
val propsFile = Utils.getPropertiesFile(project.projectDir, config.properties)
|
||||||
File(config.properties)
|
|
||||||
} else {
|
|
||||||
File("${project.projectDir}${File.separator}${config.properties}")
|
|
||||||
}
|
|
||||||
|
|
||||||
if (project.version != "unspecified") {
|
if (project.version != "unspecified") {
|
||||||
project.logger.warn(
|
project.logger.warn(
|
||||||
|
@ -93,7 +88,7 @@ class SemverPlugin : Plugin<Project> {
|
||||||
|
|
||||||
if (!hasReqProps || !isFile) {
|
if (!hasReqProps || !isFile) {
|
||||||
project.logger.info("[$simpleName] Saving version properties to `$absoluteFile`.")
|
project.logger.info("[$simpleName] Saving version properties to `$absoluteFile`.")
|
||||||
Utils.saveProperties(config, version)
|
Utils.saveProperties(project.projectDir, config, version)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,6 +66,14 @@ object Utils {
|
||||||
return true
|
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 {
|
fun loadProperties(file: File): Properties {
|
||||||
var isNew = false
|
var isNew = false
|
||||||
val props = Properties()
|
val props = Properties()
|
||||||
|
@ -197,9 +205,10 @@ object Utils {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
fun saveProperties(config: SemverConfig, version: Version) {
|
fun saveProperties(projectDir: File, config: SemverConfig, version: Version) {
|
||||||
val propsFile = File(config.properties)
|
val propsFile = getPropertiesFile(projectDir, config.properties)
|
||||||
SortedProperties().apply {
|
SortedProperties().apply {
|
||||||
|
try {
|
||||||
propsFile.apply {
|
propsFile.apply {
|
||||||
if (canReadFile()) {
|
if (canReadFile()) {
|
||||||
FileInputStream(this).reader().use { load(it) }
|
FileInputStream(this).reader().use { load(it) }
|
||||||
|
@ -230,9 +239,12 @@ object Utils {
|
||||||
store(it, "Generated by the Semver Plugin for Gradle")
|
store(it, "Generated by the Semver Plugin for Gradle")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw GradleException("Unable to write version to: `$absoluteFile`")
|
throw GradleException()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
throw GradleException("Unable to write version to: `${propsFile.absoluteFile}`")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,12 +48,13 @@ object UtilsSpec : Spek({
|
||||||
val version = Version()
|
val version = Version()
|
||||||
val config = SemverConfig()
|
val config = SemverConfig()
|
||||||
val propsFile = File("test.properties")
|
val propsFile = File("test.properties")
|
||||||
|
val projectDir = File("./")
|
||||||
lateinit var props: Properties
|
lateinit var props: Properties
|
||||||
|
|
||||||
Scenario("Save/Load Properties") {
|
Scenario("Save/Load Properties") {
|
||||||
When("saving the property") {
|
When("saving the property") {
|
||||||
config.properties = propsFile.name
|
config.properties = propsFile.name
|
||||||
Utils.saveProperties(config, version)
|
Utils.saveProperties(projectDir, config, version)
|
||||||
}
|
}
|
||||||
|
|
||||||
Then("properties file should exists and be readable") {
|
Then("properties file should exists and be readable") {
|
||||||
|
@ -115,7 +116,7 @@ object UtilsSpec : Spek({
|
||||||
}
|
}
|
||||||
|
|
||||||
When("saving properties") {
|
When("saving properties") {
|
||||||
Utils.saveProperties(config, version)
|
Utils.saveProperties(projectDir, config, version)
|
||||||
}
|
}
|
||||||
|
|
||||||
lateinit var newProps: Properties
|
lateinit var newProps: Properties
|
||||||
|
@ -196,10 +197,36 @@ object UtilsSpec : Spek({
|
||||||
|
|
||||||
Then("saving the locked properties file") {
|
Then("saving the locked properties file") {
|
||||||
assertFailsWith<GradleException> {
|
assertFailsWith<GradleException> {
|
||||||
Utils.saveProperties(config, version)
|
Utils.saveProperties(projectDir, config, version)
|
||||||
}
|
}
|
||||||
propsLocked.delete()
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue