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

Support for multiple application{} directives.

Fixes https://github.com/cbeust/kobalt/issues/430.
This commit is contained in:
Cedric Beust 2017-04-24 14:58:54 -07:00
parent 65f423ffea
commit 93f5c541f4
3 changed files with 41 additions and 36 deletions

View file

@ -1,10 +1,11 @@
package com.beust.kobalt.api
import com.beust.kobalt.TaskResult
import com.beust.kobalt.api.IClasspathDependency
/**
* Plugins that can run a project (task "run" or "test") should implement this interface.
*
* Currently not used.
*/
interface IRunnerContributor : IContributor, IProjectAffinity {
/**

View file

@ -78,7 +78,7 @@ class PluginInfo(val xml: KobaltPluginXml, val pluginClassLoader: ClassLoader?,
val compilerInterceptors = arrayListOf<ICompilerInterceptor>()
val sourceDirectoriesInterceptors = arrayListOf<ISourceDirectoryInterceptor>()
val buildDirectoryInterceptors = arrayListOf<IBuildDirectoryInterceptor>()
val runnerContributors = arrayListOf<IRunnerContributor>()
// val runnerContributors = arrayListOf<IRunnerContributor>()
val testRunnerContributors = arrayListOf<ITestRunnerContributor>()
val classpathInterceptors = arrayListOf<IClasspathInterceptor>()
val compilerContributors = arrayListOf<ICompilerContributor>()
@ -197,7 +197,7 @@ class PluginInfo(val xml: KobaltPluginXml, val pluginClassLoader: ClassLoader?,
if (this is IPlugin) plugins.add(this)
if (this is IProjectContributor) projectContributors.add(this)
if (this is IRepoContributor) repoContributors.add(this)
if (this is IRunnerContributor) runnerContributors.add(this)
// if (this is IRunnerContributor) runnerContributors.add(this)
if (this is ISourceDirectoryContributor) sourceDirContributors.add(this)
if (this is ISourceDirectoryInterceptor) sourceDirectoriesInterceptors.add(this)
if (this is ITaskContributor) taskContributors.add(this)
@ -225,7 +225,7 @@ class PluginInfo(val xml: KobaltPluginXml, val pluginClassLoader: ClassLoader?,
listOf(projectContributors, classpathContributors, templateContributors,
repoContributors, compilerFlagContributors, compilerInterceptors,
sourceDirectoriesInterceptors, buildDirectoryInterceptors,
runnerContributors, testRunnerContributors, classpathInterceptors,
/* runnerContributors, */ testRunnerContributors, classpathInterceptors,
compilerContributors, docContributors, sourceDirContributors,
testSourceDirContributors, buildConfigFieldContributors,
taskContributors, incrementalTaskContributors, assemblyContributors,
@ -252,7 +252,7 @@ class PluginInfo(val xml: KobaltPluginXml, val pluginClassLoader: ClassLoader?,
compilerInterceptors.addAll(pluginInfo.compilerInterceptors)
sourceDirectoriesInterceptors.addAll(pluginInfo.sourceDirectoriesInterceptors)
buildDirectoryInterceptors.addAll(pluginInfo.buildDirectoryInterceptors)
runnerContributors.addAll(pluginInfo.runnerContributors)
// runnerContributors.addAll(pluginInfo.runnerContributors)
testRunnerContributors.addAll(pluginInfo.testRunnerContributors)
classpathInterceptors.addAll(pluginInfo.classpathInterceptors)
compilerContributors.addAll(pluginInfo.compilerContributors)

View file

@ -6,9 +6,7 @@ import com.beust.kobalt.Plugins
import com.beust.kobalt.TaskResult
import com.beust.kobalt.api.*
import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.api.annotation.Task
import com.beust.kobalt.archive.Archives
import com.beust.kobalt.internal.ActorUtils
import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.maven.aether.Scope
import com.beust.kobalt.misc.KFiles
@ -22,6 +20,9 @@ import com.google.inject.Singleton
import java.io.File
class ApplicationConfig {
@Directive
var taskName: String = "run"
@Directive
var mainClass: String? = null
@ -43,10 +44,10 @@ fun Project.application(init: ApplicationConfig.() -> Unit): ApplicationConfig {
}
@Singleton
class ApplicationPlugin @Inject constructor(val configActor: ConfigActor<ApplicationConfig>,
class ApplicationPlugin @Inject constructor(val configActor: ConfigsActor<ApplicationConfig>,
val executors: KobaltExecutors, val nativeManager: NativeManager,
val dependencyManager: DependencyManager, val taskContributor : TaskContributor, val jvm: Jvm)
: BasePlugin(), IRunnerContributor, ITaskContributor, IConfigActor<ApplicationConfig> by configActor {
: BasePlugin(), ITaskContributor, IConfigsActor<ApplicationConfig> by configActor {
companion object {
const val PLUGIN_NAME = "Application"
@ -56,49 +57,52 @@ class ApplicationPlugin @Inject constructor(val configActor: ConfigActor<Applica
override fun apply(project: Project, context: KobaltContext) {
super.apply(project, context)
taskContributor.addVariantTasks(this, project, context, "run", group = "run", dependsOn = listOf("install"),
runTask = { taskRun(project) })
}
// taskContributor.addVariantTasks(this, project, context, "run", group = "run", dependsOn = listOf("install"),
// runTask = { taskRun(project) })
@Task(name = "run", description = "Run the main class", group = "run", dependsOn = arrayOf("install"))
fun taskRun(project: Project): TaskResult {
val runContributor = ActorUtils.selectAffinityActor(project, context,
context.pluginInfo.runnerContributors)
if (runContributor != null && runContributor.affinity(project, context) > 0) {
return runContributor.run(project, context,
dependencyManager.dependencies(project, context, listOf(Scope.RUNTIME)))
} else {
context.logger.log(project.name, 1,
"Couldn't find a runner for project ${project.name}. Please make sure" +
" your build file contains " +
"an application{} directive with a mainClass=... in it")
return TaskResult()
configurationFor(project)?.let { configs ->
configs.forEach { config ->
taskManager.addTask(this, project, config.taskName,
description = "Run the class " + config.mainClass,
group = "run",
dependsOn = listOf("install"),
task = { run(project, context, config) })
}
}
}
// fun taskRun(project: Project, config: ApplicationConfig): TaskResult {
// val runContributor = ActorUtils.selectAffinityActor(project, context,
// context.pluginInfo.runnerContributors)
// if (runContributor != null && runContributor.affinity(project, context) > 0) {
// return runContributor.run(project, context,
// dependencyManager.dependencies(project, context, listOf(Scope.RUNTIME)))
// } else {
// context.logger.log(project.name, 1,
// "Couldn't find a runner for project ${project.name}. Please make sure" +
// " your build file contains " +
// "an application{} directive with a mainClass=... in it")
// return TaskResult()
// }
// }
private fun isFatJar(packages: List<PackageConfig>, jarName: String): Boolean {
val foundJar = packages.flatMap { it.jars }.filter { jarName.endsWith(it.name) }
return foundJar.size == 1 && foundJar[0].fatJar
}
// IRunContributor
override fun affinity(project: Project, context: KobaltContext): Int {
return if (configurationFor(project) != null) IAffinity.DEFAULT_POSITIVE_AFFINITY else 0
}
override fun run(project: Project, context: KobaltContext, classpath: List<IClasspathDependency>): TaskResult {
var result = TaskResult()
private fun run(project: Project, context: KobaltContext, config: ApplicationConfig): TaskResult {
if (project.nativeDependencies.any()) {
nativeManager.installLibraries(project)
}
configurationFor(project)?.let { config ->
val result =
if (config.mainClass != null) {
result = runJarFile(project, context, config)
runJarFile(project, context, config)
} else {
throw KobaltException("No \"mainClass\" specified in the application{} part of project ${project.name}")
}
}
return result
}