From 77df0afd188c17bee957cf99cfd3b66758b35977 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Tue, 3 Nov 2015 19:31:07 -0800 Subject: [PATCH] Parse plugin.xml. --- src/main/kotlin/com/beust/kobalt/Main.kt | 18 +++--- .../com/beust/kobalt/api/KobaltPluginFile.kt | 62 +++++++++++++++++-- src/main/resources/META-INF/plugin.xml | 12 ++++ 3 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 src/main/resources/META-INF/plugin.xml diff --git a/src/main/kotlin/com/beust/kobalt/Main.kt b/src/main/kotlin/com/beust/kobalt/Main.kt index 24a04e41..c290f7d9 100644 --- a/src/main/kotlin/com/beust/kobalt/Main.kt +++ b/src/main/kotlin/com/beust/kobalt/Main.kt @@ -1,10 +1,7 @@ package com.beust.kobalt import com.beust.jcommander.JCommander -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.* import com.beust.kobalt.internal.TaskManager import com.beust.kobalt.internal.remote.KobaltClient import com.beust.kobalt.internal.remote.KobaltServer @@ -13,7 +10,6 @@ import com.beust.kobalt.kotlin.BuildFileCompiler import com.beust.kobalt.maven.DepFactory import com.beust.kobalt.maven.Http import com.beust.kobalt.maven.LocalRepo -import com.beust.kobalt.maven.MavenDependency import com.beust.kobalt.misc.* import com.beust.kobalt.wrapper.Wrapper import com.google.inject.Guice @@ -21,6 +17,7 @@ import java.io.File import java.nio.file.Paths import java.util.* import javax.inject.Inject +import javax.xml.bind.JAXBContext public fun main(argv: Array) { val result = mainNoExit(argv) @@ -72,7 +69,7 @@ private class Main @Inject constructor( val latestVersionFuture = github.latestKobaltVersion benchmark("Build", { println(AsciiArt.banner + Kobalt.version + "\n") -// runTest() + runTest() result = runWithArgs(jc, args) executors.shutdown() }) @@ -94,9 +91,14 @@ private class Main @Inject constructor( return result } + public fun runTest() { - val dep = MavenDependency.create("com.google.inject:guice:4.0:no_aop") - println("Name: " + dep) + val file = File("src\\main\\resources\\META-INF\\plugin.xml") + val jaxbContext = JAXBContext.newInstance(KobaltPluginXml::class.java) + + val kotlinPlugin : KobaltPluginXml = jaxbContext.createUnmarshaller().unmarshal(file) as KobaltPluginXml + val pluginInfo = PluginInfo.create(kotlinPlugin) + System.out.println(kotlinPlugin.name) } private fun runWithArgs(jc: JCommander, args: Args) : Int { diff --git a/src/main/kotlin/com/beust/kobalt/api/KobaltPluginFile.kt b/src/main/kotlin/com/beust/kobalt/api/KobaltPluginFile.kt index e972610c..236a6836 100644 --- a/src/main/kotlin/com/beust/kobalt/api/KobaltPluginFile.kt +++ b/src/main/kotlin/com/beust/kobalt/api/KobaltPluginFile.kt @@ -4,6 +4,8 @@ import com.beust.kobalt.maven.IClasspathDependency import com.beust.kobalt.plugin.java.JavaPlugin import com.beust.kobalt.plugin.kotlin.KotlinPlugin import java.util.* +import javax.xml.bind.annotation.XmlElement +import javax.xml.bind.annotation.XmlRootElement class ProjectDescription(val project: Project, val dependsOn: List) @@ -19,6 +21,14 @@ interface IClasspathContributor { fun entriesFor(project: Project) : Collection } +interface IFactory { + fun instanceOf(c: Class) : T +} + +class ContributorFactory : IFactory { + override fun instanceOf(c: Class) : T = Kobalt.INJECTOR.getInstance(c) +} + /** * All the information gathered from the various plugin.xml that were collected. */ @@ -41,12 +51,54 @@ class PluginInfoDescription { /** * Turn the classes found in PluginInfoDescription into concrete objects that plugins can then use. */ -class PluginInfo(val description: PluginInfoDescription) { +class PluginInfo(val description: PluginInfoDescription?) { val projectContributors = arrayListOf() val classpathContributors = arrayListOf() - init { - classpathContributors.addAll(description.classpathContributors.map { description.instanceOf(it) }) - projectContributors.addAll(description.projectContributors.map { description.instanceOf(it) }) + companion object { + fun create(xml: KobaltPluginXml) : PluginInfo { + val factory = Class.forName(xml.factoryClassName).newInstance() as IFactory + val result = PluginInfo(null) + xml.classpathContributors?.className?.forEach { + result.classpathContributors.add(factory.instanceOf(Class.forName(it)) as IClasspathContributor) + } + xml.projectContributors?.className?.forEach { + result.projectContributors.add(factory.instanceOf(Class.forName(it)) as IProjectContributor) + } + return result + } } -} \ No newline at end of file + + init { + if (description != null) { + classpathContributors.addAll(description.classpathContributors.map { description.instanceOf(it) }) + projectContributors.addAll(description.projectContributors.map { description.instanceOf(it) }) + } + } +} + +class ContributorXml { + @XmlElement @JvmField + val name: String? = null +} + +class ContributorsXml { + @XmlElement(name = "class-name") @JvmField + var className: List = arrayListOf() +} + +@XmlRootElement(name = "kobalt-plugin") +class KobaltPluginXml { + @XmlElement @JvmField + var name: String? = null + + @XmlElement(name = "factory-class-name") @JvmField + var factoryClassName: String? = null + + @XmlElement(name = "classpath-contributors") @JvmField + var classpathContributors : ContributorsXml? = null + + @XmlElement(name = "project-contributors") @JvmField + var projectContributors : ContributorsXml? = null +} + diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml new file mode 100644 index 00000000..5425b2c8 --- /dev/null +++ b/src/main/resources/META-INF/plugin.xml @@ -0,0 +1,12 @@ + + kobalt + com.beust.kobalt.api.ContributorFactory + + com.beust.kobalt.plugin.android.AndroidPlugin + com.beust.kobalt.plugin.kotlin.KotlinPlugin + + + com.beust.kobalt.plugin.java.JavaPlugin + com.beust.kobalt.plugin.kotlin.KotlinPlugin + + \ No newline at end of file