-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kapt3: Extract annotation processing to its own task in Gradle. Now t…
…he kotlinCompile task should know nothing about kapt, for the main task it's just a regular Java source root.
- Loading branch information
Showing
11 changed files
with
336 additions
and
181 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...ols/kotlin-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/internal/KaptTask.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.jetbrains.kotlin.gradle.internal | ||
|
||
import org.gradle.api.GradleException | ||
import org.gradle.api.tasks.SourceTask | ||
import org.gradle.api.tasks.TaskAction | ||
import org.gradle.api.tasks.compile.AbstractCompile | ||
import org.jetbrains.kotlin.cli.common.ExitCode | ||
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments | ||
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler | ||
import org.jetbrains.kotlin.com.intellij.openapi.util.io.FileUtil | ||
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmOptionsImpl | ||
import org.jetbrains.kotlin.gradle.dsl.fillDefaultValues | ||
import org.jetbrains.kotlin.gradle.tasks.* | ||
import java.io.File | ||
|
||
open class KaptTask : AbstractCompile() { | ||
private val rawSourceRoots = FilteringSourceRootsContainer({ !it.isInsideDestinationDir() }) | ||
private val args = K2JVMCompilerArguments().apply { fillDefaultValues() } | ||
|
||
internal val pluginOptions = CompilerPluginOptions() | ||
internal lateinit var kotlinCompileTask: KotlinCompile | ||
|
||
override fun setSource(sources: Any?) { | ||
val filteredSources = rawSourceRoots.set(sources) | ||
super.setSource(filteredSources) | ||
} | ||
|
||
override fun source(vararg sources: Any?): SourceTask? { | ||
val filteredSources = rawSourceRoots.add(*sources) | ||
return super.source(filteredSources) | ||
} | ||
|
||
private fun File.isInsideDestinationDir(): Boolean { | ||
return FileUtil.isAncestor(destinationDir, this, /* strict = */ false) | ||
} | ||
|
||
private fun processCompilerExitCode(exitCode: ExitCode) { | ||
when (exitCode) { | ||
ExitCode.COMPILATION_ERROR -> throw GradleException("Annotation processing error. See log for more details") | ||
ExitCode.INTERNAL_ERROR -> throw GradleException("Annotation processing internal error. See log for more details") | ||
else -> {} | ||
} | ||
} | ||
|
||
@TaskAction | ||
override fun compile() { | ||
/** Delete everything inside the [destinationDir] */ | ||
destinationDir.deleteRecursively() | ||
destinationDir.mkdirs() | ||
|
||
val compiler = K2JVMCompiler() | ||
val sourceRoots = SourceRoots.ForJvm.create(getSource(), rawSourceRoots) | ||
val compileClasspath = classpath.filter(File::exists) | ||
|
||
args.moduleName = kotlinCompileTask.moduleName | ||
args.pluginClasspaths = pluginOptions.classpath.toTypedArray() | ||
args.pluginOptions = pluginOptions.arguments.toTypedArray() | ||
kotlinCompileTask.parentKotlinOptionsImpl?.updateArguments(args) | ||
KotlinJvmOptionsImpl().updateArguments(args) | ||
|
||
processCompilerExitCode(compileJvmNotIncrementally(compiler, logger, | ||
sourceRoots.kotlinSourceFiles, sourceRoots.javaSourceRoots, compileClasspath, | ||
destinationDir, args)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.