json - 配置 Visual Studio Code 任务以顺序执行多个 shell 命令

标签 json shell visual-studio-code cmake vscode-tasks

是否有可能将多个 shell 命令按顺序添加到具有单独参数和选项的 Visual Studio Code 任务中?我设法使用 && 执行多个命令,然后将它们链接到一个命令,就像在任何 Linux shell 中一样。但我想,必须有更好的方法来做到这一点。

我在 Ubuntu 18.04 LTS 上使用 Visual Studio Code。

这是我当前如何将构建任务的命令链接到 task.json 文件中以使用 cmake 构建 C++ 项目的示例:

{
    "tasks": [
        {
            "type": "shell",
            "label": "build",
            "command": "cd ${workspaceFolder}/build && cmake .. && make clean && make",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ],
    "version": "2.0.0"
}

更新:---------------------------------------- --------------------------

我尝试使用 dependsOn: 属性来启动 4 个单独定义的任务。然而,这导致所有 4 个命令在不同的 shell 实例中同时执行,而不是按需要顺序执行:

{
    "tasks": [
        {
            "type": "shell",
            "label": "build project",
            "dependsOn": [
                "Go to build folder",
                "Cmake",
                "make clean",
                "make",
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "Go to build folder",
            "type": "shell",
            "command": "cd ${workspaceFolder}/build",
            "presentation": {
                "group": "cmake-complile"
            }
        },
        {
            "label": "Cmake",
            "type": "shell",
            "command": "cmake ..",
            "presentation": {
                "group": "cmake-complile"
            }
        },
        {
            "label": "make clean",
            "type": "shell",
            "command": "make clean",
            "presentation": {
                "group": "cmake-complile"
            }
        },
        {
            "label": "make",
            "type": "shell",
            "command": "make",
            "presentation": {
                "group": "cmake-complile"
            }
        }
    ],
    "version": "2.0.0"
}

最佳答案

感谢许多评论,我找到了一个通过将“dependsOrder”设置为“sequence”而运行良好的解决方案:

{
    "tasks": [
        {
            "type": "shell",
            "label": "build project",
            "dependsOrder": "sequence",
            "dependsOn": [
                "Cmake",
                "make clean",
                "make",
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "Cmake",
            "type": "shell",
            "command": "cmake ..",
            "options": {
                "cwd": "${workspaceFolder}/build",
            },
        },
        {
            "label": "make clean",
            "type": "shell",
            "command": "make clean",
            "options": {
                "cwd": "${workspaceFolder}/build",
            },
        },
        {
            "label": "make",
            "type": "shell",
            "command": "make",
            "options": {
                "cwd": "${workspaceFolder}/build",
            },
        }
    ],
    "version": "2.0.0"
}

关于json - 配置 Visual Studio Code 任务以顺序执行多个 shell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63142061/

相关文章:

javascript - Angular 2 : unable to return a json mapped from an Http response

visual-studio-code - Visual Studio Code - 快捷方式 - 折叠资源管理器中的所有文件

visual-studio-code - 使用远程 SSH 的 VS Code - 如何打开 "local"终端?

gruntjs - Gruntfile.js 中的智能感知

javascript - 如何用数组过滤对象

javascript - Parse.Object.extend 的 JSON.stringify 返回 "[object Object]"

shell - 多行变量删除换行符 - Fish

mysql - 如何从 bash 脚本中有效地处理 mysql 的结果集?

c - 在 C 程序中移动光标

javascript - JSON不添加最后一个逗号