1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-27 00:38:11 -07:00

Refactor the three compilers.

This commit is contained in:
Cedric Beust 2016-06-24 01:28:52 -08:00
parent a8b693b238
commit c9393cb0ca
8 changed files with 66 additions and 88 deletions

View file

@ -2,7 +2,7 @@ package com.beust.kobalt.api
import com.beust.kobalt.TaskResult import com.beust.kobalt.TaskResult
interface ICompiler : Comparable<ICompiler> { interface ICompilerDescription : Comparable<ICompilerDescription> {
/** /**
* The suffixes handled by this compiler (without the dot, e.g. "java" or "kt"). * The suffixes handled by this compiler (without the dot, e.g. "java" or "kt").
*/ */
@ -27,7 +27,7 @@ interface ICompiler : Comparable<ICompiler> {
*/ */
val priority: Int get() = DEFAULT_PRIORITY 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? * Can this compiler be passed directories or does it need individual source files?
@ -36,5 +36,5 @@ interface ICompiler : Comparable<ICompiler> {
} }
interface ICompilerContributor : IProjectAffinity, IContributor { interface ICompilerContributor : IProjectAffinity, IContributor {
fun compilersFor(project: Project, context: KobaltContext): List<ICompiler> fun compilersFor(project: Project, context: KobaltContext): List<ICompilerDescription>
} }

View file

@ -18,7 +18,7 @@ class CompilerUtils @Inject constructor(val files: KFiles,
class CompilerResult(val successResults: List<TaskResult>, val failedResult: TaskResult?) class CompilerResult(val successResults: List<TaskResult>, val failedResult: TaskResult?)
fun invokeCompiler(project: Project, context: KobaltContext, compiler: ICompiler, fun invokeCompiler(project: Project, context: KobaltContext, compiler: ICompilerDescription,
sourceDirectories: List<File>, isTest: Boolean): CompilerResult { sourceDirectories: List<File>, isTest: Boolean): CompilerResult {
val results = arrayListOf<TaskResult>() val results = arrayListOf<TaskResult>()
var failedResult: TaskResult? = null var failedResult: TaskResult? = null
@ -47,7 +47,7 @@ class CompilerUtils @Inject constructor(val files: KFiles,
return CompilerResult(results, failedResult) 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 { : CompilerResult {
val results = arrayListOf<TaskResult>() val results = arrayListOf<TaskResult>()
var failedResult: TaskResult? = null 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. * 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. * 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<File>, sourceSuffixes: List<String>): CompilerActionInfo { isTest: Boolean, sourceDirectories: List<File>, sourceSuffixes: List<String>): CompilerActionInfo {
copyResources(project, context, SourceSet.of(isTest)) copyResources(project, context, SourceSet.of(isTest))

View file

@ -163,7 +163,7 @@ open class JvmCompilerPlugin @Inject constructor(
/** /**
* Swap the Java and Kotlin compilers from the list. * Swap the Java and Kotlin compilers from the list.
*/ */
fun swapJavaAndKotlin(allCompilers: List<ICompiler>): List<ICompiler> { fun swapJavaAndKotlin(allCompilers: List<ICompilerDescription>): List<ICompilerDescription> {
val result = ArrayList(allCompilers) val result = ArrayList(allCompilers)
var ik = -1 var ik = -1
var ij = -1 var ij = -1

View file

@ -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<String>, 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
}
}

View file

@ -1,13 +1,16 @@
package com.beust.kobalt.plugin.groovy package com.beust.kobalt.plugin.groovy
import com.beust.kobalt.TaskResult import com.beust.kobalt.TaskResult
import com.beust.kobalt.api.* import com.beust.kobalt.api.CompilerActionInfo
import com.beust.kobalt.homeDir 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.maven.DependencyManager
import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.Strings import com.beust.kobalt.misc.Strings
import com.beust.kobalt.misc.log 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.Inject
import com.google.inject.Singleton import com.google.inject.Singleton
import java.io.File import java.io.File
@ -19,24 +22,7 @@ class GroovyPlugin @Inject constructor(val groovyCompiler: GroovyCompiler) : ICo
if (hasSourceFiles(project)) 1 else 0 if (hasSourceFiles(project)) 1 else 0
// ICompilerContributor // ICompilerContributor
val compiler = object: ICompiler { val compiler = CompilerDescription(GroovyCompiler.SUFFIXES, "groovy", groovyCompiler)
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
}
}
override fun compilersFor(project: Project, context: KobaltContext) = listOf(compiler) 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 = 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 { companion object {
val SUFFIXES = listOf("groovy") 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 { 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 val size = info.sourceFiles.size
log(1, "Groovy compiling " + size + " " + Strings.pluralize(size, "file")) log(1, "Groovy compiling " + size + " " + Strings.pluralize(size, "file"))
val result = invokeGroovyCompiler(info) val result = invokeGroovyCompiler(info)

View file

@ -12,6 +12,7 @@ import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.Strings import com.beust.kobalt.misc.Strings
import com.beust.kobalt.misc.log import com.beust.kobalt.misc.log
import com.beust.kobalt.misc.warn import com.beust.kobalt.misc.warn
import com.beust.kobalt.plugin.ICompiler
import com.google.inject.Inject import com.google.inject.Inject
import com.google.inject.Singleton import com.google.inject.Singleton
import java.io.File import java.io.File
@ -21,7 +22,7 @@ import javax.tools.JavaFileObject
import javax.tools.ToolProvider import javax.tools.ToolProvider
@Singleton @Singleton
class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) { class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) : ICompiler {
fun compilerAction(executable: File) = object : ICompilerAction { fun compilerAction(executable: File) = object : ICompilerAction {
override fun compile(projectName: String?, info: CompilerActionInfo): TaskResult { override fun compile(projectName: String?, info: CompilerActionInfo): TaskResult {
if (info.sourceFiles.isEmpty()) { if (info.sourceFiles.isEmpty()) {
@ -106,8 +107,8 @@ class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) {
return jvmCompiler.doCompile(project, context, compilerAction(executable), cai) return jvmCompiler.doCompile(project, context, compilerAction(executable), cai)
} }
fun compile(project: Project?, context: KobaltContext?, cai: CompilerActionInfo) : TaskResult override fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo) : TaskResult
= run(project, context, cai, JavaInfo.create(File(SystemProperties.javaBase)).javacExecutable!!) = run(project, context, info, JavaInfo.create(File(SystemProperties.javaBase)).javacExecutable!!)
fun javadoc(project: Project?, context: KobaltContext?, cai: CompilerActionInfo) : TaskResult fun javadoc(project: Project?, context: KobaltContext?, cai: CompilerActionInfo) : TaskResult
= run(project, context, cai, JavaInfo.create(File(SystemProperties.javaBase)).javadocExecutable!!) = run(project, context, cai, JavaInfo.create(File(SystemProperties.javaBase)).javadocExecutable!!)

View file

@ -8,6 +8,7 @@ import com.beust.kobalt.internal.BaseJvmPlugin
import com.beust.kobalt.internal.JvmCompilerPlugin import com.beust.kobalt.internal.JvmCompilerPlugin
import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.warn import com.beust.kobalt.misc.warn
import com.beust.kobalt.plugin.CompilerDescription
import java.io.File import java.io.File
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -53,24 +54,9 @@ class JavaPlugin @Inject constructor(val javaCompiler: JavaCompiler, override va
configurationFor(project)?.compilerArgs ?: listOf<String>()) configurationFor(project)?.compilerArgs ?: listOf<String>())
// ICompilerContributor // ICompilerContributor
val compiler = object: ICompiler { val compiler = CompilerDescription(listOf("java"), "java", javaCompiler)
override val sourceSuffixes = listOf("java")
override val sourceDirectory = "java" override fun compilersFor(project: Project, context: KobaltContext) = listOf(compiler)
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)
// ITestSourceDirectoryContributor // ITestSourceDirectoryContributor
override fun testSourceDirectoriesFor(project: Project, context: KobaltContext) override fun testSourceDirectoriesFor(project: Project, context: KobaltContext)

View file

@ -14,7 +14,8 @@ import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.KobaltExecutors import com.beust.kobalt.misc.KobaltExecutors
import com.beust.kobalt.misc.log import com.beust.kobalt.misc.log
import com.beust.kobalt.misc.warn 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.Inject
import javax.inject.Singleton import javax.inject.Singleton
@ -85,14 +86,16 @@ class KotlinPlugin @Inject constructor(val executors: KobaltExecutors, val depen
// return TaskResult(success) // return TaskResult(success)
// } // }
private fun compilePrivate(project: Project, cpList: List<IClasspathDependency>, sources: List<String>, class KotlinCompiler : ICompiler {
outputDirectory: File, compilerArgs: List<String>): TaskResult { override fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo): TaskResult {
return kotlinCompilePrivate { return kotlinCompilePrivate {
classpath(cpList.map { it.jarFile.get().absolutePath }) classpath(info.dependencies.map { it.jarFile.get().absolutePath })
sourceFiles(sources) sourceFiles(info.sourceFiles)
compilerArgs(compilerArgs) compilerArgs(info.compilerArgs)
output = outputDirectory output = info.outputDir
}.compile(project, context) }.compile(project, context)
}
} }
private fun getKotlinCompilerJar(name: String): String { private fun getKotlinCompilerJar(name: String): String {
@ -115,33 +118,9 @@ class KotlinPlugin @Inject constructor(val executors: KobaltExecutors, val depen
// ICompilerContributor // ICompilerContributor
val compiler = object: ICompiler { /** The Kotlin compiler should run before the Java one, hence priority - 5 */
override val sourceSuffixes = listOf("kt") val compiler = CompilerDescription(listOf("kt"), "kotlin", KotlinCompiler(),
ICompilerDescription.DEFAULT_PRIORITY - 5)
/** 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
}
}
override fun compilersFor(project: Project, context: KobaltContext) = arrayListOf(compiler) override fun compilersFor(project: Project, context: KobaltContext) = arrayListOf(compiler)