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
|
||||
project.version = version.semver
|
||||
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)
|
||||
project.version = version.semver
|
||||
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.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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,41 +205,45 @@ 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 {
|
||||
propsFile.apply {
|
||||
if (canReadFile()) {
|
||||
FileInputStream(this).reader().use { load(it) }
|
||||
} else {
|
||||
createNewFile()
|
||||
}
|
||||
|
||||
put(config.semverKey, version.semver)
|
||||
put(config.majorKey, version.major.toString())
|
||||
put(config.minorKey, version.minor.toString())
|
||||
put(config.patchKey, version.patch.toString())
|
||||
put(config.preReleaseKey, version.preRelease)
|
||||
put(config.buildMetaKey, version.buildMeta)
|
||||
put(config.semverKey, version.semver)
|
||||
|
||||
put(config.buildMetaPrefixKey, version.buildMetaPrefix,
|
||||
version.buildMetaPrefix != Version.DEFAULT_BUILDMETA_PREFIX ||
|
||||
containsKey(config.buildMetaPrefixKey))
|
||||
put(config.preReleasePrefixKey, version.preReleasePrefix,
|
||||
version.preReleasePrefix != Version.DEFAULT_PRERELEASE_PREFIX ||
|
||||
containsKey(config.preReleasePrefixKey))
|
||||
put(config.separatorKey, version.separator,
|
||||
version.separator != Version.DEFAULT_SEPARATOR ||
|
||||
containsKey(config.separatorKey))
|
||||
|
||||
if (canWrite()) {
|
||||
FileOutputStream(this).writer().use {
|
||||
store(it, "Generated by the Semver Plugin for Gradle")
|
||||
try {
|
||||
propsFile.apply {
|
||||
if (canReadFile()) {
|
||||
FileInputStream(this).reader().use { load(it) }
|
||||
} else {
|
||||
createNewFile()
|
||||
}
|
||||
|
||||
put(config.semverKey, version.semver)
|
||||
put(config.majorKey, version.major.toString())
|
||||
put(config.minorKey, version.minor.toString())
|
||||
put(config.patchKey, version.patch.toString())
|
||||
put(config.preReleaseKey, version.preRelease)
|
||||
put(config.buildMetaKey, version.buildMeta)
|
||||
put(config.semverKey, version.semver)
|
||||
|
||||
put(config.buildMetaPrefixKey, version.buildMetaPrefix,
|
||||
version.buildMetaPrefix != Version.DEFAULT_BUILDMETA_PREFIX ||
|
||||
containsKey(config.buildMetaPrefixKey))
|
||||
put(config.preReleasePrefixKey, version.preReleasePrefix,
|
||||
version.preReleasePrefix != Version.DEFAULT_PRERELEASE_PREFIX ||
|
||||
containsKey(config.preReleasePrefixKey))
|
||||
put(config.separatorKey, version.separator,
|
||||
version.separator != Version.DEFAULT_SEPARATOR ||
|
||||
containsKey(config.separatorKey))
|
||||
|
||||
if (canWrite()) {
|
||||
FileOutputStream(this).writer().use {
|
||||
store(it, "Generated by the Semver Plugin for Gradle")
|
||||
}
|
||||
} else {
|
||||
throw GradleException()
|
||||
}
|
||||
} else {
|
||||
throw GradleException("Unable to write version to: `$absoluteFile`")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
throw GradleException("Unable to write version to: `${propsFile.absoluteFile}`")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue