mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Better IClasspathContributor handling.
This commit is contained in:
parent
d388291427
commit
bcbcfa3144
8 changed files with 62 additions and 41 deletions
|
@ -1,11 +0,0 @@
|
|||
package com.beust.kobalt.api
|
||||
|
||||
import com.beust.kobalt.maven.IClasspathDependency
|
||||
|
||||
/**
|
||||
* Implement this interface in order to add your own entries to the classpath. A list of contributors
|
||||
* can be found on the `KobaltContext`.
|
||||
*/
|
||||
interface IClasspathContributor {
|
||||
fun entriesFor(project: Project) : Collection<IClasspathDependency>
|
||||
}
|
|
@ -2,11 +2,10 @@ package com.beust.kobalt.api
|
|||
|
||||
import com.beust.kobalt.Args
|
||||
import com.beust.kobalt.Plugins
|
||||
import java.util.*
|
||||
|
||||
public class KobaltContext(val args: Args) {
|
||||
fun findPlugin(name: String) = Plugins.findPlugin(name)
|
||||
val classpathContributors: ArrayList<IClasspathContributor> = arrayListOf()
|
||||
var pluginFile: KobaltPluginFile? = null
|
||||
// sourceContributors
|
||||
// projectContributors
|
||||
// compilerContributors
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package com.beust.kobalt.api
|
||||
|
||||
import com.beust.kobalt.maven.IClasspathDependency
|
||||
import com.beust.kobalt.plugin.java.JavaPlugin
|
||||
import com.beust.kobalt.plugin.kotlin.KotlinPlugin
|
||||
import java.util.*
|
||||
|
||||
class ProjectDescription(val project: Project, val dependsOn: List<Project>)
|
||||
|
||||
|
@ -9,11 +11,23 @@ interface IProjectContributor {
|
|||
fun projects() : List<ProjectDescription>
|
||||
}
|
||||
|
||||
/**
|
||||
* Implement this interface in order to add your own entries to the classpath. A list of contributors
|
||||
* can be found on the `KobaltContext`.
|
||||
*/
|
||||
interface IClasspathContributor {
|
||||
fun entriesFor(project: Project) : Collection<IClasspathDependency>
|
||||
}
|
||||
|
||||
class KobaltPluginFile {
|
||||
fun <T> instanceOf(c: Class<T>) = Kobalt.INJECTOR.getInstance(c)
|
||||
val projectContributors : List<Class<out IProjectContributor>> =
|
||||
fun <T> instanceOf(c: Class<T>) : T = Kobalt.INJECTOR.getInstance(c)
|
||||
|
||||
val projectContributors : ArrayList<Class<out IProjectContributor>> =
|
||||
arrayListOf(JavaPlugin::class.java, KotlinPlugin::class.java)
|
||||
|
||||
val classpathContributors: ArrayList<Class<out IClasspathContributor>> =
|
||||
arrayListOf(KotlinPlugin::class.java)
|
||||
|
||||
// Future contributors:
|
||||
// compilerArgs
|
||||
// source files
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.beust.kobalt.internal
|
||||
|
||||
import com.beust.kobalt.api.IClasspathContributor
|
||||
import com.beust.kobalt.api.KobaltContext
|
||||
import com.beust.kobalt.api.Project
|
||||
import com.beust.kobalt.maven.DependencyManager
|
||||
|
@ -54,8 +55,14 @@ class JvmCompiler @Inject constructor(val dependencyManager: DependencyManager)
|
|||
private fun runClasspathContributors(context: KobaltContext?, project: Project) :
|
||||
Collection<IClasspathDependency> {
|
||||
val result = arrayListOf<IClasspathDependency>()
|
||||
context?.classpathContributors?.forEach {
|
||||
result.addAll(it.entriesFor(project))
|
||||
val classes : List<Class<out IClasspathContributor>>? = context?.pluginFile?.classpathContributors
|
||||
if (classes != null) {
|
||||
val contributors: List<IClasspathContributor> = classes.map {
|
||||
context?.pluginFile?.instanceOf(it)!!
|
||||
}
|
||||
contributors.forEach { it: IClasspathContributor ->
|
||||
result.addAll(it.entriesFor(project))
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b
|
|||
|
||||
fun compileBuildFiles(args: Args): List<Project> {
|
||||
val context = KobaltContext(args)
|
||||
context.pluginFile = kobaltPluginFile
|
||||
Kobalt.context = context
|
||||
|
||||
val allProjects = findProjects()
|
||||
|
|
|
@ -46,7 +46,7 @@ public class AndroidPlugin @Inject constructor(val javaCompiler: JavaCompiler) :
|
|||
if (accept(project)) {
|
||||
project.compileDependencies.add(FileDependency(androidJar(project).toString()))
|
||||
}
|
||||
context.classpathContributors.add(this)
|
||||
context.pluginFile?.classpathContributors?.add(this.javaClass)
|
||||
|
||||
// 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")
|
||||
|
|
|
@ -7,7 +7,10 @@ import com.beust.kobalt.internal.CompilerActionInfo
|
|||
import com.beust.kobalt.internal.ICompilerAction
|
||||
import com.beust.kobalt.internal.JvmCompiler
|
||||
import com.beust.kobalt.internal.TaskResult
|
||||
import com.beust.kobalt.maven.*
|
||||
import com.beust.kobalt.maven.DepFactory
|
||||
import com.beust.kobalt.maven.FileDependency
|
||||
import com.beust.kobalt.maven.IClasspathDependency
|
||||
import com.beust.kobalt.maven.LocalRepo
|
||||
import com.beust.kobalt.misc.KobaltExecutors
|
||||
import com.beust.kobalt.misc.log
|
||||
import org.jetbrains.kotlin.cli.common.CLICompiler
|
||||
|
@ -27,7 +30,9 @@ class KotlinCompiler @Inject constructor(val localRepo : LocalRepo,
|
|||
val depFactory: DepFactory,
|
||||
val executors: KobaltExecutors,
|
||||
val jvmCompiler: JvmCompiler) {
|
||||
private val KOTLIN_VERSION = "1.0.0-beta-1038"
|
||||
companion object {
|
||||
val KOTLIN_VERSION = "1.0.0-beta-1038"
|
||||
}
|
||||
|
||||
val compilerAction = object: ICompilerAction {
|
||||
override fun compile(info: CompilerActionInfo): TaskResult {
|
||||
|
@ -44,13 +49,6 @@ class KotlinCompiler @Inject constructor(val localRepo : LocalRepo,
|
|||
}
|
||||
}
|
||||
|
||||
private fun getKotlinCompilerJar(name: String) : String {
|
||||
val id = "org.jetbrains.kotlin:$name:$KOTLIN_VERSION"
|
||||
val dep = MavenDependency.create(id, executors.miscExecutor)
|
||||
val result = dep.jarFile.get().absolutePath
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an ICompilerAction based on the parameters and send it to JvmCompiler.doCompile().
|
||||
*/
|
||||
|
@ -66,12 +64,12 @@ class KotlinCompiler @Inject constructor(val localRepo : LocalRepo,
|
|||
|
||||
executor.shutdown()
|
||||
|
||||
val classpathList = arrayListOf(
|
||||
getKotlinCompilerJar("kotlin-stdlib"),
|
||||
getKotlinCompilerJar("kotlin-compiler-embeddable"))
|
||||
.map { FileDependency(it) }
|
||||
// val classpathList = arrayListOf(
|
||||
// getKotlinCompilerJar("kotlin-stdlib"),
|
||||
// getKotlinCompilerJar("kotlin-compiler-embeddable"))
|
||||
// .map { FileDependency(it) }
|
||||
|
||||
val dependencies = compileDependencies + classpathList + otherClasspath.map { FileDependency(it)}
|
||||
val dependencies = compileDependencies + otherClasspath.map { FileDependency(it)}
|
||||
val info = CompilerActionInfo(project?.directory, dependencies, sourceFiles, outputDir, args)
|
||||
return jvmCompiler.doCompile(project, context, compilerAction, info)
|
||||
}
|
||||
|
|
|
@ -1,18 +1,12 @@
|
|||
package com.beust.kobalt.plugin.kotlin
|
||||
|
||||
import com.beust.kobalt.api.BasePlugin
|
||||
import com.beust.kobalt.api.IProjectContributor
|
||||
import com.beust.kobalt.api.Kobalt
|
||||
import com.beust.kobalt.api.Project
|
||||
import com.beust.kobalt.api.*
|
||||
import com.beust.kobalt.api.annotation.Directive
|
||||
import com.beust.kobalt.api.annotation.Task
|
||||
import com.beust.kobalt.internal.JvmCompiler
|
||||
import com.beust.kobalt.internal.JvmCompilerPlugin
|
||||
import com.beust.kobalt.internal.TaskResult
|
||||
import com.beust.kobalt.maven.DepFactory
|
||||
import com.beust.kobalt.maven.DependencyManager
|
||||
import com.beust.kobalt.maven.IClasspathDependency
|
||||
import com.beust.kobalt.maven.LocalRepo
|
||||
import com.beust.kobalt.maven.*
|
||||
import com.beust.kobalt.misc.KFiles
|
||||
import com.beust.kobalt.misc.KobaltExecutors
|
||||
import java.io.File
|
||||
|
@ -28,7 +22,7 @@ class KotlinPlugin @Inject constructor(
|
|||
override val executors: KobaltExecutors,
|
||||
override val jvmCompiler: JvmCompiler)
|
||||
: JvmCompilerPlugin(localRepo, files, depFactory, dependencyManager, executors, jvmCompiler),
|
||||
IProjectContributor {
|
||||
IProjectContributor, IClasspathContributor {
|
||||
|
||||
init {
|
||||
Kobalt.registerCompiler(KotlinCompilerInfo())
|
||||
|
@ -91,7 +85,26 @@ class KotlinPlugin @Inject constructor(
|
|||
}.compile(project, context)
|
||||
}
|
||||
|
||||
// interface IProjectContributor
|
||||
override fun projects() = projects
|
||||
|
||||
private fun getKotlinCompilerJar(name: String) : String {
|
||||
val id = "org.jetbrains.kotlin:$name:${KotlinCompiler.KOTLIN_VERSION}"
|
||||
val dep = MavenDependency.create(id, executors.miscExecutor)
|
||||
val result = dep.jarFile.get().absolutePath
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
// interface IClasspathContributor
|
||||
override fun entriesFor(project: Project) : List<IClasspathDependency> =
|
||||
if (project is KotlinProject) {
|
||||
// All Kotlin projects automatically get the Kotlin runtime added to their class path
|
||||
arrayListOf(getKotlinCompilerJar("kotlin-stdlib"), getKotlinCompilerJar("kotlin-compiler-embeddable"))
|
||||
.map { FileDependency(it) }
|
||||
} else {
|
||||
arrayListOf()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue