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

No more classes extend JvmCompilerPlugin.

This commit is contained in:
Cedric Beust 2016-02-04 21:44:51 +04:00
parent 02995ce6cb
commit 638d16588e
13 changed files with 148 additions and 143 deletions

View file

@ -24,6 +24,32 @@ open class Project(
@Directive open var packageName: String? = group,
val projectInfo: IProjectInfo) : IBuildConfig {
class ProjectExtra(project: Project) {
val suffixesFound = hashSetOf<String>()
init {
Kobalt.context?.let {
project.sourceDirectories.forEach { source ->
val sourceDir = File(KFiles.joinDir(project.directory, source))
KFiles.findRecursively(sourceDir, { file ->
val ind = file.lastIndexOf(".")
if (ind >= 0) {
suffixesFound.add(file.substring(ind + 1))
}
false
})
}
println("Suffixes: " + suffixesFound)
}
}
}
/**
* Initialized as soon as all the projects are parsed. This field caches a bunch of things we don't
* want to recalculate all the time, such as the list of suffixes found in this project.
*/
lateinit var projectExtra : ProjectExtra
val testConfigs = arrayListOf(TestConfig(this))
override var buildConfig : BuildConfig? = null //BuildConfig()
@ -38,24 +64,40 @@ open class Project(
return name.hashCode()
}
companion object {
val DEFAULT_SOURCE_DIRECTORIES = hashSetOf("src/main/java", "src/main/kotlin", "src/main/resources")
val DEFAULT_SOURCE_DIRECTORIES_TEST = hashSetOf("src/test/java", "src/test/kotlin")
}
//
// Directories
//
@Directive
fun sourceDirectories(init: Sources.() -> Unit) = Sources(this, sourceDirectories).apply { init() }
fun sourceDirectories(init: Sources.() -> Unit) : Sources {
return Sources(this, sourceDirectories).apply { init() }
}
var sourceDirectories : HashSet<String> = hashSetOf()
get() = if (field.isEmpty()) projectInfo.defaultSourceDirectories else field
private fun existing(dirs: Set<String>) = dirs.filter { File(directory, it).exists() }.toHashSet()
var sourceDirectories = hashSetOf<String>()
get() = existing(if (field.isEmpty()) DEFAULT_SOURCE_DIRECTORIES else field)
set(value) { field = value }
@Directive
fun sourceDirectoriesTest(init: Sources.() -> Unit) = Sources(this, sourceDirectoriesTest).apply { init() }
fun sourceDirectoriesTest(init: Sources.() -> Unit) : Sources {
return Sources(this, sourceDirectoriesTest).apply { init() }
}
var sourceDirectoriesTest : HashSet<String> = hashSetOf()
get() = if (field.isEmpty()) projectInfo.defaultTestDirectories else field
var sourceDirectoriesTest = hashSetOf<String>()
get() = existing(if (field.isEmpty()) DEFAULT_SOURCE_DIRECTORIES_TEST else field)
set(value) { field = value }
init {
sourceDirectories = hashSetOf()
sourceDirectoriesTest = hashSetOf()
}
//
// Dependencies
//

View file

@ -0,0 +1,26 @@
package com.beust.kobalt.internal
import com.beust.kobalt.api.*
/**
* Base class for JVM language plug-ins.
*/
abstract class BaseJvmPlugin<T>: ConfigPlugin<T>(), IProjectContributor, ICompilerFlagContributor {
override fun apply(project: Project, context: KobaltContext) {
super.apply(project, context)
project.projectProperties.put(JvmCompilerPlugin.DEPENDENT_PROJECTS, projects())
}
private val allProjects = arrayListOf<ProjectDescription>()
fun addDependentProjects(project: Project, dependents: List<Project>) {
project.projectInfo.dependsOn.addAll(dependents)
with(ProjectDescription(project, dependents)) {
allProjects.add(this)
}
}
// IProjectContributor
override fun projects() = allProjects
}

View file

@ -21,12 +21,11 @@ import javax.inject.Inject
import javax.inject.Singleton
/**
* Base classes for plug-ins that compile files on the JVM. This base class requires the bare minimum
* contributors (source files, projects and tasks). Subclasses can add more as they see fit (e.g. test
* source directory, etc...).
* This plug-in takes care of compilation: it declares a bunch of tasks ("compile", "compileTest") and
* and picks up all the compiler contributors in order to run them whenever a compilation is requested.
*/
@Singleton
abstract class JvmCompilerPlugin @Inject constructor(
open class JvmCompilerPlugin @Inject constructor(
open val localRepo: LocalRepo,
open val files: KFiles,
open val depFactory: DepFactory,
@ -53,6 +52,10 @@ abstract class JvmCompilerPlugin @Inject constructor(
const val DOCS_DIRECTORY = "docs/javadoc"
}
override val name: String = "JvmCompiler"
override fun accept(project: Project) = true
/**
* Log with a project.
*/
@ -132,12 +135,6 @@ abstract class JvmCompilerPlugin @Inject constructor(
}
}
open fun toClassFile(sourceFile: String) = sourceFile + ".class"
fun addCompilerArgs(project: Project, vararg args: String) {
project.projectProperties.put(COMPILER_ARGS, arrayListOf(*args))
}
@IncrementalTask(name = JvmCompilerPlugin.TASK_COMPILE, description = "Compile the project")
fun taskCompile(project: Project): IncrementalTaskInfo {
val inputChecksum = Md5.toMd5Directories(project.sourceDirectories.map {
@ -152,7 +149,11 @@ abstract class JvmCompilerPlugin @Inject constructor(
)
}
private fun doTaskCompile(project: Project): TaskResult {
private fun doTaskCompile(project: Project) = doTaskCompile(project, isTest = false)
private fun doTaskCompileTest(project: Project) = doTaskCompile(project, isTest = true)
private fun doTaskCompile(project: Project, isTest: Boolean): TaskResult {
// Set up the source files now that we have the variant
sourceDirectories.addAll(context.variant.sourceDirectories(project, context))
@ -160,9 +161,8 @@ abstract class JvmCompilerPlugin @Inject constructor(
if (sourceDirectory != null) {
sourceDirectories.add(sourceDirectory)
}
// val info = createCompilerActionInfo(project, context, isTest = false)
// val compiler = ActorUtils.selectAffinityActor(project, context, context.pluginInfo.compilerContributors)
val results = arrayListOf<TaskResult>()
val compilers = ActorUtils.selectAffinityActors(project, context, context.pluginInfo.compilerContributors)
var failedResult: TaskResult? = null
@ -170,8 +170,7 @@ abstract class JvmCompilerPlugin @Inject constructor(
throw KobaltException("Couldn't find any compiler for project ${project.name}")
} else {
compilers.forEach { compiler ->
val info = createCompilerActionInfo(project, context, isTest = false,
sourceSuffixes = compiler.sourceSuffixes)
val info = createCompilerActionInfo(project, context, isTest, sourceSuffixes = compiler.sourceSuffixes)
val thisResult = compiler.compile(project, context, info)
results.add(thisResult)
if (! thisResult.success && failedResult == null) {
@ -179,19 +178,12 @@ abstract class JvmCompilerPlugin @Inject constructor(
}
}
return if (failedResult != null) failedResult!!
else results[0]
else results[0]
}
}
val allProjects = arrayListOf<ProjectDescription>()
fun addDependentProjects(project: Project, dependents: List<Project>) {
project.projectInfo.dependsOn.addAll(dependents)
with(ProjectDescription(project, dependents)) {
allProjects.add(this)
}
}
override fun projects() : List<ProjectDescription> {
return allProjects
}
@ -204,7 +196,7 @@ abstract class JvmCompilerPlugin @Inject constructor(
var result: TaskResult? = null
compilers.forEach { compiler ->
result = docGenerator.generateDoc(project, context, createCompilerActionInfo(project, context,
isTest = false, sourceSuffixes = compiler.sourceSuffixes))
isTest = false, sourceSuffixes = compiler.sourceSuffixes))
}
return result!!
} else {
@ -278,7 +270,7 @@ abstract class JvmCompilerPlugin @Inject constructor(
// Finally, alter the info with the compiler interceptors before returning it
val initialActionInfo = CompilerActionInfo(projectDirectory.path, classpath, sourceFiles + extraSourceFiles,
buildDirectory, emptyList())
buildDirectory, emptyList() /* the flags will be provided by flag contributors */)
val result = context.pluginInfo.compilerInterceptors.fold(initialActionInfo, { ai, interceptor ->
interceptor.intercept(project, context, ai)
})
@ -306,6 +298,6 @@ abstract class JvmCompilerPlugin @Inject constructor(
)
}
abstract protected fun doTaskCompileTest(project: Project): TaskResult
open val compiler: ICompilerContributor? = null
}

View file

@ -11,9 +11,6 @@ import java.util.*
* Data that is useful for projects to have but should not be specified in the DSL.
*/
interface IProjectInfo {
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.