1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 16:28:12 -07:00

Compilers are now plug-in actors.

This commit is contained in:
Cedric Beust 2015-11-29 09:28:08 -08:00
parent fd5fb983e2
commit 87975284d9
15 changed files with 88 additions and 33 deletions

View file

@ -0,0 +1,18 @@
package com.beust.kobalt.api
interface IAffinity {
companion object {
/**
* The recommended default affinity if your plug-in can run this project. Use a higher
* number if you expect to compete against other actors and you'd like to win over them.
*/
const val DEFAULT_POSITIVE_AFFINITY = 100
}
/**
* @return an integer indicating the affinity of your actor for the given project. The actor that returns
* the highest affinity gets selectedyour affinity for running the current project. The runner with
* the highest affinity gets selected.
*/
fun affinity(project: Project, context: KobaltContext) : Int
}

View file

@ -0,0 +1,16 @@
package com.beust.kobalt.api
import com.beust.kobalt.TaskResult
import com.beust.kobalt.maven.dependency.IClasspathDependency
import java.io.File
interface ICompilerContributor : IAffinity {
fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo) : TaskResult
}
data class CompilerActionInfo(val directory: String?,
val dependencies: List<IClasspathDependency>,
val sourceFiles: List<String>,
val outputDir: File,
val compilerArgs: List<String>)

View file

@ -1,7 +1,5 @@
package com.beust.kobalt.api package com.beust.kobalt.api
import com.beust.kobalt.internal.CompilerActionInfo
/** /**
* Plug-ins can alter what is passed to the compiler by implementing this interface. * Plug-ins can alter what is passed to the compiler by implementing this interface.
*/ */

View file

@ -1,6 +1,6 @@
package com.beust.kobalt.internal package com.beust.kobalt.internal
import com.beust.kobalt.api.IRunnerContributor import com.beust.kobalt.api.IAffinity
import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project import com.beust.kobalt.api.Project
import com.beust.kobalt.maven.dependency.IClasspathDependency import com.beust.kobalt.maven.dependency.IClasspathDependency
@ -10,7 +10,7 @@ public class JUnitRunner() : GenericTestRunner() {
override val mainClass = "org.junit.runner.JUnitCore" override val mainClass = "org.junit.runner.JUnitCore"
override fun affinity(project: Project, context: KobaltContext) = override fun affinity(project: Project, context: KobaltContext) =
if (project.testDependencies.any { it.id.contains("junit")}) IRunnerContributor.DEFAULT_POSITIVE_AFFINITY if (project.testDependencies.any { it.id.contains("junit")}) IAffinity.DEFAULT_POSITIVE_AFFINITY
else 0 else 0
override fun args(project: Project, classpath: List<IClasspathDependency>) = findTestClasses(project, classpath) override fun args(project: Project, classpath: List<IClasspathDependency>) = findTestClasses(project, classpath)

View file

@ -2,10 +2,10 @@ package com.beust.kobalt.internal
import com.beust.kobalt.KobaltException import com.beust.kobalt.KobaltException
import com.beust.kobalt.TaskResult import com.beust.kobalt.TaskResult
import com.beust.kobalt.api.CompilerActionInfo
import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project import com.beust.kobalt.api.Project
import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.maven.dependency.IClasspathDependency
import com.google.inject.Inject import com.google.inject.Inject
import java.io.File import java.io.File
import java.util.* import java.util.*
@ -51,9 +51,6 @@ class JvmCompiler @Inject constructor(val dependencyManager: DependencyManager)
} }
} }
data class CompilerActionInfo(val directory: String?, val dependencies: List<IClasspathDependency>,
val sourceFiles: List<String>, val outputDir: File, val compilerArgs: List<String>)
interface ICompilerAction { interface ICompilerAction {
fun compile(info: CompilerActionInfo): TaskResult fun compile(info: CompilerActionInfo): TaskResult
} }

View file

@ -1,10 +1,8 @@
package com.beust.kobalt.internal package com.beust.kobalt.internal
import com.beust.kobalt.KobaltException
import com.beust.kobalt.TaskResult import com.beust.kobalt.TaskResult
import com.beust.kobalt.api.BasePlugin import com.beust.kobalt.api.*
import com.beust.kobalt.api.IProjectContributor
import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project
import com.beust.kobalt.api.annotation.ExportedProjectProperty import com.beust.kobalt.api.annotation.ExportedProjectProperty
import com.beust.kobalt.api.annotation.Task import com.beust.kobalt.api.annotation.Task
import com.beust.kobalt.maven.DepFactory import com.beust.kobalt.maven.DepFactory
@ -138,9 +136,18 @@ abstract class JvmCompilerPlugin @Inject constructor(
@Task(name = JavaPlugin.TASK_COMPILE, description = "Compile the project") @Task(name = JavaPlugin.TASK_COMPILE, description = "Compile the project")
fun taskCompile(project: Project) : TaskResult { fun taskCompile(project: Project) : TaskResult {
context.variant.maybeGenerateBuildConfig(project, context) context.variant.maybeGenerateBuildConfig(project, context)
return doCompile(project, createCompilerActionInfo(project, context)) val info = createCompilerActionInfo(project, context)
val compiler = selectAffinityActor(project, context.pluginInfo.compilerContributors)
if (compiler != null) {
return compiler.compile(project, context, info)
} else {
throw KobaltException("Couldn't find any compiler for project ${project.name}")
}
} }
private fun <T : IAffinity> selectAffinityActor(project: Project, actors: List<T>) : T? =
actors.maxBy { it.affinity(project, context) }
@Task(name = JavaPlugin.TASK_JAVADOC, description = "Run Javadoc") @Task(name = JavaPlugin.TASK_JAVADOC, description = "Run Javadoc")
fun taskJavadoc(project: Project) = doJavadoc(project, createCompilerActionInfo(project, context)) fun taskJavadoc(project: Project) = doJavadoc(project, createCompilerActionInfo(project, context))

View file

@ -70,6 +70,9 @@ class KobaltPluginXml {
@XmlElement(name = "classpath-interceptors") @JvmField @XmlElement(name = "classpath-interceptors") @JvmField
var classpathInterceptors: ClassNameXml? = null var classpathInterceptors: ClassNameXml? = null
@XmlElement(name = "compiler-contributors") @JvmField
var compilerContributors: ClassNameXml? = null
} }
class ContributorXml { class ContributorXml {
@ -100,6 +103,7 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
val runnerContributors = arrayListOf<IRunnerContributor>() val runnerContributors = arrayListOf<IRunnerContributor>()
val testRunnerContributors = arrayListOf<IRunnerContributor>() val testRunnerContributors = arrayListOf<IRunnerContributor>()
val classpathInterceptors = arrayListOf<IClasspathInterceptor>() val classpathInterceptors = arrayListOf<IClasspathInterceptor>()
val compilerContributors = arrayListOf<ICompilerContributor>()
// Future contributors: // Future contributors:
// source files // source files
@ -182,6 +186,9 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
xml.classpathInterceptors?.className?.forEach { xml.classpathInterceptors?.className?.forEach {
classpathInterceptors.add(factory.instanceOf(forName(it)) as IClasspathInterceptor) classpathInterceptors.add(factory.instanceOf(forName(it)) as IClasspathInterceptor)
} }
xml.compilerContributors?.className?.forEach {
compilerContributors.add(factory.instanceOf(forName(it)) as ICompilerContributor)
}
} }
fun addPluginInfo(pluginInfo: PluginInfo) { fun addPluginInfo(pluginInfo: PluginInfo) {

View file

@ -1,6 +1,6 @@
package com.beust.kobalt.internal package com.beust.kobalt.internal
import com.beust.kobalt.api.IRunnerContributor import com.beust.kobalt.api.IAffinity
import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project import com.beust.kobalt.api.Project
import com.beust.kobalt.maven.dependency.IClasspathDependency import com.beust.kobalt.maven.dependency.IClasspathDependency
@ -12,7 +12,7 @@ public class TestNgRunner() : GenericTestRunner() {
override val mainClass = "org.testng.TestNG" override val mainClass = "org.testng.TestNG"
override fun affinity(project: Project, context: KobaltContext) = override fun affinity(project: Project, context: KobaltContext) =
if (project.testDependencies.any { it.id.contains("testng")}) IRunnerContributor.DEFAULT_POSITIVE_AFFINITY if (project.testDependencies.any { it.id.contains("testng")}) IAffinity.DEFAULT_POSITIVE_AFFINITY
else 0 else 0
override fun args(project: Project, classpath: List<IClasspathDependency>) = arrayListOf<String>().apply { override fun args(project: Project, classpath: List<IClasspathDependency>) = arrayListOf<String>().apply {

View file

@ -4,7 +4,6 @@ import com.beust.kobalt.*
import com.beust.kobalt.api.* import com.beust.kobalt.api.*
import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.api.annotation.Task import com.beust.kobalt.api.annotation.Task
import com.beust.kobalt.internal.CompilerActionInfo
import com.beust.kobalt.maven.MavenId import com.beust.kobalt.maven.MavenId
import com.beust.kobalt.maven.dependency.FileDependency import com.beust.kobalt.maven.dependency.FileDependency
import com.beust.kobalt.maven.dependency.IClasspathDependency import com.beust.kobalt.maven.dependency.IClasspathDependency
@ -129,7 +128,6 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, v
val androidJar = Paths.get(androidHome(project), "platforms", "android-$compileSdkVersion", "android.jar") val androidJar = Paths.get(androidHome(project), "platforms", "android-$compileSdkVersion", "android.jar")
val applicationId = configurationFor(project)?.applicationId!! val applicationId = configurationFor(project)?.applicationId!!
val intermediates = AndroidFiles.intermediates(project) val intermediates = AndroidFiles.intermediates(project)
val crunchedPngDir = KFiles.joinAndMakeDir(AndroidFiles.intermediates(project).toString(), "res")
// AaptCommand(project, aapt, "crunch").call(listOf( // AaptCommand(project, aapt, "crunch").call(listOf(
// "-v", // "-v",
@ -392,7 +390,7 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler, v
// IRunContributor // IRunContributor
override fun affinity(project: Project, context: KobaltContext): Int { override fun affinity(project: Project, context: KobaltContext): Int {
val manifest = AndroidFiles.manifest(project, context) val manifest = AndroidFiles.manifest(project, context)
return if (File(manifest).exists()) IRunnerContributor.DEFAULT_POSITIVE_AFFINITY else 0 return if (File(manifest).exists()) IAffinity.DEFAULT_POSITIVE_AFFINITY else 0
} }
override fun run(project: Project, context: KobaltContext, classpath: List<IClasspathDependency>): TaskResult { override fun run(project: Project, context: KobaltContext, classpath: List<IClasspathDependency>): TaskResult {

View file

@ -74,7 +74,7 @@ class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors,
// IRunContributor // IRunContributor
override fun affinity(project: Project, context: KobaltContext): Int { override fun affinity(project: Project, context: KobaltContext): Int {
return if (configurationFor(project) != null) IRunnerContributor.DEFAULT_POSITIVE_AFFINITY else 0 return if (configurationFor(project) != null) IAffinity.DEFAULT_POSITIVE_AFFINITY else 0
} }
override fun run(project: Project, context: KobaltContext, classpath: List<IClasspathDependency>): TaskResult { override fun run(project: Project, context: KobaltContext, classpath: List<IClasspathDependency>): TaskResult {

View file

@ -3,9 +3,9 @@ package com.beust.kobalt.plugin.java
import com.beust.kobalt.JavaInfo import com.beust.kobalt.JavaInfo
import com.beust.kobalt.SystemProperties import com.beust.kobalt.SystemProperties
import com.beust.kobalt.TaskResult import com.beust.kobalt.TaskResult
import com.beust.kobalt.api.CompilerActionInfo
import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project import com.beust.kobalt.api.Project
import com.beust.kobalt.internal.CompilerActionInfo
import com.beust.kobalt.internal.ICompilerAction import com.beust.kobalt.internal.ICompilerAction
import com.beust.kobalt.internal.JvmCompiler import com.beust.kobalt.internal.JvmCompiler
import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.KFiles

View file

@ -1,12 +1,9 @@
package com.beust.kobalt.plugin.java package com.beust.kobalt.plugin.java
import com.beust.kobalt.TaskResult import com.beust.kobalt.TaskResult
import com.beust.kobalt.api.BasePlugin import com.beust.kobalt.api.*
import com.beust.kobalt.api.Kobalt
import com.beust.kobalt.api.Project
import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.api.annotation.Task import com.beust.kobalt.api.annotation.Task
import com.beust.kobalt.internal.CompilerActionInfo
import com.beust.kobalt.internal.JvmCompiler import com.beust.kobalt.internal.JvmCompiler
import com.beust.kobalt.internal.JvmCompilerPlugin import com.beust.kobalt.internal.JvmCompilerPlugin
import com.beust.kobalt.maven.DepFactory import com.beust.kobalt.maven.DepFactory
@ -30,7 +27,8 @@ class JavaPlugin @Inject constructor(
override val executors: KobaltExecutors, override val executors: KobaltExecutors,
val javaCompiler: JavaCompiler, val javaCompiler: JavaCompiler,
override val jvmCompiler: JvmCompiler) override val jvmCompiler: JvmCompiler)
: JvmCompilerPlugin(localRepo, files, depFactory, dependencyManager, executors, jvmCompiler) { : JvmCompilerPlugin(localRepo, files, depFactory, dependencyManager, executors, jvmCompiler),
ICompilerContributor {
companion object { companion object {
const val PLUGIN_NAME = "Java" const val PLUGIN_NAME = "Java"
const val TASK_COMPILE = "compile" const val TASK_COMPILE = "compile"
@ -91,6 +89,13 @@ class JavaPlugin @Inject constructor(
} }
return result return result
} }
// ICompilerContributor
override fun affinity(project: Project, context: KobaltContext) =
if (project.sourceSuffix == ".java") 1 else 0
override fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo) = doCompile(project, info)
} }
@Directive @Directive

View file

@ -1,10 +1,10 @@
package com.beust.kobalt.plugin.kotlin; package com.beust.kobalt.plugin.kotlin;
import com.beust.kobalt.TaskResult import com.beust.kobalt.TaskResult
import com.beust.kobalt.api.CompilerActionInfo
import com.beust.kobalt.api.Kobalt import com.beust.kobalt.api.Kobalt
import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project import com.beust.kobalt.api.Project
import com.beust.kobalt.internal.CompilerActionInfo
import com.beust.kobalt.internal.ICompilerAction import com.beust.kobalt.internal.ICompilerAction
import com.beust.kobalt.internal.JvmCompiler import com.beust.kobalt.internal.JvmCompiler
import com.beust.kobalt.maven.DepFactory import com.beust.kobalt.maven.DepFactory

View file

@ -1,16 +1,14 @@
package com.beust.kobalt.plugin.kotlin package com.beust.kobalt.plugin.kotlin
import com.beust.kobalt.TaskResult import com.beust.kobalt.TaskResult
import com.beust.kobalt.api.BasePlugin import com.beust.kobalt.api.*
import com.beust.kobalt.api.IClasspathContributor
import com.beust.kobalt.api.Kobalt
import com.beust.kobalt.api.Project
import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.api.annotation.Task import com.beust.kobalt.api.annotation.Task
import com.beust.kobalt.internal.CompilerActionInfo
import com.beust.kobalt.internal.JvmCompiler import com.beust.kobalt.internal.JvmCompiler
import com.beust.kobalt.internal.JvmCompilerPlugin import com.beust.kobalt.internal.JvmCompilerPlugin
import com.beust.kobalt.maven.* import com.beust.kobalt.maven.DepFactory
import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.maven.LocalRepo
import com.beust.kobalt.maven.dependency.FileDependency import com.beust.kobalt.maven.dependency.FileDependency
import com.beust.kobalt.maven.dependency.IClasspathDependency import com.beust.kobalt.maven.dependency.IClasspathDependency
import com.beust.kobalt.maven.dependency.MavenDependency import com.beust.kobalt.maven.dependency.MavenDependency
@ -30,7 +28,7 @@ class KotlinPlugin @Inject constructor(
override val executors: KobaltExecutors, override val executors: KobaltExecutors,
override val jvmCompiler: JvmCompiler) override val jvmCompiler: JvmCompiler)
: JvmCompilerPlugin(localRepo, files, depFactory, dependencyManager, executors, jvmCompiler), : JvmCompilerPlugin(localRepo, files, depFactory, dependencyManager, executors, jvmCompiler),
IClasspathContributor { IClasspathContributor, ICompilerContributor {
companion object { companion object {
const val PLUGIN_NAME = "Kotlin" const val PLUGIN_NAME = "Kotlin"
@ -111,6 +109,13 @@ class KotlinPlugin @Inject constructor(
} else { } else {
listOf() listOf()
} }
// ICompilerContributor
override fun affinity(project: Project, context: KobaltContext) =
if (project.sourceSuffix == ".kt") 1 else 0
override fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo) = doCompile(project, info)
} }
/** /**

View file

@ -48,4 +48,8 @@
<classpath-interceptors> <classpath-interceptors>
<class-name>com.beust.kobalt.plugin.android.AndroidPlugin</class-name> <class-name>com.beust.kobalt.plugin.android.AndroidPlugin</class-name>
</classpath-interceptors> </classpath-interceptors>
<compiler-contributors>
<class-name>com.beust.kobalt.plugin.java.JavaPlugin</class-name>
<class-name>com.beust.kobalt.plugin.kotlin.KotlinPlugin</class-name>
</compiler-contributors>
</kobalt-plugin> </kobalt-plugin>