|
17 | 17 | package net.kautler |
18 | 18 |
|
19 | 19 | import org.gradle.api.Project |
| 20 | +import org.gradle.api.Task |
20 | 21 | import kotlin.properties.ReadOnlyProperty |
21 | 22 | import kotlin.reflect.KProperty |
22 | 23 |
|
23 | | -abstract class Property<out T>( |
24 | | - private val project: Project, |
25 | | - private var propertyName: String?, |
26 | | - private val default: T |
27 | | -) : ReadOnlyProperty<Any, T> { |
| 24 | +sealed class Property<out T> constructor( |
| 25 | + private val default: () -> T, |
| 26 | + private var propertyName: String, |
| 27 | + private var project: Project |
| 28 | +) : ReadOnlyProperty<Any?, T> { |
28 | 29 | protected abstract fun doGetValue(project: Project, propertyName: String): T? |
29 | 30 |
|
30 | | - fun getValue() = doGetValue(project, propertyName!!) ?: default |
| 31 | + fun getValue() = doGetValue(project, propertyName) ?: default.invoke() |
31 | 32 |
|
32 | | - override fun getValue(thisRef: Any, property: KProperty<*>) = doGetValue(project, propertyName!!) ?: default |
| 33 | + override fun getValue(thisRef: Any?, property: KProperty<*>) = getValue() |
33 | 34 |
|
34 | | - operator fun provideDelegate(thisRef: Any, property: KProperty<*>) = this.apply { |
35 | | - propertyName = propertyName ?: property.name |
| 35 | + companion object { |
| 36 | + fun boolean( |
| 37 | + default: () -> Boolean = { false }, |
| 38 | + propertyName: String? = null, |
| 39 | + project: Project? = null |
| 40 | + ) = PropertyDelegateProvider( |
| 41 | + default, |
| 42 | + propertyName, |
| 43 | + project, |
| 44 | + ::BooleanProperty |
| 45 | + ) |
| 46 | + |
| 47 | + fun boolean( |
| 48 | + default: Boolean, |
| 49 | + propertyName: String? = null, |
| 50 | + project: Project? = null |
| 51 | + ) = boolean({ default }, propertyName, project) |
| 52 | + |
| 53 | + fun boolean( |
| 54 | + project: Project, |
| 55 | + propertyName: String, |
| 56 | + default: () -> Boolean = { false } |
| 57 | + ): Property<Boolean> = BooleanProperty(default, propertyName, project) |
| 58 | + |
| 59 | + fun boolean( |
| 60 | + project: Project, |
| 61 | + propertyName: String, |
| 62 | + default: Boolean |
| 63 | + ) = boolean(project, propertyName) { default } |
| 64 | + |
| 65 | + fun string( |
| 66 | + default: () -> String, |
| 67 | + propertyName: String? = null, |
| 68 | + project: Project? = null |
| 69 | + ) = PropertyDelegateProvider( |
| 70 | + default, |
| 71 | + propertyName, |
| 72 | + project, |
| 73 | + ::StringProperty |
| 74 | + ) |
| 75 | + |
| 76 | + fun string( |
| 77 | + default: String, |
| 78 | + propertyName: String? = null, |
| 79 | + project: Project? = null |
| 80 | + ) = string({ default }, propertyName, project) |
| 81 | + |
| 82 | + fun string( |
| 83 | + project: Project, |
| 84 | + propertyName: String, |
| 85 | + default: () -> String |
| 86 | + ): Property<String> = StringProperty(default, propertyName, project) |
| 87 | + |
| 88 | + fun string( |
| 89 | + project: Project, |
| 90 | + propertyName: String, |
| 91 | + default: String |
| 92 | + ) = string(project, propertyName) { default } |
| 93 | + |
| 94 | + fun optionalString( |
| 95 | + propertyName: String? = null, |
| 96 | + project: Project? = null |
| 97 | + ) = PropertyDelegateProvider( |
| 98 | + { null }, |
| 99 | + propertyName, |
| 100 | + project, |
| 101 | + ::OptionalStringProperty |
| 102 | + ) |
| 103 | + |
| 104 | + fun optionalString( |
| 105 | + project: Project, |
| 106 | + propertyName: String |
| 107 | + ): Property<String?> = OptionalStringProperty(propertyName, project) |
36 | 108 | } |
37 | 109 | } |
38 | 110 |
|
39 | | -class StringProperty( |
40 | | - project: Project, |
41 | | - propertyName: String? = null, |
42 | | - default: String? = null |
43 | | -) : Property<String?>(project, propertyName, default) { |
44 | | - override fun doGetValue(project: Project, propertyName: String) = findProperty(project, propertyName) |
| 111 | +class PropertyDelegateProvider<out T>( |
| 112 | + private val default: () -> T, |
| 113 | + private val propertyName: String? = null, |
| 114 | + private val project: Project? = null, |
| 115 | + private val delegateFactory: (() -> T, String, Project) -> Property<T> |
| 116 | +) { |
| 117 | + operator fun provideDelegate(thisRef: Project, property: KProperty<*>) = |
| 118 | + delegateFactory(default, propertyName ?: property.name, project ?: thisRef) |
| 119 | + |
| 120 | + operator fun provideDelegate(thisRef: Task, property: KProperty<*>) = |
| 121 | + provideDelegate(thisRef.project, property) |
| 122 | + |
| 123 | + operator fun provideDelegate(thisRef: Any?, property: KProperty<*>) = |
| 124 | + project?.let { |
| 125 | + provideDelegate(it, property) |
| 126 | + } ?: error("Property '${property.name}' must be declared on 'Project' or 'Task', " + |
| 127 | + "or 'Project' must be given explicitly") |
| 128 | +} |
| 129 | + |
| 130 | +private class OptionalStringProperty( |
| 131 | + default: () -> String?, |
| 132 | + propertyName: String, |
| 133 | + project: Project |
| 134 | +) : Property<String?>(default, propertyName, project) { |
| 135 | + constructor(propertyName: String, project: Project) : this({ null }, propertyName, project) |
| 136 | + |
| 137 | + override fun doGetValue(project: Project, propertyName: String) = |
| 138 | + findProperty(project, propertyName) |
45 | 139 | } |
46 | 140 |
|
47 | | -class BooleanProperty( |
48 | | - project: Project, |
49 | | - propertyName: String? = null, |
50 | | - default: Boolean = false |
51 | | -) : Property<Boolean>(project, propertyName, default) { |
52 | | - override fun doGetValue(project: Project, propertyName: String) = findProperty(project, propertyName)?.toBoolean() |
| 141 | +private class StringProperty( |
| 142 | + default: () -> String, |
| 143 | + propertyName: String, |
| 144 | + project: Project |
| 145 | +) : Property<String>(default, propertyName, project) { |
| 146 | + override fun doGetValue(project: Project, propertyName: String) = |
| 147 | + findProperty(project, propertyName) |
53 | 148 | } |
54 | 149 |
|
55 | | -private fun findProperty(project: Project, propertyName: String) = ( |
56 | | - project.findProperty("${project.rootProject.name}.$propertyName") |
57 | | - ?: project.findProperty(propertyName)) |
58 | | - as String? |
| 150 | +private class BooleanProperty( |
| 151 | + default: () -> Boolean = { false }, |
| 152 | + propertyName: String, |
| 153 | + project: Project |
| 154 | +) : Property<Boolean>(default, propertyName, project) { |
| 155 | + override fun doGetValue(project: Project, propertyName: String) = |
| 156 | + findProperty(project, propertyName)?.toBoolean() |
| 157 | +} |
| 158 | + |
| 159 | +private fun findProperty(project: Project, propertyName: String): String? { |
| 160 | + var result = project.findProperty("${project.rootProject.name}.$propertyName") as String? |
| 161 | + if (result.isNullOrBlank()) { |
| 162 | + result = project.findProperty(propertyName) as String? |
| 163 | + } |
| 164 | + return if (result.isNullOrBlank()) null else result |
| 165 | +} |
0 commit comments