@@ -19,13 +19,12 @@ package com.github.vlsi.gradle.properties.dsl
19
19
import org.gradle.api.GradleException
20
20
import org.gradle.api.Project
21
21
import kotlin.properties.ReadOnlyProperty
22
- import kotlin.reflect.KProperty
23
22
24
23
fun Project.stringProperty (property : String , required : Boolean = false): String? {
25
24
val value = project.findProperty(property)
26
25
if (value == null ) {
27
26
if (required) {
28
- throw GradleException (" Property $property is not specified" )
27
+ throw GradleException (" Project property ' $property ' is not specified" )
29
28
}
30
29
logger.debug(" Using null value for $property " )
31
30
return null
@@ -48,41 +47,78 @@ fun String?.toBool(nullAs: Boolean = false, blankAs: Boolean = true, default: Bo
48
47
else -> equals(" true" , ignoreCase = true )
49
48
}
50
49
50
+ fun String?.toBoolOrNull (blankAs : Boolean? = null) = when {
51
+ this == null -> null
52
+ isBlank() -> blankAs
53
+ this == " true" -> true
54
+ this == " false" -> false
55
+ else -> null
56
+ }
57
+
51
58
val Project .props: PropertyMapper get() = PropertyMapper (this )
52
59
53
60
class PropertyMapper internal constructor(private val project : Project ) {
54
- operator fun invoke (default : Boolean = false) = object : ReadOnlyProperty <Any ?, Boolean > {
55
- override fun getValue (thisRef : Any? , property : KProperty <* >): Boolean =
56
- bool(property.name, default = default)
57
- }
61
+ operator fun invoke (default : Boolean = false) = delegate { bool(it, default) }
58
62
59
- operator fun invoke (default : String ) = object : ReadOnlyProperty <Any ?, String > {
60
- override fun getValue (thisRef : Any? , property : KProperty <* >): String =
61
- string(property.name, default)
62
- }
63
+ operator fun invoke (default : String ) = delegate { string(it, default) }
63
64
64
- operator fun invoke (default : Int ) = object : ReadOnlyProperty <Any ?, Int > {
65
- override fun getValue (thisRef : Any? , property : KProperty <* >): Int =
66
- int(property.name, default)
67
- }
65
+ operator fun invoke (default : Int ) = delegate { int(it, default) }
68
66
69
- operator fun invoke (default : Long ) = object : ReadOnlyProperty <Any ?, Long > {
70
- override fun getValue (thisRef : Any? , property : KProperty <* >): Long =
71
- long(property.name, default)
72
- }
67
+ operator fun invoke (default : Long ) = delegate { long(it, default) }
68
+
69
+ val bool get() = delegate { requiredBool(it) }
70
+
71
+ val string get() = delegate { requiredString(it) }
72
+
73
+ val int get() = delegate { requiredInt(it) }
74
+
75
+ val long get() = delegate { requiredLong(it) }
76
+
77
+ fun requiredString (name : String ): String =
78
+ project.stringProperty(name, true )!!
79
+
80
+ fun requiredBool (name : String , blankAs : Boolean? = true) =
81
+ requiredString(name).let {
82
+ it.toBoolOrNull(blankAs = blankAs)
83
+ ? : throw GradleException (" Project property \" $name \" should be a Boolean (true/false): '$it '" )
84
+ }
85
+
86
+ fun requiredInt (name : String ) =
87
+ requiredString(name).let {
88
+ it.toIntOrNull()
89
+ ? : throw GradleException (" Project property \" $name \" should be an Int: '$it '" )
90
+ }
91
+
92
+ fun requiredLong (name : String ) =
93
+ requiredString(name).let {
94
+ it.toLongOrNull()
95
+ ? : throw GradleException (" Project property \" $name \" should be a Long: '$it '" )
96
+ }
73
97
74
98
fun bool (name : String , default : Boolean = false, nullAs : Boolean = default, blankAs : Boolean = true) =
75
- project.stringProperty(name, false )
99
+ project.stringProperty(name)
76
100
.toBool(nullAs = nullAs, blankAs = blankAs, default = default)
77
101
78
102
fun string (name : String , default : String = "") =
79
- project.stringProperty(name, false ) ? : default
103
+ project.stringProperty(name) ? : default
80
104
81
105
fun int (name : String , default : Int = 0) =
82
- project.stringProperty(name, false )?.toInt() ? : default
106
+ project.stringProperty(name)?.let { value ->
107
+ value.toIntOrNull() ? : null .also {
108
+ project.logger.debug(" Unable to parse project property $name =$value as Int, using default value: $default " )
109
+ }
110
+ } ? : default
83
111
84
112
fun long (name : String , default : Long = 0) =
85
- project.stringProperty(name, false )?.toLong() ? : default
113
+ project.stringProperty(name)?.let { value ->
114
+ value.toLongOrNull() ? : null .also {
115
+ project.logger.debug(" Unable to parse project property $name =$value as Long, using default value: $default " )
116
+ }
117
+ } ? : default
118
+
119
+ private fun <T > delegate (provider : (String ) -> T ) = ReadOnlyProperty { _: Any? , property ->
120
+ provider(property.name)
121
+ }
86
122
}
87
123
88
124
private val yearRegexp = Regex (" \\ d{4}" )
0 commit comments