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

Better PluginInfo.

This commit is contained in:
Cedric Beust 2015-11-03 10:16:10 -08:00
parent 01e5afe531
commit a5bad9b48a
7 changed files with 39 additions and 26 deletions

View file

@ -2,6 +2,8 @@ package com.beust.kobalt
import com.beust.jcommander.JCommander import com.beust.jcommander.JCommander
import com.beust.kobalt.api.Kobalt import com.beust.kobalt.api.Kobalt
import com.beust.kobalt.api.PluginInfo
import com.beust.kobalt.api.PluginInfoDescription
import com.beust.kobalt.api.Project import com.beust.kobalt.api.Project
import com.beust.kobalt.internal.TaskManager import com.beust.kobalt.internal.TaskManager
import com.beust.kobalt.internal.remote.KobaltClient import com.beust.kobalt.internal.remote.KobaltClient
@ -54,7 +56,8 @@ private class Main @Inject constructor(
val github: GithubApi, val github: GithubApi,
val updateKobalt: UpdateKobalt, val updateKobalt: UpdateKobalt,
val client: KobaltClient, val client: KobaltClient,
val server: KobaltServer) { val server: KobaltServer,
val pluginInfoDescription: PluginInfoDescription) {
data class RunInfo(val jc: JCommander, val args: Args) data class RunInfo(val jc: JCommander, val args: Args)
@ -117,8 +120,9 @@ private class Main @Inject constructor(
error(buildFile.path.toFile().path + " does not exist") error(buildFile.path.toFile().path + " does not exist")
} else { } else {
var allProjects = listOf<Project>() var allProjects = listOf<Project>()
val pluginInfo = PluginInfo(pluginInfoDescription)
try { try {
allProjects = buildFileCompilerFactory.create(listOf(buildFile)).compileBuildFiles(args) allProjects = buildFileCompilerFactory.create(listOf(buildFile), pluginInfo).compileBuildFiles(args)
} catch(ex: Throwable) { } catch(ex: Throwable) {
// This can happen if the ABI for the build script file changed. Try to wipe .kobalt. // This can happen if the ABI for the build script file changed. Try to wipe .kobalt.
log(2, "Couldn't parse preBuildScript.jar: ${ex.message}") log(2, "Couldn't parse preBuildScript.jar: ${ex.message}")
@ -127,7 +131,8 @@ private class Main @Inject constructor(
return 1 return 1
} else { } else {
log(1, "Deleted .kobalt") log(1, "Deleted .kobalt")
allProjects = buildFileCompilerFactory.create(listOf(buildFile)).compileBuildFiles(args) allProjects = buildFileCompilerFactory.create(listOf(buildFile), pluginInfo)
.compileBuildFiles(args)
} }
} }

View file

@ -5,9 +5,6 @@ import com.beust.kobalt.Plugins
public class KobaltContext(val args: Args) { public class KobaltContext(val args: Args) {
fun findPlugin(name: String) = Plugins.findPlugin(name) fun findPlugin(name: String) = Plugins.findPlugin(name)
var pluginFile: KobaltPluginFile? = null var pluginInfo: PluginInfo? = null
// sourceContributors
// projectContributors
// compilerContributors
} }

View file

@ -19,7 +19,10 @@ interface IClasspathContributor {
fun entriesFor(project: Project) : Collection<IClasspathDependency> fun entriesFor(project: Project) : Collection<IClasspathDependency>
} }
class KobaltPluginFile { /**
* All the information gathered from the various plugin.xml that were collected.
*/
class PluginInfoDescription {
fun <T> instanceOf(c: Class<T>) : T = Kobalt.INJECTOR.getInstance(c) fun <T> instanceOf(c: Class<T>) : T = Kobalt.INJECTOR.getInstance(c)
val projectContributors : ArrayList<Class<out IProjectContributor>> = val projectContributors : ArrayList<Class<out IProjectContributor>> =
@ -34,3 +37,16 @@ class KobaltPluginFile {
// compilers // compilers
// --init // --init
} }
/**
* Turn the classes found in PluginInfoDescription into concrete objects that plugins can then use.
*/
class PluginInfo(val description: PluginInfoDescription) {
val projectContributors = arrayListOf<IProjectContributor>()
val classpathContributors = arrayListOf<IClasspathContributor>()
init {
classpathContributors.addAll(description.classpathContributors.map { description.instanceOf(it) })
projectContributors.addAll(description.projectContributors.map { description.instanceOf(it) })
}
}

View file

@ -55,14 +55,8 @@ class JvmCompiler @Inject constructor(val dependencyManager: DependencyManager)
private fun runClasspathContributors(context: KobaltContext?, project: Project) : private fun runClasspathContributors(context: KobaltContext?, project: Project) :
Collection<IClasspathDependency> { Collection<IClasspathDependency> {
val result = arrayListOf<IClasspathDependency>() val result = arrayListOf<IClasspathDependency>()
val classes : List<Class<out IClasspathContributor>>? = context?.pluginFile?.classpathContributors context?.pluginInfo?.classpathContributors?.forEach { it: IClasspathContributor ->
if (classes != null) { result.addAll(it.entriesFor(project))
val contributors: List<IClasspathContributor> = classes.map {
context?.pluginFile?.instanceOf(it)!!
}
contributors.forEach { it: IClasspathContributor ->
result.addAll(it.entriesFor(project))
}
} }
return result return result
} }

View file

@ -1,6 +1,8 @@
package com.beust.kobalt.internal.remote package com.beust.kobalt.internal.remote
import com.beust.kobalt.Args import com.beust.kobalt.Args
import com.beust.kobalt.api.PluginInfo
import com.beust.kobalt.api.PluginInfoDescription
import com.beust.kobalt.kotlin.BuildFile import com.beust.kobalt.kotlin.BuildFile
import com.beust.kobalt.kotlin.BuildFileCompiler import com.beust.kobalt.kotlin.BuildFileCompiler
import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.maven.DependencyManager
@ -10,8 +12,8 @@ import com.beust.kobalt.misc.KobaltExecutors
import com.beust.kobalt.misc.log import com.beust.kobalt.misc.log
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.JsonObject import com.google.gson.JsonObject
import javax.inject.Inject
import java.nio.file.Paths import java.nio.file.Paths
import javax.inject.Inject
/** /**
* This command returns the list of dependencies for the given buildFile. * This command returns the list of dependencies for the given buildFile.
@ -24,11 +26,11 @@ import java.nio.file.Paths
*/ */
class GetDependenciesCommand @Inject constructor(val executors: KobaltExecutors, class GetDependenciesCommand @Inject constructor(val executors: KobaltExecutors,
val buildFileCompilerFactory: BuildFileCompiler.IFactory, val args: Args, val buildFileCompilerFactory: BuildFileCompiler.IFactory, val args: Args,
val dependencyManager: DependencyManager) : ICommand { val dependencyManager: DependencyManager, val pluginInfoDescription: PluginInfoDescription) : ICommand {
override val name = "getDependencies" override val name = "getDependencies"
override fun run(sender: ICommandSender, received: JsonObject) { override fun run(sender: ICommandSender, received: JsonObject) {
val buildFile = BuildFile(Paths.get(received.get("buildFile").asString), "GetDependenciesCommand") val buildFile = BuildFile(Paths.get(received.get("buildFile").asString), "GetDependenciesCommand")
val scriptCompiler = buildFileCompilerFactory.create(listOf(buildFile)) val scriptCompiler = buildFileCompilerFactory.create(listOf(buildFile), PluginInfo(pluginInfoDescription))
scriptCompiler.observable.subscribe { scriptCompiler.observable.subscribe {
buildScriptInfo -> if (buildScriptInfo.projects.size > 0) { buildScriptInfo -> if (buildScriptInfo.projects.size > 0) {
sender.sendData(toData(buildScriptInfo)) sender.sendData(toData(buildScriptInfo))

View file

@ -25,10 +25,10 @@ import java.util.jar.JarInputStream
import javax.inject.Inject import javax.inject.Inject
public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val buildFiles: List<BuildFile>, public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val buildFiles: List<BuildFile>,
val files: KFiles, val plugins: Plugins, val kobaltPluginFile: KobaltPluginFile) { @Assisted val pluginInfo: PluginInfo, val files: KFiles, val plugins: Plugins) {
interface IFactory { interface IFactory {
fun create(@Assisted("buildFiles") buildFiles: List<BuildFile>) : BuildFileCompiler fun create(@Assisted("buildFiles") buildFiles: List<BuildFile>, pluginInfo: PluginInfo) : BuildFileCompiler
} }
val observable = PublishSubject.create<BuildScriptInfo>() val observable = PublishSubject.create<BuildScriptInfo>()
@ -37,7 +37,7 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b
fun compileBuildFiles(args: Args): List<Project> { fun compileBuildFiles(args: Args): List<Project> {
val context = KobaltContext(args) val context = KobaltContext(args)
context.pluginFile = kobaltPluginFile context.pluginInfo = pluginInfo
Kobalt.context = context Kobalt.context = context
val allProjects = findProjects() val allProjects = findProjects()
@ -226,9 +226,8 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b
// //
Topological<Project>().let { topologicalProjects -> Topological<Project>().let { topologicalProjects ->
val all = hashSetOf<Project>() val all = hashSetOf<Project>()
kobaltPluginFile.projectContributors.forEach { cls -> pluginInfo.projectContributors.forEach { contributor ->
val ip: IProjectContributor = kobaltPluginFile.instanceOf(cls) val descriptions = contributor.projects()
val descriptions = ip.projects()
descriptions.forEach { pd -> descriptions.forEach { pd ->
all.add(pd.project) all.add(pd.project)
pd.dependsOn.forEach { dependsOn -> pd.dependsOn.forEach { dependsOn ->

View file

@ -46,7 +46,7 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler) :
if (accept(project)) { if (accept(project)) {
project.compileDependencies.add(FileDependency(androidJar(project).toString())) project.compileDependencies.add(FileDependency(androidJar(project).toString()))
} }
context.pluginFile?.classpathContributors?.add(this.javaClass) context.pluginInfo?.classpathContributors?.add(this)
// TODO: Find a more flexible way of enabling this, e.g. creating a contributor for it // TODO: Find a more flexible way of enabling this, e.g. creating a contributor for it
(Kobalt.findPlugin("java") as JvmCompilerPlugin).addCompilerArgs("-target", "1.6", "-source", "1.6") (Kobalt.findPlugin("java") as JvmCompilerPlugin).addCompilerArgs("-target", "1.6", "-source", "1.6")