Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

groovy - Use Gradle to run Yarn in every folder

I have a folder with a lot of directories inside. I want to have a gradle script that will loop through them (not recursively) and run

 yarn build 

in them.

I have tried two approaches (and started several different things), but alas no luck.

task build (description: "Loops through folders and builds"){
    FileTree tree = fileTree(dir: 'admin', include: '*/package.json')

    tree.each {File file -> println file}

}

task yarnBuild (type: Exec){
    executable "bash"
    args "find . -type d -exec ls {} \;"
}

With the task build I wanted to find the directories that had the package.json (all of them, really), but then I don't know how to change to that directory to do a "yarn build"

With the task yarnBuild I wanted to just do it with a unix command. But I couldn't get it to work either.

I would be more interested in finding a solution more in line with the "build" task, using actual Gradle. Can anybody give me some pointers? How can I change into those directories? I'm guessing once I'm in the right folder I can just use Exec to run "yarn build".

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'd probably create a task per directory and the wire them all into the DAG

apply plugn: 'base'
FileTree directories = fileTree(projectDir).matching {
    include { FileTreeElement el ->
       return el.directory && el.file.parentFile == projectDir
    }
}
directories.files.each { File dir ->
    // create a task for the directory
    Task yarnTask = tasks.create("yarn${dir.name}", Exec) {
        workingDir dir
        commandLine 'yarn', 'build'

        // TODO: set these so gradle's up-to-date checks can work
        inputs.dir = ???
        outputs.dir = ???
    }
    // wire the task into the DAG
    build.dependsOn yarnTask
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...