1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 16:28:12 -07:00

No more classes extend JvmCompilerPlugin.

This commit is contained in:
Cedric Beust 2016-02-04 21:44:51 +04:00
parent 02995ce6cb
commit 638d16588e
13 changed files with 148 additions and 143 deletions

View file

@ -24,6 +24,32 @@ open class Project(
@Directive open var packageName: String? = group, @Directive open var packageName: String? = group,
val projectInfo: IProjectInfo) : IBuildConfig { val projectInfo: IProjectInfo) : IBuildConfig {
class ProjectExtra(project: Project) {
val suffixesFound = hashSetOf<String>()
init {
Kobalt.context?.let {
project.sourceDirectories.forEach { source ->
val sourceDir = File(KFiles.joinDir(project.directory, source))
KFiles.findRecursively(sourceDir, { file ->
val ind = file.lastIndexOf(".")
if (ind >= 0) {
suffixesFound.add(file.substring(ind + 1))
}
false
})
}
println("Suffixes: " + suffixesFound)
}
}
}
/**
* Initialized as soon as all the projects are parsed. This field caches a bunch of things we don't
* want to recalculate all the time, such as the list of suffixes found in this project.
*/
lateinit var projectExtra : ProjectExtra
val testConfigs = arrayListOf(TestConfig(this)) val testConfigs = arrayListOf(TestConfig(this))
override var buildConfig : BuildConfig? = null //BuildConfig() override var buildConfig : BuildConfig? = null //BuildConfig()
@ -38,24 +64,40 @@ open class Project(
return name.hashCode() return name.hashCode()
} }
companion object {
val DEFAULT_SOURCE_DIRECTORIES = hashSetOf("src/main/java", "src/main/kotlin", "src/main/resources")
val DEFAULT_SOURCE_DIRECTORIES_TEST = hashSetOf("src/test/java", "src/test/kotlin")
}
// //
// Directories // Directories
// //
@Directive @Directive
fun sourceDirectories(init: Sources.() -> Unit) = Sources(this, sourceDirectories).apply { init() } fun sourceDirectories(init: Sources.() -> Unit) : Sources {
return Sources(this, sourceDirectories).apply { init() }
}
var sourceDirectories : HashSet<String> = hashSetOf() private fun existing(dirs: Set<String>) = dirs.filter { File(directory, it).exists() }.toHashSet()
get() = if (field.isEmpty()) projectInfo.defaultSourceDirectories else field
var sourceDirectories = hashSetOf<String>()
get() = existing(if (field.isEmpty()) DEFAULT_SOURCE_DIRECTORIES else field)
set(value) { field = value } set(value) { field = value }
@Directive @Directive
fun sourceDirectoriesTest(init: Sources.() -> Unit) = Sources(this, sourceDirectoriesTest).apply { init() } fun sourceDirectoriesTest(init: Sources.() -> Unit) : Sources {
return Sources(this, sourceDirectoriesTest).apply { init() }
}
var sourceDirectoriesTest : HashSet<String> = hashSetOf() var sourceDirectoriesTest = hashSetOf<String>()
get() = if (field.isEmpty()) projectInfo.defaultTestDirectories else field get() = existing(if (field.isEmpty()) DEFAULT_SOURCE_DIRECTORIES_TEST else field)
set(value) { field = value } set(value) { field = value }
init {
sourceDirectories = hashSetOf()
sourceDirectoriesTest = hashSetOf()
}
// //
// Dependencies // Dependencies
// //

View file

@ -0,0 +1,26 @@
package com.beust.kobalt.internal
import com.beust.kobalt.api.*
/**
* Base class for JVM language plug-ins.
*/
abstract class BaseJvmPlugin<T>: ConfigPlugin<T>(), IProjectContributor, ICompilerFlagContributor {
override fun apply(project: Project, context: KobaltContext) {
super.apply(project, context)
project.projectProperties.put(JvmCompilerPlugin.DEPENDENT_PROJECTS, projects())
}
private val allProjects = arrayListOf<ProjectDescription>()
fun addDependentProjects(project: Project, dependents: List<Project>) {
project.projectInfo.dependsOn.addAll(dependents)
with(ProjectDescription(project, dependents)) {
allProjects.add(this)
}
}
// IProjectContributor
override fun projects() = allProjects
}

View file

@ -21,12 +21,11 @@ import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
/** /**
* Base classes for plug-ins that compile files on the JVM. This base class requires the bare minimum * This plug-in takes care of compilation: it declares a bunch of tasks ("compile", "compileTest") and
* contributors (source files, projects and tasks). Subclasses can add more as they see fit (e.g. test * and picks up all the compiler contributors in order to run them whenever a compilation is requested.
* source directory, etc...).
*/ */
@Singleton @Singleton
abstract class JvmCompilerPlugin @Inject constructor( open class JvmCompilerPlugin @Inject constructor(
open val localRepo: LocalRepo, open val localRepo: LocalRepo,
open val files: KFiles, open val files: KFiles,
open val depFactory: DepFactory, open val depFactory: DepFactory,
@ -53,6 +52,10 @@ abstract class JvmCompilerPlugin @Inject constructor(
const val DOCS_DIRECTORY = "docs/javadoc" const val DOCS_DIRECTORY = "docs/javadoc"
} }
override val name: String = "JvmCompiler"
override fun accept(project: Project) = true
/** /**
* Log with a project. * Log with a project.
*/ */
@ -132,12 +135,6 @@ abstract class JvmCompilerPlugin @Inject constructor(
} }
} }
open fun toClassFile(sourceFile: String) = sourceFile + ".class"
fun addCompilerArgs(project: Project, vararg args: String) {
project.projectProperties.put(COMPILER_ARGS, arrayListOf(*args))
}
@IncrementalTask(name = JvmCompilerPlugin.TASK_COMPILE, description = "Compile the project") @IncrementalTask(name = JvmCompilerPlugin.TASK_COMPILE, description = "Compile the project")
fun taskCompile(project: Project): IncrementalTaskInfo { fun taskCompile(project: Project): IncrementalTaskInfo {
val inputChecksum = Md5.toMd5Directories(project.sourceDirectories.map { val inputChecksum = Md5.toMd5Directories(project.sourceDirectories.map {
@ -152,7 +149,11 @@ abstract class JvmCompilerPlugin @Inject constructor(
) )
} }
private fun doTaskCompile(project: Project): TaskResult { private fun doTaskCompile(project: Project) = doTaskCompile(project, isTest = false)
private fun doTaskCompileTest(project: Project) = doTaskCompile(project, isTest = true)
private fun doTaskCompile(project: Project, isTest: Boolean): TaskResult {
// Set up the source files now that we have the variant // Set up the source files now that we have the variant
sourceDirectories.addAll(context.variant.sourceDirectories(project, context)) sourceDirectories.addAll(context.variant.sourceDirectories(project, context))
@ -160,9 +161,8 @@ abstract class JvmCompilerPlugin @Inject constructor(
if (sourceDirectory != null) { if (sourceDirectory != null) {
sourceDirectories.add(sourceDirectory) sourceDirectories.add(sourceDirectory)
} }
// val info = createCompilerActionInfo(project, context, isTest = false)
// val compiler = ActorUtils.selectAffinityActor(project, context, context.pluginInfo.compilerContributors)
val results = arrayListOf<TaskResult>() val results = arrayListOf<TaskResult>()
val compilers = ActorUtils.selectAffinityActors(project, context, context.pluginInfo.compilerContributors) val compilers = ActorUtils.selectAffinityActors(project, context, context.pluginInfo.compilerContributors)
var failedResult: TaskResult? = null var failedResult: TaskResult? = null
@ -170,8 +170,7 @@ abstract class JvmCompilerPlugin @Inject constructor(
throw KobaltException("Couldn't find any compiler for project ${project.name}") throw KobaltException("Couldn't find any compiler for project ${project.name}")
} else { } else {
compilers.forEach { compiler -> compilers.forEach { compiler ->
val info = createCompilerActionInfo(project, context, isTest = false, val info = createCompilerActionInfo(project, context, isTest, sourceSuffixes = compiler.sourceSuffixes)
sourceSuffixes = compiler.sourceSuffixes)
val thisResult = compiler.compile(project, context, info) val thisResult = compiler.compile(project, context, info)
results.add(thisResult) results.add(thisResult)
if (! thisResult.success && failedResult == null) { if (! thisResult.success && failedResult == null) {
@ -179,19 +178,12 @@ abstract class JvmCompilerPlugin @Inject constructor(
} }
} }
return if (failedResult != null) failedResult!! return if (failedResult != null) failedResult!!
else results[0] else results[0]
} }
} }
val allProjects = arrayListOf<ProjectDescription>() val allProjects = arrayListOf<ProjectDescription>()
fun addDependentProjects(project: Project, dependents: List<Project>) {
project.projectInfo.dependsOn.addAll(dependents)
with(ProjectDescription(project, dependents)) {
allProjects.add(this)
}
}
override fun projects() : List<ProjectDescription> { override fun projects() : List<ProjectDescription> {
return allProjects return allProjects
} }
@ -204,7 +196,7 @@ abstract class JvmCompilerPlugin @Inject constructor(
var result: TaskResult? = null var result: TaskResult? = null
compilers.forEach { compiler -> compilers.forEach { compiler ->
result = docGenerator.generateDoc(project, context, createCompilerActionInfo(project, context, result = docGenerator.generateDoc(project, context, createCompilerActionInfo(project, context,
isTest = false, sourceSuffixes = compiler.sourceSuffixes)) isTest = false, sourceSuffixes = compiler.sourceSuffixes))
} }
return result!! return result!!
} else { } else {
@ -278,7 +270,7 @@ abstract class JvmCompilerPlugin @Inject constructor(
// Finally, alter the info with the compiler interceptors before returning it // Finally, alter the info with the compiler interceptors before returning it
val initialActionInfo = CompilerActionInfo(projectDirectory.path, classpath, sourceFiles + extraSourceFiles, val initialActionInfo = CompilerActionInfo(projectDirectory.path, classpath, sourceFiles + extraSourceFiles,
buildDirectory, emptyList()) buildDirectory, emptyList() /* the flags will be provided by flag contributors */)
val result = context.pluginInfo.compilerInterceptors.fold(initialActionInfo, { ai, interceptor -> val result = context.pluginInfo.compilerInterceptors.fold(initialActionInfo, { ai, interceptor ->
interceptor.intercept(project, context, ai) interceptor.intercept(project, context, ai)
}) })
@ -306,6 +298,6 @@ abstract class JvmCompilerPlugin @Inject constructor(
) )
} }
abstract protected fun doTaskCompileTest(project: Project): TaskResult open val compiler: ICompilerContributor? = null
} }

View file

@ -11,9 +11,6 @@ import java.util.*
* Data that is useful for projects to have but should not be specified in the DSL. * Data that is useful for projects to have but should not be specified in the DSL.
*/ */
interface IProjectInfo { interface IProjectInfo {
val defaultSourceDirectories: HashSet<String>
val defaultTestDirectories: HashSet<String>
/** /**
* If at least one build config was found either on the project or the variant, this function * If at least one build config was found either on the project or the variant, this function
* will be used to generate the BuildConfig file with the correct language. * will be used to generate the BuildConfig file with the correct language.

View file

@ -55,6 +55,9 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b
// Find all the projects in the build file, possibly compiling them // Find all the projects in the build file, possibly compiling them
// //
val allProjects = findProjects(context) val allProjects = findProjects(context)
allProjects.forEach {
it.projectExtra = Project.ProjectExtra(it)
}
plugins.applyPlugins(context, allProjects) plugins.applyPlugins(context, allProjects)
return allProjects return allProjects

View file

@ -5,8 +5,8 @@ import com.beust.kobalt.plugin.java.JavaProjectInfo
import com.google.inject.Inject import com.google.inject.Inject
public class JavaBuildGenerator @Inject constructor (val projectInfo: JavaProjectInfo) : BuildGenerator() { public class JavaBuildGenerator @Inject constructor (val projectInfo: JavaProjectInfo) : BuildGenerator() {
override val defaultSourceDirectories = projectInfo.defaultSourceDirectories override val defaultSourceDirectories = hashSetOf("src/main/java")
override val defaultTestDirectories = projectInfo.defaultTestDirectories override val defaultTestDirectories = hashSetOf("src/test/java")
override val directive = "javaProject" override val directive = "javaProject"
override val name = "java" override val name = "java"
override val fileMatch = { f: String -> f.endsWith(".java") } override val fileMatch = { f: String -> f.endsWith(".java") }

View file

@ -5,8 +5,8 @@ import com.beust.kobalt.plugin.kotlin.KotlinProjectInfo
import com.google.inject.Inject import com.google.inject.Inject
public class KotlinBuildGenerator @Inject constructor (val projectInfo: KotlinProjectInfo) : BuildGenerator() { public class KotlinBuildGenerator @Inject constructor (val projectInfo: KotlinProjectInfo) : BuildGenerator() {
override val defaultSourceDirectories = projectInfo.defaultSourceDirectories override val defaultSourceDirectories = hashSetOf("src/main/kotlin")
override val defaultTestDirectories = projectInfo.defaultTestDirectories override val defaultTestDirectories = hashSetOf("src/test/kotlin")
override val directive = "kotlinProject" override val directive = "kotlinProject"
override val name = "kotlin" override val name = "kotlin"
override val fileMatch = { f: String -> f.endsWith(".kt") } override val fileMatch = { f: String -> f.endsWith(".kt") }

View file

@ -52,6 +52,7 @@ class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) {
command = "javac " + allArgs.joinToString(" ") + " " + info.sourceFiles.joinToString(" ") command = "javac " + allArgs.joinToString(" ") + " " + info.sourceFiles.joinToString(" ")
log(2, "Launching\n$command") log(2, "Launching\n$command")
log(1, " Java compiling ${info.sourceFiles.size} files")
val result = task.call() val result = task.call()
errorMessage = dc.diagnostics.joinToString("\n") errorMessage = dc.diagnostics.joinToString("\n")
result result
@ -71,8 +72,8 @@ class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) {
val pb = ProcessBuilder(allArgs) val pb = ProcessBuilder(allArgs)
pb.inheritIO() pb.inheritIO()
val line = allArgs.joinToString(" ") val line = allArgs.joinToString(" ")
log(1, " Compiling ${info.sourceFiles.size} files") log(1, " Java compiling ${info.sourceFiles.size} files")
log(2, " Compiling $line") log(2, " Java compiling $line")
command = allArgs.joinToString(" ") + " " + info.sourceFiles.joinToString(" ") command = allArgs.joinToString(" ") + " " + info.sourceFiles.joinToString(" ")
val process = pb.start() val process = pb.start()

View file

@ -3,37 +3,22 @@ package com.beust.kobalt.plugin.java
import com.beust.kobalt.TaskResult import com.beust.kobalt.TaskResult
import com.beust.kobalt.api.* import com.beust.kobalt.api.*
import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.internal.JvmCompiler import com.beust.kobalt.internal.BaseJvmPlugin
import com.beust.kobalt.internal.JvmCompilerPlugin
import com.beust.kobalt.maven.DepFactory
import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.maven.LocalRepo
import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.KobaltExecutors
import com.beust.kobalt.misc.warn import com.beust.kobalt.misc.warn
import java.io.File import java.io.File
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class JavaPlugin @Inject constructor( class JavaPlugin @Inject constructor(val javaCompiler: JavaCompiler)
override val localRepo: LocalRepo, : BaseJvmPlugin<JavaConfig>(), IDocContributor, ICompilerContributor, ITestSourceDirectoryContributor {
override val files: KFiles,
override val depFactory: DepFactory,
override val dependencyManager: DependencyManager,
override val executors: KobaltExecutors,
val javaCompiler: JavaCompiler,
override val jvmCompiler: JvmCompiler,
override val taskContributor : TaskContributor)
: JvmCompilerPlugin(localRepo, files, depFactory, dependencyManager, executors, jvmCompiler, taskContributor),
ICompilerContributor, IDocContributor, ITestSourceDirectoryContributor {
companion object { companion object {
const val PLUGIN_NAME = "Java" const val PLUGIN_NAME = "Java"
} }
override val name = PLUGIN_NAME override val name = PLUGIN_NAME
override fun accept(project: Project) = project.sourceDirectories.any { it.contains("java") } override fun accept(project: Project) = project.projectExtra.suffixesFound.contains("java")
// IDocContributor // IDocContributor
override fun affinity(project: Project, context: KobaltContext) = override fun affinity(project: Project, context: KobaltContext) =
@ -42,7 +27,7 @@ class JavaPlugin @Inject constructor(
override fun generateDoc(project: Project, context: KobaltContext, info: CompilerActionInfo) : TaskResult { override fun generateDoc(project: Project, context: KobaltContext, info: CompilerActionInfo) : TaskResult {
val result = val result =
if (info.sourceFiles.size > 0) { if (info.sourceFiles.size > 0) {
javaCompiler.javadoc(project, context, info.copy(compilerArgs = compilerArgsFor(project))) javaCompiler.javadoc(project, context, info)
} else { } else {
warn("Couldn't find any source files to run Javadoc on") warn("Couldn't find any source files to run Javadoc on")
TaskResult() TaskResult()
@ -50,13 +35,9 @@ class JavaPlugin @Inject constructor(
return result return result
} }
override fun doTaskCompileTest(project: Project): TaskResult { // ICompilerFlagsContributor
copyResources(project, JvmCompilerPlugin.SOURCE_SET_TEST) override fun flagsFor(project: Project, context: KobaltContext, currentFlags: List<String>)
val compilerActionInfo = createCompilerActionInfo(project, context, isTest = true, = configurationFor(project)?.compilerArgs ?: listOf<String>()
sourceSuffixes = sourceSuffixes)
val result = javaCompiler.compile(project, context, compilerActionInfo)
return result
}
// ICompilerContributor // ICompilerContributor
override val sourceSuffixes = listOf("java") override val sourceSuffixes = listOf("java")
@ -64,7 +45,7 @@ class JavaPlugin @Inject constructor(
override fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo) : TaskResult { override fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo) : TaskResult {
val result = val result =
if (info.sourceFiles.size > 0) { if (info.sourceFiles.size > 0) {
javaCompiler.compile(project, context, info.copy(compilerArgs = compilerArgsFor(project))) javaCompiler.compile(project, context, info)
} else { } else {
warn("Couldn't find any source files to compile") warn("Couldn't find any source files to compile")
TaskResult() TaskResult()
@ -75,24 +56,25 @@ class JavaPlugin @Inject constructor(
// ITestSourceDirectoryContributor // ITestSourceDirectoryContributor
override fun testSourceDirectoriesFor(project: Project, context: KobaltContext) override fun testSourceDirectoriesFor(project: Project, context: KobaltContext)
= project.sourceDirectoriesTest.map { File(it) }.toList() = project.sourceDirectoriesTest.map { File(it) }.toList()
} }
@Directive @Directive
public fun javaProject(vararg projects: Project, init: JavaProject.() -> Unit): JavaProject { public fun javaProject(vararg projects: Project, init: JavaProject.() -> Unit): JavaProject {
return JavaProject().apply { return JavaProject().apply {
init() init()
(Kobalt.findPlugin(JavaPlugin.PLUGIN_NAME) as JvmCompilerPlugin).addDependentProjects(this, projects.toList()) (Kobalt.findPlugin(JavaPlugin.PLUGIN_NAME) as JavaPlugin).addDependentProjects(this, projects.toList())
} }
} }
class JavaCompilerConfig(val project: Project) { class JavaConfig(val project: Project) {
fun args(vararg options: String) { val compilerArgs = arrayListOf<String>()
(Kobalt.findPlugin(JavaPlugin.PLUGIN_NAME) as JvmCompilerPlugin).addCompilerArgs(project, *options) fun args(vararg options: String) = compilerArgs.addAll(options)
}
} }
@Directive @Directive
fun Project.javaCompiler(init: JavaCompilerConfig.() -> Unit) = let { fun Project.javaCompiler(init: JavaConfig.() -> Unit) = let {
JavaCompilerConfig(it).init() val config = JavaConfig(it)
config.init()
(Kobalt.findPlugin(JavaPlugin.PLUGIN_NAME) as JavaPlugin).addConfiguration(this, config)
} }

View file

@ -10,9 +10,6 @@ import com.google.inject.Singleton
@Singleton @Singleton
class JavaProjectInfo : BaseProjectInfo() { class JavaProjectInfo : BaseProjectInfo() {
override val defaultSourceDirectories = hashSetOf("src/main/java", "src/main/resources")
override val defaultTestDirectories = hashSetOf("src/test/java", "src/test/resources")
override fun generate(field: BuildConfigField) = with(field) { override fun generate(field: BuildConfigField) = with(field) {
" public static final $type $name = $value;" " public static final $type $name = $value;"
} }

View file

@ -38,7 +38,7 @@ class KotlinCompiler @Inject constructor(
val compilerAction = object: ICompilerAction { val compilerAction = object: ICompilerAction {
override fun compile(projectName: String?, info: CompilerActionInfo): TaskResult { override fun compile(projectName: String?, info: CompilerActionInfo): TaskResult {
if (info.sourceFiles.size > 1) { if (info.sourceFiles.size > 1) {
log(1, " Compiling ${info.sourceFiles.size} files") log(1, " Kotlin compiling ${info.sourceFiles.size} files")
} }
val cp = compilerFirst(info.dependencies.map {it.jarFile.get()}) val cp = compilerFirst(info.dependencies.map {it.jarFile.get()})
val outputDir = if (info.directory != null) { val outputDir = if (info.directory != null) {
@ -152,7 +152,7 @@ class KConfiguration @Inject constructor(val compiler: KotlinCompiler){
fun compilerArgs(s: List<String>) = args.addAll(s) fun compilerArgs(s: List<String>) = args.addAll(s)
fun compile(project: Project? = null, context: KobaltContext? = null) : TaskResult { fun compile(project: Project? = null, context: KobaltContext? = null) : TaskResult {
return compiler.compile(project, context, dependencies, classpath, source, output, args + "-no-stdlib") return compiler.compile(project, context, dependencies, classpath, source, output, args /* + "-no-stdlib" */)
} }
} }

View file

@ -3,43 +3,27 @@ package com.beust.kobalt.plugin.kotlin
import com.beust.kobalt.TaskResult import com.beust.kobalt.TaskResult
import com.beust.kobalt.api.* import com.beust.kobalt.api.*
import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.internal.JvmCompiler import com.beust.kobalt.internal.BaseJvmPlugin
import com.beust.kobalt.internal.JvmCompilerPlugin
import com.beust.kobalt.maven.DepFactory
import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.maven.LocalRepo
import com.beust.kobalt.maven.dependency.FileDependency import com.beust.kobalt.maven.dependency.FileDependency
import com.beust.kobalt.maven.dependency.MavenDependency import com.beust.kobalt.maven.dependency.MavenDependency
import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.KobaltExecutors import com.beust.kobalt.misc.KobaltExecutors
import com.beust.kobalt.misc.log
import com.beust.kobalt.misc.warn import com.beust.kobalt.misc.warn
import java.io.File import java.io.File
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Singleton import javax.inject.Singleton
@Singleton @Singleton
class KotlinPlugin @Inject constructor( class KotlinPlugin @Inject constructor(val executors: KobaltExecutors)
override val localRepo: LocalRepo, : BaseJvmPlugin<KotlinConfig>(), IDocContributor, IClasspathContributor, ICompilerContributor {
override val files: KFiles,
override val depFactory: DepFactory,
override val dependencyManager: DependencyManager,
override val executors: KobaltExecutors,
override val jvmCompiler: JvmCompiler,
override val taskContributor : TaskContributor)
: JvmCompilerPlugin(localRepo, files, depFactory, dependencyManager, executors, jvmCompiler, taskContributor),
IClasspathContributor, ICompilerContributor, IDocContributor {
companion object { companion object {
const val PLUGIN_NAME = "Kotlin" const val PLUGIN_NAME = "Kotlin"
} }
override fun apply(project: Project, context: KobaltContext) {
super.apply(project, context)
}
override val name = PLUGIN_NAME override val name = PLUGIN_NAME
override fun accept(project: Project) = project.sourceDirectories.any { it.contains("kotlin") } override fun accept(project: Project) = project.projectExtra.suffixesFound.contains("kt")
// IDocContributor // IDocContributor
override fun affinity(project: Project, context: KobaltContext) = override fun affinity(project: Project, context: KobaltContext) =
@ -49,7 +33,11 @@ class KotlinPlugin @Inject constructor(
return TaskResult() return TaskResult()
} }
// override fun generateDoc(project: Project, context: KobaltContext, info: CompilerActionInfo) : TaskResult { // ICompilerFlagsContributor
override fun flagsFor(project: Project, context: KobaltContext, currentFlags: List<String>)
= configurationFor(project)?.compilerArgs ?: listOf<String>()
// override fun generateDoc(project: Project, context: KobaltContext, info: CompilerActionInfo) : TaskResult {
// val configs = dokkaConfigurations[project.name] // val configs = dokkaConfigurations[project.name]
// val classpath = context.dependencyManager.calculateDependencies(project, context) // val classpath = context.dependencyManager.calculateDependencies(project, context)
// val buildDir = project.buildDirectory // val buildDir = project.buildDirectory
@ -80,35 +68,12 @@ class KotlinPlugin @Inject constructor(
// return TaskResult(success) // return TaskResult(success)
// } // }
override protected fun doTaskCompileTest(project: Project) : TaskResult {
copyResources(project, JvmCompilerPlugin.SOURCE_SET_TEST)
val projectDir = File(project.directory)
val sourceFiles = files.findRecursively(projectDir, project.sourceDirectoriesTest.map { File(it) })
{ file: String -> sourceSuffixes.any { file.endsWith(it) } }
.map { File(projectDir, it).absolutePath }
val result =
if (sourceFiles.size > 0) {
compilePrivate(project, dependencyManager.testDependencies(project, context),
sourceFiles,
KFiles.makeOutputTestDir(project))
} else {
warn("Couldn't find any source test files")
TaskResult()
}
lp(project, "Compilation of tests succeeded")
return result
}
private fun compilePrivate(project: Project, cpList: List<IClasspathDependency>, sources: List<String>, private fun compilePrivate(project: Project, cpList: List<IClasspathDependency>, sources: List<String>,
outputDirectory: File): TaskResult { outputDirectory: File, compilerArgs: List<String>): TaskResult {
return kotlinCompilePrivate { return kotlinCompilePrivate {
classpath(cpList.map { it.jarFile.get().absolutePath }) classpath(cpList.map { it.jarFile.get().absolutePath })
sourceFiles(sources) sourceFiles(sources)
compilerArgs(compilerArgsFor(project)) compilerArgs(compilerArgs)
output = outputDirectory output = outputDirectory
}.compile(project, context) }.compile(project, context)
} }
@ -120,10 +85,9 @@ class KotlinPlugin @Inject constructor(
return result return result
} }
// interface IClasspathContributor // interface IClasspathContributor
override fun entriesFor(project: Project?): List<IClasspathDependency> = override fun entriesFor(project: Project?): List<IClasspathDependency> =
if (project == null || project is KotlinProject) { if (project == null || accept(project)) {
// 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
listOf(getKotlinCompilerJar("kotlin-stdlib"), getKotlinCompilerJar("kotlin-runtime")) listOf(getKotlinCompilerJar("kotlin-stdlib"), getKotlinCompilerJar("kotlin-runtime"))
.map { FileDependency(it) } .map { FileDependency(it) }
@ -138,7 +102,7 @@ class KotlinPlugin @Inject constructor(
override fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo) : TaskResult { override fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo) : TaskResult {
val result = val result =
if (info.sourceFiles.size > 0) { if (info.sourceFiles.size > 0) {
compilePrivate(project, info.dependencies, info.sourceFiles, info.outputDir) compilePrivate(project, info.dependencies, info.sourceFiles, info.outputDir, info.compilerArgs)
} else { } else {
warn("Couldn't find any source files") warn("Couldn't find any source files")
TaskResult() TaskResult()
@ -154,7 +118,10 @@ class KotlinPlugin @Inject constructor(
// dokkaConfigurations.put(project.name, dokkaConfig) // dokkaConfigurations.put(project.name, dokkaConfig)
// } // }
override fun toClassFile(sourceFile: String) = sourceFile + "Kt.class" protected fun lp(project: Project, s: String) {
log(2, "${project.name}: $s")
}
} }
/** /**
@ -164,19 +131,20 @@ class KotlinPlugin @Inject constructor(
fun kotlinProject(vararg projects: Project, init: KotlinProject.() -> Unit): KotlinProject { fun kotlinProject(vararg projects: Project, init: KotlinProject.() -> Unit): KotlinProject {
return KotlinProject().apply { return KotlinProject().apply {
init() init()
(Kobalt.findPlugin(KotlinPlugin.PLUGIN_NAME) as JvmCompilerPlugin).addDependentProjects(this, projects.toList()) (Kobalt.findPlugin(KotlinPlugin.PLUGIN_NAME) as KotlinPlugin).addDependentProjects(this, projects.toList())
} }
} }
class KotlinCompilerConfig(val project: Project) { class KotlinConfig(val project: Project) {
fun args(vararg options: String) { val compilerArgs = arrayListOf<String>()
(Kobalt.findPlugin(KotlinPlugin.PLUGIN_NAME) as JvmCompilerPlugin).addCompilerArgs(project, *options) fun args(vararg options: String) = compilerArgs.addAll(options)
}
} }
@Directive @Directive
fun Project.kotlinCompiler(init: KotlinCompilerConfig.() -> Unit) = let { fun Project.kotlinCompiler(init: KotlinConfig.() -> Unit) = let {
KotlinCompilerConfig(it).init() val config = KotlinConfig(it)
config.init()
(Kobalt.findPlugin(KotlinPlugin.PLUGIN_NAME) as KotlinPlugin).addConfiguration(this, config)
} }
//class SourceLinkMapItem { //class SourceLinkMapItem {

View file

@ -10,9 +10,6 @@ import com.google.inject.Singleton
@Singleton @Singleton
class KotlinProjectInfo : BaseProjectInfo() { class KotlinProjectInfo : BaseProjectInfo() {
override val defaultSourceDirectories = hashSetOf("src/main/kotlin", "src/main/resources")
override val defaultTestDirectories = hashSetOf("src/test/kotlin", "src/test/resources")
override fun generate(field: BuildConfigField) = with(field) { override fun generate(field: BuildConfigField) = with(field) {
" val $name : $type = $value" " val $name : $type = $value"
} }