使用Visual Studio代码的多个命令/任务

Nia*_*all 25 visual-studio-code vscode-tasks

我有一个本地文件夹,我用作多个小样本和玩具代码的便笺簿.我在这个目录中存储了一大堆python,C++,shell脚本等.

我正在使用Visual Studio Code(在OS X上)并且正在研究运行/编译代码片段的任务,而无需切换到终端.

例如,我发现以下任务将在当前打开的文件上运行python.

// A task runner that runs a python program
{
    "version": "0.1.0",
    "command": "/usr/bin/python",
    "args": ["${file}"]
}
Run Code Online (Sandbox Code Playgroud)

此任务将使用python作为任务运行器,而不管我当前正在编辑的文件类型.

如何实现基于文件类型运行命令的任务(或在多个命令之间进行选择)?即如果我正在编辑C++文件,它将运行clang ++.

  • 如果我不能根据文件类型做到这一点; 有什么替代品吗?
  • 另一种选择是; 是否支持多个命令?

bin*_*les 34

您始终可以使用bash作为任务运行器,然后将任意终端命令指定为您的任务.

{
    "version": "0.1.0",
    "command": "bash",
    "isShellCommand": true,
    "showOutput": "always",
    "args": [
        "-c"
    ],
    "tasks": [
        {
            "taskName": "My First Command",
            "suppressTaskName": true,
            "isBuildCommand": true,
            "args": ["echo cmd1"]
        },
        {
            "taskName": "My Command Requiring .bash_profile",
            "suppressTaskName": true,
            "args": ["source ~/.bash_profile && echo cmd2"]
        },
        {
            "taskName": "My Python task",
            "suppressTaskName": true,
            "args": ["/usr/bin/python ${file}"]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

关于这里发生的事情的一些注意事项:

  • 通过将bash -c放在args命令列表中来为所有任务使用bash ,以便我们可以运行任意命令.这些echo语句只是示例,但可以是您的bash终端可执行的任何内容.
  • args数组将包含一个要传递给的字符串bash -c(单独的项将被视为bash命令的多个参数,而不是与-carg 关联的命令).
  • suppressTaskName被用来保持taskName混合
  • 第二个命令显示~/.bash_profile如果你需要它提供的任何东西,如别名,env变量等,你可以加载你的
  • 第三个命令显示了如何使用您提到的Python命令

这不会给你任何类型的文件扩展名检测,但你至少可以使用cmd+ p然后输入"task"来获取你的任务列表.您始终可以使用+ 标记2个最常用的命令,isBuildCommand并分别isTestCommand通过cmd+ shift+ bcmd+ shift+ 运行它们t.

这个答案有一些有用的信息,也可能对你有用.


mar*_*ira 13

您可以使用复合任务来运行多个命令 https://code.visualstudio.com/docs/editor/tasks#_compound-tasks


Adr*_* Jr 7

最简单的方法是将它们添加到;shell中分隔开:

任务.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "test",
            "type": "shell",
            "command": "cd ~/dev/xxxx;source ~/dev/yyyy;ls",
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

  • 这适用于一些短命令,但不幸的是,json 中不能有多行字符串,因此如果有很多命令,则无法添加换行符以提高视觉可读性。 (3认同)

Nia*_*all 6

这个答案最初的目标是一个更复杂的解决方案,但是在接受的答案中提出的简单shell运行程序任务格式证明更有用.请参阅下文,了解现在的情况.


这里的限制是VS Code仅限于给定工作空间的单个高级构建任务/命令.允许多个子任务,但它们仅限于使用顶级"命令",但可以提供不同的"参数".这非常适合使用类似于make,ant或msbuild的构建系统的环境.例如;

{
    "version": "0.1.0",
    "command": "make", // command must appear here
    "tasks" : [
        {
            "taskName": "clean",
            "suppressTaskName": false, // false by default
            //"command": "somethingelse", // not valid here
            "args": ["${file}"] // if required
        },
        {
            "taskName": "install"
            // ...
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

有两种选择;

  • 让自定义脚本尝试仅在task.json中给出参数来运行编译/执行.

    -- the shell file would be a simple
    "$@" # run what I get
    -- the tasks.json
    "args": ["clang++", "-std=c++14", "-O2", "${file}"]
    
    Run Code Online (Sandbox Code Playgroud)

    让exectuable run(./a.out)更加努力.简单地添加它作为参数不起作用,如果它在那里,则需要shell脚本执行它.

  • 在给定文件扩展名和文件名的情况下,将输出切换和执行输出到自定义脚本.这证明更容易实现,并在shell脚本中提供更多控制.

    {
        "version": "0.1.0",
        "isShellCommand": true,
        "taskName": "generic build",
        "showOutput": "always",
        "args": ["${fileExtname}", "${file}"]
        "command": "./.vscode/compileme.sh", // expected in the "local settings" folder
        //"command": "~/compileme.sh", // if in HOME folder
    }
    
    Run Code Online (Sandbox Code Playgroud)

    和shell脚本,compileme.sh;

    #!/bin/sh
    # basic error checking not shown...
    echo "compilation being executed with the arguments;"
    echo "$@"
    filetype=$1
    file=$2
    if [ $filetype = ".cpp" -o $filetype = ".cxx" ] ; then 
        clang++ -std=c++14 -Wall -Wextra -pedantic -pthread $file && ./a.out
    elif [ $filetype = ".c" ]
        then 
        clang -std=c11 -Wall -Wextra -pedantic -pthread $file && ./a.out
    elif [ $filetype = ".sh" ]
        then
        $file
    elif [ $filetype = ".py" ]
        then
        python $file
    else
        echo "file type not supported..."
        exit 1
    fi
    
    Run Code Online (Sandbox Code Playgroud)

鉴于上面列出的选项,第二种选择更可取.此实现适用于OS X,但可以根据需要轻松移植到Linux和Windows.我将继续关注这一点,并尝试跟踪VS代码构建任务的更改,基于文件的构建或支持多个命令可能是一个受欢迎的补充.


我的tasks.json支持一些runners,以及构建的默认值,它将消息打印为提醒.它使用shell作为跑步者,现在看起来像......

{
    "version": "0.1.0",
    "isShellCommand": true,
    "taskName": "GenericBuild",
    "showOutput": "always",
    "command": "sh",
    "suppressTaskName": false,
    "args": ["-c"],
    "tasks": [
        {
            "taskName": "no build",
            "suppressTaskName": true,
            "isBuildCommand": true,
            "args": [
                "echo There is no default build task, run a task by name..."
            ]
        },
        {
            "taskName": "cpp",
            "suppressTaskName": true,
            "args": [
                "clang++ -std=c++14 -Wall -Wextra -pedantic -pthread \"${file}\" && ./a.out"
            ]
        },
        {
            "taskName": "shell",
            "suppressTaskName": true,
            "args": [
                "\"${file}\""
            ]
        },
        {
            "taskName": "python",
            "suppressTaskName": true,
            "args": [
                "python \"${file}\""
            ]
        },
        {
            "taskName": "c",
            "suppressTaskName": true,
            "args": [
                "clang -std=c11 -Wall -Wextra -pedantic -pthread \"${file}\" && ./a.out"
            ]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

26088 次

最近记录:

6 年,11 月 前