Can now map a property file contents onto an arbitrary object
This commit is contained in:
parent
19cadfff2a
commit
fb39509861
|
@ -15,9 +15,6 @@
|
|||
*/
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
import java.time.Instant
|
||||
import java.util.*
|
||||
import kotlin.reflect.KMutableProperty
|
||||
import kotlin.reflect.full.declaredMemberProperties
|
||||
|
||||
println("Gradle ${project.gradle.gradleVersion}")
|
||||
|
||||
|
@ -26,9 +23,10 @@ plugins {
|
|||
`java-gradle-plugin`
|
||||
|
||||
id("com.gradle.plugin-publish") version "0.10.1"
|
||||
|
||||
id("com.dorkbox.Licensing") version "1.4"
|
||||
id("com.dorkbox.VersionUpdate") version "1.4.1"
|
||||
id("com.dorkbox.GradleUtils") version "1.0"
|
||||
id("com.dorkbox.GradleUtils") version "1.2"
|
||||
|
||||
kotlin("jvm") version "1.3.21"
|
||||
}
|
||||
|
@ -37,7 +35,7 @@ object Extras {
|
|||
// set for the project
|
||||
const val description = "Gradle Plugin to manage various Gradle tasks, such as updating gradle and dependencies"
|
||||
const val group = "com.dorkbox"
|
||||
const val version = "1.1"
|
||||
const val version = "1.2"
|
||||
|
||||
// set as project.ext
|
||||
const val name = "Gradle Utils"
|
||||
|
@ -53,34 +51,11 @@ object Extras {
|
|||
///////////////////////////////
|
||||
///// assign 'Extras'
|
||||
///////////////////////////////
|
||||
GradleUtils.load("$projectDir/../../gradle.properties", Extras)
|
||||
description = Extras.description
|
||||
group = Extras.group
|
||||
version = Extras.version
|
||||
|
||||
val propsFile = File("$projectDir/../../gradle.properties").normalize()
|
||||
if (propsFile.canRead()) {
|
||||
println("\tLoading custom property data from: [$propsFile]")
|
||||
|
||||
val props = Properties()
|
||||
propsFile.inputStream().use {
|
||||
props.load(it)
|
||||
}
|
||||
|
||||
val extraProperties = Extras::class.declaredMemberProperties.filterIsInstance<KMutableProperty<String>>()
|
||||
props.forEach { (k, v) -> run {
|
||||
val key = k as String
|
||||
val value = v as String
|
||||
|
||||
val member = extraProperties.find { it.name == key }
|
||||
if (member != null) {
|
||||
member.setter.call(Extras::class.objectInstance, value)
|
||||
}
|
||||
else {
|
||||
project.extra.set(k, v)
|
||||
}
|
||||
}}
|
||||
}
|
||||
|
||||
|
||||
licensing {
|
||||
license(License.APACHE_2) {
|
||||
|
|
|
@ -24,7 +24,10 @@ import org.gradle.api.Project
|
|||
* For managing (what should be common sense) gradle tasks, such as updating gradle and dependencies
|
||||
*/
|
||||
class GradleUtils : Plugin<Project> {
|
||||
private lateinit var propertyMappingExtension: LoadPropertyFile
|
||||
|
||||
override fun apply(project: Project) {
|
||||
propertyMappingExtension = project.extensions.create("GradleUtils", LoadPropertyFile::class.java, project)
|
||||
|
||||
project.tasks.create("autoUpdateGradle", GradleUpdateTask::class.java).apply {
|
||||
group = "gradle"
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
package dorkbox.gradle
|
||||
|
||||
import org.gradle.api.Project
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
import kotlin.reflect.KMutableProperty
|
||||
import kotlin.reflect.full.declaredMemberProperties
|
||||
|
||||
open class LoadPropertyFile(private val project: Project) {
|
||||
/**
|
||||
* Maps the property (key/value) pairs of a property file onto the specified target object.
|
||||
*/
|
||||
fun load(propertyFile: String, targetObject: Any) {
|
||||
val kClass = targetObject::class
|
||||
|
||||
val propsFile = File(propertyFile).normalize()
|
||||
if (propsFile.canRead()) {
|
||||
println("\tLoading custom property data from: [$propsFile]")
|
||||
|
||||
val props = Properties()
|
||||
propsFile.inputStream().use {
|
||||
props.load(it)
|
||||
}
|
||||
|
||||
val extraProperties = kClass.declaredMemberProperties.filterIsInstance<KMutableProperty<String>>()
|
||||
props.forEach { (k, v) -> run {
|
||||
val key = k as String
|
||||
val value = v as String
|
||||
|
||||
val member = extraProperties.find { it.name == key }
|
||||
if (member != null) {
|
||||
member.setter.call(kClass.objectInstance, value)
|
||||
}
|
||||
else {
|
||||
project.extensions.extraProperties.set(k, v)
|
||||
}
|
||||
}}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue