Refactor compiler arguments in Gradle tasks:

Pull create/setup args functions up to CompilerArgumentsAware interface
Remove diamond-shaped CompilerArgumentsAware inheritance
Provide the default implementation for serialization in the interface
Make KotlinJsDce implement CompilerArgumentsAware
Implement the compiler args logic for Kapt & GenerateStubs tasks
This commit is contained in:
Sergey Igushkin
2017-11-15 16:16:58 +03:00
parent 6b2c22aff1
commit 62f2876230
5 changed files with 62 additions and 47 deletions
@@ -18,14 +18,30 @@ package org.jetbrains.kotlin.gradle.dsl
import groovy.lang.Closure
import org.gradle.api.Task
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.jetbrains.kotlin.cli.common.arguments.CommonToolArguments
import org.jetbrains.kotlin.compilerRunner.ArgumentUtils
interface CompilerArgumentAware {
interface CompilerArgumentAware<T : CommonToolArguments> {
@get:Input
val serializedCompilerArguments: List<String>
get() = ArgumentUtils.convertArgumentsToStringList(prepareCompilerArguments())
@get:Internal
val defaultSerializedCompilerArguments: List<String>
get() = createCompilerArgs()
.also { setupCompilerArgs(it, defaultsOnly = true) }
.let(ArgumentUtils::convertArgumentsToStringList)
fun createCompilerArgs(): T
fun setupCompilerArgs(args: T, defaultsOnly: Boolean = false)
}
interface KotlinCompile<T : KotlinCommonOptions> : Task, CompilerArgumentAware {
internal fun <T : CommonToolArguments> CompilerArgumentAware<T>.prepareCompilerArguments() =
createCompilerArgs().also { setupCompilerArgs(it) }
interface KotlinCompile<T : KotlinCommonOptions> : Task {
@get:Internal
val kotlinOptions: T
@@ -19,6 +19,8 @@ package org.jetbrains.kotlin.gradle.internal
import com.intellij.openapi.util.io.FileUtil
import org.gradle.api.tasks.*
import org.gradle.api.tasks.incremental.IncrementalTaskInputs
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.gradle.dsl.prepareCompilerArguments
import org.jetbrains.kotlin.gradle.plugin.kotlinDebug
import org.jetbrains.kotlin.gradle.tasks.FilteringSourceRootsContainer
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
@@ -58,6 +60,15 @@ open class KaptGenerateStubsTask : KotlinCompile() {
!source.isInside(generatedSourcesDir)
}
override fun setupCompilerArgs(args: K2JVMCompilerArguments, defaultsOnly: Boolean) {
kotlinCompileTask.setupCompilerArgs(args)
args.pluginClasspaths = (pluginOptions.classpath + args.pluginClasspaths!!).toSet().toTypedArray()
args.pluginOptions = (pluginOptions.arguments + args.pluginOptions!!).toTypedArray()
args.verbose = project.hasProperty("kapt.verbose") && project.property("kapt.verbose").toString().toBoolean() == true
args.classpathAsList = this.compileClasspath.toList()
args.destinationAsFile = this.destinationDir
}
override fun execute(inputs: IncrementalTaskInputs) {
val sourceRoots = kotlinCompileTask.getSourceRoots()
val allKotlinSources = sourceRoots.kotlinSourceFiles
@@ -70,15 +81,7 @@ open class KaptGenerateStubsTask : KotlinCompile() {
}
sourceRoots.log(this.name, logger)
// todo handle the args like those of the compile tasks
val args = createCompilerArgs()
kotlinCompileTask.setupCompilerArgs(args)
args.pluginClasspaths = (pluginOptions.classpath + args.pluginClasspaths!!).toSet().toTypedArray()
args.pluginOptions = (pluginOptions.arguments + args.pluginOptions!!).toTypedArray()
args.verbose = project.hasProperty("kapt.verbose") && project.property("kapt.verbose").toString().toBoolean() == true
args.classpathAsList = this.compileClasspath.toList()
args.destinationAsFile = this.destinationDir
val args = prepareCompilerArguments()
compilerCalled = true
callCompiler(args, sourceRoots, ChangedFiles(inputs))
@@ -1,6 +1,7 @@
package org.jetbrains.kotlin.gradle.internal
import com.intellij.openapi.util.io.FileUtil
import com.intellij.openapi.util.text.StringUtil.compareVersionNumbers
import org.gradle.api.GradleException
import org.gradle.api.file.FileCollection
import org.gradle.api.internal.ConventionTask
@@ -9,11 +10,13 @@ import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.compilerRunner.GradleCompilerEnvironment
import org.jetbrains.kotlin.compilerRunner.GradleCompilerRunner
import org.jetbrains.kotlin.compilerRunner.OutputItemsCollectorImpl
import org.jetbrains.kotlin.gradle.plugin.compareVersionNumbers
import org.jetbrains.kotlin.gradle.dsl.CompilerArgumentAware
import org.jetbrains.kotlin.gradle.dsl.prepareCompilerArguments
import org.jetbrains.kotlin.gradle.tasks.*
import java.io.File
open class KaptTask : ConventionTask() {
open class KaptTask : ConventionTask(), CompilerArgumentAware<K2JVMCompilerArguments> {
@get:Internal
internal val pluginOptions = CompilerPluginOptions()
@@ -37,6 +40,16 @@ open class KaptTask : ConventionTask() {
@get:OutputDirectory
lateinit var destinationDir: File
override fun createCompilerArgs(): K2JVMCompilerArguments = K2JVMCompilerArguments()
override fun setupCompilerArgs(args: K2JVMCompilerArguments, defaultsOnly: Boolean) {
kotlinCompileTask.setupCompilerArgs(args)
args.pluginClasspaths = (pluginOptions.classpath + args.pluginClasspaths!!).toSet().toTypedArray()
args.pluginOptions = (pluginOptions.arguments + args.pluginOptions!!).toTypedArray()
args.verbose = project.hasProperty("kapt.verbose") && project.property("kapt.verbose").toString().toBoolean() == true
}
@get:Classpath @get:InputFiles
val classpath: FileCollection
get() = kotlinCompileTask.classpath
@@ -61,13 +74,7 @@ open class KaptTask : ConventionTask() {
val rawSourceRoots = FilteringSourceRootsContainer(sourceRootsFromKotlin, { !isInsideDestinationDirs(it) })
val sourceRoots = SourceRoots.ForJvm.create(kotlinCompileTask.source, rawSourceRoots)
// todo handle the args like those of the compile tasks
val args = K2JVMCompilerArguments()
kotlinCompileTask.setupCompilerArgs(args)
args.pluginClasspaths = (pluginOptions.classpath + args.pluginClasspaths!!).toSet().toTypedArray()
args.pluginOptions = (pluginOptions.arguments + args.pluginOptions!!).toTypedArray()
args.verbose = project.hasProperty("kapt.verbose") && project.property("kapt.verbose").toString().toBoolean() == true
val args = prepareCompilerArguments()
val messageCollector = GradleMessageCollector(logger)
val outputItemCollector = OutputItemsCollectorImpl()
@@ -32,6 +32,14 @@ import org.jetbrains.kotlin.gradle.dsl.KotlinJsDceOptionsImpl
import java.io.File
open class KotlinJsDce : AbstractKotlinCompileTool<K2JSDceArguments>(), KotlinJsDce {
override fun createCompilerArgs(): K2JSDceArguments = K2JSDceArguments()
override fun setupCompilerArgs(args: K2JSDceArguments, defaultsOnly: Boolean) {
dceOptionsImpl.updateArguments(args)
args.declarationsToKeep = keep.toTypedArray()
}
private val dceOptionsImpl = KotlinJsDceOptionsImpl()
@get:Internal
@@ -55,12 +63,7 @@ open class KotlinJsDce : AbstractKotlinCompileTool<K2JSDceArguments>(), KotlinJs
val outputDirArgs = arrayOf("-output-dir", destinationDir.path)
val args = K2JSDceArguments()
dceOptionsImpl.updateArguments(args)
args.declarationsToKeep = keep.toTypedArray()
//todo handle the args like those of the compile tasks
val argsArray = ArgumentUtils.convertArgumentsToStringList(args).toTypedArray()
val argsArray = serializedCompilerArguments.toTypedArray()
val log = GradleKotlinLogger(project.logger)
val allArgs = argsArray + outputDirArgs + inputFiles
@@ -36,6 +36,9 @@ import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.compilerRunner.*
import org.jetbrains.kotlin.gradle.dsl.*
import org.jetbrains.kotlin.gradle.internal.CompilerArgumentAware
import org.jetbrains.kotlin.gradle.internal.CompilerArgumentAware
import org.jetbrains.kotlin.gradle.internal.prepareCompilerArguments
import org.jetbrains.kotlin.gradle.plugin.kotlinDebug
import org.jetbrains.kotlin.gradle.plugin.kotlinInfo
import org.jetbrains.kotlin.gradle.utils.ParsedGradleVersion
@@ -52,7 +55,7 @@ const val KOTLIN_BUILD_DIR_NAME = "kotlin"
const val USING_INCREMENTAL_COMPILATION_MESSAGE = "Using Kotlin incremental compilation"
const val USING_EXPERIMENTAL_JS_INCREMENTAL_COMPILATION_MESSAGE = "Using experimental Kotlin/JS incremental compilation"
abstract class AbstractKotlinCompileTool<T : CommonToolArguments>() : AbstractCompile() {
abstract class AbstractKotlinCompileTool<T : CommonToolArguments>() : AbstractCompile(), CompilerArgumentAware<T> {
// TODO: deprecate and remove
@get:Internal
var compilerJarFile: File? = null
@@ -73,7 +76,7 @@ abstract class AbstractKotlinCompileTool<T : CommonToolArguments>() : AbstractCo
protected abstract fun findKotlinCompilerClasspath(project: Project): List<File>
}
abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : AbstractKotlinCompileTool<T>(), CompilerArgumentAware {
abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : AbstractKotlinCompileTool<T>() {
@get:LocalState
internal val taskBuildDirectory: File
get() = File(File(project.buildDir, KOTLIN_BUILD_DIR_NAME), name).apply { mkdirs() }
@@ -101,22 +104,6 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : AbstractKo
get() = (classpath + additionalClasspath)
.filterTo(LinkedHashSet(), File::exists)
@get:Input
override val serializedCompilerArguments: List<String>
get() {
val arguments = createCompilerArgs()
setupCompilerArgs(arguments)
return ArgumentUtils.convertArgumentsToStringList(arguments)
}
@get:Internal
override val defaultSerializedCompilerArguments: List<String>
get() {
val arguments = createCompilerArgs()
setupCompilerArgs(arguments, true)
return ArgumentUtils.convertArgumentsToStringList(arguments)
}
private val kotlinExt: KotlinProjectExtension
get() = project.extensions.findByType(KotlinProjectExtension::class.java)!!
@@ -213,8 +200,7 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : AbstractKo
}
sourceRoots.log(this.name, logger)
val args = createCompilerArgs()
setupCompilerArgs(args)
val args = prepareCompilerArguments()
compilerCalled = true
callCompiler(args, sourceRoots, ChangedFiles(inputs))
@@ -225,7 +211,7 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments>() : AbstractKo
internal abstract fun callCompiler(args: T, sourceRoots: SourceRoots, changedFiles: ChangedFiles)
open fun setupCompilerArgs(args: T, defaultsOnly: Boolean = false) {
override fun setupCompilerArgs(args: T, defaultsOnly: Boolean) {
args.coroutinesState = when (coroutines) {
Coroutines.ENABLE -> CommonCompilerArguments.ENABLE
Coroutines.WARN -> CommonCompilerArguments.WARN