From e2b8d78cac9e0cfbe81ce6fa34f19dc0070ba567 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Mon, 15 Feb 2016 10:12:09 -0800 Subject: [PATCH] Fix resources for flavors. Fix #129. --- .../main/kotlin/com/beust/kobalt/Variant.kt | 28 ++++++++-------- .../kobalt/internal/JvmCompilerPlugin.kt | 2 +- .../kotlin/com/beust/kobalt/misc/JarUtils.kt | 4 +-- .../kotlin/com/beust/kobalt/misc/KFiles.kt | 32 +++++++++---------- .../beust/kobalt/plugin/java/JavaPlugin.kt | 2 +- .../kobalt/plugin/kotlin/KotlinPlugin.kt | 2 +- .../kobalt/plugin/packaging/JarGenerator.kt | 4 +-- 7 files changed, 37 insertions(+), 37 deletions(-) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Variant.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Variant.kt index 2a81837b..5bb96999 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Variant.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Variant.kt @@ -41,10 +41,11 @@ class Variant(val initialProductFlavor: ProductFlavorConfig? = null, context.pluginInfo.compilerContributors) compilerContributors.forEach { it.compilersFor(project, context).forEach { compiler -> - val sourceSuffixes = compilerContributors.flatMap { compiler.sourceSuffixes } - result.addAll(sourceSuffixes.flatMap { - sourceDirectories(project, it) - }) + val sourceSuffixes = compiler.sourceSuffixes + val suffixes = sourceSuffixes.flatMap { thisSuffix -> + sourceDirectories(project, thisSuffix) + } + result.addAll(suffixes) } } @@ -54,13 +55,13 @@ class Variant(val initialProductFlavor: ProductFlavorConfig? = null, /** * Might be used by plug-ins. */ - fun resDirectories(project: Project) : List = sourceDirectories(project, "res") + fun resourceDirectories(project: Project) = sourceDirectories(project, "resources") /** - * suffix is either "java" (to find source files) or "res" (to find resources) + * suffix is either "java" (to find source files) or "resources" (to find resources) */ - fun sourceDirectories(project: Project, suffix: String) : List { - val result = arrayListOf() + private fun sourceDirectories(project: Project, suffix: String) : Set { + val result = hashSetOf() val sourceDirectories = project.sourceDirectories.map { File(it) } if (isDefault) { result.addAll(sourceDirectories) @@ -85,7 +86,7 @@ class Variant(val initialProductFlavor: ProductFlavorConfig? = null, // Now that all the variant source directories have been added, add the project's default ones result.addAll(sourceDirectories) - return result + return result.filter { it.exists() }.toHashSet() } // Generated directory, if applicable @@ -93,7 +94,7 @@ class Variant(val initialProductFlavor: ProductFlavorConfig? = null, result.add(it) } - return result + return result.filter { File(project.directory, it.path).exists() }.toHashSet() } fun archiveName(project: Project, archiveName: String?, suffix: String) : String { @@ -102,9 +103,10 @@ class Variant(val initialProductFlavor: ProductFlavorConfig? = null, archiveName ?: project.name + "-" + project.version + suffix } else { val base = if (archiveName != null) archiveName.substring(0, archiveName.length - suffix.length) - else project.name + "-" + project.version - val result: String = - base + "-${productFlavor.name}" + "-${buildType.name}" + else project.name + "-" + project.version + val flavor = if (productFlavor.name.isEmpty()) "" else "-" + productFlavor.name + val type = if (buildType.name.isEmpty()) "" else "-" + buildType.name + val result: String = base + flavor + type + suffix result } 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 432820d8..4fc0b184 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,7 @@ open class JvmCompilerPlugin @Inject constructor( } else { compilerContributors.forEach { contributor -> contributor.compilersFor(project, context).forEach { compiler -> - val sourceFiles = KFiles.findSourceFiles(project, + val sourceFiles = KFiles.findSourceFiles(project.directory, context.sourceDirectories(project).map { it.path }, compiler.sourceSuffixes) if (sourceFiles.size > 0) { // TODO: createCompilerActionInfo recalculates the source files, only compute them diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/JarUtils.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/JarUtils.kt index 3c9dd6b4..e70d01a9 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/JarUtils.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/JarUtils.kt @@ -21,7 +21,7 @@ public class JarUtils { } } - public fun addFiles(directory: String, files: List, target: ZipOutputStream, + fun addFiles(directory: String, files: List, target: ZipOutputStream, expandJarFiles: Boolean, onError: (Exception) -> Unit = DEFAULT_HANDLER) { files.forEach { @@ -32,7 +32,7 @@ public class JarUtils { private val DEFAULT_JAR_EXCLUDES = Glob("META-INF/*.SF", "META-INF/*.DSA", "META-INF/*.RSA") - public fun addSingleFile(directory: String, file: IncludedFile, outputStream: ZipOutputStream, + fun addSingleFile(directory: String, file: IncludedFile, outputStream: ZipOutputStream, expandJarFiles: Boolean, onError: (Exception) -> Unit = DEFAULT_HANDLER) { val allFiles = file.allFromFiles(directory) allFiles.forEach { relSource -> 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 3e18598a..18b19122 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 @@ -288,26 +288,24 @@ class KFiles { /** * TODO: cache these per project so we don't do it more than once. */ - fun findSourceFiles(project: Project, sourceDirectories: Collection, + fun findSourceFiles(projectDirectory: String, 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) - } + sourceDirectories.forEach { source -> + val sourceDir = File(KFiles.joinDir(projectDirectory, 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") - } + } + false + }) + } else { + log(2, "Skipping nonexistent directory $sourceDir") } } return result 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 8a294453..29c305df 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/java/JavaPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/java/JavaPlugin.kt @@ -27,7 +27,7 @@ class JavaPlugin @Inject constructor(val javaCompiler: JavaCompiler) // IBuildConfigContributor private fun hasSourceFiles(project: Project) - = KFiles.findSourceFiles(project, project.sourceDirectories, listOf("java")).size > 0 + = KFiles.findSourceFiles(project.directory, project.sourceDirectories, listOf("java")).size > 0 override fun affinity(project: Project) = if (hasSourceFiles(project)) 1 else 0 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 9a7c0b2d..55263b6f 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/kotlin/KotlinPlugin.kt @@ -32,7 +32,7 @@ class KotlinPlugin @Inject constructor(val executors: KobaltExecutors) // IBuildConfigContributor private fun hasSourceFiles(project: Project) - = KFiles.findSourceFiles(project, project.sourceDirectories, listOf("kt")).size > 0 + = KFiles.findSourceFiles(project.directory, project.sourceDirectories, listOf("kt")).size > 0 override fun affinity(project: Project) = if (hasSourceFiles(project)) 1 else 0 diff --git a/src/main/kotlin/com/beust/kobalt/plugin/packaging/JarGenerator.kt b/src/main/kotlin/com/beust/kobalt/plugin/packaging/JarGenerator.kt index 51d44271..e00287b4 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/packaging/JarGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/packaging/JarGenerator.kt @@ -41,8 +41,8 @@ class JarGenerator @Inject constructor(val dependencyManager: DependencyManager) result.add(IncludedFile(From(prefixPath.toString() + "/"), To(""), fileSpecs)) // Resources, if applicable - project.sourceDirectories.filter { it.contains("resources") }.forEach { - result.add(IncludedFile(From(it), To(""), listOf(IFileSpec.GlobSpec("**")))) + context.variant.resourceDirectories(project).forEach { + result.add(IncludedFile(From(it.path), To(""), listOf(IFileSpec.GlobSpec("**")))) } } else { //