mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-27 08:38:13 -07:00
Introducing IRunContributor.
This commit is contained in:
parent
1ae3559f70
commit
55e6cff0f5
4 changed files with 92 additions and 38 deletions
25
src/main/kotlin/com/beust/kobalt/api/IRunContributor.kt
Normal file
25
src/main/kotlin/com/beust/kobalt/api/IRunContributor.kt
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
package com.beust.kobalt.api
|
||||||
|
|
||||||
|
import com.beust.kobalt.TaskResult
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Plugins that can implement the "task" run should implement this interface.
|
||||||
|
*/
|
||||||
|
interface IRunContributor : IContributor {
|
||||||
|
companion object {
|
||||||
|
const val DEFAULT_POSITIVE_AFFINITY = 100
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return an integer indicating your affinity for running the current project. The runner with
|
||||||
|
* the highest affinity is selected to run it.
|
||||||
|
*/
|
||||||
|
fun runAffinity(project: Project, context: KobaltContext) : Int
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Run the project.
|
||||||
|
*/
|
||||||
|
fun run(project: Project, context: KobaltContext) : TaskResult
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -61,6 +61,9 @@ class KobaltPluginXml {
|
||||||
|
|
||||||
@XmlElement(name = "build-directory-interceptors") @JvmField
|
@XmlElement(name = "build-directory-interceptors") @JvmField
|
||||||
var buildDirectoryInterceptors: ClassNameXml? = null
|
var buildDirectoryInterceptors: ClassNameXml? = null
|
||||||
|
|
||||||
|
@XmlElement(name = "run-contributors") @JvmField
|
||||||
|
var runContributors: ClassNameXml? = null
|
||||||
}
|
}
|
||||||
|
|
||||||
class ContributorXml {
|
class ContributorXml {
|
||||||
|
@ -88,6 +91,7 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
|
||||||
val compilerInterceptors = arrayListOf<ICompilerInterceptor>()
|
val compilerInterceptors = arrayListOf<ICompilerInterceptor>()
|
||||||
val sourceDirectoriesInterceptors = arrayListOf<ISourceDirectoriesIncerceptor>()
|
val sourceDirectoriesInterceptors = arrayListOf<ISourceDirectoriesIncerceptor>()
|
||||||
val buildDirectoryInterceptors = arrayListOf<IBuildDirectoryIncerceptor>()
|
val buildDirectoryInterceptors = arrayListOf<IBuildDirectoryIncerceptor>()
|
||||||
|
val runContributors = arrayListOf<IRunContributor>()
|
||||||
|
|
||||||
// Future contributors:
|
// Future contributors:
|
||||||
// source files
|
// source files
|
||||||
|
@ -161,6 +165,9 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
|
||||||
xml.buildDirectoryInterceptors?.className?.forEach {
|
xml.buildDirectoryInterceptors?.className?.forEach {
|
||||||
buildDirectoryInterceptors.add(factory.instanceOf(forName(it)) as IBuildDirectoryIncerceptor)
|
buildDirectoryInterceptors.add(factory.instanceOf(forName(it)) as IBuildDirectoryIncerceptor)
|
||||||
}
|
}
|
||||||
|
xml.runContributors?.className?.forEach {
|
||||||
|
runContributors.add(factory.instanceOf(forName(it)) as IRunContributor)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addPluginInfo(pluginInfo: PluginInfo) {
|
fun addPluginInfo(pluginInfo: PluginInfo) {
|
||||||
|
|
|
@ -1,16 +1,14 @@
|
||||||
package com.beust.kobalt.plugin.application
|
package com.beust.kobalt.plugin.application
|
||||||
|
|
||||||
import com.beust.kobalt.*
|
import com.beust.kobalt.*
|
||||||
import com.beust.kobalt.api.ConfigPlugin
|
import com.beust.kobalt.api.*
|
||||||
import com.beust.kobalt.api.KobaltContext
|
|
||||||
import com.beust.kobalt.api.Project
|
|
||||||
import com.beust.kobalt.api.ProjectDescription
|
|
||||||
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.JvmCompilerPlugin
|
import com.beust.kobalt.internal.JvmCompilerPlugin
|
||||||
import com.beust.kobalt.maven.DependencyManager
|
import com.beust.kobalt.maven.DependencyManager
|
||||||
import com.beust.kobalt.misc.KobaltExecutors
|
import com.beust.kobalt.misc.KobaltExecutors
|
||||||
import com.beust.kobalt.misc.RunCommand
|
import com.beust.kobalt.misc.RunCommand
|
||||||
|
import com.beust.kobalt.misc.warn
|
||||||
import com.beust.kobalt.plugin.packaging.PackageConfig
|
import com.beust.kobalt.plugin.packaging.PackageConfig
|
||||||
import com.beust.kobalt.plugin.packaging.PackagingPlugin
|
import com.beust.kobalt.plugin.packaging.PackagingPlugin
|
||||||
import com.google.inject.Inject
|
import com.google.inject.Inject
|
||||||
|
@ -35,7 +33,7 @@ fun Project.application(init: ApplicationConfig.() -> Unit) {
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors,
|
class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors,
|
||||||
val dependencyManager: DependencyManager) : ConfigPlugin<ApplicationConfig>() {
|
val dependencyManager: DependencyManager) : ConfigPlugin<ApplicationConfig>(), IRunContributor {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val NAME = "application"
|
const val NAME = "application"
|
||||||
|
@ -50,40 +48,13 @@ class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors,
|
||||||
|
|
||||||
@Task(name = "run", description = "Run the main class", runAfter = arrayOf("assemble"))
|
@Task(name = "run", description = "Run the main class", runAfter = arrayOf("assemble"))
|
||||||
fun taskRun(project: Project): TaskResult {
|
fun taskRun(project: Project): TaskResult {
|
||||||
var result = TaskResult()
|
val runContributor = context.pluginInfo.runContributors.maxBy { it.runAffinity(project, context)}
|
||||||
configurationFor(project)?.let { config ->
|
if (runContributor != null && runContributor.runAffinity(project, context) > 0) {
|
||||||
val java = JavaInfo.create(File(SystemProperties.javaBase)).javaExecutable!!
|
return runContributor.run(project, context)
|
||||||
if (config.mainClass != null) {
|
} else {
|
||||||
val jarName = project.projectProperties.get(PackagingPlugin.JAR_NAME) as String
|
warn("Couldn't find a runner for project ${project.name}")
|
||||||
val packages = project.projectProperties.get(PackagingPlugin.PACKAGES) as List<PackageConfig>
|
return TaskResult()
|
||||||
val allDeps = arrayListOf(jarName)
|
|
||||||
if (! isFatJar(packages, jarName)) {
|
|
||||||
val projDeps = project.projectProperties.get(JvmCompilerPlugin.DEPENDENT_PROJECTS)
|
|
||||||
as List<ProjectDescription>
|
|
||||||
// If the jar file is not fat, we need to add the transitive closure of all dependencies
|
|
||||||
// on the classpath
|
|
||||||
val allTheDependencies =
|
|
||||||
dependencyManager.calculateDependencies(project, context, projDeps,
|
|
||||||
allDependencies = project.compileDependencies).map { it.jarFile.get().path }
|
|
||||||
allDeps.addAll(allTheDependencies)
|
|
||||||
}
|
|
||||||
val allDepsJoined = allDeps.joinToString(File.pathSeparator)
|
|
||||||
val args = listOf("-classpath", allDepsJoined) + config.jvmArgs + config.mainClass!!
|
|
||||||
val exitCode = RunCommand(java.absolutePath).run(args,
|
|
||||||
successCallback = { output: List<String> ->
|
|
||||||
println(output.joinToString("\n"))
|
|
||||||
},
|
|
||||||
errorCallback = { output: List<String> ->
|
|
||||||
println("ERROR")
|
|
||||||
println(output.joinToString("\n"))
|
|
||||||
}
|
|
||||||
)
|
|
||||||
result = TaskResult(exitCode == 0)
|
|
||||||
} else {
|
|
||||||
throw KobaltException("No \"mainClass\" specified in the application{} part of project ${project.name}")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun isFatJar(packages: List<PackageConfig>, jarName: String): Boolean {
|
private fun isFatJar(packages: List<PackageConfig>, jarName: String): Boolean {
|
||||||
|
@ -96,5 +67,53 @@ class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors,
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IRunContributor
|
||||||
|
|
||||||
|
override fun runAffinity(project: Project, context: KobaltContext): Int {
|
||||||
|
return if (configurationFor(project) != null) IRunContributor.DEFAULT_POSITIVE_AFFINITY else 0
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun run(project: Project, context: KobaltContext): TaskResult {
|
||||||
|
var result = TaskResult()
|
||||||
|
configurationFor(project)?.let { config ->
|
||||||
|
if (config.mainClass != null) {
|
||||||
|
result = runJarFile(project, config)
|
||||||
|
} else {
|
||||||
|
throw KobaltException("No \"mainClass\" specified in the application{} part of project ${project.name}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun runJarFile(project: Project, config: ApplicationConfig) : TaskResult {
|
||||||
|
val jarName = project.projectProperties.get(PackagingPlugin.JAR_NAME) as String
|
||||||
|
val packages = project.projectProperties.get(PackagingPlugin.PACKAGES) as List<PackageConfig>
|
||||||
|
val allDeps = arrayListOf(jarName)
|
||||||
|
val java = JavaInfo.create(File(SystemProperties.javaBase)).javaExecutable!!
|
||||||
|
if (! isFatJar(packages, jarName)) {
|
||||||
|
val projDeps = project.projectProperties.get(JvmCompilerPlugin.DEPENDENT_PROJECTS)
|
||||||
|
as List<ProjectDescription>
|
||||||
|
// If the jar file is not fat, we need to add the transitive closure of all dependencies
|
||||||
|
// on the classpath
|
||||||
|
val allTheDependencies =
|
||||||
|
dependencyManager.calculateDependencies(project, context, projDeps,
|
||||||
|
allDependencies = project.compileDependencies).map { it.jarFile.get().path }
|
||||||
|
allDeps.addAll(allTheDependencies)
|
||||||
|
}
|
||||||
|
val allDepsJoined = allDeps.joinToString(File.pathSeparator)
|
||||||
|
val args = listOf("-classpath", allDepsJoined) + config.jvmArgs + config.mainClass!!
|
||||||
|
val exitCode = RunCommand(java.absolutePath).run(args,
|
||||||
|
successCallback = { output: List<String> ->
|
||||||
|
println(output.joinToString("\n"))
|
||||||
|
},
|
||||||
|
errorCallback = { output: List<String> ->
|
||||||
|
println("ERROR")
|
||||||
|
println(output.joinToString("\n"))
|
||||||
|
}
|
||||||
|
)
|
||||||
|
return TaskResult(exitCode == 0)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,4 +37,7 @@
|
||||||
<compiler-interceptors>
|
<compiler-interceptors>
|
||||||
<class-name>com.beust.kobalt.plugin.android.AndroidPlugin</class-name>
|
<class-name>com.beust.kobalt.plugin.android.AndroidPlugin</class-name>
|
||||||
</compiler-interceptors>
|
</compiler-interceptors>
|
||||||
|
<run-contributors>
|
||||||
|
<class-name>com.beust.kobalt.plugin.application.ApplicationPlugin</class-name>
|
||||||
|
</run-contributors>
|
||||||
</kobalt-plugin>
|
</kobalt-plugin>
|
Loading…
Add table
Add a link
Reference in a new issue