From 3bec28e8e075227d79269c9046fe6e4e8ffc680c Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Mon, 11 Apr 2016 09:06:07 -0700 Subject: [PATCH] Fix bug mixing R.java on Kotlin sources. Fix https://github.com/cbeust/kobalt-android/issues/6 --- .../beust/kobalt/api/ICompilerContributor.kt | 5 +++ .../kobalt/internal/JvmCompilerPlugin.kt | 32 ++++++++++++------- .../beust/kobalt/plugin/java/JavaCompiler.kt | 6 +++- .../kobalt/plugin/kotlin/KotlinPlugin.kt | 3 ++ 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/ICompilerContributor.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/ICompilerContributor.kt index 2a7d7b29..be488994 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/ICompilerContributor.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/ICompilerContributor.kt @@ -28,6 +28,11 @@ interface ICompiler : Comparable { val priority: Int get() = DEFAULT_PRIORITY override fun compareTo(other: ICompiler) = priority.compareTo(other.priority) + + /** + * Can this compiler be passed directories or does it need individual source files? + */ + val canCompileDirectories: Boolean get() = false } interface ICompilerContributor : IProjectAffinity { diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt index 623cdf3f..44feb5ea 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt @@ -207,7 +207,7 @@ open class JvmCompilerPlugin @Inject constructor( if (sourceFiles.size > 0) { // TODO: createCompilerActionInfo recalculates the source files, only compute them // once and pass them - val info = createCompilerActionInfo(project, context, isTest, sourceDirectories, + val info = createCompilerActionInfo(project, context, compiler, isTest, sourceDirectories, sourceSuffixes = compiler.sourceSuffixes) val thisResult = compiler.compile(project, context, info) results.add(thisResult) @@ -239,11 +239,13 @@ open class JvmCompilerPlugin @Inject constructor( fun taskJavadoc(project: Project): TaskResult { val docGenerator = ActorUtils.selectAffinityActor(project, context, context.pluginInfo.docContributors) if (docGenerator != null) { - val contributors = ActorUtils.selectAffinityActors(project, context, context.pluginInfo.compilerContributors) + val contributors = + ActorUtils.selectAffinityActors(project, context, context.pluginInfo.compilerContributors) var result: TaskResult? = null contributors.forEach { it.compilersFor(project, context).forEach { compiler -> result = docGenerator.generateDoc(project, context, createCompilerActionInfo(project, context, + compiler, isTest = false, sourceDirectories = sourceDirectories, sourceSuffixes = compiler.sourceSuffixes)) } @@ -267,8 +269,8 @@ open 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, - sourceDirectories: List, sourceSuffixes: List): CompilerActionInfo { + protected fun createCompilerActionInfo(project: Project, context: KobaltContext, compiler: ICompiler, + isTest: Boolean, sourceDirectories: List, sourceSuffixes: List): CompilerActionInfo { copyResources(project, SourceSet.of(isTest)) val fullClasspath = if (isTest) dependencyManager.testDependencies(project, context) @@ -286,14 +288,16 @@ open class JvmCompilerPlugin @Inject constructor( val initialSourceDirectories = ArrayList(sourceDirectories) // Source directories from the contributors - initialSourceDirectories.addAll( + val contributedSourceDirs = if (isTest) { context.pluginInfo.testSourceDirContributors.flatMap { it.testSourceDirectoriesFor(project, context) } } else { context.pluginInfo.sourceDirContributors.flatMap { it.sourceDirectoriesFor(project, context) } - }) + } - // Transform them with the interceptors, if any + initialSourceDirectories.addAll(contributedSourceDirs) + + // Transform them with the interceptors, if any val allSourceDirectories = if (isTest) { initialSourceDirectories @@ -308,9 +312,13 @@ open class JvmCompilerPlugin @Inject constructor( // Now that we have all the source directories, find all the source files in them val projectDirectory = File(project.directory) - val sourceFiles = files.findRecursively(projectDirectory, allSourceDirectories, - { file -> sourceSuffixes.any { file.endsWith(it) }}) - .map { File(projectDirectory, it).path } + val sourceFiles = if (compiler.canCompileDirectories) { + allSourceDirectories.map { it.path } + } else { + files.findRecursively(projectDirectory, allSourceDirectories, + { 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 @@ -333,8 +341,10 @@ open class JvmCompilerPlugin @Inject constructor( } } + extraSourceFiles.addAll(contributedSourceDirs.filter { it.exists() }.map { it.path }) + val allSources= (sourceFiles + extraSourceFiles).distinct() // 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, allSources, sourceSuffixes, buildDirectory, emptyList() /* the flags will be provided by flag contributors */) val result = context.pluginInfo.compilerInterceptors.fold(initialActionInfo, { ai, interceptor -> interceptor.intercept(project, context, ai) diff --git a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt index d176c314..688651ab 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaCompiler.kt @@ -44,7 +44,11 @@ class JavaCompiler @Inject constructor(val jvmCompiler: JvmCompiler) { allArgs.addAll(info.compilerArgs) val fileManager = compiler.getStandardFileManager(null, null, null) - val fileObjects = fileManager.getJavaFileObjectsFromFiles(info.sourceFiles.map { File(it) }) + val fileObjects = fileManager.getJavaFileObjectsFromFiles(info.sourceFiles.map { + File(it) + }.filter { + it.isFile + }) val dc = DiagnosticCollector() val classes = arrayListOf() val writer = PrintWriter(System.out) diff --git a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt index 4a8fa76f..2d44c181 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt @@ -121,6 +121,9 @@ class KotlinPlugin @Inject constructor(val executors: KobaltExecutors, val depen override val sourceDirectory = "kotlin" + /** kotlinc can be passed directories */ + override val canCompileDirectories = true + override fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo): TaskResult { val result = if (info.sourceFiles.size > 0) {