diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Plugins.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Plugins.kt index a31ddf41..29e902e1 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Plugins.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Plugins.kt @@ -79,7 +79,9 @@ class Plugins @Inject constructor (val taskManagerProvider : Provider + taskManager.dynamicTasks.addAll(it.tasksFor(project, context)) + } } // Now that we have collected all static and dynamic tasks, turn them all into plug-in tasks diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/ITaskContributor.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/ITaskContributor.kt index 55eed61a..83621451 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/ITaskContributor.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/ITaskContributor.kt @@ -8,7 +8,7 @@ import com.beust.kobalt.internal.TaskResult2 * to implement this interface. */ interface ITaskContributor : IContributor { - fun tasksFor(context: KobaltContext) : List + fun tasksFor(project: Project, context: KobaltContext) : List } class DynamicTask(override val plugin: IPlugin, override val name: String, override val doc: String, diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/TaskContributor.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/TaskContributor.kt index 63cc655a..65eb529d 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/TaskContributor.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/TaskContributor.kt @@ -63,5 +63,5 @@ class TaskContributor @Inject constructor(val incrementalManagerFactory: Increme } } - override fun tasksFor(context: KobaltContext) : List = dynamicTasks + override fun tasksFor(project: Project, context: KobaltContext) : List = dynamicTasks } 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 c078c996..9e0747b0 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 @@ -34,10 +34,10 @@ class CompilerUtils @Inject constructor(val files: KFiles, // once and pass them val info = createCompilerActionInfo(project, context, compiler, isTest, sourceDirectories, sourceSuffixes = compiler.sourceSuffixes) - val thisResult = compiler.compile(project, context, info) - results.add(thisResult) - if (!thisResult.success && failedResult == null) { - failedResult = thisResult + val thisResult = invokeCompiler(project, context, compiler, info) + results.addAll(thisResult.successResults) + if (failedResult == null) { + failedResult = thisResult.failedResult } } else { log(2, "Compiler $compiler not running on ${project.name} since no source files were found") @@ -46,6 +46,18 @@ class CompilerUtils @Inject constructor(val files: KFiles, return CompilerResult(results, failedResult) } + fun invokeCompiler(project: Project, context: KobaltContext, compiler: ICompiler, info: CompilerActionInfo) + : CompilerResult { + val results = arrayListOf() + var failedResult: TaskResult? = null + val thisResult = compiler.compile(project, context, info) + results.add(thisResult) + if (!thisResult.success && failedResult == null) { + failedResult = thisResult + } + return CompilerResult(results, failedResult) + } + /** * 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. @@ -55,7 +67,7 @@ class CompilerUtils @Inject constructor(val files: KFiles, copyResources(project, context, SourceSet.of(isTest)) val fullClasspath = if (isTest) dependencyManager.testDependencies(project, context) - else dependencyManager.dependencies(project, context) + else dependencyManager.dependencies(project, context) // Remove all the excluded dependencies from the classpath val classpath = fullClasspath.filter { @@ -66,7 +78,6 @@ class CompilerUtils @Inject constructor(val files: KFiles, else File(project.classesDir(context)) buildDirectory.mkdirs() - val initialSourceDirectories = ArrayList(sourceDirectories) // Source directories from the contributors val contributedSourceDirs = diff --git a/src/main/kotlin/com/beust/kobalt/plugin/application/ApplicationPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/application/ApplicationPlugin.kt index 9f6a4e9f..483a9f3f 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/application/ApplicationPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/application/ApplicationPlugin.kt @@ -127,6 +127,6 @@ class ApplicationPlugin @Inject constructor(val configActor: ConfigActor = taskContributor.dynamicTasks + override fun tasksFor(project: Project, context: KobaltContext): List = taskContributor.dynamicTasks } diff --git a/src/main/kotlin/com/beust/kobalt/plugin/apt/AptPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/apt/AptPlugin.kt index 386574e5..cf2b93d5 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/apt/AptPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/apt/AptPlugin.kt @@ -2,12 +2,16 @@ package com.beust.kobalt.plugin.apt import com.beust.kobalt.api.* import com.beust.kobalt.api.annotation.Directive +import com.beust.kobalt.internal.ActorUtils +import com.beust.kobalt.internal.CompilerUtils import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.log import com.google.common.collect.ArrayListMultimap import com.google.inject.Inject import java.io.File +import java.nio.file.Files +import java.util.* import javax.inject.Singleton /** @@ -18,8 +22,10 @@ import javax.inject.Singleton * (outputDir, etc...). */ @Singleton -class AptPlugin @Inject constructor(val dependencyManager: DependencyManager, val configActor: ConfigActor) - : BasePlugin(), ICompilerFlagContributor, ISourceDirectoryContributor, IConfigActor by configActor { +class AptPlugin @Inject constructor(val dependencyManager: DependencyManager, val configActor: ConfigActor, + val compilerUtils: CompilerUtils) + : BasePlugin(), ICompilerFlagContributor, ISourceDirectoryContributor, ITaskContributor, + IConfigActor by configActor { // ISourceDirectoryContributor @@ -45,6 +51,51 @@ class AptPlugin @Inject constructor(val dependencyManager: DependencyManager, va override val name = PLUGIN_NAME + override fun apply(project: Project, context: KobaltContext) { + } + + // ITaskContributor + override fun tasksFor(project: Project, context: KobaltContext) : List { +// val kapt = kaptConfigs[project.name] +// if (kapt != null) { +// return listOf(DynamicTask(this, "kapt", "Run kapt", project = project, +// reverseDependsOn = listOf(JvmCompilerPlugin.TASK_COMPILE), +// group = AnnotationDefault.GROUP, +// closure = { project -> +// runApt(project, context) +// TaskResult() +// })) +// } else { + return emptyList() +// } +// + } + + private fun runApt(project: Project, context: KobaltContext) { + val kapt = kaptConfigs[project.name] + if (kapt != null) { + + val sourceDir = Files.createTempDirectory("kobalt").toFile() + val javaFile = File(sourceDir, "A.java").apply { + appendText("class A {}") + } + val compilerContributors = context.pluginInfo.compilerContributors + ActorUtils.selectAffinityActors(project, context, + context.pluginInfo.compilerContributors) + + val allCompilers = compilerContributors.flatMap { it.compilersFor(project, context) }.sorted() + val javaCompiler = allCompilers.filter { it.sourceSuffixes.contains("java") }[0] + + val dependencies = dependencyManager.calculateDependencies(project, context) + val info = CompilerActionInfo(sourceDir.absolutePath, dependencies, + listOf(javaFile.absolutePath), listOf("java"), File(sourceDir, "generated"), + listOf()) + + val results = compilerUtils.invokeCompiler(project, context, javaCompiler, info) + println("RESULTS: $results") + } + } + private fun generated(project: Project, context: KobaltContext, outputDir: String) = KFiles.joinAndMakeDir(project.directory, project.buildDirectory, outputDir, context.variant.toIntermediateDir()) @@ -55,6 +106,7 @@ class AptPlugin @Inject constructor(val dependencyManager: DependencyManager, va if (!suffixesBeingCompiled.contains("java")) return emptyList() val result = arrayListOf() + configurationFor(project)?.let { config -> aptDependencies[project.name]?.let { aptDependencies -> result.add("-s") @@ -70,12 +122,16 @@ class AptPlugin @Inject constructor(val dependencyManager: DependencyManager, va fun addAptDependency(dependencies: Dependencies, it: String) { aptDependencies.put(dependencies.project.name, it) } + + private val kaptConfigs: HashMap = hashMapOf() + + fun addKaptConfig(project: Project, kapt: KaptConfig) = kaptConfigs.put(project.name, kapt) } class AptConfig(var outputDir: String = "generated/source/apt") @Directive -public fun Project.apt(init: AptConfig.() -> Unit) { +fun Project.apt(init: AptConfig.() -> Unit) { AptConfig().let { it.init() (Kobalt.findPlugin(AptPlugin.PLUGIN_NAME) as AptPlugin).addConfiguration(this, it) @@ -88,3 +144,13 @@ fun Dependencies.apt(vararg dep: String) { (Kobalt.findPlugin(AptPlugin.PLUGIN_NAME) as AptPlugin).addAptDependency(this, it) } } + +class KaptConfig(var outputDir: String = "generated/source/apt") + +@Directive +fun Project.kapt(init: KaptConfig.() -> Unit) { + KaptConfig().let { + it.init() + (Kobalt.findPlugin(AptPlugin.PLUGIN_NAME) as AptPlugin).addKaptConfig(this, it) + } +} diff --git a/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt index 79c5695e..aba925bc 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/PackagingPlugin.kt @@ -151,7 +151,7 @@ class PackagingPlugin @Inject constructor(val dependencyManager : DependencyMana } //ITaskContributor - override fun tasksFor(context: KobaltContext): List = taskContributor.dynamicTasks + override fun tasksFor(project: Project, context: KobaltContext): List = taskContributor.dynamicTasks } @Directive