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:
+5
-11
@@ -215,23 +215,17 @@ class KonanPlugin @Inject constructor(private val registry: ToolingModelBuilderR
|
|||||||
}.getProperty("default-konan-version") ?: throw RuntimeException("Cannot read the default compiler version")
|
}.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.
|
* Looks for task with given name in the given project.
|
||||||
* If such task isn't found, will create it. Returns created/found task.
|
* 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 = with(tasks) {
|
||||||
|
findByPath(name) ?: create(name, DefaultTask::class.java)
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Create default config? what about test sources?
|
|
||||||
override fun apply(project: Project?) {
|
override fun apply(project: Project?) {
|
||||||
if (project == null) { return }
|
if (project == null) { return }
|
||||||
registry.register(KonanToolingModelBuilder)
|
registry.register(KonanToolingModelBuilder)
|
||||||
|
|||||||
+9
-1
@@ -18,6 +18,7 @@ class KonanProject {
|
|||||||
|
|
||||||
File buildFile
|
File buildFile
|
||||||
File propertiesFile
|
File propertiesFile
|
||||||
|
File settingsFile
|
||||||
|
|
||||||
Set<File> srcFiles = []
|
Set<File> srcFiles = []
|
||||||
|
|
||||||
@@ -77,12 +78,18 @@ class KonanProject {
|
|||||||
createSubDir("src", "main", "kotlin")
|
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) {
|
File generateBuildFile(String content) {
|
||||||
buildFile = createFile(projectPath, "build.gradle", content)
|
buildFile = createFile(projectPath, "build.gradle", content)
|
||||||
return buildFile
|
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)
|
* Generates a build.gradle file in root project directory with the default content (see below)
|
||||||
* and fills the compilationTasks array.
|
* and fills the compilationTasks array.
|
||||||
@@ -197,6 +204,7 @@ class KonanProject {
|
|||||||
generateFolders()
|
generateFolders()
|
||||||
generateBuildFile()
|
generateBuildFile()
|
||||||
generatePropertiesFile(konanHome)
|
generatePropertiesFile(konanHome)
|
||||||
|
generateSettingsFile("")
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|||||||
+38
@@ -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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user