[gradle-plugin] Create a separate run task for each executable

This commit is contained in:
Ilya Matveev
2018-05-17 19:13:17 +07:00
committed by ilmat192
parent 5b087ae04c
commit d2670b4921
7 changed files with 97 additions and 22 deletions
+1 -1
View File
@@ -155,7 +155,7 @@ def tasksOf(Closure<Task> filter) {
.matching { filter(it) && it.enabled && (prefix != null ? it.name.startsWith(prefix) : true) }
}
run {
task run {
def tasks = tasksOf { it instanceof KonanTest && it.inDevelopersRun }
dependsOn(tasks)
+1 -1
View File
@@ -25,7 +25,7 @@ konanArtifacts {
}
}
run.dependsOn 'warning'
runTensorflow.dependsOn 'warning'
task warning {
doLast {
+1 -1
View File
@@ -25,7 +25,7 @@ konanArtifacts {
}
}
run.dependsOn 'warning'
runTorch.dependsOn 'warning'
task warning {
doLast {
@@ -39,7 +39,7 @@ import org.jetbrains.kotlin.konan.KonanVersion
import org.jetbrains.kotlin.konan.target.HostManager
import org.jetbrains.kotlin.konan.target.KonanTarget
import org.jetbrains.kotlin.konan.target.customerDistribution
import org.jetbrains.kotlin.konan.util.*
import org.jetbrains.kotlin.konan.util.DependencyProcessor
import java.io.File
import javax.inject.Inject
@@ -341,24 +341,6 @@ class KonanPlugin @Inject constructor(private val registry: ToolingModelBuilderR
doLast { project.cleanKonan() }
}
// Create task to run supported executables.
project.getOrCreateTask("run").apply {
dependsOn(project.getTask("build"))
doLast {
for (task in project.tasks
.withType(KonanCompileProgramTask::class.java)
.matching { !it.isCrossCompile }) {
project.exec {
with(it) {
commandLine(task.artifact.canonicalPath)
if (project.extensions.extraProperties.has("runArgs")) {
args(project.extensions.extraProperties.get("runArgs").toString().split(' '))
}
}
}
}
}
}
//project.gradle.experimentalFeatures.enable()
// Enable multiplatform support
@@ -16,14 +16,18 @@
package org.jetbrains.kotlin.gradle.plugin.tasks
import groovy.lang.Closure
import org.codehaus.groovy.runtime.GStringImpl
import org.gradle.api.file.ConfigurableFileTree
import org.gradle.api.file.FileCollection
import org.gradle.api.tasks.*
import org.gradle.process.CommandLineArgumentProvider
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.model.KonanModelArtifact
import org.jetbrains.kotlin.gradle.plugin.model.KonanModelArtifactImpl
import org.jetbrains.kotlin.konan.library.defaultResolver
import org.jetbrains.kotlin.konan.target.CompilerOutputKind
import org.jetbrains.kotlin.konan.target.KonanTarget
import org.jetbrains.kotlin.konan.target.Distribution
import java.io.File
@@ -241,6 +245,34 @@ abstract class KonanCompileTask: KonanBuildingTask(), KonanCompileSpec {
open class KonanCompileProgramTask: KonanCompileTask() {
override val produce: CompilerOutputKind get() = CompilerOutputKind.PROGRAM
var runTask: Exec? = null
inner class RunArgumentProvider(): CommandLineArgumentProvider {
override fun asArguments() = project.findProperty("runArgs")?.let {
it.toString().split(' ')
} ?: emptyList()
}
// Create tasks to run supported executables.
override fun init(config: KonanBuildingConfig<*>, destinationDir: File, artifactName: String, target: KonanTarget) {
super.init(config, destinationDir, artifactName, target)
if (!isCrossCompile) {
runTask = project.tasks.create("run${artifactName.capitalize()}", Exec::class.java).apply {
group = "run"
dependsOn(this@KonanCompileProgramTask)
val artifactPathClosure = object : Closure<String>(this) {
override fun call() = artifactPath
}
// Use GString to evaluate a path to the artifact lazily thus allow changing it at configuration phase.
val lazyArtifactPath = GStringImpl(arrayOf(artifactPathClosure), arrayOf(""))
executable(lazyArtifactPath)
// Add values passed in the runArgs project property as arguments.
argumentProviders.add(RunArgumentProvider())
}
}
}
}
open class KonanCompileDynamicTask: KonanCompileTask() {
@@ -167,6 +167,7 @@ open class PropertiesAsEnvVariablesTest {
assertFileExists(destination2, artifactFileName("main", ArtifactType.LIBRARY))
}
@Test
fun `Up-to-date checks should work with different directories for different targets`() {
val project = KonanProject.createEmpty(projectDirectory)
val fooDir = project.createSubDir("foo")
@@ -0,0 +1,60 @@
/*
* Copyright 2010-2018 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.gradle.plugin.test
import org.junit.Rule
import org.junit.rules.TemporaryFolder
import java.io.File
import kotlin.test.*
class TaskTests {
val tmpFolder = TemporaryFolder()
@Rule get
val projectDirectory: File
get() = tmpFolder.root
@Test
fun `Plugin should support separate run tasks for different binaries`() {
val project = KonanProject.createEmpty(projectDirectory).apply {
buildFile.appendText("""
konanArtifacts {
program('foo') {
srcDir 'src/foo/kotlin'
}
program('bar') {
srcDir 'src/bar/kotlin'
}
}
""".trimIndent())
}
project.generateSrcFile(
listOf("src", "foo", "kotlin"),
"main.kt",
"fun main(args: Array<String>) = println(\"Run Foo: \${args[0]}, \${args[1]}\")")
project.generateSrcFile(
listOf("src", "bar", "kotlin"),
"main.kt",
"fun main(args: Array<String>) = println(\"Run Bar: \${args[0]}, \${args[1]}\")")
val result = project.createRunner()
.withArguments("runFoo", "-PrunArgs=arg1 arg2")
.build()
assertTrue(result.output.contains("Run Foo: arg1, arg2"), "No Foo output!")
assertTrue(!result.output.contains("Run Bar: "), "There is Bar output!")
}
}