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

问题描述 投票:21回答:4

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

我正在使用Visual Studio Code(在OS X上),我正在研究its tasks来运行/编译代码片段而无需切换到终端。

例如,我发现this following task将在当前打开的文件上运行python。

// A task runner that runs a python program
{
    "version": "0.1.0",
    "command": "/usr/bin/python",
    "args": ["${file}"]
}

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

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

  • 如果我不能根据文件类型做到这一点;有什么替代品吗?
  • 另一种选择是;是否支持多个命令?
visual-studio-code vscode-tasks
4个回答
31
投票

您始终可以使用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}"]
        }
    ]
}

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

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

这不会为您提供任何类型的文件扩展名检测,但您至少可以使用cmd + p然后键入“task”来获取任务列表。您可以使用isBuildCommandisTestCommand标记2个最常用的命令,分别通过cmd + shift + b或cmd + shift + t运行它们。

This answer有一些可能对您有用的有用信息。


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"
            // ...
        }
    ]
}

有两种选择;

  • 让自定义脚本尝试仅在task.json中给出参数来运行编译/执行。 -- the shell file would be a simple "$@" # run what I get -- the tasks.json "args": ["clang++", "-std=c++14", "-O2", "${file}"] 让exectuable运行(./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 } 和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

鉴于上面列出的选项,第二种选择更可取。此实现适用于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"
            ]
        }
    ]
}

-1
投票

我制作了这个剧本。

它要求您在您的环境中安装python IDLE。这将打开IDLE并在每次运行任务时运行python文件(CTRL + Shift + B)。

{
    "version": "0.1.0",             
    "command": "/usr/bin/idle",
    "isShellCommand": false,
    "showOutput": "never",
    "args": ["-r","${file}"]    
} 

-1
投票

您可以直接编写和运行自定义脚本文件而不是python等。在脚本文件中,您将提取文件扩展名,以便调用pythonclang或编译器/翻译器所需的任何内容。

所以你的任务文件看起来像这样;

// A task runner that runs a program
{
   "version": "0.1.0",
   "command": "${workspaceRoot}\\runProgram.sh"
   "args": ["${file}"]
}
© www.soinside.com 2019 - 2024. All rights reserved.