mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
BuildConfig work.
This commit is contained in:
parent
bc04dd6055
commit
f37df1acc1
8 changed files with 166 additions and 59 deletions
59
src/main/kotlin/com/beust/kobalt/Variant.kt
Normal file
59
src/main/kotlin/com/beust/kobalt/Variant.kt
Normal file
|
@ -0,0 +1,59 @@
|
|||
package com.beust.kobalt
|
||||
|
||||
import com.beust.kobalt.api.BuildTypeConfig
|
||||
import com.beust.kobalt.api.ProductFlavorConfig
|
||||
import com.beust.kobalt.api.Project
|
||||
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 productFlavor: ProductFlavorConfig? = null, val buildType: BuildTypeConfig? = null) {
|
||||
val isDefault : Boolean
|
||||
get() = productFlavor == null && buildType == null
|
||||
|
||||
fun toTask(taskName: String) = taskName + productFlavor?.name?.capitalize() + buildType?.name?.capitalize()
|
||||
|
||||
fun sourceDirectories(project: Project) : List<File> {
|
||||
val sourceDirectories = project.sourceDirectories.map { File(it) }
|
||||
if (isDefault) return sourceDirectories
|
||||
else {
|
||||
val result = arrayListOf<File>()
|
||||
// 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(2, "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(2, "Adding source for product flavor ${it.name}: ${dir.path}")
|
||||
result.add(dir)
|
||||
}
|
||||
result.addAll(sourceDirectories)
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
fun archiveName(project: Project, archiveName: String?, suffix: String) : String {
|
||||
val result: String =
|
||||
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
|
||||
base +
|
||||
if (productFlavor == null) "" else "-${productFlavor.name}" +
|
||||
if (buildType == null) "" else "-${buildType.name}" +
|
||||
suffix
|
||||
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
val hasBuildConfig: Boolean
|
||||
get() {
|
||||
return productFlavor?.buildConfig != null || buildType?.buildConfig != null
|
||||
}
|
||||
}
|
|
@ -2,8 +2,8 @@ package com.beust.kobalt.api
|
|||
|
||||
import com.beust.kobalt.Plugins
|
||||
import com.beust.kobalt.TaskResult
|
||||
import com.beust.kobalt.Variant
|
||||
import com.beust.kobalt.internal.TaskManager
|
||||
import com.beust.kobalt.internal.Variant
|
||||
import java.util.*
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
|
@ -32,8 +32,10 @@ abstract public class BasePlugin : IPlugin {
|
|||
*/
|
||||
protected fun addVariantTasks(project: Project, taskName: String, runAfter : List<String>,
|
||||
runTask: (Project) -> TaskResult) {
|
||||
project.productFlavors.keys.forEach { pf ->
|
||||
project.buildTypes.keys.forEach { bt ->
|
||||
project.productFlavors.keys.forEach {
|
||||
val pf = project.productFlavors.get(it)
|
||||
project.buildTypes.keys.forEach { btName ->
|
||||
val bt = project.buildTypes[btName]
|
||||
val variant = Variant(pf, bt)
|
||||
val taskName = variant.toTask(taskName)
|
||||
addTask(project, taskName, taskName,
|
||||
|
|
|
@ -3,7 +3,7 @@ package com.beust.kobalt.api
|
|||
import com.beust.kobalt.Args
|
||||
import com.beust.kobalt.Plugins
|
||||
import com.beust.kobalt.internal.PluginInfo
|
||||
import com.beust.kobalt.internal.Variant
|
||||
import com.beust.kobalt.Variant
|
||||
import com.beust.kobalt.maven.DependencyManager
|
||||
import com.beust.kobalt.misc.KobaltExecutors
|
||||
|
||||
|
|
|
@ -21,7 +21,10 @@ open public class Project(
|
|||
@Directive open var scm : Scm? = null,
|
||||
@Directive open var url: String? = null,
|
||||
@Directive open var licenses: List<License> = arrayListOf<License>(),
|
||||
val projectInfo: IProjectInfo) {
|
||||
@Directive open var packageName: String? = group,
|
||||
val projectInfo: IProjectInfo) : IBuildConfig {
|
||||
|
||||
override var buildConfig: BuildConfig? = null
|
||||
|
||||
var testArgs: ArrayList<String> = arrayListOf()
|
||||
|
||||
|
@ -98,14 +101,12 @@ open public class Project(
|
|||
val productFlavors = hashMapOf<String, ProductFlavorConfig>()
|
||||
|
||||
fun addProductFlavor(name: String, pf: ProductFlavorConfig) {
|
||||
println("Adding ProductFlavor $name")
|
||||
productFlavors.put(name, pf)
|
||||
}
|
||||
|
||||
val buildTypes = hashMapOf<String, BuildTypeConfig>()
|
||||
|
||||
fun addBuildType(name: String, bt: BuildTypeConfig) {
|
||||
println("Adding BuildType $name")
|
||||
buildTypes.put(name, bt)
|
||||
}
|
||||
}
|
||||
|
@ -148,21 +149,43 @@ public class License(val name: String, val url: String) {
|
|||
|
||||
}
|
||||
|
||||
class ProductFlavorConfig {
|
||||
var description: String = ""
|
||||
class BuildConfig {
|
||||
class Field(val name: String, val type: String, val value: Any)
|
||||
|
||||
val fields = arrayListOf<Field>()
|
||||
|
||||
fun field(name: String, type: String, value: Any) {
|
||||
println("Adding field $name")
|
||||
fields.add(Field(name, type, value))
|
||||
}
|
||||
}
|
||||
|
||||
interface IBuildConfig {
|
||||
var buildConfig: BuildConfig?
|
||||
|
||||
fun buildConfig(init: BuildConfig.() -> Unit) {
|
||||
buildConfig = BuildConfig().apply {
|
||||
init()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ProductFlavorConfig(val name: String) : IBuildConfig {
|
||||
override var buildConfig: BuildConfig? = null
|
||||
}
|
||||
|
||||
@Directive
|
||||
fun Project.productFlavor(name: String, init: ProductFlavorConfig.() -> Unit) = ProductFlavorConfig().apply {
|
||||
fun Project.productFlavor(name: String, init: ProductFlavorConfig.() -> Unit) = ProductFlavorConfig(name).apply {
|
||||
init()
|
||||
addProductFlavor(name, this)
|
||||
}
|
||||
|
||||
class BuildTypeConfig {
|
||||
class BuildTypeConfig(val name: String) : IBuildConfig {
|
||||
override var buildConfig: BuildConfig? = null
|
||||
}
|
||||
|
||||
@Directive
|
||||
fun Project.buildType(name: String, init: BuildTypeConfig.() -> Unit) = BuildTypeConfig().apply {
|
||||
fun Project.buildType(name: String, init: BuildTypeConfig.() -> Unit) = BuildTypeConfig(name).apply {
|
||||
init()
|
||||
addBuildType(name, this)
|
||||
}
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
package com.beust.kobalt.internal
|
||||
|
||||
import com.beust.kobalt.KobaltException
|
||||
import com.beust.kobalt.TaskResult
|
||||
import com.beust.kobalt.api.BasePlugin
|
||||
import com.beust.kobalt.api.IProjectContributor
|
||||
import com.beust.kobalt.api.KobaltContext
|
||||
import com.beust.kobalt.api.Project
|
||||
import com.beust.kobalt.api.*
|
||||
import com.beust.kobalt.api.annotation.ExportedProjectProperty
|
||||
import com.beust.kobalt.api.annotation.Task
|
||||
import com.beust.kobalt.maven.*
|
||||
|
@ -149,7 +147,30 @@ abstract class JvmCompilerPlugin @Inject constructor(
|
|||
override fun projects() = projects
|
||||
|
||||
@Task(name = JavaPlugin.TASK_COMPILE, description = "Compile the project")
|
||||
fun taskCompile(project: Project) = doCompile(project, createCompilerActionInfo(project, context))
|
||||
fun taskCompile(project: Project) : TaskResult {
|
||||
maybeGenerateBuildConfig(project)
|
||||
return doCompile(project, createCompilerActionInfo(project, context))
|
||||
}
|
||||
|
||||
/**
|
||||
* If either the Project or the current variant has a build config defined, generate BuildConfig.java
|
||||
*/
|
||||
private fun maybeGenerateBuildConfig(project: Project) {
|
||||
println("Maybe generate build config")
|
||||
if (project.buildConfig != null || context.variant.hasBuildConfig) {
|
||||
val buildConfigs = arrayListOf<BuildConfig>()
|
||||
if (project.buildConfig != null) buildConfigs.add(project.buildConfig!!)
|
||||
with (context.variant) {
|
||||
if (buildType?.buildConfig != null) buildConfigs.add(buildType?.buildConfig!!)
|
||||
if (productFlavor?.buildConfig != null) buildConfigs.add(productFlavor?.buildConfig!!)
|
||||
}
|
||||
var 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(pkg, context.variant, buildConfigs)
|
||||
println("Generating: " + code)
|
||||
}
|
||||
}
|
||||
|
||||
@Task(name = JavaPlugin.TASK_JAVADOC, description = "Run Javadoc")
|
||||
fun taskJavadoc(project: Project) = doJavadoc(project, createCompilerActionInfo(project, context))
|
||||
|
@ -178,45 +199,3 @@ abstract class JvmCompilerPlugin @Inject constructor(
|
|||
abstract fun doJavadoc(project: Project, cai: CompilerActionInfo) : TaskResult
|
||||
}
|
||||
|
||||
class Variant(val productFlavorName: String = "", val buildTypeName: String = "") {
|
||||
val isDefault : Boolean
|
||||
get() = productFlavorName.isBlank() && buildTypeName.isBlank()
|
||||
|
||||
fun toTask(taskName: String) = taskName + productFlavorName.capitalize() + buildTypeName.capitalize()
|
||||
|
||||
fun sourceDirectories(project: Project) : List<File> {
|
||||
val sourceDirectories = project.sourceDirectories.map { File(it) }
|
||||
if (isDefault) return sourceDirectories
|
||||
else {
|
||||
val result = arrayListOf<File>()
|
||||
// The ordering of files is: 1) build type 2) product flavor 3) default
|
||||
if (! buildTypeName.isBlank()) {
|
||||
val dir = File(KFiles.joinDir("src", buildTypeName, project.projectInfo.sourceDirectory))
|
||||
log(2, "Adding source for build type $buildTypeName: ${dir.path}")
|
||||
result.add(dir)
|
||||
}
|
||||
if (! productFlavorName.isBlank()) {
|
||||
val dir = File(KFiles.joinDir("src", productFlavorName, project.projectInfo.sourceDirectory))
|
||||
log(2, "Adding source for product flavor $productFlavorName: ${dir.path}")
|
||||
result.add(dir)
|
||||
}
|
||||
result.addAll(sourceDirectories)
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
fun archiveName(project: Project, archiveName: String?, suffix: String) : String {
|
||||
val result: String =
|
||||
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
|
||||
base +
|
||||
if (productFlavorName.isEmpty()) "" else "-$productFlavorName" +
|
||||
if (buildTypeName.isEmpty()) "" else "-$buildTypeName" +
|
||||
suffix
|
||||
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package com.beust.kobalt.internal
|
||||
|
||||
import com.beust.kobalt.Variant
|
||||
import com.beust.kobalt.api.BuildConfig
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
|
@ -8,6 +10,13 @@ import java.util.*
|
|||
interface IProjectInfo {
|
||||
/** Used to determine the last directory segment of the flavored sources, e.g. src/main/JAVA */
|
||||
val sourceDirectory : String
|
||||
|
||||
val defaultSourceDirectories: HashSet<String>
|
||||
val defaultTestDirectories: HashSet<String>
|
||||
|
||||
/**
|
||||
* If at least one build config was found either on the project or the variant, this function
|
||||
* will be used to generate the BuildConfig file with the correct language.
|
||||
*/
|
||||
fun generateBuildConfig(packageName: String, variant: Variant, buildConfigs: List<BuildConfig>) : String
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package com.beust.kobalt.plugin.java
|
||||
|
||||
import com.beust.kobalt.Variant
|
||||
import com.beust.kobalt.api.BuildConfig
|
||||
import com.beust.kobalt.internal.IProjectInfo
|
||||
import com.google.inject.Singleton
|
||||
|
||||
|
@ -8,4 +10,31 @@ class JavaProjectInfo : IProjectInfo {
|
|||
override val sourceDirectory = "java"
|
||||
override val defaultSourceDirectories = hashSetOf("src/main/java", "src/main/resources")
|
||||
override val defaultTestDirectories = hashSetOf("src/test/java", "src/test/resources")
|
||||
|
||||
private fun generate(type: String, name: String, value: Any) =
|
||||
" public static $type $name = $value;"
|
||||
|
||||
override fun generateBuildConfig(packageName: String, variant: Variant, buildConfigs: List<BuildConfig>) : String {
|
||||
val lines = arrayListOf<String>()
|
||||
with(lines) {
|
||||
add("package $packageName;")
|
||||
add("")
|
||||
add("public class BuildConfig {")
|
||||
if (variant.productFlavor != null) {
|
||||
add(generate("String", "PRODUCT_FLAVOR", "\"" + variant.productFlavor.name + "\""))
|
||||
}
|
||||
if (variant.buildType != null) {
|
||||
add(generate("String", "BUILD_TYPE", "\"" + variant.buildType.name + "\""))
|
||||
}
|
||||
buildConfigs.forEach {
|
||||
it.fields.forEach { field ->
|
||||
add(generate(field.type, field.name, field.value))
|
||||
}
|
||||
}
|
||||
add("}")
|
||||
}
|
||||
|
||||
return lines.joinToString("\n")
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package com.beust.kobalt.plugin.kotlin
|
||||
|
||||
import com.beust.kobalt.Variant
|
||||
import com.beust.kobalt.api.BuildConfig
|
||||
import com.beust.kobalt.internal.IProjectInfo
|
||||
import com.google.inject.Singleton
|
||||
|
||||
|
@ -8,5 +10,9 @@ class KotlinProjectInfo : IProjectInfo {
|
|||
override val sourceDirectory = "kotlin"
|
||||
override val defaultSourceDirectories = hashSetOf("src/main/kotlin", "src/main/resources")
|
||||
override val defaultTestDirectories = hashSetOf("src/test/kotlin", "src/test/resources")
|
||||
|
||||
override fun generateBuildConfig(packageName: String, variant: Variant, buildConfigs: List<BuildConfig>) : String {
|
||||
return "package $packageName"
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue