From c9393cb0ca7dc39b500f8959cdc516b240b806f4 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Fri, 24 Jun 2016 01:28:52 -0800 Subject: [PATCH] Refactor the three compilers. --- .../beust/kobalt/api/ICompilerContributor.kt | 6 +-- .../beust/kobalt/internal/CompilerUtils.kt | 6 +-- .../kobalt/internal/JvmCompilerPlugin.kt | 2 +- .../com/beust/kobalt/plugin/Compilers.kt | 28 ++++++++++ .../kobalt/plugin/groovy/GroovyPlugin.kt | 34 ++++--------- .../beust/kobalt/plugin/java/JavaCompiler.kt | 7 +-- .../beust/kobalt/plugin/java/JavaPlugin.kt | 20 ++------ .../kobalt/plugin/kotlin/KotlinPlugin.kt | 51 ++++++------------- 8 files changed, 66 insertions(+), 88 deletions(-) create mode 100644 src/main/kotlin/com/beust/kobalt/plugin/Compilers.kt diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/ICompilerContributor.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/ICompilerContributor.kt index 84f6694d..e925788b 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/ICompilerContributor.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/ICompilerContributor.kt @@ -2,7 +2,7 @@ package com.beust.kobalt.api import com.beust.kobalt.TaskResult -interface ICompiler : Comparable { +interface ICompilerDescription : Comparable { /** * The suffixes handled by this compiler (without the dot, e.g. "java" or "kt"). */ @@ -27,7 +27,7 @@ interface ICompiler : Comparable { */ val priority: Int get() = DEFAULT_PRIORITY - override fun compareTo(other: ICompiler) = priority.compareTo(other.priority) + override fun compareTo(other: ICompilerDescription) = priority.compareTo(other.priority) /** * Can this compiler be passed directories or does it need individual source files? @@ -36,5 +36,5 @@ interface ICompiler : Comparable { } interface ICompilerContributor : IProjectAffinity, IContributor { - fun compilersFor(project: Project, context: KobaltContext): List + fun compilersFor(project: Project, context: KobaltContext): List } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/CompilerUtils.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/CompilerUtils.kt index fb551133..ffd05b1b 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/CompilerUtils.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/CompilerUtils.kt @@ -18,7 +18,7 @@ class CompilerUtils @Inject constructor(val files: KFiles, class CompilerResult(val successResults: List, val failedResult: TaskResult?) - fun invokeCompiler(project: Project, context: KobaltContext, compiler: ICompiler, + fun invokeCompiler(project: Project, context: KobaltContext, compiler: ICompilerDescription, sourceDirectories: List, isTest: Boolean): CompilerResult { val results = arrayListOf() var failedResult: TaskResult? = null @@ -47,7 +47,7 @@ class CompilerUtils @Inject constructor(val files: KFiles, return CompilerResult(results, failedResult) } - fun invokeCompiler(project: Project, context: KobaltContext, compiler: ICompiler, info: CompilerActionInfo) + fun invokeCompiler(project: Project, context: KobaltContext, compiler: ICompilerDescription, info: CompilerActionInfo) : CompilerResult { val results = arrayListOf() var failedResult: TaskResult? = null @@ -63,7 +63,7 @@ class CompilerUtils @Inject constructor(val files: KFiles, * Create a CompilerActionInfo (all the information that a compiler needs to know) for the given parameters. * Runs all the contributors and interceptors relevant to that task. */ - fun createCompilerActionInfo(project: Project, context: KobaltContext, compiler: ICompiler, + fun createCompilerActionInfo(project: Project, context: KobaltContext, compiler: ICompilerDescription, isTest: Boolean, sourceDirectories: List, sourceSuffixes: List): CompilerActionInfo { copyResources(project, context, SourceSet.of(isTest)) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt index a7dab585..8596cd8c 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt @@ -163,7 +163,7 @@ open class JvmCompilerPlugin @Inject constructor( /** * Swap the Java and Kotlin compilers from the list. */ - fun swapJavaAndKotlin(allCompilers: List): List { + fun swapJavaAndKotlin(allCompilers: List): List { val result = ArrayList(allCompilers) var ik = -1 var ij = -1 diff --git a/src/main/kotlin/com/beust/kobalt/plugin/Compilers.kt b/src/main/kotlin/com/beust/kobalt/plugin/Compilers.kt new file mode 100644 index 00000000..6ed3b8a7 --- /dev/null +++ b/src/main/kotlin/com/beust/kobalt/plugin/Compilers.kt @@ -0,0 +1,28 @@ +package com.beust.kobalt.plugin + +import com.beust.kobalt.TaskResult +import com.beust.kobalt.api.CompilerActionInfo +import com.beust.kobalt.api.ICompilerDescription +import com.beust.kobalt.api.KobaltContext +import com.beust.kobalt.api.Project +import com.beust.kobalt.misc.warn + +interface ICompiler { + fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo): TaskResult +} + +class CompilerDescription(override val sourceSuffixes: List, override val sourceDirectory: String, + val compiler: ICompiler, override val priority: Int = ICompilerDescription.DEFAULT_PRIORITY) + : ICompilerDescription { + override fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo): TaskResult { + val result = + if (info.sourceFiles.size > 0) { + compiler.compile(project, context, info) + } else { + warn("Couldn't find any source files to compile") + TaskResult() + } + return result + } +} + diff --git a/src/main/kotlin/com/beust/kobalt/plugin/groovy/GroovyPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/groovy/GroovyPlugin.kt index 0e35d50d..2a6c0c72 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/groovy/GroovyPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/groovy/GroovyPlugin.kt @@ -1,13 +1,16 @@ package com.beust.kobalt.plugin.groovy import com.beust.kobalt.TaskResult -import com.beust.kobalt.api.* -import com.beust.kobalt.homeDir +import com.beust.kobalt.api.CompilerActionInfo +import com.beust.kobalt.api.ICompilerContributor +import com.beust.kobalt.api.KobaltContext +import com.beust.kobalt.api.Project import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.Strings import com.beust.kobalt.misc.log -import com.beust.kobalt.misc.warn +import com.beust.kobalt.plugin.CompilerDescription +import com.beust.kobalt.plugin.ICompiler import com.google.inject.Inject import com.google.inject.Singleton import java.io.File @@ -19,24 +22,7 @@ class GroovyPlugin @Inject constructor(val groovyCompiler: GroovyCompiler) : ICo if (hasSourceFiles(project)) 1 else 0 // ICompilerContributor - val compiler = object: ICompiler { - override val sourceSuffixes = GroovyCompiler.SUFFIXES - - override val sourceDirectory = "groovy" - - override val priority = 1 - - override fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo): TaskResult { - val result = - if (info.sourceFiles.size > 0) { - groovyCompiler.compile(project, context, info) - } else { - warn("Couldn't find any source files to compile") - TaskResult() - } - return result - } - } + val compiler = CompilerDescription(GroovyCompiler.SUFFIXES, "groovy", groovyCompiler) override fun compilersFor(project: Project, context: KobaltContext) = listOf(compiler) @@ -44,11 +30,9 @@ class GroovyPlugin @Inject constructor(val groovyCompiler: GroovyCompiler) : ICo = KFiles.findSourceFiles(project.directory, project.sourceDirectories, GroovyCompiler.SUFFIXES).size > 0 } -class GroovyCompiler @Inject constructor(dependencyManager: DependencyManager){ +class GroovyCompiler @Inject constructor(dependencyManager: DependencyManager) : ICompiler { companion object { val SUFFIXES = listOf("groovy") - val GROOVY_HOME = homeDir("java/groovy-2.4.7") - val GROOVYC = KFiles.joinDir(GROOVY_HOME, "bin/groovyc") } private val groovyCompilerClass: Class<*> by lazy { @@ -70,7 +54,7 @@ class GroovyCompiler @Inject constructor(dependencyManager: DependencyManager){ } } - fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo): TaskResult { + override fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo): TaskResult { val size = info.sourceFiles.size log(1, "Groovy compiling " + size + " " + Strings.pluralize(size, "file")) val result = invokeGroovyCompiler(info) diff --git a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt index f3436ca4..e8fac6ff 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt @@ -12,6 +12,7 @@ import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.Strings import com.beust.kobalt.misc.log import com.beust.kobalt.misc.warn +import com.beust.kobalt.plugin.ICompiler import com.google.inject.Inject import com.google.inject.Singleton import java.io.File @@ -21,7 +22,7 @@ import javax.tools.JavaFileObject import javax.tools.ToolProvider @Singleton -class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) { +class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) : ICompiler { fun compilerAction(executable: File) = object : ICompilerAction { override fun compile(projectName: String?, info: CompilerActionInfo): TaskResult { if (info.sourceFiles.isEmpty()) { @@ -106,8 +107,8 @@ class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) { return jvmCompiler.doCompile(project, context, compilerAction(executable), cai) } - fun compile(project: Project?, context: KobaltContext?, cai: CompilerActionInfo) : TaskResult - = run(project, context, cai, JavaInfo.create(File(SystemProperties.javaBase)).javacExecutable!!) + override fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo) : TaskResult + = run(project, context, info, JavaInfo.create(File(SystemProperties.javaBase)).javacExecutable!!) fun javadoc(project: Project?, context: KobaltContext?, cai: CompilerActionInfo) : TaskResult = run(project, context, cai, JavaInfo.create(File(SystemProperties.javaBase)).javadocExecutable!!) diff --git a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaPlugin.kt index fbe065e1..8cc934e2 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaPlugin.kt @@ -8,6 +8,7 @@ import com.beust.kobalt.internal.BaseJvmPlugin import com.beust.kobalt.internal.JvmCompilerPlugin import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.warn +import com.beust.kobalt.plugin.CompilerDescription import java.io.File import javax.inject.Inject import javax.inject.Singleton @@ -53,24 +54,9 @@ class JavaPlugin @Inject constructor(val javaCompiler: JavaCompiler, override va configurationFor(project)?.compilerArgs ?: listOf()) // ICompilerContributor - val compiler = object: ICompiler { - override val sourceSuffixes = listOf("java") + val compiler = CompilerDescription(listOf("java"), "java", javaCompiler) - override val sourceDirectory = "java" - - override fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo): TaskResult { - val result = - if (info.sourceFiles.size > 0) { - javaCompiler.compile(project, context, info) - } else { - warn("Couldn't find any source files to compile") - TaskResult() - } - return result - } - } - - override fun compilersFor(project: Project, context: KobaltContext) = arrayListOf(compiler) + override fun compilersFor(project: Project, context: KobaltContext) = listOf(compiler) // ITestSourceDirectoryContributor override fun testSourceDirectoriesFor(project: Project, context: KobaltContext) diff --git a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt index c0d975f3..439cdd97 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt @@ -14,7 +14,8 @@ import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.KobaltExecutors import com.beust.kobalt.misc.log import com.beust.kobalt.misc.warn -import java.io.File +import com.beust.kobalt.plugin.CompilerDescription +import com.beust.kobalt.plugin.ICompiler import javax.inject.Inject import javax.inject.Singleton @@ -85,14 +86,16 @@ class KotlinPlugin @Inject constructor(val executors: KobaltExecutors, val depen // return TaskResult(success) // } - private fun compilePrivate(project: Project, cpList: List, sources: List, - outputDirectory: File, compilerArgs: List): TaskResult { - return kotlinCompilePrivate { - classpath(cpList.map { it.jarFile.get().absolutePath }) - sourceFiles(sources) - compilerArgs(compilerArgs) - output = outputDirectory - }.compile(project, context) + class KotlinCompiler : ICompiler { + override fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo): TaskResult { + return kotlinCompilePrivate { + classpath(info.dependencies.map { it.jarFile.get().absolutePath }) + sourceFiles(info.sourceFiles) + compilerArgs(info.compilerArgs) + output = info.outputDir + }.compile(project, context) + } + } private fun getKotlinCompilerJar(name: String): String { @@ -115,33 +118,9 @@ class KotlinPlugin @Inject constructor(val executors: KobaltExecutors, val depen // ICompilerContributor - val compiler = object: ICompiler { - override val sourceSuffixes = listOf("kt") - - /** The Kotlin compiler should run before the Java one */ - override val priority: Int get() = ICompiler.DEFAULT_PRIORITY - 5 - - override val sourceDirectory = "kotlin" - - /** kotlinc can be passed directories */ - override val canCompileDirectories = true - - override fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo): TaskResult { - val result = - if (info.sourceFiles.size > 0) { - compilePrivate(project, info.dependencies, info.sourceFiles, info.outputDir, info.compilerArgs) - } else { - warn("Couldn't find any source files") - TaskResult() - } - - lp(project, "Compilation " + if (result.success) "succeeded" else "failed") - if (! result.success && result.errorMessage != null) { - error(result.errorMessage!!) - } - return result - } - } + /** The Kotlin compiler should run before the Java one, hence priority - 5 */ + val compiler = CompilerDescription(listOf("kt"), "kotlin", KotlinCompiler(), + ICompilerDescription.DEFAULT_PRIORITY - 5) override fun compilersFor(project: Project, context: KobaltContext) = arrayListOf(compiler)