mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-29 17:38:12 -07:00
Break Kobalt in two projects.
This commit is contained in:
parent
733c4b52b5
commit
3914e92f6d
114 changed files with 114 additions and 98 deletions
|
@ -0,0 +1,201 @@
|
|||
package com.beust.kobalt
|
||||
|
||||
import com.beust.kobalt.api.*
|
||||
import com.beust.kobalt.misc.KFiles
|
||||
import com.beust.kobalt.misc.log
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
* Capture the product flavor and the build type of a build.
|
||||
*/
|
||||
class Variant(val initialProductFlavor: ProductFlavorConfig? = null,
|
||||
val initialBuildType: BuildTypeConfig? = null) {
|
||||
|
||||
val productFlavor: ProductFlavorConfig by lazy {
|
||||
initialProductFlavor ?: Variant.DEFAULT_PRODUCT_FLAVOR
|
||||
}
|
||||
val buildType: BuildTypeConfig by lazy {
|
||||
initialBuildType ?: Variant.DEFAULT_BUILD_TYPE
|
||||
}
|
||||
|
||||
val isDefault : Boolean
|
||||
get() = productFlavor == DEFAULT_PRODUCT_FLAVOR && buildType == DEFAULT_BUILD_TYPE
|
||||
|
||||
fun toTask(taskName: String) = taskName + productFlavor.name.capitalize() + buildType.name.capitalize()
|
||||
|
||||
/**
|
||||
* for {internal, release}, return [internalRelease, internal, release]
|
||||
*/
|
||||
fun allDirectories(project: Project): List<String> {
|
||||
val result = arrayListOf<String>()
|
||||
result.add(toCamelcaseDir())
|
||||
if (productFlavor != null) result.add(productFlavor.name)
|
||||
if (buildType != null) result.add(buildType.name)
|
||||
return result
|
||||
}
|
||||
|
||||
fun resDirectories(project: Project) : List<File> = sourceDirectories(project, "res")
|
||||
|
||||
fun sourceDirectories(project: Project) : List<File> =
|
||||
sourceDirectories(project, project.projectInfo.sourceDirectory)
|
||||
|
||||
/**
|
||||
* suffix is either "java" (to find source files) or "res" (to find resources)
|
||||
*/
|
||||
private fun sourceDirectories(project: Project, suffix: String) : List<File> {
|
||||
val result = arrayListOf<File>()
|
||||
val sourceDirectories = project.sourceDirectories.map { File(it) }
|
||||
if (isDefault) {
|
||||
result.addAll(sourceDirectories)
|
||||
} else {
|
||||
result.addAll(allDirectories(project).map {
|
||||
File(KFiles.joinDir("src", it, suffix))
|
||||
}.filter {
|
||||
it.exists()
|
||||
})
|
||||
|
||||
// // 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))
|
||||
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))
|
||||
log(3, "Adding source for product flavor ${it.name}: ${dir.path}")
|
||||
result.add(dir)
|
||||
}
|
||||
|
||||
// Now that all the variant source directories have been added, add the project's default ones
|
||||
result.addAll(sourceDirectories)
|
||||
return result
|
||||
}
|
||||
|
||||
// Generated directory, if applicable
|
||||
generatedSourceDirectory?.let {
|
||||
result.add(it)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
fun archiveName(project: Project, archiveName: String?, suffix: String) : String {
|
||||
val result =
|
||||
if (isDefault) {
|
||||
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}"
|
||||
|
||||
result
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
val shortArchiveName = if (isDefault) "" else "-" + productFlavor.name + "-" + buildType.name
|
||||
|
||||
var generatedSourceDirectory: File? = null
|
||||
|
||||
private fun findBuildTypeBuildConfig(project: Project, variant: Variant?) : BuildConfig? {
|
||||
val buildTypeName = variant?.buildType?.name
|
||||
return project.buildTypes.getRaw(buildTypeName)?.buildConfig ?: null
|
||||
}
|
||||
|
||||
private fun findProductFlavorBuildConfig(project: Project, variant: Variant?) : BuildConfig? {
|
||||
val buildTypeName = variant?.productFlavor?.name
|
||||
return project.productFlavors.getRaw(buildTypeName)?.buildConfig ?: null
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of the BuildConfigs found on the productFlavor{}, buildType{} and project{} (in that order).
|
||||
*/
|
||||
private fun findBuildConfigs(project: Project, variant: Variant?) : List<BuildConfig> {
|
||||
val result = listOf(
|
||||
findBuildTypeBuildConfig(project, variant),
|
||||
findProductFlavorBuildConfig(project, variant),
|
||||
project.buildConfig)
|
||||
.filterNotNull()
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate BuildConfig.java if requested. Also look up if any BuildConfig is defined on the current build type,
|
||||
* product flavor or main project, and use them to generate any additional field (in that order to
|
||||
* respect the priorities). Return the generated file if it was generated, null otherwise.
|
||||
*/
|
||||
fun maybeGenerateBuildConfig(project: Project, context: KobaltContext) : File? {
|
||||
val buildConfigs = findBuildConfigs(project, this)
|
||||
|
||||
if (buildConfigs.size > 0) {
|
||||
val pkg = project.packageName ?: project.group
|
||||
?: throw KobaltException(
|
||||
"packageName needs to be defined on the project in order to generate BuildConfig")
|
||||
|
||||
val code = project.projectInfo.generateBuildConfig(project, context, pkg, this, buildConfigs)
|
||||
val result = KFiles.makeDir(KFiles.generatedSourceDir(project, this, "buildConfig"))
|
||||
// Make sure the generatedSourceDirectory doesn't contain the project.directory since
|
||||
// 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)
|
||||
KFiles.saveFile(outputDir, code)
|
||||
log(2, "Generated ${outputDir.path}")
|
||||
return result
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = toTask("")
|
||||
|
||||
companion object {
|
||||
val DEFAULT_PRODUCT_FLAVOR = ProductFlavorConfig("")
|
||||
val DEFAULT_BUILD_TYPE = BuildTypeConfig(null, "")
|
||||
|
||||
fun allVariants(project: Project): List<Variant> {
|
||||
val result = arrayListOf<Variant>()
|
||||
|
||||
if (project.buildTypes.size > 0) {
|
||||
project.buildTypes.keys.forEach {
|
||||
val bt = project.buildTypes[it]
|
||||
if (project.productFlavors.size > 0) {
|
||||
project.productFlavors.keys.forEach {
|
||||
result.add(Variant(project.productFlavors[it], bt))
|
||||
}
|
||||
} else {
|
||||
result.add(Variant(null, bt))
|
||||
}
|
||||
}
|
||||
} else if (project.productFlavors.size > 0) {
|
||||
project.productFlavors.keys.forEach {
|
||||
val pf = project.productFlavors[it]
|
||||
if (project.buildTypes.size > 0) {
|
||||
project.buildTypes.keys.forEach {
|
||||
result.add(Variant(pf, project.buildTypes[it]))
|
||||
}
|
||||
} else {
|
||||
result.add(Variant(pf, null))
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
fun toCamelcaseDir() : String {
|
||||
val pfName = productFlavor.name
|
||||
val btName = buildType.name
|
||||
return pfName[0].toLowerCase() + pfName.substring(1) + btName[0].toUpperCase() + btName.substring(1)
|
||||
}
|
||||
|
||||
fun toIntermediateDir() : String {
|
||||
if (isDefault) {
|
||||
return ""
|
||||
} else {
|
||||
return KFiles.joinDir(productFlavor.name, buildType.name)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue