Kapt: Add IDEA integration (KT-19097, KT-17923, KT-19823)
This commit is contained in:
committed by
Yan Zhulanow
parent
b574ddfba0
commit
8e72a1211b
Generated
+3
@@ -109,6 +109,9 @@
|
||||
<element id="library" level="project" name="kotlinx-coroutines-core" />
|
||||
<element id="library" level="project" name="javaslang" />
|
||||
<element id="library" level="project" name="kotlinx-coroutines-jdk8" />
|
||||
<element id="archive" name="kapt-idea.jar">
|
||||
<element id="module-output" name="kapt3-idea" />
|
||||
</element>
|
||||
</element>
|
||||
<element id="directory" name="kotlinc">
|
||||
<element id="dir-copy" path="$PROJECT_DIR$/dist/kotlinc" />
|
||||
|
||||
Generated
+1
@@ -78,6 +78,7 @@
|
||||
<module fileurl="file://$PROJECT_DIR$/js/js.translator/js.translator.iml" filepath="$PROJECT_DIR$/js/js.translator/js.translator.iml" group="compiler/js" />
|
||||
<module fileurl="file://$PROJECT_DIR$/jps-plugin/kannotator-jps-plugin-test/kannotator-jps-plugin-test.iml" filepath="$PROJECT_DIR$/jps-plugin/kannotator-jps-plugin-test/kannotator-jps-plugin-test.iml" group="ide/jps" />
|
||||
<module fileurl="file://$PROJECT_DIR$/plugins/kapt3/kapt3.iml" filepath="$PROJECT_DIR$/plugins/kapt3/kapt3.iml" group="plugins" />
|
||||
<module fileurl="file://$PROJECT_DIR$/plugins/kapt3-idea/kapt3-idea.iml" filepath="$PROJECT_DIR$/plugins/kapt3-idea/kapt3-idea.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/idea/kotlin-gradle-tooling/kotlin-gradle-tooling.iml" filepath="$PROJECT_DIR$/idea/kotlin-gradle-tooling/kotlin-gradle-tooling.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/compiler/light-classes/light-classes.iml" filepath="$PROJECT_DIR$/compiler/light-classes/light-classes.iml" group="compiler/java" />
|
||||
<module fileurl="file://$PROJECT_DIR$/plugins/lint/lint-api/lint-api.iml" filepath="$PROJECT_DIR$/plugins/lint/lint-api/lint-api.iml" group="plugins/lint" />
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.gradle
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.api.Task
|
||||
import org.gradle.api.artifacts.ProjectDependency
|
||||
import org.gradle.tooling.ModelBuilder
|
||||
import org.jetbrains.plugins.gradle.tooling.ErrorMessageBuilder
|
||||
import org.jetbrains.plugins.gradle.tooling.ModelBuilderService
|
||||
import java.io.File
|
||||
@@ -60,7 +61,7 @@ class KotlinGradleModelImpl(
|
||||
override val transitiveCommonDependencies: Set<String>
|
||||
) : KotlinGradleModel
|
||||
|
||||
class KotlinGradleModelBuilder : ModelBuilderService {
|
||||
abstract class AbstractKotlinGradleModelBuilder : ModelBuilderService {
|
||||
companion object {
|
||||
val kotlinCompileTaskClasses = listOf("org.jetbrains.kotlin.gradle.tasks.KotlinCompile_Decorated",
|
||||
"org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile_Decorated")
|
||||
@@ -70,10 +71,20 @@ class KotlinGradleModelBuilder : ModelBuilderService {
|
||||
"kotlin2js" to "kotlin-platform-js"
|
||||
)
|
||||
val kotlinPluginIds = listOf("kotlin", "kotlin2js", "kotlin-android")
|
||||
private val kotlinPlatformCommonPluginId = "kotlin-platform-common"
|
||||
private val ABSTRACT_KOTLIN_COMPILE_CLASS = "org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile"
|
||||
}
|
||||
val kotlinPlatformCommonPluginId = "kotlin-platform-common"
|
||||
val ABSTRACT_KOTLIN_COMPILE_CLASS = "org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile"
|
||||
|
||||
fun Task.getSourceSetName(): String {
|
||||
return try {
|
||||
javaClass.methods.firstOrNull { it.name.startsWith("getSourceSetName") && it.parameterTypes.isEmpty() }?.invoke(this) as? String
|
||||
} catch (e : InvocationTargetException) {
|
||||
null // can be thrown if property is not initialized yet
|
||||
} ?: "main"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class KotlinGradleModelBuilder : AbstractKotlinGradleModelBuilder() {
|
||||
override fun getErrorMessageBuilder(project: Project, e: Exception): ErrorMessageBuilder {
|
||||
return ErrorMessageBuilder.create(project, e, "Gradle import errors").withDescription("Unable to build Kotlin project configuration")
|
||||
}
|
||||
@@ -168,14 +179,6 @@ class KotlinGradleModelBuilder : ModelBuilderService {
|
||||
}
|
||||
}
|
||||
|
||||
private fun Task.getSourceSetName(): String {
|
||||
return try {
|
||||
javaClass.methods.firstOrNull { it.name.startsWith("getSourceSetName") && it.parameterTypes.isEmpty() }?.invoke(this) as? String
|
||||
} catch (e : InvocationTargetException) {
|
||||
null // can be thrown if property is not initialized yet
|
||||
} ?: "main"
|
||||
}
|
||||
|
||||
override fun buildAll(modelName: String?, project: Project): KotlinGradleModelImpl {
|
||||
val kotlinPluginId = kotlinPluginIds.singleOrNull { project.plugins.findPlugin(it) != null }
|
||||
val platformPluginId = platformPluginIds.singleOrNull { project.plugins.findPlugin(it) != null }
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
<frameworkSupport implementation="org.jetbrains.kotlin.idea.configuration.GradleKotlinMPPJSFrameworkSupportProvider"/>
|
||||
<pluginDescriptions implementation="org.jetbrains.kotlin.idea.configuration.KotlinGradlePluginDescription"/>
|
||||
<projectResolve implementation="org.jetbrains.kotlin.idea.configuration.KotlinGradleProjectResolverExtension" order="first"/>
|
||||
<projectResolve implementation="org.jetbrains.kotlin.kapt.idea.KaptProjectResolverExtension" order="last"/>
|
||||
</extensions>
|
||||
|
||||
<extensionPoints>
|
||||
|
||||
+1
-1
@@ -53,7 +53,7 @@ class Android25ProjectHandler(kotlinConfigurationTools: KotlinConfigurationTools
|
||||
}
|
||||
if (Kapt3GradleSubplugin.isEnabled(project)) {
|
||||
// Add Kapt3 output as well, since there's no SyncOutputTask with the new API
|
||||
val kaptClasssesDir = Kapt3KotlinGradleSubplugin.getKaptClasssesDir(project, getVariantName(variantData))
|
||||
val kaptClasssesDir = Kapt3GradleSubplugin.getKaptGeneratedClassesDir(project, getVariantName(variantData))
|
||||
add(kaptClasssesDir)
|
||||
}
|
||||
}
|
||||
|
||||
+20
-16
@@ -34,11 +34,26 @@ import java.io.ByteArrayOutputStream
|
||||
import java.io.File
|
||||
import java.io.ObjectOutputStream
|
||||
import javax.xml.bind.DatatypeConverter.printBase64Binary
|
||||
import org.jetbrains.kotlin.gradle.internal.Kapt3GradleSubplugin.Companion.getKaptGeneratedSourcesDir
|
||||
import org.jetbrains.kotlin.gradle.internal.Kapt3GradleSubplugin.Companion.getKaptGeneratedClassesDir
|
||||
import org.jetbrains.kotlin.gradle.internal.Kapt3GradleSubplugin.Companion.getKaptGeneratedKotlinSourcesDir
|
||||
|
||||
// apply plugin: 'kotlin-kapt'
|
||||
class Kapt3GradleSubplugin : Plugin<Project> {
|
||||
companion object {
|
||||
fun isEnabled(project: Project) = project.plugins.findPlugin(Kapt3GradleSubplugin::class.java) != null
|
||||
|
||||
@JvmStatic
|
||||
fun getKaptGeneratedClassesDir(project: Project, sourceSetName: String) =
|
||||
File(project.project.buildDir, "tmp/kapt3/classes/$sourceSetName")
|
||||
|
||||
@JvmStatic
|
||||
fun getKaptGeneratedSourcesDir(project: Project, sourceSetName: String) =
|
||||
File(project.project.buildDir, "generated/source/kapt/$sourceSetName")
|
||||
|
||||
@JvmStatic
|
||||
fun getKaptGeneratedKotlinSourcesDir(project: Project, sourceSetName: String) =
|
||||
File(project.project.buildDir, "generated/source/kaptKotlin/$sourceSetName")
|
||||
}
|
||||
|
||||
override fun apply(project: Project) {}
|
||||
@@ -77,23 +92,12 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
||||
}
|
||||
|
||||
fun findMainKaptConfiguration(project: Project) = project.findKaptConfiguration(MAIN_KAPT_CONFIGURATION_NAME)
|
||||
|
||||
fun getKaptClasssesDir(project: Project, sourceSetName: String): File =
|
||||
File(project.project.buildDir, "tmp/kapt3/classes/$sourceSetName")
|
||||
}
|
||||
|
||||
private val kotlinToKaptGenerateStubsTasksMap = mutableMapOf<KotlinCompile, KaptGenerateStubsTask>()
|
||||
|
||||
override fun isApplicable(project: Project, task: AbstractCompile) = task is KotlinCompile && Kapt3GradleSubplugin.isEnabled(project)
|
||||
|
||||
fun getKaptGeneratedDir(project: Project, sourceSetName: String): File {
|
||||
return File(project.project.buildDir, "generated/source/kapt/$sourceSetName")
|
||||
}
|
||||
|
||||
fun getKaptGeneratedDirForKotlin(project: Project, sourceSetName: String): File {
|
||||
return File(project.project.buildDir, "generated/source/kaptKotlin/$sourceSetName")
|
||||
}
|
||||
|
||||
private fun Kapt3SubpluginContext.getKaptStubsDir() = createAndReturnTemporaryKaptDirectory("stubs")
|
||||
|
||||
private fun Kapt3SubpluginContext.getKaptIncrementalDataDir() = createAndReturnTemporaryKaptDirectory("incrementalData")
|
||||
@@ -113,9 +117,9 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
||||
val javaSourceSet: SourceSet?,
|
||||
val kaptExtension: KaptExtension,
|
||||
val kaptClasspath: MutableList<File>) {
|
||||
val sourcesOutputDir = getKaptGeneratedDir(project, sourceSetName)
|
||||
val kotlinSourcesOutputDir = getKaptGeneratedDirForKotlin(project, sourceSetName)
|
||||
val classesOutputDir = getKaptClasssesDir(project, sourceSetName)
|
||||
val sourcesOutputDir = getKaptGeneratedSourcesDir(project, sourceSetName)
|
||||
val kotlinSourcesOutputDir = getKaptGeneratedKotlinSourcesDir(project, sourceSetName)
|
||||
val classesOutputDir = getKaptGeneratedClassesDir(project, sourceSetName)
|
||||
|
||||
val kaptClasspathArtifacts = project
|
||||
.resolveSubpluginArtifacts(listOf(this@Kapt3KotlinGradleSubplugin))
|
||||
@@ -193,7 +197,7 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
||||
private fun Kapt3SubpluginContext.buildOptions(aptMode: String): List<SubpluginOption> {
|
||||
val pluginOptions = mutableListOf<SubpluginOption>()
|
||||
|
||||
val generatedFilesDir = getKaptGeneratedDir(project, sourceSetName)
|
||||
val generatedFilesDir = getKaptGeneratedSourcesDir(project, sourceSetName)
|
||||
kaptVariantData?.addJavaSourceFoldersToModel(generatedFilesDir)
|
||||
|
||||
pluginOptions += SubpluginOption("aptMode", aptMode)
|
||||
@@ -207,7 +211,7 @@ class Kapt3KotlinGradleSubplugin : KotlinGradleSubplugin<KotlinCompile> {
|
||||
javaCompile.source(generatedFilesDir)
|
||||
|
||||
pluginOptions += SubpluginOption("sources", generatedFilesDir.canonicalPath)
|
||||
pluginOptions += SubpluginOption("classes", getKaptClasssesDir(project, sourceSetName).canonicalPath)
|
||||
pluginOptions += SubpluginOption("classes", getKaptGeneratedClassesDir(project, sourceSetName).canonicalPath)
|
||||
|
||||
pluginOptions += SubpluginOption("incrementalData", getKaptIncrementalDataDir().canonicalPath)
|
||||
|
||||
|
||||
+2
-2
@@ -27,7 +27,7 @@ import org.jetbrains.kotlin.com.intellij.openapi.util.text.StringUtil.compareVer
|
||||
import org.jetbrains.kotlin.com.intellij.util.ReflectionUtil
|
||||
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptionsImpl
|
||||
import org.jetbrains.kotlin.gradle.internal.*
|
||||
import org.jetbrains.kotlin.gradle.internal.Kapt3KotlinGradleSubplugin.Companion.getKaptClasssesDir
|
||||
import org.jetbrains.kotlin.gradle.internal.Kapt3GradleSubplugin.Companion.getKaptGeneratedClassesDir
|
||||
import org.jetbrains.kotlin.gradle.tasks.*
|
||||
import org.jetbrains.kotlin.gradle.utils.ParsedGradleVersion
|
||||
import org.jetbrains.kotlin.gradle.utils.checkedReflection
|
||||
@@ -679,7 +679,7 @@ internal fun createSyncOutputTask(
|
||||
syncTask.kotlinTask = kotlinCompile
|
||||
kotlinTask.javaOutputDir = javaDir
|
||||
kotlinAfterJavaTask?.javaOutputDir = javaDir
|
||||
syncTask.kaptClassesDir = getKaptClasssesDir(project, variantName)
|
||||
syncTask.kaptClassesDir = getKaptGeneratedClassesDir(project, variantName)
|
||||
|
||||
// copying should be executed after a latter task
|
||||
val previousTask = kotlinAfterJavaTask ?: javaTask
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="library" name="kotlin-runtime" level="project" />
|
||||
<orderEntry type="library" name="gradle-and-groovy-plugin" level="project" />
|
||||
<orderEntry type="library" name="idea-full" level="project" />
|
||||
<orderEntry type="module" module-name="kapt3" />
|
||||
<orderEntry type="module" module-name="frontend" />
|
||||
<orderEntry type="module" module-name="idea" />
|
||||
<orderEntry type="module" module-name="kotlin-gradle-tooling" />
|
||||
<orderEntry type="library" name="android-plugin" level="project" />
|
||||
</component>
|
||||
</module>
|
||||
+1
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.kapt.idea.KaptModelBuilderService
|
||||
+190
@@ -0,0 +1,190 @@
|
||||
/*
|
||||
* Copyright 2010-2017 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.kapt.idea
|
||||
|
||||
import com.android.tools.idea.gradle.project.model.AndroidModuleModel
|
||||
import com.intellij.openapi.externalSystem.model.DataNode
|
||||
import com.intellij.openapi.externalSystem.model.ProjectKeys
|
||||
import com.intellij.openapi.externalSystem.model.project.*
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.tooling.model.idea.IdeaModule
|
||||
import org.jetbrains.kotlin.gradle.AbstractKotlinGradleModelBuilder
|
||||
import org.jetbrains.kotlin.idea.framework.GRADLE_SYSTEM_ID
|
||||
import org.jetbrains.plugins.gradle.model.data.GradleSourceSetData
|
||||
import org.jetbrains.plugins.gradle.service.project.AbstractProjectResolverExtension
|
||||
import org.jetbrains.plugins.gradle.tooling.ErrorMessageBuilder
|
||||
import java.io.File
|
||||
import java.io.Serializable
|
||||
import java.lang.Exception
|
||||
import java.lang.reflect.Modifier
|
||||
|
||||
interface KaptSourceSetModel : Serializable {
|
||||
val sourceSetName: String
|
||||
val isTest: Boolean
|
||||
val generatedSourcesDir: String
|
||||
val generatedClassesDir: String
|
||||
val generatedKotlinSourcesDir: String
|
||||
|
||||
val generatedSourcesDirFile get() = generatedSourcesDir.takeIf { it.isNotEmpty() }?.let(::File)
|
||||
val generatedClassesDirFile get() = generatedClassesDir.takeIf { it.isNotEmpty() }?.let(::File)
|
||||
val generatedKotlinSourcesDirFile get() = generatedKotlinSourcesDir.takeIf { it.isNotEmpty() }?.let(::File)
|
||||
}
|
||||
|
||||
class KaptSourceSetModelImpl(
|
||||
override val sourceSetName: String,
|
||||
override val isTest: Boolean,
|
||||
override val generatedSourcesDir: String,
|
||||
override val generatedClassesDir: String,
|
||||
override val generatedKotlinSourcesDir: String
|
||||
) : KaptSourceSetModel
|
||||
|
||||
interface KaptGradleModel : Serializable {
|
||||
val isEnabled: Boolean
|
||||
val buildDirectory: File
|
||||
val sourceSets: List<KaptSourceSetModel>
|
||||
}
|
||||
|
||||
class KaptGradleModelImpl(
|
||||
override val isEnabled: Boolean,
|
||||
override val buildDirectory: File,
|
||||
override val sourceSets: List<KaptSourceSetModel>
|
||||
) : KaptGradleModel
|
||||
|
||||
@Suppress("unused")
|
||||
class KaptProjectResolverExtension : AbstractProjectResolverExtension() {
|
||||
override fun getExtraProjectModelClasses() = setOf(KaptGradleModel::class.java)
|
||||
override fun getToolingExtensionsClasses() = setOf(KaptModelBuilderService::class.java)
|
||||
|
||||
override fun populateModuleExtraModels(gradleModule: IdeaModule, ideModule: DataNode<ModuleData>) {
|
||||
val kaptModel = resolverCtx.getExtraProject(gradleModule, KaptGradleModel::class.java) ?: return
|
||||
|
||||
if (kaptModel.isEnabled) {
|
||||
for (sourceSet in kaptModel.sourceSets) {
|
||||
populateAndroidModuleModelIfNeeded(ideModule, sourceSet)
|
||||
|
||||
val sourceSetDataNode = ideModule.findGradleSourceSet(sourceSet.sourceSetName) ?: continue
|
||||
|
||||
fun addSourceSet(path: String, type: ExternalSystemSourceType) {
|
||||
val contentRootData = ContentRootData(GRADLE_SYSTEM_ID, path)
|
||||
contentRootData.storePath(type, path)
|
||||
val child = sourceSetDataNode.createChild(ProjectKeys.CONTENT_ROOT, contentRootData)
|
||||
sourceSetDataNode.addChild(child)
|
||||
}
|
||||
|
||||
val sourceType = if (sourceSet.isTest) ExternalSystemSourceType.TEST_GENERATED else ExternalSystemSourceType.SOURCE_GENERATED
|
||||
sourceSet.generatedSourcesDirFile?.let { addSourceSet(it.absolutePath, sourceType) }
|
||||
sourceSet.generatedKotlinSourcesDirFile?.let { addSourceSet(it.absolutePath, sourceType) }
|
||||
|
||||
sourceSet.generatedClassesDirFile?.let { generatedClassesDir ->
|
||||
val libraryData = LibraryData(GRADLE_SYSTEM_ID, "Kapt generated classes")
|
||||
libraryData.addPath(LibraryPathType.BINARY, generatedClassesDir.absolutePath)
|
||||
val libraryDependencyData = LibraryDependencyData(ideModule.data, libraryData, LibraryLevel.MODULE)
|
||||
val child = sourceSetDataNode.createChild(ProjectKeys.LIBRARY_DEPENDENCY, libraryDependencyData)
|
||||
sourceSetDataNode.addChild(child)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
super.populateModuleExtraModels(gradleModule, ideModule)
|
||||
}
|
||||
|
||||
private fun populateAndroidModuleModelIfNeeded(ideModule: DataNode<ModuleData>, sourceSet: KaptSourceSetModel) {
|
||||
if (true) return
|
||||
|
||||
ideModule.findAndroidModuleModel()?.let { androidModelAny ->
|
||||
// We can cast to AndroidModuleModel cause we already checked in findAndroidModuleModel() that the class exists
|
||||
|
||||
val androidModel = androidModelAny.data as? AndroidModuleModel
|
||||
val generatedKotlinSources = sourceSet.generatedKotlinSourcesDirFile
|
||||
|
||||
if (androidModel != null && generatedKotlinSources != null) {
|
||||
// Looks like we can't add generated class root as a library dependency
|
||||
androidModel.registerExtraGeneratedSourceFolder(generatedKotlinSources)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun DataNode<ModuleData>.findAndroidModuleModel(): DataNode<*>? {
|
||||
val modelClassName = "com.android.tools.idea.gradle.project.model.AndroidModuleModel"
|
||||
val node = children.firstOrNull { it.key.dataType == modelClassName } ?: return null
|
||||
return if (!hasClassInClasspath(modelClassName)) null else node
|
||||
}
|
||||
|
||||
private fun hasClassInClasspath(name: String): Boolean {
|
||||
return try {
|
||||
Class.forName(name) != null
|
||||
} catch (thr: Throwable) {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
private fun DataNode<ModuleData>.findGradleSourceSet(sourceSetName: String): DataNode<GradleSourceSetData>? {
|
||||
val moduleName = data.id
|
||||
for (child in children) {
|
||||
val gradleSourceSetData = child.data as? GradleSourceSetData ?: continue
|
||||
if (gradleSourceSetData.id == "$moduleName:$sourceSetName") {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return child as DataNode<GradleSourceSetData>?
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
class KaptModelBuilderService : AbstractKotlinGradleModelBuilder() {
|
||||
override fun getErrorMessageBuilder(project: Project, e: Exception): ErrorMessageBuilder {
|
||||
return ErrorMessageBuilder.create(project, e, "Gradle import errors")
|
||||
.withDescription("Unable to build kotlin-kapt plugin configuration")
|
||||
}
|
||||
|
||||
override fun canBuild(modelName: String?): Boolean = modelName == KaptGradleModel::class.java.name
|
||||
|
||||
override fun buildAll(modelName: String?, project: Project): Any {
|
||||
val kaptPlugin = project.plugins.findPlugin("kotlin-kapt")
|
||||
val kaptIsEnabled = kaptPlugin != null
|
||||
|
||||
val sourceSets = mutableListOf<KaptSourceSetModel>()
|
||||
|
||||
if (kaptIsEnabled) {
|
||||
project.getAllTasks(false)[project]?.forEach { compileTask ->
|
||||
if (compileTask.javaClass.name !in kotlinCompileTaskClasses) return@forEach
|
||||
|
||||
val sourceSetName = compileTask.getSourceSetName()
|
||||
val isTest = sourceSetName.toLowerCase().endsWith("test")
|
||||
|
||||
val kaptGeneratedSourcesDir = getKaptDirectory("getKaptGeneratedSourcesDir", project, sourceSetName)
|
||||
val kaptGeneratedClassesDir = getKaptDirectory("getKaptGeneratedClassesDir", project, sourceSetName)
|
||||
val kaptGeneratedKotlinSourcesDir = getKaptDirectory("getKaptGeneratedKotlinSourcesDir", project, sourceSetName)
|
||||
sourceSets += KaptSourceSetModelImpl(
|
||||
sourceSetName, isTest, kaptGeneratedSourcesDir, kaptGeneratedClassesDir, kaptGeneratedKotlinSourcesDir)
|
||||
}
|
||||
}
|
||||
|
||||
return KaptGradleModelImpl(kaptIsEnabled, project.buildDir, sourceSets)
|
||||
}
|
||||
|
||||
private fun getKaptDirectory(funName: String, project: Project, sourceSetName: String): String {
|
||||
val kotlinKaptPlugin = project.plugins.findPlugin("kotlin-kapt") ?: return ""
|
||||
|
||||
val targetMethod = kotlinKaptPlugin::class.java.methods.firstOrNull {
|
||||
Modifier.isStatic(it.modifiers) && it.name == funName && it.parameterCount == 2
|
||||
} ?: return ""
|
||||
|
||||
return (targetMethod(null, project, sourceSetName) as? File)?.absolutePath ?: ""
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user