mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Variant support for the compile target.
This commit is contained in:
parent
3b741d3a5d
commit
8728ba4637
7 changed files with 72 additions and 13 deletions
|
@ -3,6 +3,7 @@ package com.beust.kobalt.api
|
||||||
import com.beust.kobalt.Args
|
import com.beust.kobalt.Args
|
||||||
import com.beust.kobalt.Plugins
|
import com.beust.kobalt.Plugins
|
||||||
import com.beust.kobalt.internal.PluginInfo
|
import com.beust.kobalt.internal.PluginInfo
|
||||||
|
import com.beust.kobalt.internal.Variant
|
||||||
import com.beust.kobalt.maven.DependencyManager
|
import com.beust.kobalt.maven.DependencyManager
|
||||||
import com.beust.kobalt.misc.KobaltExecutors
|
import com.beust.kobalt.misc.KobaltExecutors
|
||||||
|
|
||||||
|
@ -12,5 +13,6 @@ public class KobaltContext(val args: Args) {
|
||||||
lateinit var pluginProperties: PluginProperties
|
lateinit var pluginProperties: PluginProperties
|
||||||
lateinit var dependencyManager: DependencyManager
|
lateinit var dependencyManager: DependencyManager
|
||||||
lateinit var executors: KobaltExecutors
|
lateinit var executors: KobaltExecutors
|
||||||
|
var variant: Variant = Variant()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -95,14 +95,14 @@ open public class Project(
|
||||||
@Directive
|
@Directive
|
||||||
val projectName: String get() = name
|
val projectName: String get() = name
|
||||||
|
|
||||||
private val productFlavors = hashMapOf<String, ProductFlavorConfig>()
|
val productFlavors = hashMapOf<String, ProductFlavorConfig>()
|
||||||
|
|
||||||
fun addProductFlavor(name: String, pf: ProductFlavorConfig) {
|
fun addProductFlavor(name: String, pf: ProductFlavorConfig) {
|
||||||
println("Adding ProductFlavor $name")
|
println("Adding ProductFlavor $name")
|
||||||
productFlavors.put(name, pf)
|
productFlavors.put(name, pf)
|
||||||
}
|
}
|
||||||
|
|
||||||
private val buildTypes = hashMapOf<String, BuildTypeConfig>()
|
val buildTypes = hashMapOf<String, BuildTypeConfig>()
|
||||||
|
|
||||||
fun addBuildType(name: String, bt: BuildTypeConfig) {
|
fun addBuildType(name: String, bt: BuildTypeConfig) {
|
||||||
println("Adding BuildType $name")
|
println("Adding BuildType $name")
|
||||||
|
|
|
@ -53,6 +53,20 @@ abstract class JvmCompilerPlugin @Inject constructor(
|
||||||
super.apply(project, context)
|
super.apply(project, context)
|
||||||
project.projectProperties.put(BUILD_DIR, project.buildDirectory + File.separator + "classes")
|
project.projectProperties.put(BUILD_DIR, project.buildDirectory + File.separator + "classes")
|
||||||
project.projectProperties.put(DEPENDENT_PROJECTS, projects())
|
project.projectProperties.put(DEPENDENT_PROJECTS, projects())
|
||||||
|
|
||||||
|
project.productFlavors.keys.forEach { pf ->
|
||||||
|
project.buildTypes.keys.forEach { bt ->
|
||||||
|
val taskName = Variant(pf, bt).toTask("compile")
|
||||||
|
addTask(project, taskName, "Compile $taskName",
|
||||||
|
task = { p: Project ->
|
||||||
|
context.variant = Variant(pf, bt)
|
||||||
|
taskCompile(project)
|
||||||
|
TaskResult()
|
||||||
|
})
|
||||||
|
println("New task: " + Variant(pf, bt).toTask("compile"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println("Done")
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -148,12 +162,12 @@ abstract class JvmCompilerPlugin @Inject constructor(
|
||||||
override fun projects() = projects
|
override fun projects() = projects
|
||||||
|
|
||||||
@Task(name = JavaPlugin.TASK_COMPILE, description = "Compile the project")
|
@Task(name = JavaPlugin.TASK_COMPILE, description = "Compile the project")
|
||||||
fun taskCompile(project: Project) = doCompile(project, createCompilerActionInfo(project))
|
fun taskCompile(project: Project) = doCompile(project, createCompilerActionInfo(project, context))
|
||||||
|
|
||||||
@Task(name = JavaPlugin.TASK_JAVADOC, description = "Run Javadoc")
|
@Task(name = JavaPlugin.TASK_JAVADOC, description = "Run Javadoc")
|
||||||
fun taskJavadoc(project: Project) = doJavadoc(project, createCompilerActionInfo(project))
|
fun taskJavadoc(project: Project) = doJavadoc(project, createCompilerActionInfo(project, context))
|
||||||
|
|
||||||
private fun createCompilerActionInfo(project: Project) : CompilerActionInfo {
|
private fun createCompilerActionInfo(project: Project, context: KobaltContext) : CompilerActionInfo {
|
||||||
copyResources(project, JvmCompilerPlugin.SOURCE_SET_MAIN)
|
copyResources(project, JvmCompilerPlugin.SOURCE_SET_MAIN)
|
||||||
|
|
||||||
val classpath = dependencyManager.calculateDependencies(project, context, projects,
|
val classpath = dependencyManager.calculateDependencies(project, context, projects,
|
||||||
|
@ -163,7 +177,8 @@ abstract class JvmCompilerPlugin @Inject constructor(
|
||||||
val buildDirectory = File(projectDirectory, project.buildDirectory + File.separator + "classes")
|
val buildDirectory = File(projectDirectory, project.buildDirectory + File.separator + "classes")
|
||||||
buildDirectory.mkdirs()
|
buildDirectory.mkdirs()
|
||||||
|
|
||||||
val sourceFiles = files.findRecursively(projectDirectory, project.sourceDirectories.map { File(it) },
|
val sourceDirectories = context.variant.sourceDirectories(project)
|
||||||
|
val sourceFiles = files.findRecursively(projectDirectory, sourceDirectories,
|
||||||
{ it .endsWith(project.sourceSuffix) })
|
{ it .endsWith(project.sourceSuffix) })
|
||||||
.map { File(projectDirectory, it).absolutePath }
|
.map { File(projectDirectory, it).absolutePath }
|
||||||
|
|
||||||
|
@ -175,3 +190,30 @@ abstract class JvmCompilerPlugin @Inject constructor(
|
||||||
abstract fun doCompile(project: Project, cai: CompilerActionInfo) : TaskResult
|
abstract fun doCompile(project: Project, cai: CompilerActionInfo) : TaskResult
|
||||||
abstract fun doJavadoc(project: Project, cai: CompilerActionInfo) : TaskResult
|
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>()
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
result.addAll(sourceDirectories)
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -6,6 +6,8 @@ import java.util.*
|
||||||
* Data that is useful for projects to have but should not be specified in the DSL.
|
* Data that is useful for projects to have but should not be specified in the DSL.
|
||||||
*/
|
*/
|
||||||
interface IProjectInfo {
|
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 defaultSourceDirectories: HashSet<String>
|
||||||
val defaultTestDirectories: HashSet<String>
|
val defaultTestDirectories: HashSet<String>
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import java.io.File
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
|
import java.nio.file.Paths
|
||||||
import java.nio.file.StandardCopyOption
|
import java.nio.file.StandardCopyOption
|
||||||
|
|
||||||
class KFiles {
|
class KFiles {
|
||||||
|
@ -100,23 +101,33 @@ class KFiles {
|
||||||
allDirs.addAll(directories.map { File(rootDir, it.path) })
|
allDirs.addAll(directories.map { File(rootDir, it.path) })
|
||||||
}
|
}
|
||||||
|
|
||||||
allDirs.forEach {
|
val seen = hashSetOf<Path>()
|
||||||
if (! it.exists()) {
|
allDirs.forEach { dir ->
|
||||||
log(2, "Couldn't find directory $it")
|
if (! dir.exists()) {
|
||||||
|
log(2, "Couldn't find directory $dir")
|
||||||
} else {
|
} else {
|
||||||
result.addAll(findRecursively(it, function))
|
val files = findRecursively(dir, function)
|
||||||
|
files.map { Paths.get(it) }.forEach {
|
||||||
|
val rel = Paths.get(dir.path).relativize(it)
|
||||||
|
if (! seen.contains(rel)) {
|
||||||
|
result.add(File(dir, rel.toFile().path).path)
|
||||||
|
seen.add(rel)
|
||||||
|
} else {
|
||||||
|
log(2, "Skipped file already seen in previous flavor: $rel")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Return files relative to rootDir
|
// Return files relative to rootDir
|
||||||
val r = result.map { it.substring(rootDir.absolutePath.length + 1)}
|
val r = result.map { it.substring(rootDir.path.length + 1)}
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
fun findRecursively(directory: File, function: Function1<String, Boolean>): List<String> {
|
fun findRecursively(directory: File, function: Function1<String, Boolean>): List<String> {
|
||||||
var result = arrayListOf<String>()
|
var result = arrayListOf<String>()
|
||||||
directory.listFiles().forEach {
|
directory.listFiles().forEach {
|
||||||
if (it.isFile && function(it.absolutePath)) {
|
if (it.isFile && function(it.path)) {
|
||||||
result.add(it.absolutePath)
|
result.add(it.path)
|
||||||
} else if (it.isDirectory) {
|
} else if (it.isDirectory) {
|
||||||
result.addAll(findRecursively(it, function))
|
result.addAll(findRecursively(it, function))
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import com.google.inject.Singleton
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
class JavaProjectInfo : IProjectInfo {
|
class JavaProjectInfo : IProjectInfo {
|
||||||
|
override val sourceDirectory = "java"
|
||||||
override val defaultSourceDirectories = hashSetOf("src/main/java", "src/main/resources")
|
override val defaultSourceDirectories = hashSetOf("src/main/java", "src/main/resources")
|
||||||
override val defaultTestDirectories = hashSetOf("src/test/java", "src/test/resources")
|
override val defaultTestDirectories = hashSetOf("src/test/java", "src/test/resources")
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import com.google.inject.Singleton
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
class KotlinProjectInfo : IProjectInfo {
|
class KotlinProjectInfo : IProjectInfo {
|
||||||
|
override val sourceDirectory = "kotlin"
|
||||||
override val defaultSourceDirectories = hashSetOf("src/main/kotlin", "src/main/resources")
|
override val defaultSourceDirectories = hashSetOf("src/main/kotlin", "src/main/resources")
|
||||||
override val defaultTestDirectories = hashSetOf("src/test/kotlin", "src/test/resources")
|
override val defaultTestDirectories = hashSetOf("src/test/kotlin", "src/test/resources")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue