diff --git a/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KonanPlugin.kt b/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KonanPlugin.kt index 50a0211ab40..03741d58b85 100644 --- a/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KonanPlugin.kt +++ b/tools/kotlin-native-gradle-plugin/src/main/kotlin/org/jetbrains/kotlin/gradle/plugin/KonanPlugin.kt @@ -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) diff --git a/tools/kotlin-native-gradle-plugin/src/test/groovy/org/jetbrains/kotlin/gradle/plugin/test/KonanProject.groovy b/tools/kotlin-native-gradle-plugin/src/test/groovy/org/jetbrains/kotlin/gradle/plugin/test/KonanProject.groovy index 8eac3dd1eb1..5d655a9ea1a 100644 --- a/tools/kotlin-native-gradle-plugin/src/test/groovy/org/jetbrains/kotlin/gradle/plugin/test/KonanProject.groovy +++ b/tools/kotlin-native-gradle-plugin/src/test/groovy/org/jetbrains/kotlin/gradle/plugin/test/KonanProject.groovy @@ -18,6 +18,7 @@ class KonanProject { File buildFile File propertiesFile + File settingsFile Set 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 } diff --git a/tools/kotlin-native-gradle-plugin/src/test/groovy/org/jetbrains/kotlin/gradle/plugin/test/RegressionSpecification.groovy b/tools/kotlin-native-gradle-plugin/src/test/groovy/org/jetbrains/kotlin/gradle/plugin/test/RegressionSpecification.groovy new file mode 100644 index 00000000000..d08c49bd25b --- /dev/null +++ b/tools/kotlin-native-gradle-plugin/src/test/groovy/org/jetbrains/kotlin/gradle/plugin/test/RegressionSpecification.groovy @@ -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 { + void apply(Project project) { + project.configurations.maybeCreate("libs") + } + } + """.stripIndent()) + } + + def result = project.createRunner().withArguments('tasks').build() + then: + result.task(':tasks').outcome == TaskOutcome.SUCCESS + } + +}