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

Fix resources for flavors.

Fix #129.
This commit is contained in:
Cedric Beust 2016-02-15 10:12:09 -08:00
parent 981979d9ed
commit e2b8d78cac
7 changed files with 37 additions and 37 deletions

View file

@ -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<File> = 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<File> {
val result = arrayListOf<File>()
private fun sourceDirectories(project: Project, suffix: String) : Set<File> {
val result = hashSetOf<File>()
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
}

View file

@ -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

View file

@ -21,7 +21,7 @@ public class JarUtils {
}
}
public fun addFiles(directory: String, files: List<IncludedFile>, target: ZipOutputStream,
fun addFiles(directory: String, files: List<IncludedFile>, 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 ->

View file

@ -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<String>,
fun findSourceFiles(projectDirectory: String, sourceDirectories: Collection<String>,
suffixes: List<String>) : Set<String> {
val result = hashSetOf<String>()
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

View file

@ -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

View file

@ -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

View file

@ -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 {
//