From 569eaf9fa85d4615a76889548d178befe94f8d03 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Sun, 29 Nov 2015 09:31:41 -0800 Subject: [PATCH] Extract affinity selection. --- .../kotlin/com/beust/kobalt/internal/ActorUtils.kt | 13 +++++++++++++ .../com/beust/kobalt/internal/JvmCompilerPlugin.kt | 8 +++----- .../kobalt/plugin/application/ApplicationPlugin.kt | 4 +++- 3 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 src/main/kotlin/com/beust/kobalt/internal/ActorUtils.kt diff --git a/src/main/kotlin/com/beust/kobalt/internal/ActorUtils.kt b/src/main/kotlin/com/beust/kobalt/internal/ActorUtils.kt new file mode 100644 index 00000000..5772a428 --- /dev/null +++ b/src/main/kotlin/com/beust/kobalt/internal/ActorUtils.kt @@ -0,0 +1,13 @@ +package com.beust.kobalt.internal + +import com.beust.kobalt.api.IAffinity +import com.beust.kobalt.api.KobaltContext +import com.beust.kobalt.api.Project + +class ActorUtils { + companion object { + fun selectAffinityActor(project: Project, context: KobaltContext, + actors: List) : T? + = actors.maxBy { it.affinity(project, context) } + } +} diff --git a/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt b/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt index e98cc4c6..75ceb525 100644 --- a/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/internal/JvmCompilerPlugin.kt @@ -59,7 +59,8 @@ abstract class JvmCompilerPlugin @Inject constructor( fun taskTest(project: Project) : TaskResult { lp(project, "Running tests") - val runContributor = context.pluginInfo.testRunnerContributors.maxBy { it.affinity(project, context)} + val runContributor = ActorUtils.selectAffinityActor(project, context, + context.pluginInfo.testRunnerContributors) if (runContributor != null && runContributor.affinity(project, context) > 0) { return runContributor.run(project, context, dependencyManager.testDependencies(project, context, projects())) @@ -137,7 +138,7 @@ abstract class JvmCompilerPlugin @Inject constructor( fun taskCompile(project: Project) : TaskResult { context.variant.maybeGenerateBuildConfig(project, context) val info = createCompilerActionInfo(project, context) - val compiler = selectAffinityActor(project, context.pluginInfo.compilerContributors) + val compiler = ActorUtils.selectAffinityActor(project, context, context.pluginInfo.compilerContributors) if (compiler != null) { return compiler.compile(project, context, info) } else { @@ -145,9 +146,6 @@ abstract class JvmCompilerPlugin @Inject constructor( } } - private fun selectAffinityActor(project: Project, actors: List) : T? = - actors.maxBy { it.affinity(project, context) } - @Task(name = JavaPlugin.TASK_JAVADOC, description = "Run Javadoc") fun taskJavadoc(project: Project) = doJavadoc(project, createCompilerActionInfo(project, context)) diff --git a/src/main/kotlin/com/beust/kobalt/plugin/application/ApplicationPlugin.kt b/src/main/kotlin/com/beust/kobalt/plugin/application/ApplicationPlugin.kt index 6ea31b35..b337ff0a 100644 --- a/src/main/kotlin/com/beust/kobalt/plugin/application/ApplicationPlugin.kt +++ b/src/main/kotlin/com/beust/kobalt/plugin/application/ApplicationPlugin.kt @@ -4,6 +4,7 @@ import com.beust.kobalt.* import com.beust.kobalt.api.* import com.beust.kobalt.api.annotation.Directive import com.beust.kobalt.api.annotation.Task +import com.beust.kobalt.internal.ActorUtils import com.beust.kobalt.internal.JvmCompilerPlugin import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.maven.dependency.IClasspathDependency @@ -49,7 +50,8 @@ class ApplicationPlugin @Inject constructor(val executors: KobaltExecutors, @Task(name = "run", description = "Run the main class", runAfter = arrayOf("install")) fun taskRun(project: Project): TaskResult { - val runContributor = context.pluginInfo.runnerContributors.maxBy { it.affinity(project, context)} + val runContributor = ActorUtils.selectAffinityActor(project, context, + context.pluginInfo.runnerContributors) if (runContributor != null && runContributor.affinity(project, context) > 0) { return runContributor.run(project, context, dependencyManager.dependencies(project, context, projects())) } else {