41

我有一个本地文件夹,用作多个小示例和玩具代码的便笺簿。我在这个目录中存储了大量的 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}"]
}

无论我当前正在编辑的文件类型如何,此任务都将使用 python 作为任务运行器。

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

  • 如果我不能根据文件类型来做;有没有其他选择?
  • 另一种选择是;是否支持多个命令?
4

6 回答 6

44

您始终可以使用 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命令列表中来对所有任务使用 bash,以便我们可以运行任意命令。这些echo语句只是示例,但可以是从 bash 终端可执行的任何内容。
  • args数组将包含要传递给的单个字符串bash -c(单独的项目将被视为 bash 命令的多个参数,而不是与-carg 关联的命令)。
  • suppressTaskName被用来防止taskName混音
  • ~/.bash_profile第二个命令显示了如果您需要它提供的任何内容(例如别名、环境变量等),您如何加载
  • 第三个命令显示了如何使用您提到的 Python 命令

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

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

于 2015-08-29T19:44:28.340 回答
9

最近对 的更改tasks.json似乎为列出的每个任务提供了一个命令。请参阅https://code.visualstudio.com/docs/editor/tasks,这让很多事情变得毫无意义。


这个答案最初是针对更复杂的解决方案,但在接受的答案中提供的简单 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}"]
    

让可执行文件运行 ( ./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 Code 构建任务、基于文件的构建或对多个命令的支持的更改,这可能是一个受欢迎的补充。


我的 tasks.json 支持一些跑步者,并且默认为构建打印消息作为提醒。它使用外壳作为跑步者,现在看起来像......

{
    "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"
            ]
        }
    ]
}
于 2015-07-30T10:06:50.290 回答
9

最简单的方法是在 shell中添加它们;(或)分隔:&&

任务.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "test",
            "type": "shell",
            "command": "cd ~/dev/xxxx;source ~/dev/yyyy;ls",
        }
    ]
}
于 2020-12-17T04:04:24.657 回答
5

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

于 2020-08-20T21:10:07.060 回答
0

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

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

// A task runner that runs a program
{
   "version": "0.1.0",
   "command": "${workspaceRoot}\\runProgram.sh",
   "args": ["${file}"]
}
于 2015-07-29T14:42:47.443 回答
-1

我做了这个脚本。

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

{
    "version": "0.1.0",             
    "command": "/usr/bin/idle",
    "isShellCommand": false,
    "showOutput": "never",
    "args": ["-r","${file}"]    
} 
于 2015-12-15T16:36:09.220 回答