使用Gradle在每个文件夹中运行Yarn [英] Use Gradle to run Yarn in every folder

查看:160
本文介绍了使用Gradle在每个文件夹中运行Yarn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件夹,里面有很多目录.我希望有一个gradle脚本可以遍历它们(而不是递归地)并运行

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 

在其中.

我尝试了两种方法(并开始了一些不同的事情),但是可惜没有运气.

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 {} \\;"
}

通过任务构建,我想找到具有package.json的目录(实际上都是这些),但是后来我不知道如何更改为该目录以进行纱线构建"

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"

对于任务yarnBuild,我只想使用unix命令来完成.但是我也无法使它正常工作.

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

我会更感兴趣的是使用实际的Gradle查找更符合构建"任务的解决方案.有人可以给我一些指示吗?如何更改到这些目录?我猜一旦进入正确的文件夹,我就可以使用Exec运行纱线构建".

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".

谢谢!

推荐答案

我可能会为每个目录创建一个任务,并将它们全部连接到DAG

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
}

这篇关于使用Gradle在每个文件夹中运行Yarn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆