|
17 | 17 | package org.gradle.api.tasks.options |
18 | 18 |
|
19 | 19 | import org.gradle.integtests.fixtures.AbstractIntegrationSpec |
| 20 | +import org.gradle.integtests.fixtures.polyglot.PolyglotTestFixture |
| 21 | +import org.gradle.test.fixtures.dsl.GradleDsl |
20 | 22 |
|
21 | | -abstract class AbstractOptionIntegrationSpec extends AbstractIntegrationSpec { |
| 23 | +abstract class AbstractOptionIntegrationSpec extends AbstractIntegrationSpec implements PolyglotTestFixture { |
22 | 24 | String taskWithSingleOption(String optionType) { |
23 | 25 | """ |
24 | 26 | import org.gradle.api.DefaultTask; |
@@ -98,6 +100,31 @@ abstract class AbstractOptionIntegrationSpec extends AbstractIntegrationSpec { |
98 | 100 | """ |
99 | 101 | } |
100 | 102 |
|
| 103 | + String kotlinTaskWithSingleOption(String optionType) { |
| 104 | + String kotlinType = kotlinType(optionType) |
| 105 | + String initialValue = optionType == 'boolean' ? 'false' : 'null' |
| 106 | + """ |
| 107 | + abstract class SampleTask : DefaultTask() { |
| 108 | + @get:Internal |
| 109 | + @set:Option("myProp", "Configures command line option 'myProp'.") |
| 110 | + var myProp: $kotlinType = $initialValue |
| 111 | +
|
| 112 | + @TaskAction |
| 113 | + fun renderOptionValue() { |
| 114 | + println("Value of myProp: " + myProp) |
| 115 | + } |
| 116 | +
|
| 117 | + enum class TestEnum { |
| 118 | + OPT_1, OPT_2, OPT_3 |
| 119 | + } |
| 120 | + } |
| 121 | + """ |
| 122 | + } |
| 123 | + |
| 124 | + String buildScriptTaskWithSingleOption(String optionType) { |
| 125 | + currentDsl() == GradleDsl.KOTLIN ? kotlinTaskWithSingleOption(optionType) : groovyTaskWithSingleOption(optionType) |
| 126 | + } |
| 127 | + |
101 | 128 | String taskWithSinglePropertyOption(String propertyType, String optionType) { |
102 | 129 | String methodName = propertyType.substring(0, 1).toLowerCase() + propertyType.substring(1) |
103 | 130 |
|
@@ -152,6 +179,29 @@ abstract class AbstractOptionIntegrationSpec extends AbstractIntegrationSpec { |
152 | 179 | """ |
153 | 180 | } |
154 | 181 |
|
| 182 | + String kotlinTaskWithSinglePropertyOption(String propertyType, String optionType) { |
| 183 | + """ |
| 184 | + abstract class SampleTask : DefaultTask() { |
| 185 | + @get:Internal |
| 186 | + @get:Option("myProp", "Configures command line option 'myProp'.") |
| 187 | + abstract val myProp: $propertyType<${kotlinType(optionType, false)}> |
| 188 | +
|
| 189 | + @TaskAction |
| 190 | + fun renderOptionValue() { |
| 191 | + println("Value of myProp: " + myProp.orNull) |
| 192 | + } |
| 193 | +
|
| 194 | + enum class TestEnum { |
| 195 | + OPT_1, OPT_2, OPT_3 |
| 196 | + } |
| 197 | + } |
| 198 | + """ |
| 199 | + } |
| 200 | + |
| 201 | + String buildScriptTaskWithSinglePropertyOption(String propertyType, String optionType) { |
| 202 | + currentDsl() == GradleDsl.KOTLIN ? kotlinTaskWithSinglePropertyOption(propertyType, optionType) : groovyTaskWithSinglePropertyOption(propertyType, optionType) |
| 203 | + } |
| 204 | + |
155 | 205 | String taskWithUnparameterizedPropertyOption(String propertyType, String methodName) { |
156 | 206 | """ |
157 | 207 | import org.gradle.api.DefaultTask; |
@@ -194,6 +244,36 @@ abstract class AbstractOptionIntegrationSpec extends AbstractIntegrationSpec { |
194 | 244 | """ |
195 | 245 | } |
196 | 246 |
|
| 247 | + String kotlinTaskWithUnparameterizedPropertyOption(String propertyType) { |
| 248 | + """ |
| 249 | + abstract class SampleTask : DefaultTask() { |
| 250 | + @get:Internal |
| 251 | + @get:Option("myProp", "Configures command line option 'myProp'.") |
| 252 | + abstract val myProp: $propertyType |
| 253 | +
|
| 254 | + @TaskAction |
| 255 | + fun renderOptionValue() { |
| 256 | + println("Value of myProp: " + myProp.orNull) |
| 257 | + } |
| 258 | + } |
| 259 | + """ |
| 260 | + } |
| 261 | + |
| 262 | + String buildScriptTaskWithUnparameterizedPropertyOption(String propertyType, String methodName) { |
| 263 | + currentDsl() == GradleDsl.KOTLIN ? kotlinTaskWithUnparameterizedPropertyOption(propertyType) : groovyTaskWithUnparameterizedPropertyOption(propertyType, methodName) |
| 264 | + } |
| 265 | + |
| 266 | + /** |
| 267 | + * Maps a Java type as used in the Groovy fixtures to its Kotlin spelling. |
| 268 | + */ |
| 269 | + static String kotlinType(String javaType, boolean nullable = true) { |
| 270 | + String type = javaType.replace('Integer', 'Int').replace('Object', 'Any') |
| 271 | + if (javaType == 'boolean') { |
| 272 | + return 'Boolean' |
| 273 | + } |
| 274 | + nullable ? "$type?" : type |
| 275 | + } |
| 276 | + |
197 | 277 | String taskWithMultipleOptions() { |
198 | 278 | """ |
199 | 279 | import org.gradle.api.DefaultTask; |
|
0 commit comments