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

Fold IInitContributor into IAffinity.

This commit is contained in:
Cedric Beust 2015-11-30 17:21:58 -08:00
parent fc7df24e1a
commit fcfa0653a2
15 changed files with 51 additions and 50 deletions

View file

@ -1,12 +1,11 @@
package com.beust.kobalt
import com.beust.kobalt.api.IInitContributor
import com.beust.kobalt.internal.ActorUtils
import com.beust.kobalt.internal.PluginInfo
import com.beust.kobalt.misc.log
import com.google.inject.Inject
import java.io.File
import java.io.FileOutputStream
import java.util.*
/**
* Invoked with --init. Generate a new project.
@ -25,7 +24,7 @@ public class ProjectGenerator @Inject constructor(val pluginInfo: PluginInfo){
}
fun run(args: Args) {
val contributor = findBestInitContributor(File("."))
val contributor = ActorUtils.selectAffinityActor(pluginInfo.initContributors, File("."))
if (contributor != null) {
contributor.generateBuildFile(FileOutputStream(File(args.buildFile)))
log(1, "Created ${args.buildFile}")
@ -33,25 +32,5 @@ public class ProjectGenerator @Inject constructor(val pluginInfo: PluginInfo){
log(1, "Couldn't identify project, not generating any build file")
}
}
/**
* Run through all the IInitContributors and return the best one.
*/
private fun findBestInitContributor(dir: File) : IInitContributor? {
val result = arrayListOf<Pair<IInitContributor, Int>>()
pluginInfo.initContributors.forEach {
it.filesManaged(dir).let { count ->
if (count > 0) {
result.add(Pair(it, count))
}
}
}
if (result.size > 0) {
Collections.sort(result, { p1, p2 -> p2.second.compareTo(p1.second) })
return result[0].first
} else {
return null
}
}
}

View file

@ -1,5 +1,8 @@
package com.beust.kobalt.api
/**
* Base interface for affinity interfaces.
*/
interface IAffinity {
companion object {
/**
@ -8,10 +11,5 @@ interface IAffinity {
*/
const val DEFAULT_POSITIVE_AFFINITY = 100
}
/**
* @return an integer indicating the affinity of your actor for the given project. The actor that returns
* the highest affinity gets selected.
*/
fun affinity(project: Project, context: KobaltContext) : Int
}

View file

@ -4,6 +4,6 @@ import com.beust.kobalt.TaskResult
import com.beust.kobalt.api.IClasspathDependency
import java.io.File
interface ICompilerContributor : IAffinity {
interface ICompilerContributor : IProjectAffinity {
fun compile(project: Project, context: KobaltContext, info: CompilerActionInfo) : TaskResult
}

View file

@ -2,7 +2,7 @@ package com.beust.kobalt.api
import com.beust.kobalt.TaskResult
interface IDocContributor : IAffinity {
interface IDocContributor : IProjectAffinity {
fun generateDoc(project: Project, context: KobaltContext, info: CompilerActionInfo) : TaskResult
}

View file

@ -1,19 +1,12 @@
package com.beust.kobalt.api
import java.io.File
import java.io.OutputStream
/**
* Plugins that want to participate in the --init process (they can generate files to initialize
* a new project).
*/
interface IInitContributor : IContributor {
/**
* How many files your plug-in understands in the given directory. The contributor with the
* highest number will be asked to generate the build file.
*/
fun filesManaged(dir: File): Int
interface IInitContributor<T> : ISimpleAffinity<T> {
/**
* Generate the Build.kt file into the given OutputStream.
*/

View file

@ -0,0 +1,12 @@
package com.beust.kobalt.api
/**
* An affinity interface that uses a project and a context to calculate its affinity.
*/
interface IProjectAffinity : IAffinity {
/**
* @return an integer indicating the affinity of your actor for the given project. The actor that returns
* the highest affinity gets selected.
*/
fun affinity(project: Project, context: KobaltContext) : Int
}

View file

@ -6,7 +6,7 @@ import com.beust.kobalt.api.IClasspathDependency
/**
* Plugins that can run a project (task "run" or "test") should implement this interface.
*/
interface IRunnerContributor : IContributor, IAffinity {
interface IRunnerContributor : IContributor, IProjectAffinity {
/**
* Run the project.
*/

View file

@ -0,0 +1,12 @@
package com.beust.kobalt.api
/**
* An affinity interface that gets run without a project nor a context.
*/
interface ISimpleAffinity<T> : IAffinity {
/**
* @return an integer indicating the affinity of your actor. The actor that returns
* the highest affinity gets selected.
*/
fun affinity(arg: T) : Int
}

View file

@ -5,7 +5,7 @@ import com.beust.kobalt.TaskResult
/**
* Plugins that can run a project (task "run" or "test") should implement this interface.
*/
interface ITestRunnerContributor : IContributor, IAffinity {
interface ITestRunnerContributor : IContributor, IProjectAffinity {
/**
* Run the project.
*/

View file

@ -1,6 +1,7 @@
package com.beust.kobalt.internal
import com.beust.kobalt.api.IAffinity
import com.beust.kobalt.api.IProjectAffinity
import com.beust.kobalt.api.ISimpleAffinity
import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project
@ -9,7 +10,13 @@ class ActorUtils {
/**
* Return the plug-in actor with the highest affinity.
*/
fun <T : IAffinity> selectAffinityActor(project: Project, context: KobaltContext, actors: List<T>) : T?
fun <T : IProjectAffinity> selectAffinityActor(project: Project, context: KobaltContext, actors: List<T>)
= actors.maxBy { it.affinity(project, context) }
/**
* Return the plug-in actor with the highest affinity.
*/
fun <T : ISimpleAffinity<A>, A> selectAffinityActor(actors: List<T>, arg: A) = actors.maxBy { it.affinity(arg) }
}
}

View file

@ -11,7 +11,7 @@ import java.util.*
/**
* Abstract base class for the build generators that use build-template.mustache.
*/
abstract class BuildGenerator : IInitContributor {
abstract class BuildGenerator : IInitContributor<File> {
abstract val defaultSourceDirectories : HashSet<String>
abstract val defaultTestDirectories : HashSet<String>
abstract val directive : String
@ -24,7 +24,7 @@ abstract class BuildGenerator : IInitContributor {
}
}
override fun filesManaged(dir: File) = KFiles.findRecursively(dir, fileMatch).size
override fun affinity(dir: File) = KFiles.findRecursively(dir, fileMatch).size
private fun importPom(pomFile: File, mainDeps: ArrayList<Pom.Dependency>, testDeps: ArrayList<Pom.Dependency>,
map: HashMap<String, Any?>) {

View file

@ -1,9 +1,9 @@
package com.beust.kobalt.internal
import com.beust.kobalt.api.IAffinity
import com.beust.kobalt.api.IClasspathDependency
import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project
import com.beust.kobalt.api.IClasspathDependency
public class JUnitRunner() : GenericTestRunner() {

View file

@ -3,6 +3,7 @@ package com.beust.kobalt.internal
import com.beust.kobalt.api.*
import com.beust.kobalt.misc.log
import java.io.ByteArrayInputStream
import java.io.File
import java.io.InputStream
import javax.xml.bind.JAXBContext
import javax.xml.bind.annotation.XmlElement
@ -58,7 +59,7 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
val plugins = arrayListOf<IPlugin>()
val projectContributors = arrayListOf<IProjectContributor>()
val classpathContributors = arrayListOf<IClasspathContributor>()
val initContributors = arrayListOf<IInitContributor>()
val initContributors = arrayListOf<IInitContributor<File>>()
val repoContributors = arrayListOf<IRepoContributor>()
val compilerFlagContributors = arrayListOf<ICompilerFlagContributor>()
val compilerInterceptors = arrayListOf<ICompilerInterceptor>()
@ -123,7 +124,7 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
if (this is IPlugin) plugins.add(this)
if (this is IClasspathContributor) classpathContributors.add(this)
if (this is IProjectContributor) projectContributors.add(this)
if (this is IInitContributor) initContributors.add(this)
if (this is IInitContributor<*>) initContributors.add(this as IInitContributor<File>)
if (this is IRepoContributor) repoContributors.add(this)
if (this is ICompilerContributor) compilerContributors.add(this)
if (this is ICompilerInterceptor) compilerInterceptors.add(this)

View file

@ -1,9 +1,9 @@
package com.beust.kobalt.internal
import com.beust.kobalt.api.IAffinity
import com.beust.kobalt.api.IClasspathDependency
import com.beust.kobalt.api.KobaltContext
import com.beust.kobalt.api.Project
import com.beust.kobalt.api.IClasspathDependency
import com.beust.kobalt.misc.KFiles
import java.io.File

View file

@ -6,7 +6,6 @@ import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.api.annotation.Task
import com.beust.kobalt.maven.MavenId
import com.beust.kobalt.maven.dependency.FileDependency
import com.beust.kobalt.api.IClasspathDependency
import com.beust.kobalt.maven.dependency.MavenDependency
import com.beust.kobalt.misc.*
import com.beust.kobalt.plugin.java.JavaCompiler