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

Application plug-in.

This commit is contained in:
Cedric Beust 2015-11-06 09:59:52 -08:00
parent 445b7023cd
commit 349a54d528
7 changed files with 75 additions and 23 deletions

View file

@ -13,6 +13,7 @@ import com.beust.kobalt.misc.KobaltExecutors
import com.beust.kobalt.misc.log import com.beust.kobalt.misc.log
import com.beust.kobalt.plugin.DefaultPlugin import com.beust.kobalt.plugin.DefaultPlugin
import com.beust.kobalt.plugin.android.AndroidPlugin import com.beust.kobalt.plugin.android.AndroidPlugin
import com.beust.kobalt.plugin.application.ApplicationPlugin
import com.beust.kobalt.plugin.java.JavaPlugin import com.beust.kobalt.plugin.java.JavaPlugin
import com.beust.kobalt.plugin.kotlin.KotlinPlugin import com.beust.kobalt.plugin.kotlin.KotlinPlugin
import com.beust.kobalt.plugin.packaging.PackagingPlugin import com.beust.kobalt.plugin.packaging.PackagingPlugin
@ -70,7 +71,8 @@ public class Plugins @Inject constructor (val taskManagerProvider : Provider<Tas
KotlinPlugin::class.java, KotlinPlugin::class.java,
PackagingPlugin::class.java, PackagingPlugin::class.java,
PublishPlugin::class.java, PublishPlugin::class.java,
AndroidPlugin::class.java AndroidPlugin::class.java,
ApplicationPlugin::class.java
// AptPlugin::class.java // AptPlugin::class.java
).map { ).map {
addPluginInstance(Kobalt.INJECTOR.getInstance(it)) addPluginInstance(Kobalt.INJECTOR.getInstance(it))

View file

@ -29,7 +29,7 @@ class ProjectDescription(val project: Project, val dependsOn: List<Project>)
* Plugins that export classpath entries need to implement this interface. * Plugins that export classpath entries need to implement this interface.
*/ */
interface IClasspathContributor { interface IClasspathContributor {
fun entriesFor(project: Project) : Collection<IClasspathDependency> fun entriesFor(project: Project?) : Collection<IClasspathDependency>
} }
/** /**

View file

@ -45,14 +45,12 @@ class JvmCompiler @Inject constructor(val dependencyManager: DependencyManager)
allDependencies.forEach { dependencies -> allDependencies.forEach { dependencies ->
result.addAll(dependencyManager.transitiveClosure(dependencies)) result.addAll(dependencyManager.transitiveClosure(dependencies))
} }
if (project != null) { result.addAll(runClasspathContributors(project, context))
result.addAll(runClasspathContributors(context, project))
}
return result return result
} }
private fun runClasspathContributors(context: KobaltContext?, project: Project) : private fun runClasspathContributors(project: Project?, context: KobaltContext?) :
Collection<IClasspathDependency> { Collection<IClasspathDependency> {
val result = arrayListOf<IClasspathDependency>() val result = arrayListOf<IClasspathDependency>()
context!!.pluginInfo.classpathContributors.forEach { it: IClasspathContributor -> context!!.pluginInfo.classpathContributors.forEach { it: IClasspathContributor ->

View file

@ -4,6 +4,8 @@ import com.beust.kobalt.Args
import com.beust.kobalt.Plugins import com.beust.kobalt.Plugins
import com.beust.kobalt.api.* import com.beust.kobalt.api.*
import com.beust.kobalt.api.annotation.Task import com.beust.kobalt.api.annotation.Task
import com.beust.kobalt.internal.JvmCompiler
import com.beust.kobalt.maven.IClasspathDependency
import com.beust.kobalt.maven.KobaltException import com.beust.kobalt.maven.KobaltException
import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.Topological import com.beust.kobalt.misc.Topological
@ -26,7 +28,7 @@ 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>,
@Assisted val pluginInfo: PluginInfo, val files: KFiles, val plugins: Plugins, @Assisted val pluginInfo: PluginInfo, val files: KFiles, val plugins: Plugins,
val pluginProperties: PluginProperties) { val jvmCompiler: JvmCompiler, val pluginProperties: PluginProperties) {
interface IFactory { interface IFactory {
fun create(@Assisted("buildFiles") buildFiles: List<BuildFile>, pluginInfo: PluginInfo) : BuildFileCompiler fun create(@Assisted("buildFiles") buildFiles: List<BuildFile>, pluginInfo: PluginInfo) : BuildFileCompiler
@ -42,19 +44,19 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b
context.pluginProperties = pluginProperties context.pluginProperties = pluginProperties
Kobalt.context = context Kobalt.context = context
val allProjects = findProjects() val allProjects = findProjects(context)
plugins.applyPlugins(context, allProjects) plugins.applyPlugins(context, allProjects)
return allProjects return allProjects
} }
private fun findProjects(): List<Project> { private fun findProjects(context: KobaltContext): List<Project> {
val result = arrayListOf<Project>() val result = arrayListOf<Project>()
buildFiles.forEach { buildFile -> buildFiles.forEach { buildFile ->
val pluginUrls = findPlugInUrls(buildFile) val pluginUrls = findPlugInUrls(context, buildFile)
val buildScriptJarFile = File(KFiles.findBuildScriptLocation(buildFile, SCRIPT_JAR)) val buildScriptJarFile = File(KFiles.findBuildScriptLocation(buildFile, SCRIPT_JAR))
maybeCompileBuildFile(buildFile, buildScriptJarFile, pluginUrls) maybeCompileBuildFile(context, buildFile, buildScriptJarFile, pluginUrls)
val buildScriptInfo = parseBuildScriptJarFile(buildScriptJarFile, pluginUrls) val buildScriptInfo = parseBuildScriptJarFile(buildScriptJarFile, pluginUrls)
result.addAll(buildScriptInfo.projects) result.addAll(buildScriptInfo.projects)
} }
@ -65,7 +67,7 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b
buildFile.exists() && jarFile.exists() buildFile.exists() && jarFile.exists()
&& buildFile.lastModified < jarFile.lastModified() && buildFile.lastModified < jarFile.lastModified()
private fun maybeCompileBuildFile(buildFile: BuildFile, buildScriptJarFile: File, private fun maybeCompileBuildFile(context: KobaltContext, buildFile: BuildFile, buildScriptJarFile: File,
pluginUrls: List<URL>) { pluginUrls: List<URL>) {
log(2, "Running build file ${buildFile.name} jar: $buildScriptJarFile") log(2, "Running build file ${buildFile.name} jar: $buildScriptJarFile")
@ -79,7 +81,7 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b
classpath(pluginUrls.map { it.file }) classpath(pluginUrls.map { it.file })
sourceFiles(listOf(buildFile.path.toFile().absolutePath)) sourceFiles(listOf(buildFile.path.toFile().absolutePath))
output = buildScriptJarFile output = buildScriptJarFile
}.compile() }.compile(context = context)
} }
} }
@ -87,7 +89,7 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b
* Generate the script file with only the plugins()/repos() directives and run it. Then return * Generate the script file with only the plugins()/repos() directives and run it. Then return
* the URL's of all the plug-ins that were found. * the URL's of all the plug-ins that were found.
*/ */
private fun findPlugInUrls(buildFile: BuildFile): List<URL> { private fun findPlugInUrls(context: KobaltContext, buildFile: BuildFile): List<URL> {
val result = arrayListOf<URL>() val result = arrayListOf<URL>()
val pluginCode = arrayListOf( val pluginCode = arrayListOf(
"import com.beust.kobalt.*", "import com.beust.kobalt.*",
@ -124,7 +126,7 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b
val buildScriptJarFile = File(buildScriptJar) val buildScriptJarFile = File(buildScriptJar)
if (! isUpToDate(buildFile, File(buildScriptJar))) { if (! isUpToDate(buildFile, File(buildScriptJar))) {
buildScriptJarFile.parentFile.mkdirs() buildScriptJarFile.parentFile.mkdirs()
generateJarFile(BuildFile(Paths.get(pluginSourceFile.path), "Plugins"), buildScriptJarFile) generateJarFile(context, BuildFile(Paths.get(pluginSourceFile.path), "Plugins"), buildScriptJarFile)
} }
// //
@ -142,12 +144,15 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b
return result return result
} }
private fun generateJarFile(buildFile: BuildFile, buildScriptJarFile: File) { private fun generateJarFile(context: KobaltContext, buildFile: BuildFile, buildScriptJarFile: File) {
val kotlintDeps = jvmCompiler.calculateDependencies(null, context, listOf<IClasspathDependency>())
val deps: List<String> = kotlintDeps.map { it.jarFile.get().absolutePath }
kotlinCompilePrivate { kotlinCompilePrivate {
classpath(files.kobaltJar) classpath(files.kobaltJar)
classpath(deps)
sourceFiles(buildFile.path.toFile().absolutePath) sourceFiles(buildFile.path.toFile().absolutePath)
output = File(buildScriptJarFile.absolutePath) output = File(buildScriptJarFile.absolutePath)
}.compile() }.compile(context = context)
} }
class BuildScriptInfo(val projects: List<Project>, val classLoader: ClassLoader) class BuildScriptInfo(val projects: List<Project>, val classLoader: ClassLoader)

View file

@ -245,8 +245,12 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler) :
private val classpathEntries = HashMultimap.create<String, IClasspathDependency>() private val classpathEntries = HashMultimap.create<String, IClasspathDependency>()
override fun entriesFor(project: Project): Collection<IClasspathDependency> { override fun entriesFor(project: Project?): Collection<IClasspathDependency> {
if (project != null) {
return classpathEntries.get(project.name!!) ?: listOf() return classpathEntries.get(project.name!!) ?: listOf()
} else {
return listOf()
}
} }
} }

View file

@ -0,0 +1,43 @@
package com.beust.kobalt.plugin.application
import com.beust.kobalt.Plugins
import com.beust.kobalt.api.BasePlugin
import com.beust.kobalt.api.Project
import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.maven.LocalRepo
import com.beust.kobalt.misc.KobaltExecutors
import com.google.inject.Inject
import com.google.inject.Singleton
@Directive
class ApplicationConfig {
var mainClass: String? = null
var jvmArgs = arrayListOf<String>()
fun jvmArgs(vararg args: String) = args.forEach { jvmArgs.add(it) }
}
@Directive
fun Project.application(init: ApplicationConfig.() -> Unit) {
ApplicationConfig().let { config ->
config.init()
(Plugins.findPlugin(ApplicationPlugin.NAME) as ApplicationPlugin).addConfig(this, config)
}
}
@Singleton
class ApplicationPlugin @Inject constructor(val dependencyManager : DependencyManager,
val executors: KobaltExecutors, val localRepo: LocalRepo) : BasePlugin() {
companion object {
const val NAME = "application"
}
override val name = NAME
fun addConfig(project: Project, config: ApplicationConfig) {
println("Adding config $config")
}
}

View file

@ -93,13 +93,13 @@ class KotlinPlugin @Inject constructor(
// interface IClasspathContributor // interface IClasspathContributor
override fun entriesFor(project: Project) : List<IClasspathDependency> = override fun entriesFor(project: Project?) : List<IClasspathDependency> =
if (project is KotlinProject) { if (project == null || project is KotlinProject) {
// All Kotlin projects automatically get the Kotlin runtime added to their class path // All Kotlin projects automatically get the Kotlin runtime added to their class path
arrayListOf(getKotlinCompilerJar("kotlin-stdlib"), getKotlinCompilerJar("kotlin-compiler-embeddable")) listOf(getKotlinCompilerJar("kotlin-stdlib"), getKotlinCompilerJar("kotlin-compiler-embeddable"))
.map { FileDependency(it) } .map { FileDependency(it) }
} else { } else {
arrayListOf() listOf()
} }
} }