问题
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!
回答1:
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
}
来源:https://stackoverflow.com/questions/42024645/use-gradle-to-run-yarn-in-every-folder