gradle-plugin: Don't use Project.getTasksByName method

Call of Project.getTasksByName in KonanPluing.apply causes subproject
evaluation. So in such a build script the plugin 'foo' will not be
applied to the subprojects:

subprojects {
    apply 'konan'
    apply 'foo'
}

This patch uses methods available via Project.tasks property to
work with tasks without causing subproject evaluation.

Related issue: KT-19916
This commit is contained in:
Ilya Matveev
2017-08-29 13:50:58 +07:00
committed by ilmat192
parent 7aa6977ea6
commit 98fe7995bb
3 changed files with 52 additions and 12 deletions
@@ -215,23 +215,17 @@ class KonanPlugin @Inject constructor(private val registry: ToolingModelBuilderR
}.getProperty("default-konan-version") ?: throw RuntimeException("Cannot read the default compiler version")
}
/** Looks for task with given name in the given project. Throws [UnknownTaskException] if there's not such task. */
private fun Project.getTask(name: String): Task = tasks.getByPath(name)
/**
* Looks for task with given name in the given project.
* If such task isn't found, will create it. Returns created/found task.
*/
private fun Project.getTask(name: String): Task = getTasksByName(name, false).single()
private fun Project.getOrCreateTask(name: String): Task {
val tasks = getTasksByName(name, false)
assert(tasks.size <= 1)
return if (tasks.isEmpty()) {
this.tasks.create(name, DefaultTask::class.java)
} else {
tasks.single()
}
private fun Project.getOrCreateTask(name: String): Task = with(tasks) {
findByPath(name) ?: create(name, DefaultTask::class.java)
}
// TODO: Create default config? what about test sources?
override fun apply(project: Project?) {
if (project == null) { return }
registry.register(KonanToolingModelBuilder)
@@ -18,6 +18,7 @@ class KonanProject {
File buildFile
File propertiesFile
File settingsFile
Set<File> srcFiles = []
@@ -77,12 +78,18 @@ class KonanProject {
createSubDir("src", "main", "kotlin")
}
/** Generates a build.gradle file in root project directory with the given content. */
/** Generates a build.gradle file in the root project directory with the given content. */
File generateBuildFile(String content) {
buildFile = createFile(projectPath, "build.gradle", content)
return buildFile
}
/** Generates a settings.gradle file in the root project directory with the given content. */
File generateSettingsFile(String content) {
settingsFile = createFile(projectPath, "settings.gradle", content)
return settingsFile
}
/**
* Generates a build.gradle file in root project directory with the default content (see below)
* and fills the compilationTasks array.
@@ -197,6 +204,7 @@ class KonanProject {
generateFolders()
generateBuildFile()
generatePropertiesFile(konanHome)
generateSettingsFile("")
}
return result
}
@@ -0,0 +1,38 @@
package org.jetbrains.kotlin.gradle.plugin.test
import org.gradle.testkit.runner.TaskOutcome
class RegressionSpecification extends BaseKonanSpecification {
def 'KT-19916'() {
when:
def project = KonanProject.createEmpty(getProjectDirectory()) { KonanProject prj ->
prj.generateSettingsFile("include ':subproject'")
def subprojectDir = prj.projectPath.resolve("subproject").toFile()
subprojectDir.mkdirs()
subprojectDir.toPath().resolve("build.gradle").write("""
dependencies {
libs gradleApi()
}
""".stripIndent())
prj.buildFile.append("""
subprojects {
apply plugin: 'konan'
apply plugin: Foo
}
class Foo implements Plugin<Project> {
void apply(Project project) {
project.configurations.maybeCreate("libs")
}
}
""".stripIndent())
}
def result = project.createRunner().withArguments('tasks').build()
then:
result.task(':tasks').outcome == TaskOutcome.SUCCESS
}
}