Thread
#multiplatform
    nikola

    nikola

    1 year ago
    Hi all. I am trying to migrate from build.gradle (Groovy) to build.gradle.kts and I am stuck with translating certain part related to tasks. When running build I do not see this task executing. What am I doing wrong?
    // -----------Groovy Old
    task generateCinteropConfig(type: Exec) {
        workingDir "${projectDir}"
        commandLine 'sh', "${projectDir}/generate_cinterop_conf.sh"
    }
    
    gradle.taskGraph.beforeTask { Task task ->
        if (task.project.name.toLowerCase().contains("ios")) {
            "sh ${projectDir}/generate_cinterop_conf.sh ${projectDir}".execute().text
        }
    }
    
    // ------------Kotlin DSL New
    task<Exec>("generateCinteropConfig") {
        workingDir = File("${projectDir}")
        commandLine = listOf("sh", "${projectDir}/generate_cinterop_conf.sh")
    }
    
    tasks.register("generateCinteropConfig") {
        doFirst() {
            if (this.project.name.toLowerCase().contains("ios")) {
                Runtime.getRuntime().exec("${projectDir}/generate_cinterop_conf.sh ${projectDir}")
            }
        }
    }
    Kris Wong

    Kris Wong

    1 year ago
    in your second block you are just registering a task
    unless some other task is dependent on it, it won't execute unless you specify it on the command line
    nikola

    nikola

    1 year ago
    I wanted this to be a prebuild task. How can I specify that this should execute before anything else in the build.gradle.kts?
    Kris Wong

    Kris Wong

    1 year ago
    val myTask = tasks.register<Exec>("myTask") {}
    
    tasks.named("preBuild") {
        dependsOn(myTask)
    }
    that might not be 100% right, but you get the idea
    nikola

    nikola

    1 year ago
    @Kris Wong thank you for your help, I managed to make it work Here is the final solution:
    val generateCInteropConfig = task<Exec>("generateCinteropConfig") {
        workingDir = File("${projectDir}")
        commandLine = listOf("sh", "${projectDir}/generate_cinterop_conf.sh")
    }
    
    tasks.named("preBuild") {
        dependsOn(generateCInteropConfig)
    }