1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-27 08:38:13 -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

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

View file

@ -5,8 +5,8 @@ import com.beust.kobalt.plugin.java.JavaProjectInfo
import com.google.inject.Inject
public class JavaBuildGenerator @Inject constructor (val projectInfo: JavaProjectInfo) : BuildGenerator() {
override val defaultSourceDirectories = projectInfo.defaultSourceDirectories
override val defaultTestDirectories = projectInfo.defaultTestDirectories
override val defaultSourceDirectories = hashSetOf("src/main/java")
override val defaultTestDirectories = hashSetOf("src/test/java")
override val directive = "javaProject"
override val name = "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
public class KotlinBuildGenerator @Inject constructor (val projectInfo: KotlinProjectInfo) : BuildGenerator() {
override val defaultSourceDirectories = projectInfo.defaultSourceDirectories
override val defaultTestDirectories = projectInfo.defaultTestDirectories
override val defaultSourceDirectories = hashSetOf("src/main/kotlin")
override val defaultTestDirectories = hashSetOf("src/test/kotlin")
override val directive = "kotlinProject"
override val name = "kotlin"
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(" ")
log(2, "Launching\n$command")
log(1, " Java compiling ${info.sourceFiles.size} files")
val result = task.call()
errorMessage = dc.diagnostics.joinToString("\n")
result
@ -71,8 +72,8 @@ class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) {
val pb = ProcessBuilder(allArgs)
pb.inheritIO()
val line = allArgs.joinToString(" ")
log(1, " Compiling ${info.sourceFiles.size} files")
log(2, " Compiling $line")
log(1, " Java compiling ${info.sourceFiles.size} files")
log(2, " Java compiling $line")
command = allArgs.joinToString(" ") + " " + info.sourceFiles.joinToString(" ")
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.api.*
import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.internal.JvmCompiler
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.internal.BaseJvmPlugin
import com.beust.kobalt.misc.warn
import java.io.File
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class JavaPlugin @Inject constructor(
override val localRepo: LocalRepo,
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 {
class JavaPlugin @Inject constructor(val javaCompiler: JavaCompiler)
: BaseJvmPlugin<JavaConfig>(), IDocContributor, ICompilerContributor, ITestSourceDirectoryContributor {
companion object {
const val PLUGIN_NAME = "Java"
}
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
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 {
val result =
if (info.sourceFiles.size > 0) {
javaCompiler.javadoc(project, context, info.copy(compilerArgs = compilerArgsFor(project)))
javaCompiler.javadoc(project, context, info)
} else {
warn("Couldn't find any source files to run Javadoc on")
TaskResult()
@ -50,13 +35,9 @@ class JavaPlugin @Inject constructor(
return result
}
override fun doTaskCompileTest(project: Project): TaskResult {
copyResources(project, JvmCompilerPlugin.SOURCE_SET_TEST)
val compilerActionInfo = createCompilerActionInfo(project, context, isTest = true,
sourceSuffixes = sourceSuffixes)
val result = javaCompiler.compile(project, context, compilerActionInfo)
return result
}
// ICompilerFlagsContributor
override fun flagsFor(project: Project, context: KobaltContext, currentFlags: List<String>)
= configurationFor(project)?.compilerArgs ?: listOf<String>()
// ICompilerContributor
override val sourceSuffixes = listOf("java")
@ -64,7 +45,7 @@ class JavaPlugin @Inject constructor(
override fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo) : TaskResult {
val result =
if (info.sourceFiles.size > 0) {
javaCompiler.compile(project, context, info.copy(compilerArgs = compilerArgsFor(project)))
javaCompiler.compile(project, context, info)
} else {
warn("Couldn't find any source files to compile")
TaskResult()
@ -75,24 +56,25 @@ class JavaPlugin @Inject constructor(
// ITestSourceDirectoryContributor
override fun testSourceDirectoriesFor(project: Project, context: KobaltContext)
= project.sourceDirectoriesTest.map { File(it) }.toList()
}
@Directive
public fun javaProject(vararg projects: Project, init: JavaProject.() -> Unit): JavaProject {
return JavaProject().apply {
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) {
fun args(vararg options: String) {
(Kobalt.findPlugin(JavaPlugin.PLUGIN_NAME) as JvmCompilerPlugin).addCompilerArgs(project, *options)
}
class JavaConfig(val project: Project) {
val compilerArgs = arrayListOf<String>()
fun args(vararg options: String) = compilerArgs.addAll(options)
}
@Directive
fun Project.javaCompiler(init: JavaCompilerConfig.() -> Unit) = let {
JavaCompilerConfig(it).init()
fun Project.javaCompiler(init: JavaConfig.() -> Unit) = let {
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
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) {
" public static final $type $name = $value;"
}

View file

@ -38,7 +38,7 @@ class KotlinCompiler @Inject constructor(
val compilerAction = object: ICompilerAction {
override fun compile(projectName: String?, info: CompilerActionInfo): TaskResult {
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 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 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.api.*
import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.internal.JvmCompiler
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.internal.BaseJvmPlugin
import com.beust.kobalt.maven.dependency.FileDependency
import com.beust.kobalt.maven.dependency.MavenDependency
import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.KobaltExecutors
import com.beust.kobalt.misc.log
import com.beust.kobalt.misc.warn
import java.io.File
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class KotlinPlugin @Inject constructor(
override val localRepo: LocalRepo,
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 {
class KotlinPlugin @Inject constructor(val executors: KobaltExecutors)
: BaseJvmPlugin<KotlinConfig>(), IDocContributor, IClasspathContributor, ICompilerContributor {
companion object {
const val PLUGIN_NAME = "Kotlin"
}
override fun apply(project: Project, context: KobaltContext) {
super.apply(project, context)
}
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
override fun affinity(project: Project, context: KobaltContext) =
@ -49,7 +33,11 @@ class KotlinPlugin @Inject constructor(
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 classpath = context.dependencyManager.calculateDependencies(project, context)
// val buildDir = project.buildDirectory
@ -80,35 +68,12 @@ class KotlinPlugin @Inject constructor(
// 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>,
outputDirectory: File): TaskResult {
outputDirectory: File, compilerArgs: List<String>): TaskResult {
return kotlinCompilePrivate {
classpath(cpList.map { it.jarFile.get().absolutePath })
sourceFiles(sources)
compilerArgs(compilerArgsFor(project))
compilerArgs(compilerArgs)
output = outputDirectory
}.compile(project, context)
}
@ -120,10 +85,9 @@ class KotlinPlugin @Inject constructor(
return result
}
// interface IClasspathContributor
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
listOf(getKotlinCompilerJar("kotlin-stdlib"), getKotlinCompilerJar("kotlin-runtime"))
.map { FileDependency(it) }
@ -138,7 +102,7 @@ class KotlinPlugin @Inject constructor(
override fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo) : TaskResult {
val result =
if (info.sourceFiles.size > 0) {
compilePrivate(project, info.dependencies, info.sourceFiles, info.outputDir)
compilePrivate(project, info.dependencies, info.sourceFiles, info.outputDir, info.compilerArgs)
} else {
warn("Couldn't find any source files")
TaskResult()
@ -154,7 +118,10 @@ class KotlinPlugin @Inject constructor(
// 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 {
return KotlinProject().apply {
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) {
fun args(vararg options: String) {
(Kobalt.findPlugin(KotlinPlugin.PLUGIN_NAME) as JvmCompilerPlugin).addCompilerArgs(project, *options)
}
class KotlinConfig(val project: Project) {
val compilerArgs = arrayListOf<String>()
fun args(vararg options: String) = compilerArgs.addAll(options)
}
@Directive
fun Project.kotlinCompiler(init: KotlinCompilerConfig.() -> Unit) = let {
KotlinCompilerConfig(it).init()
fun Project.kotlinCompiler(init: KotlinConfig.() -> Unit) = let {
val config = KotlinConfig(it)
config.init()
(Kobalt.findPlugin(KotlinPlugin.PLUGIN_NAME) as KotlinPlugin).addConfiguration(this, config)
}
//class SourceLinkMapItem {

View file

@ -10,9 +10,6 @@ import com.google.inject.Singleton
@Singleton
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) {
" val $name : $type = $value"
}