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

Make both kotlin and java test compile incremental.

This commit is contained in:
Cedric Beust 2016-01-02 10:29:49 -08:00
parent e7f844ab7f
commit b3107f52b5
3 changed files with 49 additions and 52 deletions

View file

@ -69,7 +69,7 @@ abstract class JvmCompilerPlugin @Inject constructor(
@Task(name = TASK_TEST, description = "Run the tests", @Task(name = TASK_TEST, description = "Run the tests",
runAfter = arrayOf(JvmCompilerPlugin.TASK_COMPILE, JvmCompilerPlugin.TASK_COMPILE_TEST)) runAfter = arrayOf(JvmCompilerPlugin.TASK_COMPILE, JvmCompilerPlugin.TASK_COMPILE_TEST))
fun taskTest(project: Project) : TaskResult { fun taskTest(project: Project): TaskResult {
lp(project, "Running tests") lp(project, "Running tests")
val runContributor = ActorUtils.selectAffinityActor(project, context, val runContributor = ActorUtils.selectAffinityActor(project, context,
@ -84,9 +84,9 @@ abstract class JvmCompilerPlugin @Inject constructor(
} }
@Task(name = TASK_CLEAN, description = "Clean the project") @Task(name = TASK_CLEAN, description = "Clean the project")
fun taskClean(project : Project ) : TaskResult { fun taskClean(project: Project): TaskResult {
java.io.File(project.directory, project.buildDirectory).let { dir -> java.io.File(project.directory, project.buildDirectory).let { dir ->
if (! dir.deleteRecursively()) { if (!dir.deleteRecursively()) {
warn("Couldn't delete $dir") warn("Couldn't delete $dir")
} }
} }
@ -114,7 +114,7 @@ abstract class JvmCompilerPlugin @Inject constructor(
val absOutputDir = File(KFiles.joinDir(project.directory, project.buildDirectory, outputDir)) val absOutputDir = File(KFiles.joinDir(project.directory, project.buildDirectory, outputDir))
sourceDirs.map { File(project.directory, it) }.filter { sourceDirs.map { File(project.directory, it) }.filter {
it.exists() it.exists()
} .forEach { }.forEach {
log(2, "Copying from $sourceDirs to $absOutputDir") log(2, "Copying from $sourceDirs to $absOutputDir")
KFiles.copyRecursively(it, absOutputDir, deleteFirst = false) KFiles.copyRecursively(it, absOutputDir, deleteFirst = false)
} }
@ -123,7 +123,7 @@ abstract class JvmCompilerPlugin @Inject constructor(
} }
} }
protected fun compilerArgsFor(project: Project) : List<String> { protected fun compilerArgsFor(project: Project): List<String> {
val result = project.projectProperties.get(COMPILER_ARGS) val result = project.projectProperties.get(COMPILER_ARGS)
if (result != null) { if (result != null) {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
@ -139,8 +139,8 @@ abstract class JvmCompilerPlugin @Inject constructor(
project.projectProperties.put(COMPILER_ARGS, arrayListOf(*args)) project.projectProperties.put(COMPILER_ARGS, arrayListOf(*args))
} }
fun isOutdated(project: Project, context: KobaltContext, actionInfo: CompilerActionInfo) : Boolean { fun isOutdated(project: Project, context: KobaltContext, actionInfo: CompilerActionInfo): Boolean {
fun stripSourceDir(sourceFile: String) : String { fun stripSourceDir(sourceFile: String): String {
project.sourceDirectories.forEach { project.sourceDirectories.forEach {
val d = listOf(project.directory, it).joinToString("/") val d = listOf(project.directory, it).joinToString("/")
if (sourceFile.startsWith(d)) return sourceFile.substring(d.length + 1) if (sourceFile.startsWith(d)) return sourceFile.substring(d.length + 1)
@ -148,7 +148,7 @@ abstract class JvmCompilerPlugin @Inject constructor(
throw KobaltException("Couldn't strip source dir from $sourceFile") throw KobaltException("Couldn't strip source dir from $sourceFile")
} }
fun stripSuffix( sourceFile: String) : String { fun stripSuffix(sourceFile: String): String {
val index = sourceFile.indexOf(project.sourceSuffix) val index = sourceFile.indexOf(project.sourceSuffix)
if (index >= 0) return sourceFile.substring(0, index) if (index >= 0) return sourceFile.substring(0, index)
else return sourceFile else return sourceFile
@ -158,9 +158,9 @@ abstract class JvmCompilerPlugin @Inject constructor(
val stripped = stripSourceDir(sourceFile) val stripped = stripSourceDir(sourceFile)
val classFile = File(KFiles.joinDir(project.directory, project.classesDir(context), val classFile = File(KFiles.joinDir(project.directory, project.classesDir(context),
toClassFile(stripSuffix(stripped)))) toClassFile(stripSuffix(stripped))))
if (! classFile.exists() || File(sourceFile).lastModified() > classFile.lastModified()) { if (!classFile.exists() || File(sourceFile).lastModified() > classFile.lastModified()) {
log(2, "Outdated $sourceFile $classFile " + Date(File(sourceFile).lastModified()) + log(2, "Outdated $sourceFile $classFile " + Date(File(sourceFile).lastModified()) +
" " + classFile.lastModified()) " " + classFile.lastModified())
return true return true
} }
} }
@ -168,7 +168,7 @@ abstract class JvmCompilerPlugin @Inject constructor(
} }
@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 {
File(project.directory, it) File(project.directory, it)
}) })
@ -181,7 +181,7 @@ abstract class JvmCompilerPlugin @Inject constructor(
) )
} }
private fun doTaskCompile(project: Project) : TaskResult { private fun doTaskCompile(project: Project): 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)) sourceDirectories.addAll(context.variant.sourceDirectories(project))
@ -201,7 +201,7 @@ abstract class JvmCompilerPlugin @Inject constructor(
override fun projects() = projects override fun projects() = projects
@Task(name = "doc", description = "Generate the documentation for the project") @Task(name = "doc", description = "Generate the documentation for the project")
fun taskJavadoc(project: Project) : TaskResult { fun taskJavadoc(project: Project): TaskResult {
val docGenerator = ActorUtils.selectAffinityActor(project, context, context.pluginInfo.docContributors) val docGenerator = ActorUtils.selectAffinityActor(project, context, context.pluginInfo.docContributors)
if (docGenerator != null) { if (docGenerator != null) {
return docGenerator.generateDoc(project, context, createCompilerActionInfo(project, context, return docGenerator.generateDoc(project, context, createCompilerActionInfo(project, context,
@ -216,43 +216,43 @@ abstract class JvmCompilerPlugin @Inject constructor(
* Create a CompilerActionInfo (all the information that a compiler needs to know) for the given parameters. * Create a CompilerActionInfo (all the information that a compiler needs to know) for the given parameters.
* Runs all the contributors and interceptors relevant to that task. * Runs all the contributors and interceptors relevant to that task.
*/ */
protected fun createCompilerActionInfo(project: Project, context: KobaltContext, isTest: Boolean) : protected fun createCompilerActionInfo(project: Project, context: KobaltContext, isTest: Boolean):
CompilerActionInfo { CompilerActionInfo {
copyResources(project, JvmCompilerPlugin.SOURCE_SET_MAIN) copyResources(project, JvmCompilerPlugin.SOURCE_SET_MAIN)
val classpath = if (isTest) val classpath = if (isTest)
dependencyManager.testDependencies(project, context, projects) dependencyManager.testDependencies(project, context, projects)
else else
dependencyManager.dependencies(project, context, projects) dependencyManager.dependencies(project, context, projects)
val projectDirectory = File(project.directory) val projectDirectory = File(project.directory)
val buildDirectory = if (isTest) KFiles.makeOutputTestDir(project) val buildDirectory = if (isTest) KFiles.makeOutputTestDir(project)
else File(project.classesDir(context)) else File(project.classesDir(context))
buildDirectory.mkdirs() buildDirectory.mkdirs()
val initialSourceDirectories = arrayListOf<File>() val initialSourceDirectories = arrayListOf<File>()
// Source directories from the contributors // Source directories from the contributors
initialSourceDirectories.addAll( initialSourceDirectories.addAll(
if (isTest) { if (isTest) {
context.pluginInfo.testSourceDirContributors.flatMap { it.testSourceDirectoriesFor(project, context) } context.pluginInfo.testSourceDirContributors.flatMap { it.testSourceDirectoriesFor(project, context) }
} else { } else {
context.pluginInfo.sourceDirContributors.flatMap { it.sourceDirectoriesFor(project, context) } context.pluginInfo.sourceDirContributors.flatMap { it.sourceDirectoriesFor(project, context) }
}) })
// Transform them with the interceptors, if any // Transform them with the interceptors, if any
val sourceDirectories = if (isTest) { val sourceDirectories = if (isTest) {
initialSourceDirectories initialSourceDirectories
} else { } else {
context.pluginInfo.sourceDirectoriesInterceptors.fold(initialSourceDirectories.toList(), context.pluginInfo.sourceDirectoriesInterceptors.fold(initialSourceDirectories.toList(),
{ sd, interceptor -> interceptor.intercept(project, context, sd) }) { sd, interceptor -> interceptor.intercept(project, context, sd) })
}.filter { }.filter {
File(project.directory, it.path).exists() File(project.directory, it.path).exists()
} }
// Now that we have the final list of source dirs, find source files in them // Now that we have the final list of source dirs, find source files in them
val sourceFiles = files.findRecursively(projectDirectory, sourceDirectories, val sourceFiles = files.findRecursively(projectDirectory, sourceDirectories,
{ it .endsWith(project.sourceSuffix) }) { it.endsWith(project.sourceSuffix) })
.map { File(projectDirectory, it).path } .map { File(projectDirectory, it).path }
// Finally, alter the info with the compiler interceptors before returning it // Finally, alter the info with the compiler interceptors before returning it
@ -269,5 +269,22 @@ abstract class JvmCompilerPlugin @Inject constructor(
// ISourceDirectoryContributor // ISourceDirectoryContributor
override fun sourceDirectoriesFor(project: Project, context: KobaltContext) override fun sourceDirectoriesFor(project: Project, context: KobaltContext)
= if (accept(project)) sourceDirectories.toList() else arrayListOf() = if (accept(project)) sourceDirectories.toList() else arrayListOf()
@IncrementalTask(name = TASK_COMPILE_TEST, description = "Compile the tests",
runAfter = arrayOf(JvmCompilerPlugin.TASK_COMPILE))
fun taskCompileTest(project: Project): IncrementalTaskInfo {
val inputChecksum = Md5.toMd5Directories(project.sourceDirectoriesTest.map {
File(project.directory, it)
})
return IncrementalTaskInfo(
inputChecksum = inputChecksum,
outputChecksum = {
Md5.toMd5Directories(listOf(KFiles.makeOutputTestDir(project)))
},
task = { project -> doTaskCompileTest(project) }
)
}
abstract protected fun doTaskCompileTest(project: Project): TaskResult
} }

View file

@ -3,7 +3,6 @@ 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.api.annotation.Task
import com.beust.kobalt.internal.JvmCompiler import com.beust.kobalt.internal.JvmCompiler
import com.beust.kobalt.internal.JvmCompilerPlugin import com.beust.kobalt.internal.JvmCompilerPlugin
import com.beust.kobalt.maven.DepFactory import com.beust.kobalt.maven.DepFactory
@ -51,9 +50,7 @@ class JavaPlugin @Inject constructor(
return result return result
} }
@Task(name = TASK_COMPILE_TEST, description = "Compile the tests", override fun doTaskCompileTest(project: Project): TaskResult {
runAfter = arrayOf(JvmCompilerPlugin.TASK_COMPILE))
fun taskCompileTest(project: Project): TaskResult {
copyResources(project, JvmCompilerPlugin.SOURCE_SET_TEST) copyResources(project, JvmCompilerPlugin.SOURCE_SET_TEST)
val compilerActionInfo = createCompilerActionInfo(project, context, isTest = true) val compilerActionInfo = createCompilerActionInfo(project, context, isTest = true)
val result = javaCompiler.compile(project, context, compilerActionInfo) val result = javaCompiler.compile(project, context, compilerActionInfo)

View file

@ -1,16 +1,13 @@
package com.beust.kobalt.plugin.kotlin package com.beust.kobalt.plugin.kotlin
import com.beust.kobalt.IncrementalTaskInfo
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.api.annotation.IncrementalTask
import com.beust.kobalt.internal.JvmCompiler import com.beust.kobalt.internal.JvmCompiler
import com.beust.kobalt.internal.JvmCompilerPlugin import com.beust.kobalt.internal.JvmCompilerPlugin
import com.beust.kobalt.maven.DepFactory import com.beust.kobalt.maven.DepFactory
import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.maven.LocalRepo import com.beust.kobalt.maven.LocalRepo
import com.beust.kobalt.maven.Md5
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.KFiles
@ -75,21 +72,7 @@ class KotlinPlugin @Inject constructor(
// return TaskResult(success) // return TaskResult(success)
// } // }
@IncrementalTask(name = TASK_COMPILE_TEST, description = "Compile the tests", runAfter = arrayOf(TASK_COMPILE)) override protected fun doTaskCompileTest(project: Project) : TaskResult {
fun taskCompileTest(project: Project): IncrementalTaskInfo {
val inputChecksum = Md5.toMd5Directories(project.sourceDirectoriesTest.map {
File(project.directory, it)
})
return IncrementalTaskInfo(
inputChecksum = inputChecksum,
outputChecksum = {
Md5.toMd5Directories(listOf(KFiles.makeOutputTestDir(project)))
},
task = { project -> doTaskCompileTest(project) }
)
}
private fun doTaskCompileTest(project: Project) : TaskResult {
copyResources(project, JvmCompilerPlugin.SOURCE_SET_TEST) copyResources(project, JvmCompilerPlugin.SOURCE_SET_TEST)
val projectDir = File(project.directory) val projectDir = File(project.directory)