diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/ISourceDirectoryContributor.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/ISourceDirectoryContributor.kt index 29394d40..91ea96a0 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/ISourceDirectoryContributor.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/ISourceDirectoryContributor.kt @@ -8,3 +8,10 @@ import java.io.File interface ISourceDirectoryContributor { fun sourceDirectoriesFor(project: Project, context: KobaltContext): List } + +fun KobaltContext.sourceDirectories(project: Project) : List { + val result = pluginInfo.sourceDirContributors.flatMap { + it.sourceDirectoriesFor(project, this) + } + return result +} diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Project.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Project.kt index d2349bd7..98f0f67b 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Project.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/Project.kt @@ -5,7 +5,6 @@ import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.internal.JvmCompilerPlugin import com.beust.kobalt.maven.dependency.MavenDependency import com.beust.kobalt.misc.KFiles -import com.beust.kobalt.misc.log import java.io.File import java.util.* @@ -25,27 +24,6 @@ open class Project( @Directive open var packageName: String? = group) : IBuildConfig { class ProjectExtra(project: Project) { - val suffixesFound : Set by lazy { - val sf = hashSetOf() - Kobalt.context?.let { - project.sourceDirectories.forEach { source -> - val sourceDir = File(KFiles.joinDir(project.directory, source)) - if (sourceDir.exists()) { - KFiles.findRecursively(sourceDir, { file -> - val ind = file.lastIndexOf(".") - if (ind >= 0) { - sf.add(file.substring(ind + 1)) - } - false - }) - } else { - log(2, "Skipping nonexistent directory $sourceDir") - } - } - } - sf - } - val dependsOn = arrayListOf() var isDirty = false @@ -64,7 +42,8 @@ open class Project( val testConfigs = arrayListOf(TestConfig(this)) - override var buildConfig : BuildConfig? = BuildConfig() + // If one is specified by default, we only generate a BuildConfig, find a way to fix that + override var buildConfig : BuildConfig? = null // BuildConfig() val projectProperties = ProjectProperties() 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 e85d003c..8c7da2c7 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 @@ -174,7 +174,11 @@ open class JvmCompilerPlugin @Inject constructor( } else { compilerContributors.forEach { contributor -> contributor.compilersFor(project, context).forEach { compiler -> - if (containsSourceFiles(project, compiler)) { + val sourceFiles = KFiles.findSourceFiles(project, + context.sourceDirectories(project).map { it.path }, compiler.sourceSuffixes) + if (sourceFiles.size > 0) { + // TODO: createCompilerActionInfo recalculates the source files, only compute them + // once and pass them val info = createCompilerActionInfo(project, context, isTest, sourceDirectories, sourceSuffixes = compiler.sourceSuffixes) val thisResult = compiler.compile(project, context, info) @@ -192,13 +196,6 @@ open class JvmCompilerPlugin @Inject constructor( } } - private fun containsSourceFiles(project: Project, compiler: ICompiler): Boolean { - project.projectExtra.suffixesFound.forEach { - if (compiler.sourceSuffixes.contains(it)) return true - } - return false - } - val allProjects = arrayListOf() override fun projects() = allProjects @@ -270,7 +267,7 @@ open class JvmCompilerPlugin @Inject constructor( }) // Transform them with the interceptors, if any - val sourceDirectories = if (isTest) { + val allSourceDirectories = if (isTest) { initialSourceDirectories } else { context.pluginInfo.sourceDirectoriesInterceptors.fold(initialSourceDirectories.toList(), @@ -280,7 +277,7 @@ open class JvmCompilerPlugin @Inject constructor( } // Now that we have all the source directories, find all the source files in them - val sourceFiles = files.findRecursively(projectDirectory, sourceDirectories, + val sourceFiles = files.findRecursively(projectDirectory, allSourceDirectories, { file -> sourceSuffixes.any { file.endsWith(it) }}) .map { File(projectDirectory, it).path } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt index 3d9028fb..3e18598a 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/KFiles.kt @@ -284,6 +284,34 @@ class KFiles { fun isExcluded(file: File, excludes: List) = isExcluded(file.path, excludes) fun isExcluded(file: String, excludes: List): Boolean = excludes.any { it.matches(file) } + + /** + * TODO: cache these per project so we don't do it more than once. + */ + fun findSourceFiles(project: Project, sourceDirectories: Collection, + suffixes: List) : Set { + val result = hashSetOf() + Kobalt.context?.let { + sourceDirectories.forEach { source -> + val sourceDir = File(KFiles.joinDir(project.directory, source)) + if (sourceDir.exists()) { + KFiles.findRecursively(sourceDir, { file -> + val ind = file.lastIndexOf(".") + if (ind >= 0) { + val suffix = file.substring(ind + 1) + if (suffixes.contains(suffix)) { + result.add(file) + } + } + false + }) + } else { + log(2, "Skipping nonexistent directory $sourceDir") + } + } + } + return result + } } fun findRecursively(directory: File, function: Function1): List { diff --git a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaPlugin.kt index c413b385..8a294453 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaPlugin.kt @@ -6,6 +6,7 @@ import com.beust.kobalt.api.* import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.internal.BaseJvmPlugin import com.beust.kobalt.internal.JvmCompilerPlugin +import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.warn import java.io.File import javax.inject.Inject @@ -21,7 +22,14 @@ class JavaPlugin @Inject constructor(val javaCompiler: JavaCompiler) override val name = PLUGIN_NAME - override fun accept(project: Project) = project.projectExtra.suffixesFound.contains("java") + override fun accept(project: Project) = hasSourceFiles(project) + + // IBuildConfigContributor + + private fun hasSourceFiles(project: Project) + = KFiles.findSourceFiles(project, project.sourceDirectories, listOf("java")).size > 0 + + override fun affinity(project: Project) = if (hasSourceFiles(project)) 1 else 0 // IDocContributor override fun affinity(project: Project, context: KobaltContext) = @@ -66,9 +74,6 @@ class JavaPlugin @Inject constructor(val javaCompiler: JavaCompiler) override fun testSourceDirectoriesFor(project: Project, context: KobaltContext) = project.sourceDirectoriesTest.map { File(it) }.toList() - // IBuildConfigContributor - override fun affinity(project: Project) = if (project.projectExtra.suffixesFound.contains("java")) 1 else 0 - override val buildConfigSuffix = compiler.sourceSuffixes[0] override fun generateBuildConfig(project: Project, context: KobaltContext, packageName: String, 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 6a1288b4..21c65d98 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt @@ -8,6 +8,7 @@ import com.beust.kobalt.internal.BaseJvmPlugin import com.beust.kobalt.internal.JvmCompilerPlugin 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 @@ -26,7 +27,14 @@ class KotlinPlugin @Inject constructor(val executors: KobaltExecutors) override val name = PLUGIN_NAME - override fun accept(project: Project) = project.projectExtra.suffixesFound.contains("kt") + override fun accept(project: Project) = hasSourceFiles(project) + + // IBuildConfigContributor + + private fun hasSourceFiles(project: Project) + = KFiles.findSourceFiles(project, project.sourceDirectories, listOf("kt")).size > 0 + + override fun affinity(project: Project) = if (hasSourceFiles(project)) 1 else 0 // IDocContributor override fun affinity(project: Project, context: KobaltContext) = @@ -134,9 +142,6 @@ class KotlinPlugin @Inject constructor(val executors: KobaltExecutors) log(2, "${project.name}: $s") } - // IBuildConfigContributor - override fun affinity(project: Project) = if (project.projectExtra.suffixesFound.contains("kotlin")) 2 else 0 - override val buildConfigSuffix = "kt" override fun generateBuildConfig(project: Project, context: KobaltContext, packageName: String,