【问题标题】:Multiple commands/tasks with Visual Studio Code使用 Visual Studio Code 执行多个命令/任务
【发布时间】:2015-10-20 12:40:17
【问题描述】:

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

我正在使用 Visual Studio Code(在 OS X 上)并且正在研究 its tasks 以运行/编译代码 sn-ps 而无需切换到终端。

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

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

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

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

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

【问题讨论】:

    标签: visual-studio-code vscode-tasks


    【解决方案1】:

    您始终可以使用 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 排除在外
    • 第二个命令显示了如何加载 ~/.bash_profile 如果您需要它提供的任何东西,例如别名、环境变量等
    • 第三条命令展示了如何使用您提到的 Python 命令

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

    This answer 提供了一些可能对您也有用的有用信息。

    【讨论】:

    • 我又回来了,我真的很喜欢使用bash -c 作为命令运行程序的想法。这很适合我。
    • 我已经创建了你上面提到的一个变体来编译一个简单的 C++ 程序。不幸的是,我无法将其粘贴到评论中。但是我注意到任务的输出没有显示在输出窗格中。例如,如果我的任务执行“bash -c helloworld”,则输出窗格中不会出现“Hello world”。现在正试图弄清楚如何做到这一点。
    • 这就是我收敛到的:github.com/ataiya/hellovsc 请注意,由于某种原因,当我执行“echoCommand”时,我只能看到计算机/运行输出:false,即特别反直觉!此外,您不知道任务何时完成执行,这很糟糕。
    • 我没有使用 echoCommand,所以不确定其中的细微差别。至于知道您的任务何时完成,您始终可以将 && echo done 连接到命令的末尾。 "args": ["g++ -v hello.cpp -o hello && echo done"]
    【解决方案2】:

    最近对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 支持一些跑步者,以及打印消息作为提醒的构建的默认设置。它使用 shell 作为 runner,现在看起来像......

    {
        "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"
                ]
            }
        ]
    }
    

    【讨论】:

      【解决方案3】:

      最简单的方法是在 shell 中添加它们,以 ; 分隔:

      tasks.json:

      {
          "version": "2.0.0",
          "tasks": [
              {
                  "label": "test",
                  "type": "shell",
                  "command": "cd ~/dev/xxxx;source ~/dev/yyyy;ls",
              }
          ]
      }
      

      【讨论】:

      • 这适用于一些简短的命令,但不幸的是,您不能在 json 中包含多行字符串,因此如果有很多命令,则无法添加换行符以提高视觉可读性。
      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

        所以你的任务文件应该是这样的;

        // A task runner that runs a program
        {
           "version": "0.1.0",
           "command": "${workspaceRoot}\\runProgram.sh",
           "args": ["${file}"]
        }
        

        【讨论】:

          【解决方案6】:

          我制作了这个脚本。

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

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

          【讨论】:

          • 可以使用CTRL+D关闭IDLE
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-01-07
          • 2022-11-09
          • 2018-11-08
          • 2016-05-21
          • 2016-02-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多