17
17
package org.jetbrains.kotlin.gradle
18
18
19
19
import org.gradle.api.Project
20
+ import org.gradle.api.Task
20
21
import org.gradle.api.artifacts.ProjectDependency
21
22
import org.jetbrains.plugins.gradle.tooling.ErrorMessageBuilder
22
23
import org.jetbrains.plugins.gradle.tooling.ModelBuilderService
23
24
import java.io.Serializable
24
25
import java.lang.Exception
26
+ import java.lang.reflect.InvocationTargetException
27
+ import java.lang.reflect.Method
28
+
29
+ typealias CompilerArgumentsBySourceSet = Map <String , List <String >>
25
30
26
31
interface KotlinGradleModel : Serializable {
27
32
val implements: String?
28
- val currentCompilerArguments : List < String > ?
29
- val defaultCompilerArguments : List < String > ?
33
+ val currentCompilerArgumentsBySourceSet : CompilerArgumentsBySourceSet
34
+ val defaultCompilerArgumentsBySourceSet : CompilerArgumentsBySourceSet
30
35
val coroutines: String?
31
36
}
32
37
33
38
class KotlinGradleModelImpl (
34
39
override val implements : String? ,
35
- override val currentCompilerArguments : List < String > ? ,
36
- override val defaultCompilerArguments : List < String > ? ,
40
+ override val currentCompilerArgumentsBySourceSet : CompilerArgumentsBySourceSet ,
41
+ override val defaultCompilerArgumentsBySourceSet : CompilerArgumentsBySourceSet ,
37
42
override val coroutines : String?
38
43
) : KotlinGradleModel
39
44
40
45
class KotlinGradleModelBuilder : ModelBuilderService {
41
46
companion object {
42
- val compileTasks = listOf (" compileKotlin" , " compileKotlin2Js" )
47
+ val kotlinCompileTaskClasses = listOf (" org.jetbrains.kotlin.gradle.tasks.KotlinCompile_Decorated" ,
48
+ " org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile_Decorated" )
43
49
}
44
50
45
51
override fun getErrorMessageBuilder (project : Project , e : Exception ): ErrorMessageBuilder {
@@ -57,15 +63,35 @@ class KotlinGradleModelBuilder : ModelBuilderService {
57
63
return null
58
64
}
59
65
66
+ private fun Class <* >.findGetterMethod (name : String ): Method ? {
67
+ generateSequence(this ) { it.superclass }.forEach {
68
+ try {
69
+ return it.getDeclaredMethod(name)
70
+ }
71
+ catch (e: Exception ) {
72
+ // Check next super class
73
+ }
74
+ }
75
+ return null
76
+ }
77
+
60
78
@Suppress(" UNCHECKED_CAST" )
61
- private fun getCompilerArguments (project : Project , methodName : String ): List <String >? {
62
- val compileTask = compileTasks.mapNotNull { project.getTasksByName(it, false ).firstOrNull() }.firstOrNull() ? : return null
79
+ private fun collectCompilerArguments (
80
+ compileTask : Task ,
81
+ methodName : String ,
82
+ argumentsBySourceSet : MutableMap <String , List <String >>
83
+ ) {
63
84
val taskClass = compileTask.javaClass
64
- return try {
65
- taskClass.getDeclaredMethod(methodName).invoke(compileTask) as List <String >
85
+ val sourceSetName = try {
86
+ taskClass.findGetterMethod(" getSourceSetName\$ kotlin_gradle_plugin" )?.invoke(compileTask) as ? String
87
+ } catch (e : InvocationTargetException ) {
88
+ null // can be thrown if property is not initialized yet
89
+ } ? : " main"
90
+ try {
91
+ argumentsBySourceSet[sourceSetName] = taskClass.getDeclaredMethod(methodName).invoke(compileTask) as List <String >
66
92
}
67
93
catch (e : NoSuchMethodException ) {
68
- null
94
+ // No argument accessor method is available
69
95
}
70
96
}
71
97
@@ -86,11 +112,22 @@ class KotlinGradleModelBuilder : ModelBuilderService {
86
112
}
87
113
}
88
114
89
- override fun buildAll (modelName : String? , project : Project ) =
90
- KotlinGradleModelImpl (
91
- getImplements(project),
92
- getCompilerArguments(project, " getSerializedCompilerArguments" ),
93
- getCompilerArguments(project, " getDefaultSerializedCompilerArguments" ),
94
- getCoroutines(project)
95
- )
115
+ override fun buildAll (modelName : String? , project : Project ): KotlinGradleModelImpl {
116
+ val currentCompilerArgumentsBySourceSet = LinkedHashMap <String , List <String >>()
117
+ val defaultCompilerArgumentsBySourceSet = LinkedHashMap <String , List <String >>()
118
+
119
+ project.getAllTasks(false )[project]?.forEach { compileTask ->
120
+ if (compileTask.javaClass.name !in kotlinCompileTaskClasses) return @forEach
121
+
122
+ collectCompilerArguments(compileTask, " getSerializedCompilerArguments" , currentCompilerArgumentsBySourceSet)
123
+ collectCompilerArguments(compileTask, " getDefaultSerializedCompilerArguments" , defaultCompilerArgumentsBySourceSet)
124
+ }
125
+
126
+ return KotlinGradleModelImpl (
127
+ getImplements(project),
128
+ currentCompilerArgumentsBySourceSet,
129
+ defaultCompilerArgumentsBySourceSet,
130
+ getCoroutines(project)
131
+ )
132
+ }
96
133
}
0 commit comments