Home > Blockchain >  Gradle task : Git with dynamic values & specific treatment automation
Gradle task : Git with dynamic values & specific treatment automation

Time:08-09

i'm trying to build tasks to help my team. i have 2 mains branch for front & back and if the developper working on back and need to update front i want to cherry pick all commits from front and vice versa for front to back.

I'm using gradle 7.4.2

Here is my pseudocode to what i want to do :

if (${git branch --show-current} == 'front') {
    def st = git cherry-pick $(git merge-base --fork-point back)..back
} else if (${git branch --show-current} == 'back') {
    def st = git cherry-pick $(git merge-base --fork-point front)..front
}

But yeah, gradle don't look at that friendly ahahah

Well.. i'm badly new with gradle tasks so don't hesitate to tips me

Actually i have an error to the step to construct the request to get the commit to my final request

plugins {
    id 'java'
    id 'io.quarkus'
    id 'base'
}

ext {
    charset = 'UTF-8'
}

def current
task getGitCurrent(type: Exec) {
    commandLine 'git', 'branch', '--show-current'

    standardOutput = new ByteArrayOutputStream()
    ext.current = {
        standardOutput.toString(charset)
    }
    def currentRes = file("${buildDir}/current.txt")
    outputs.file currentRes
    doLast {
        if (!buildDir.exists()) {
            buildDir.mkdirs()
        }
        current = tasks.getGitCurrent.current()
        println "current branch : ${current}"
    }
}

def commitStart
task getGitCommitStart(dependsOn: 'getGitCurrent') {
    doLast {
        println "Branche A : ${current}"
        def String notCurrent
        if (current == "front") {
            notCurrent = "back"
        } else if (current == "back") {
            notCurrent = "front"
        } else notCurrent = "master"
        println "Branche B : ${notCurrent}"
        exec {
            commandLine 'git', 'merge-base', '--fork-point', "$notCurrent"
            standardOutput = new ByteArrayOutputStream()
            ext.commitStartRes = {
                standardOutput.toString(charset)
            }
            def commitStartRes = file("${buildDir}/commitStart.txt")
            outputs.file commitStartRes
            if (!buildDir.exists()) {
                buildDir.mkdirs()
            }
            commitStart = tasks.getGitCommitStart.commitStart()
        }
    }
}

And i have this exception who correspond to the line :

outputs.file commitStartRes

* What went wrong:
Execution failed for task ':getGitCommitStart'.
> Cannot call TaskOutputs.file(Object) on task ':getGitCommitStart' after task has started execution.

I just don't understand why my task getGitCommitStart() don't working like getGitCurrent() because it gives me good results. The problem looks about doLast scope, something looks wrong but I'm a little confused, what i'm missing there ?

CodePudding user response:

Well i decomposed step-by-step and i'm so close to get a result, here i am :

task first(type: Exec) {
    commandLine 'git', 'branch', '--show-current'
    standardOutput = new ByteArrayOutputStream()
    ext.current = {
        standardOutput.toString(charset)
    }
}
task second(dependsOn: 'first') {
    def currentRes = file("${buildDir}/current.txt")
    outputs.file currentRes
    doLast {
        if (!buildDir.exists()) {
            buildDir.mkdirs()
        }
        current = tasks.first.current()
        currentRes.write(current, charset)
        if (current == "front") {
            notCurrent = "back"
        } else if (current == "back") {
            notCurrent = "front"
        } else notCurrent = "main"
    }
}

task third(type: Exec) {
    commandLine 'cmd', 'git', 'merge-base', '--fork-point', "$notCurrent"
    standardOutput = new ByteArrayOutputStream()
    ext.commitStart = {
        standardOutput.toString(charset)
    }
}

task last(dependsOn :[second,third]) {
    commitStartRes = file("${buildDir}/commitStart.txt")
    outputs.file commitStartRes
    doLast {
        if (!buildDir.exists()) {
            buildDir.mkdirs()
        }
        commitStart = tasks.third.commitStart()
        commitStartRes.write(commitStart, charset)
        println "current branch : ${current}"
        println "not current branch : ${notCurrent}"
        println "commit start from : ${commitStart}"
    }
}

And this... is working up to the task third where my output is unfortunatly not a commit id but a

Microsoft Windows [version 10.0.22000.795]
(c) Microsoft Corporation. Tous droits r�serv�s.

C:\Users\xxxxx\Documents\Projects\xxx\back>

Well i'm on progress i guess...

  • Related