mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Merge branch 'master' of github.com:cbeust/kobalt
This commit is contained in:
commit
9e53359790
25 changed files with 170 additions and 64 deletions
|
@ -1 +1 @@
|
|||
kobalt.version=0.407
|
||||
kobalt.version=0.408
|
|
@ -1,6 +1,7 @@
|
|||
package com.beust.kobalt
|
||||
|
||||
import com.beust.kobalt.api.*
|
||||
import com.beust.kobalt.internal.ActorUtils
|
||||
import com.beust.kobalt.misc.KFiles
|
||||
import com.beust.kobalt.misc.log
|
||||
import java.io.File
|
||||
|
@ -34,10 +35,14 @@ class Variant(val initialProductFlavor: ProductFlavorConfig? = null,
|
|||
return result
|
||||
}
|
||||
|
||||
fun resDirectories(project: Project) : List<File> = sourceDirectories(project, "res")
|
||||
|
||||
fun sourceDirectories(project: Project) : List<File> =
|
||||
sourceDirectories(project, project.projectInfo.sourceDirectory)
|
||||
fun sourceDirectories(project: Project, context: KobaltContext) : List<File> {
|
||||
val compilers = ActorUtils.selectAffinityActors(project, context, context.pluginInfo.compilerContributors)
|
||||
val sourceSuffixes = compilers.flatMap { it.sourceSuffixes }
|
||||
val result = sourceSuffixes.flatMap {
|
||||
sourceDirectories(project, it)
|
||||
}.toHashSet()
|
||||
return result.toList()
|
||||
}
|
||||
|
||||
/**
|
||||
* suffix is either "java" (to find source files) or "res" (to find resources)
|
||||
|
@ -56,12 +61,12 @@ class Variant(val initialProductFlavor: ProductFlavorConfig? = null,
|
|||
|
||||
// // The ordering of files is: 1) build type 2) product flavor 3) default
|
||||
buildType.let {
|
||||
val dir = File(KFiles.joinDir("src", it.name, project.projectInfo.sourceDirectory))
|
||||
val dir = File(KFiles.joinDir("src", it.name, suffix))
|
||||
log(3, "Adding source for build type ${it.name}: ${dir.path}")
|
||||
result.add(dir)
|
||||
}
|
||||
productFlavor.let {
|
||||
val dir = File(KFiles.joinDir("src", it.name, project.projectInfo.sourceDirectory))
|
||||
val dir = File(KFiles.joinDir("src", it.name, suffix))
|
||||
log(3, "Adding source for product flavor ${it.name}: ${dir.path}")
|
||||
result.add(dir)
|
||||
}
|
||||
|
@ -140,7 +145,9 @@ class Variant(val initialProductFlavor: ProductFlavorConfig? = null,
|
|||
// that directory will be added when trying to find recursively all the sources in it
|
||||
generatedSourceDirectory = File(result.relativeTo(File(project.directory)))
|
||||
val outputGeneratedSourceDirectory = File(result, pkg.replace('.', File.separatorChar))
|
||||
val outputDir = File(outputGeneratedSourceDirectory, "BuildConfig" + project.sourceSuffix)
|
||||
val compilers = ActorUtils.selectAffinityActors(project, context, context.pluginInfo.compilerContributors)
|
||||
val outputDir = File(outputGeneratedSourceDirectory,
|
||||
"BuildConfig" + compilers[0].sourceSuffixes[0])
|
||||
KFiles.saveFile(outputDir, code)
|
||||
log(2, "Generated ${outputDir.path}")
|
||||
return result
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
package com.beust.kobalt.api
|
||||
|
||||
import com.beust.kobalt.TaskResult
|
||||
import com.beust.kobalt.api.IClasspathDependency
|
||||
import java.io.File
|
||||
|
||||
interface ICompilerContributor : IProjectAffinity {
|
||||
val sourceSuffixes: List<String>
|
||||
fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo) : TaskResult
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@ open class Project(
|
|||
@Directive open var artifactId: String? = null,
|
||||
@Directive open var packaging: String? = null,
|
||||
@Directive open var dependencies: Dependencies? = null,
|
||||
@Directive open var sourceSuffix : String = "",
|
||||
@Directive open var description : String = "",
|
||||
@Directive open var scm : Scm? = null,
|
||||
@Directive open var url: String? = null,
|
||||
|
|
|
@ -13,6 +13,13 @@ class ActorUtils {
|
|||
fun <T : IProjectAffinity> selectAffinityActor(project: Project, context: KobaltContext, actors: List<T>)
|
||||
= actors.maxBy { it.affinity(project, context) }
|
||||
|
||||
/**
|
||||
* Return all the plug-in actors with a non zero affinity sorted from the highest to the lowest.
|
||||
*/
|
||||
fun <T : IProjectAffinity> selectAffinityActors(project: Project, context: KobaltContext, actors: List<T>)
|
||||
= actors.filter { it.affinity(project, context) > 0 }
|
||||
.sortedByDescending { it.affinity(project, context) }
|
||||
|
||||
/**
|
||||
* Return the plug-in actor with the highest affinity.
|
||||
*/
|
||||
|
|
|
@ -138,34 +138,6 @@ abstract class JvmCompilerPlugin @Inject constructor(
|
|||
project.projectProperties.put(COMPILER_ARGS, arrayListOf(*args))
|
||||
}
|
||||
|
||||
fun isOutdated(project: Project, context: KobaltContext, actionInfo: CompilerActionInfo): Boolean {
|
||||
fun stripSourceDir(sourceFile: String): String {
|
||||
project.sourceDirectories.forEach {
|
||||
val d = listOf(project.directory, it).joinToString("/")
|
||||
if (sourceFile.startsWith(d)) return sourceFile.substring(d.length + 1)
|
||||
}
|
||||
throw KobaltException("Couldn't strip source dir from $sourceFile")
|
||||
}
|
||||
|
||||
fun stripSuffix(sourceFile: String): String {
|
||||
val index = sourceFile.indexOf(project.sourceSuffix)
|
||||
if (index >= 0) return sourceFile.substring(0, index)
|
||||
else return sourceFile
|
||||
}
|
||||
|
||||
actionInfo.sourceFiles.map { it.replace("\\", "/") }.forEach { sourceFile ->
|
||||
val stripped = stripSourceDir(sourceFile)
|
||||
val classFile = File(KFiles.joinDir(project.directory, project.classesDir(context),
|
||||
toClassFile(stripSuffix(stripped))))
|
||||
if (!classFile.exists() || File(sourceFile).lastModified() > classFile.lastModified()) {
|
||||
log(2, "Outdated $sourceFile $classFile " + Date(File(sourceFile).lastModified()) +
|
||||
" " + classFile.lastModified())
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@IncrementalTask(name = JvmCompilerPlugin.TASK_COMPILE, description = "Compile the project")
|
||||
fun taskCompile(project: Project): IncrementalTaskInfo {
|
||||
val inputChecksum = Md5.toMd5Directories(project.sourceDirectories.map {
|
||||
|
@ -182,18 +154,32 @@ abstract class JvmCompilerPlugin @Inject constructor(
|
|||
|
||||
private fun doTaskCompile(project: Project): TaskResult {
|
||||
// Set up the source files now that we have the variant
|
||||
sourceDirectories.addAll(context.variant.sourceDirectories(project))
|
||||
sourceDirectories.addAll(context.variant.sourceDirectories(project, context))
|
||||
|
||||
val sourceDirectory = context.variant.maybeGenerateBuildConfig(project, context)
|
||||
if (sourceDirectory != null) {
|
||||
sourceDirectories.add(sourceDirectory)
|
||||
}
|
||||
val info = createCompilerActionInfo(project, context, isTest = false)
|
||||
val compiler = ActorUtils.selectAffinityActor(project, context, context.pluginInfo.compilerContributors)
|
||||
if (compiler != null) {
|
||||
return compiler.compile(project, context, info)
|
||||
} else {
|
||||
// val info = createCompilerActionInfo(project, context, isTest = false)
|
||||
// val compiler = ActorUtils.selectAffinityActor(project, context, context.pluginInfo.compilerContributors)
|
||||
val results = arrayListOf<TaskResult>()
|
||||
val compilers = ActorUtils.selectAffinityActors(project, context, context.pluginInfo.compilerContributors)
|
||||
|
||||
var failedResult: TaskResult? = null
|
||||
if (compilers.isEmpty()) {
|
||||
throw KobaltException("Couldn't find any compiler for project ${project.name}")
|
||||
} else {
|
||||
compilers.forEach { compiler ->
|
||||
val info = createCompilerActionInfo(project, context, isTest = false,
|
||||
sourceSuffixes = compiler.sourceSuffixes)
|
||||
val thisResult = compiler.compile(project, context, info)
|
||||
results.add(thisResult)
|
||||
if (! thisResult.success && failedResult == null) {
|
||||
failedResult = thisResult
|
||||
}
|
||||
}
|
||||
return if (failedResult != null) failedResult!!
|
||||
else results[0]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -214,8 +200,13 @@ abstract class JvmCompilerPlugin @Inject constructor(
|
|||
fun taskJavadoc(project: Project): TaskResult {
|
||||
val docGenerator = ActorUtils.selectAffinityActor(project, context, context.pluginInfo.docContributors)
|
||||
if (docGenerator != null) {
|
||||
return docGenerator.generateDoc(project, context, createCompilerActionInfo(project, context,
|
||||
isTest = false))
|
||||
val compilers = ActorUtils.selectAffinityActors(project, context, context.pluginInfo.compilerContributors)
|
||||
var result: TaskResult? = null
|
||||
compilers.forEach { compiler ->
|
||||
result = docGenerator.generateDoc(project, context, createCompilerActionInfo(project, context,
|
||||
isTest = false, sourceSuffixes = compiler.sourceSuffixes))
|
||||
}
|
||||
return result!!
|
||||
} else {
|
||||
warn("Couldn't find any doc contributor for project ${project.name}")
|
||||
return TaskResult()
|
||||
|
@ -234,8 +225,8 @@ abstract class JvmCompilerPlugin @Inject constructor(
|
|||
* 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.
|
||||
*/
|
||||
protected fun createCompilerActionInfo(project: Project, context: KobaltContext, isTest: Boolean):
|
||||
CompilerActionInfo {
|
||||
protected fun createCompilerActionInfo(project: Project, context: KobaltContext, isTest: Boolean,
|
||||
sourceSuffixes: List<String>): CompilerActionInfo {
|
||||
copyResources(project, JvmCompilerPlugin.SOURCE_SET_MAIN)
|
||||
|
||||
val fullClasspath = if (isTest) dependencyManager.testDependencies(project, context)
|
||||
|
@ -271,14 +262,23 @@ abstract class JvmCompilerPlugin @Inject constructor(
|
|||
File(project.directory, it.path).exists()
|
||||
}
|
||||
|
||||
// Now that we have the final list of source dirs, find source files in them
|
||||
val sourceFiles = files.findRecursively(projectDirectory, sourceDirectories,
|
||||
{ it.endsWith(project.sourceSuffix) })
|
||||
{ file -> sourceSuffixes.any { file.endsWith(it) }})
|
||||
.map { File(projectDirectory, it).path }
|
||||
|
||||
// Special treatment if we are compiling Kotlin files and the project also has a java source
|
||||
// directory. In this case, also pass that java source directory to the Kotlin compiler as is
|
||||
// so that it can parse its symbols
|
||||
val extraSourceFiles = arrayListOf<String>()
|
||||
if (sourceSuffixes.any { it.contains("kt")}) {
|
||||
project.sourceDirectories.forEach {
|
||||
if (it.contains("java")) extraSourceFiles.add(it)
|
||||
}
|
||||
}
|
||||
|
||||
// Finally, alter the info with the compiler interceptors before returning it
|
||||
val initialActionInfo = CompilerActionInfo(projectDirectory.path, classpath, sourceFiles, buildDirectory,
|
||||
emptyList())
|
||||
val initialActionInfo = CompilerActionInfo(projectDirectory.path, classpath, sourceFiles + extraSourceFiles,
|
||||
buildDirectory, emptyList())
|
||||
val result = context.pluginInfo.compilerInterceptors.fold(initialActionInfo, { ai, interceptor ->
|
||||
interceptor.intercept(project, context, ai)
|
||||
})
|
||||
|
|
|
@ -11,9 +11,6 @@ import java.util.*
|
|||
* Data that is useful for projects to have but should not be specified in the DSL.
|
||||
*/
|
||||
interface IProjectInfo {
|
||||
/** Used to determine the last directory segment of the flavored sources, e.g. src/main/JAVA */
|
||||
val sourceDirectory : String
|
||||
|
||||
val defaultSourceDirectories: HashSet<String>
|
||||
val defaultTestDirectories: HashSet<String>
|
||||
|
||||
|
|
|
@ -37,7 +37,7 @@ class JavaPlugin @Inject constructor(
|
|||
|
||||
// IDocContributor
|
||||
override fun affinity(project: Project, context: KobaltContext) =
|
||||
if (project.sourceSuffix == ".java") 1 else 0
|
||||
if (project.sourceDirectories.any { it.contains("java") }) 1 else 0
|
||||
|
||||
override fun generateDoc(project: Project, context: KobaltContext, info: CompilerActionInfo) : TaskResult {
|
||||
val result =
|
||||
|
@ -52,12 +52,15 @@ class JavaPlugin @Inject constructor(
|
|||
|
||||
override fun doTaskCompileTest(project: Project): TaskResult {
|
||||
copyResources(project, JvmCompilerPlugin.SOURCE_SET_TEST)
|
||||
val compilerActionInfo = createCompilerActionInfo(project, context, isTest = true)
|
||||
val compilerActionInfo = createCompilerActionInfo(project, context, isTest = true,
|
||||
sourceSuffixes = sourceSuffixes)
|
||||
val result = javaCompiler.compile(project, context, compilerActionInfo)
|
||||
return result
|
||||
}
|
||||
|
||||
// ICompilerContributor
|
||||
override val sourceSuffixes = listOf("java")
|
||||
|
||||
override fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo) : TaskResult {
|
||||
val result =
|
||||
if (info.sourceFiles.size > 0) {
|
||||
|
|
|
@ -10,7 +10,6 @@ import com.google.inject.Singleton
|
|||
|
||||
@Singleton
|
||||
class JavaProjectInfo : BaseProjectInfo() {
|
||||
override val sourceDirectory = "java"
|
||||
override val defaultSourceDirectories = hashSetOf("src/main/java", "src/main/resources", "src/main/res")
|
||||
override val defaultTestDirectories = hashSetOf("src/test/java", "src/test/resources", "src/test/res")
|
||||
|
||||
|
|
|
@ -76,8 +76,9 @@ class KotlinPlugin @Inject constructor(
|
|||
copyResources(project, JvmCompilerPlugin.SOURCE_SET_TEST)
|
||||
val projectDir = File(project.directory)
|
||||
|
||||
|
||||
val sourceFiles = files.findRecursively(projectDir, project.sourceDirectoriesTest.map { File(it) })
|
||||
{ it: String -> it.endsWith(project.sourceSuffix) }
|
||||
{ file: String -> sourceSuffixes.any { file.endsWith(it) } }
|
||||
.map { File(projectDir, it).absolutePath }
|
||||
|
||||
val result =
|
||||
|
@ -124,8 +125,10 @@ class KotlinPlugin @Inject constructor(
|
|||
|
||||
// ICompilerContributor
|
||||
|
||||
override val sourceSuffixes = listOf("kt")
|
||||
|
||||
override fun affinity(project: Project, context: KobaltContext) =
|
||||
if (project.sourceSuffix == ".kt") 1 else 0
|
||||
if (project.sourceDirectories.any { it.contains("kotlin") }) 2 else 0
|
||||
|
||||
override fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo) : TaskResult {
|
||||
val result =
|
||||
|
|
|
@ -10,7 +10,6 @@ import com.google.inject.Singleton
|
|||
|
||||
@Singleton
|
||||
class KotlinProjectInfo : BaseProjectInfo() {
|
||||
override val sourceDirectory = "kotlin"
|
||||
override val defaultSourceDirectories = hashSetOf("src/main/kotlin", "src/main/resources", "src/main/res")
|
||||
override val defaultTestDirectories = hashSetOf("src/test/kotlin", "src/test/resources", "src/test/res")
|
||||
|
||||
|
|
|
@ -276,7 +276,7 @@ class PackageConfig(val project: Project) : AttributeHolder {
|
|||
jar {
|
||||
name = "${project.name}-${project.version}-sources.jar"
|
||||
project.sourceDirectories.forEach {
|
||||
include(from(it), to(""), glob("**${project.sourceSuffix}"))
|
||||
include(from(it), to(""), glob("src/**"))
|
||||
}
|
||||
}
|
||||
jar {
|
||||
|
|
|
@ -1 +1 @@
|
|||
kobalt.version=0.407
|
||||
kobalt.version=0.408
|
||||
|
|
27
src/test/resources/projects/javaFirst/kobalt/src/Build.kt
Normal file
27
src/test/resources/projects/javaFirst/kobalt/src/Build.kt
Normal file
|
@ -0,0 +1,27 @@
|
|||
import com.beust.kobalt.*
|
||||
import com.beust.kobalt.plugin.packaging.assemble
|
||||
import com.beust.kobalt.plugin.kotlin.*
|
||||
|
||||
val repos = repos()
|
||||
|
||||
|
||||
val p = kotlinProject {
|
||||
|
||||
name = "mixed"
|
||||
group = "com.example"
|
||||
artifactId = name
|
||||
version = "0.1"
|
||||
|
||||
sourceDirectories {
|
||||
path("src/main/java", "src/main/kotlin")
|
||||
}
|
||||
|
||||
assemble {
|
||||
jar {
|
||||
fatJar = true
|
||||
manifest {
|
||||
attributes("Main-Class", "example.KotlinMainKt")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1 @@
|
|||
kobalt.version=0.408
|
2
src/test/resources/projects/javaFirst/kobaltw
Executable file
2
src/test/resources/projects/javaFirst/kobaltw
Executable file
|
@ -0,0 +1,2 @@
|
|||
#!/usr/bin/env bash
|
||||
java -jar $(dirname $0)/kobalt/wrapper/kobalt-wrapper.jar $*
|
|
@ -0,0 +1,8 @@
|
|||
package example;
|
||||
|
||||
public class JavaClass {
|
||||
public void run() {
|
||||
System.out.println("JavaClass run()");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
package example
|
||||
|
||||
fun main(argv: Array<String>) {
|
||||
println("KotlinMain calling JavaClass().run()")
|
||||
JavaClass().run()
|
||||
}
|
27
src/test/resources/projects/kotlinFirst/kobalt/src/Build.kt
Normal file
27
src/test/resources/projects/kotlinFirst/kobalt/src/Build.kt
Normal file
|
@ -0,0 +1,27 @@
|
|||
import com.beust.kobalt.*
|
||||
import com.beust.kobalt.plugin.packaging.assemble
|
||||
import com.beust.kobalt.plugin.kotlin.*
|
||||
|
||||
val repos = repos()
|
||||
|
||||
|
||||
val p = kotlinProject {
|
||||
|
||||
name = "mixed"
|
||||
group = "com.example"
|
||||
artifactId = name
|
||||
version = "0.1"
|
||||
|
||||
sourceDirectories {
|
||||
path("src/main/java", "src/main/kotlin")
|
||||
}
|
||||
|
||||
assemble {
|
||||
jar {
|
||||
fatJar = true
|
||||
manifest {
|
||||
attributes("Main-Class", "example.KotlinMainKt")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Binary file not shown.
|
@ -0,0 +1 @@
|
|||
kobalt.version=0.408
|
2
src/test/resources/projects/kotlinFirst/kobaltw
Executable file
2
src/test/resources/projects/kotlinFirst/kobaltw
Executable file
|
@ -0,0 +1,2 @@
|
|||
#!/usr/bin/env bash
|
||||
java -jar $(dirname $0)/kobalt/wrapper/kobalt-wrapper.jar $*
|
|
@ -0,0 +1,8 @@
|
|||
package example;
|
||||
|
||||
public class JavaMain {
|
||||
public static void main(String[] argv) {
|
||||
System.out.println("JavaMain calling into Kotlin");
|
||||
new KotlinClass().run();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package example
|
||||
|
||||
import kotlin.io.*
|
||||
|
||||
class KotlinClass {
|
||||
fun run() {
|
||||
println("KotlinClass run()")
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue