Skip to content
This repository was archived by the owner on Aug 19, 2020. It is now read-only.

Commit 8f160d3

Browse files
authored
Merge pull request #1164 from gradle/bamboo/generate-compilable-element-accessors
Generate compilable accessors for container elements of default package type
2 parents 27a12cb + 1006942 commit 8f160d3

File tree

4 files changed

+145
-119
lines changed

4 files changed

+145
-119
lines changed

subprojects/integ-tests/src/test/kotlin/org/gradle/kotlin/dsl/integration/ProjectSchemaAccessorsIntegrationTest.kt

Lines changed: 100 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,18 @@ class ProjectSchemaAccessorsIntegrationTest : AbstractPluginIntegrationTest() {
3838

3939
lateinit var extensionSourceFile: File
4040

41-
withFolders {
42-
43-
"buildSrc" {
44-
45-
withPrecompiledPlugins()
46-
47-
"src/main/kotlin" {
41+
withBuildSrc {
42+
"src/main/kotlin" {
4843

49-
extensionSourceFile =
50-
withFile("Extension.kt", """
51-
internal
52-
class Extension
53-
""")
54-
55-
withFile("plugin.gradle.kts", """
56-
extensions.add("extension", Extension())
44+
extensionSourceFile =
45+
withFile("Extension.kt", """
46+
internal
47+
class Extension
5748
""")
58-
}
49+
50+
withFile("plugin.gradle.kts", """
51+
extensions.add("extension", Extension())
52+
""")
5953
}
6054
}
6155

@@ -99,25 +93,19 @@ class ProjectSchemaAccessorsIntegrationTest : AbstractPluginIntegrationTest() {
9993
@Test
10094
fun `can access extension of default package type`() {
10195

102-
withFolders {
96+
withBuildSrc {
10397

104-
"buildSrc" {
105-
106-
withPrecompiledPlugins()
98+
"src/main/kotlin" {
10799

108-
"src/main/kotlin" {
109-
110-
withFile("Extension.kt", """
111-
class Extension(private val name: String) : org.gradle.api.Named {
112-
override fun getName() = name
113-
}
114-
""")
100+
withFile("Extension.kt", """
101+
class Extension(private val name: String) : org.gradle.api.Named {
102+
override fun getName() = name
103+
}
104+
""")
115105

116-
withFile("plugin.gradle.kts", """
117-
extensions.add("extension", Extension("foo"))
118-
extensions.add("beans", container(Extension::class))
119-
""")
120-
}
106+
withFile("plugin.gradle.kts", """
107+
extensions.add("extension", Extension("foo"))
108+
""")
121109
}
122110
}
123111

@@ -129,41 +117,30 @@ class ProjectSchemaAccessorsIntegrationTest : AbstractPluginIntegrationTest() {
129117
130118
println("extension: " + typeOf(extension))
131119
extension { println("extension{} " + typeOf(this)) }
132-
133-
println("beans: " + typeOf(beans))
134-
beans { println("beans{} " + typeOf(beans)) }
135120
""")
136121

137122
assertThat(
138123
build("help", "-q").output,
139124
containsMultiLineString("""
140125
extension: Extension
141126
extension{} Extension
142-
beans: org.gradle.api.NamedDomainObjectContainer<Extension>
143-
beans{} org.gradle.api.NamedDomainObjectContainer<Extension>
144127
""")
145128
)
146129
}
147130

148131
@Test
149132
fun `can access task of default package type`() {
150133

151-
withFolders {
152-
153-
"buildSrc" {
154-
155-
withPrecompiledPlugins()
156-
157-
"src/main/kotlin" {
134+
withBuildSrc {
135+
"src/main/kotlin" {
158136

159-
withFile("CustomTask.kt", """
160-
open class CustomTask : org.gradle.api.DefaultTask()
161-
""")
137+
withFile("CustomTask.kt", """
138+
open class CustomTask : org.gradle.api.DefaultTask()
139+
""")
162140

163-
withFile("plugin.gradle.kts", """
164-
tasks.register<CustomTask>("customTask")
165-
""")
166-
}
141+
withFile("plugin.gradle.kts", """
142+
tasks.register<CustomTask>("customTask")
143+
""")
167144
}
168145
}
169146

@@ -189,35 +166,28 @@ class ProjectSchemaAccessorsIntegrationTest : AbstractPluginIntegrationTest() {
189166
@Test
190167
fun `can access extension of nested type`() {
191168

192-
withFolders {
193-
194-
"buildSrc" {
195-
196-
withPrecompiledPlugins()
197-
198-
"src/main/kotlin/my" {
169+
withBuildSrc {
170+
"src/main/kotlin/my" {
199171

200-
withFile("Extension.kt", """
201-
package my
172+
withFile("Extension.kt", """
173+
package my
202174
203-
class Nested {
204-
class Extension(private val name: String) : org.gradle.api.Named {
205-
override fun getName() = name
206-
}
175+
class Nested {
176+
class Extension(private val name: String) : org.gradle.api.Named {
177+
override fun getName() = name
207178
}
208-
""")
179+
}
180+
""")
209181

210-
withFile("plugin.gradle.kts", """
211-
package my
182+
withFile("plugin.gradle.kts", """
183+
package my
212184
213-
extensions.add("nested", Nested.Extension("foo"))
214-
extensions.add("beans", container(Nested.Extension::class))
215-
""")
216-
}
185+
extensions.add("nested", Nested.Extension("foo"))
186+
extensions.add("beans", container(Nested.Extension::class))
187+
""")
217188
}
218189
}
219190

220-
221191
withBuildScript("""
222192
223193
plugins { id("my.plugin") }
@@ -251,40 +221,35 @@ class ProjectSchemaAccessorsIntegrationTest : AbstractPluginIntegrationTest() {
251221
@Test
252222
fun `multiple generic extension targets`() {
253223

254-
withFolders {
224+
withBuildSrc {
255225

256-
"buildSrc" {
257-
258-
withPrecompiledPlugins()
226+
"src/main/kotlin" {
227+
withFile("types.kt", """
259228
260-
"src/main/kotlin" {
261-
withFile("types.kt", """
262-
263-
package my
229+
package my
264230
265-
data class NamedString(val name: String, var value: String? = null)
231+
data class NamedString(val name: String, var value: String? = null)
266232
267-
data class NamedLong(val name: String, var value: Long? = null)
268-
""")
233+
data class NamedLong(val name: String, var value: Long? = null)
234+
""")
269235

270-
withFile("plugin.gradle.kts", """
236+
withFile("plugin.gradle.kts", """
271237
272-
package my
238+
package my
273239
274-
val strings = container(NamedString::class) { NamedString(it) }
275-
extensions.add("strings", strings)
240+
val strings = container(NamedString::class) { NamedString(it) }
241+
extensions.add("strings", strings)
276242
277-
val longs = container(NamedLong::class) { NamedLong(it) }
278-
extensions.add("longs", longs)
243+
val longs = container(NamedLong::class) { NamedLong(it) }
244+
extensions.add("longs", longs)
279245
280-
tasks.register("printStringsAndLongs") {
281-
doLast {
282-
strings.forEach { println("string: " + it) }
283-
longs.forEach { println("long: " + it) }
284-
}
246+
tasks.register("printStringsAndLongs") {
247+
doLast {
248+
strings.forEach { println("string: " + it) }
249+
longs.forEach { println("long: " + it) }
285250
}
286-
""")
287-
}
251+
}
252+
""")
288253
}
289254
}
290255

@@ -439,25 +404,25 @@ class ProjectSchemaAccessorsIntegrationTest : AbstractPluginIntegrationTest() {
439404
@Test
440405
fun `can access NamedDomainObjectContainer extension via generated accessor`() {
441406

442-
withKotlinBuildSrc()
443-
444-
withFile("buildSrc/src/main/kotlin/my/DocumentationPlugin.kt", """
445-
package my
407+
withBuildSrc {
408+
withFile("src/main/kotlin/my/DocumentationPlugin.kt", """
409+
package my
446410
447-
import org.gradle.api.*
448-
import org.gradle.kotlin.dsl.*
411+
import org.gradle.api.*
412+
import org.gradle.kotlin.dsl.*
449413
450-
class DocumentationPlugin : Plugin<Project> {
414+
class DocumentationPlugin : Plugin<Project> {
451415
452-
override fun apply(project: Project) {
453-
val books = project.container(Book::class, ::Book)
454-
project.extensions.add("the books", books)
416+
override fun apply(project: Project) {
417+
val books = project.container(Book::class, ::Book)
418+
project.extensions.add("the books", books)
419+
}
455420
}
456-
}
457421
458-
data class Book(val name: String)
422+
data class Book(val name: String)
459423
460-
""")
424+
""")
425+
}
461426

462427
val buildFile = withBuildScript("""
463428
@@ -467,7 +432,8 @@ class ProjectSchemaAccessorsIntegrationTest : AbstractPluginIntegrationTest() {
467432

468433

469434
println(
470-
build("kotlinDslAccessorsSnapshot").output)
435+
build("kotlinDslAccessorsSnapshot").output
436+
)
471437

472438

473439
buildFile.appendText("""
@@ -488,7 +454,8 @@ class ProjectSchemaAccessorsIntegrationTest : AbstractPluginIntegrationTest() {
488454
""")
489455
assertThat(
490456
build("books").output,
491-
containsString("quickStart, userGuide"))
457+
containsString("quickStart, userGuide")
458+
)
492459
}
493460

494461
@Test
@@ -506,7 +473,8 @@ class ProjectSchemaAccessorsIntegrationTest : AbstractPluginIntegrationTest() {
506473

507474
assertThat(
508475
build("mainClassName").output,
509-
containsString("*App*"))
476+
containsString("*App*")
477+
)
510478
}
511479

512480
@Test
@@ -563,7 +531,9 @@ class ProjectSchemaAccessorsIntegrationTest : AbstractPluginIntegrationTest() {
563531
containsString("a/build/classes/java/main"),
564532
containsString("b/build/classes/java/main"),
565533
not(containsString("logback-core")),
566-
not(containsString("commons-io"))))
534+
not(containsString("commons-io"))
535+
)
536+
)
567537
}
568538

569539
@Test
@@ -577,7 +547,13 @@ class ProjectSchemaAccessorsIntegrationTest : AbstractPluginIntegrationTest() {
577547
assertThat(aTasks, not(containsString("kotlinDslAccessorsSnapshot")))
578548

579549
val rootTasks = build(":tasks").output
580-
assertThat(rootTasks, allOf(containsString("kotlinDslAccessorsReport"), containsString("kotlinDslAccessorsSnapshot")))
550+
assertThat(
551+
rootTasks,
552+
allOf(
553+
containsString("kotlinDslAccessorsReport"),
554+
containsString("kotlinDslAccessorsSnapshot")
555+
)
556+
)
581557
}
582558

583559
@Test
@@ -948,6 +924,16 @@ class ProjectSchemaAccessorsIntegrationTest : AbstractPluginIntegrationTest() {
948924
build("help")
949925
}
950926

927+
private
928+
fun withBuildSrc(contents: FoldersDslExpression) {
929+
withFolders {
930+
"buildSrc" {
931+
withPrecompiledPlugins()
932+
contents()
933+
}
934+
}
935+
}
936+
951937
private
952938
fun withFolders(folders: FoldersDslExpression) =
953939
projectRoot.withFolders(folders)

subprojects/provider-plugins/src/main/kotlin/org/gradle/kotlin/dsl/provider/plugins/DefaultProjectSchemaProvider.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class DefaultProjectSchemaProvider : ProjectSchemaProvider {
4242
targetSchema.conventions,
4343
targetSchema.tasks,
4444
targetSchema.containerElements,
45-
accessibleConfigurationsOf(project))
45+
accessibleConfigurationsOf(project)
46+
)
4647
}
4748
}
4849

@@ -96,7 +97,12 @@ fun targetSchemaFor(target: Any, targetType: TypeOf<*>): TargetTypedSchema {
9697

9798
collectSchemaOf(target, targetType)
9899

99-
return TargetTypedSchema(extensions.distinct(), conventions.distinct(), tasks.distinct(), containerElements.distinct())
100+
return TargetTypedSchema(
101+
extensions.distinct(),
102+
conventions.distinct(),
103+
tasks.distinct(),
104+
containerElements.distinct()
105+
)
100106
}
101107

102108

subprojects/provider/src/main/kotlin/org/gradle/kotlin/dsl/accessors/AccessorsClassPath.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,23 @@ fun sourceFilesWithAccessorsFor(projectSchema: ProjectSchema<TypeAccessibility>,
247247
}
248248

249249

250-
private
250+
internal
251251
fun importsRequiredBy(schemaSubset: ProjectSchema<TypeAccessibility>): List<String> =
252252
defaultPackageTypesIn(
253-
schemaSubset
254-
.run { extensions.flatMap { listOf(it.target, it.type) } + tasks.map { it.type } }
253+
candidateTypesForImportIn(schemaSubset)
255254
.filterIsInstance<TypeAccessibility.Accessible>()
256255
.map { it.type }
257256
)
258257

259258

259+
private
260+
fun candidateTypesForImportIn(projectSchema: ProjectSchema<TypeAccessibility>) = projectSchema.run {
261+
(extensions.flatMap { listOf(it.target, it.type) }
262+
+ tasks.map { it.type }
263+
+ containerElements.map { it.type })
264+
}
265+
266+
260267
internal
261268
fun defaultPackageTypesIn(typeStrings: List<String>): List<String> =
262269
typeStrings

0 commit comments

Comments
 (0)