Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio code 具有Visual Studio代码的多个命令/任务_Visual Studio Code_Vscode Tasks - Fatal编程技术网

Visual studio code 具有Visual Studio代码的多个命令/任务

Visual studio code 具有Visual Studio代码的多个命令/任务,visual-studio-code,vscode-tasks,Visual Studio Code,Vscode Tasks,我有一个本地文件夹,用作多个小样本和玩具代码的便笺簿。我在这个目录中存储了大量的Python、C++、shell脚本等。 我正在使用visualstudio代码(在osx上),并且正在研究如何在不切换到终端的情况下运行/编译代码片段 例如,我发现将在当前打开的文件上运行python //运行python程序的任务运行程序 { “版本”:“0.1.0”, “命令”:“/usr/bin/python”, “args”:[“${file}”] } 此任务将使用python作为任务运行程序,而与我当前

我有一个本地文件夹,用作多个小样本和玩具代码的便笺簿。我在这个目录中存储了大量的Python、C++、shell脚本等。 我正在使用visualstudio代码(在osx上),并且正在研究如何在不切换到终端的情况下运行/编译代码片段

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

//运行python程序的任务运行程序
{
“版本”:“0.1.0”,
“命令”:“/usr/bin/python”,
“args”:[“${file}”]
}
此任务将使用python作为任务运行程序,而与我当前编辑的文件类型无关

如何根据文件类型(或在多个命令之间进行选择)执行任务以运行命令?也就是说,如果我正在编辑一个C++文件,它将运行CLAN+++。
  • 如果我不能根据文件类型来做;有没有别的办法
  • 另一种选择是:;是否支持多个命令

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

因此,您的任务文件如下所示

//运行程序的任务运行程序
{
“版本”:“0.1.0”,
“命令”:“${workspaceRoot}\\runProgram.sh”,
“args”:[“${file}”]
}

最近对
任务的更改。json
似乎为列出的每个任务提供了一个命令。看看是什么让这一切变得毫无意义


这个答案最初是针对一个更复杂的解决方案,但在接受的答案中所呈现的简单shell runner任务格式被证明更有用。现在,请参见下面的内容


这里的限制是VS代码仅限于给定工作区的单个高级构建任务/命令。允许多个子任务,但它们仅限于使用顶级“命令”,但可以提供不同的“参数”。这非常适合使用类似于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
鉴于上述选项,第二个选项更可取。这个实现可以在OSX上运行,但可以根据需要轻松地移植到Linux和Windows。我将继续关注这一点,并尝试跟踪对VS代码构建任务的更改,基于文件的构建或对多个命令的支持可能是一个受欢迎的补充


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

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

{
“版本”:“0.1.0”,
“命令”:“bash”,
“isShellCommand”:正确,
“showOutput”:“始终”,
“args”:[
“-c”
],
“任务”:[
{
“任务名”:“我的第一个命令”,
“suppressTaskName”:true,
“isBuildCommand”:正确,
“args”:[“echo cmd1”]
},
{
“任务名”:“我的命令需要.bash_配置文件”,
“suppressTaskName”:true,
“args”:[“source~/.bash_profile&&echo cmd2”]
},
{
“任务名”:“我的Python任务”,
“suppressTaskName”:true,
“args”:[“/usr/bin/python${file}”]
}
]
}
关于这里发生的事情的一些注意事项:

  • 通过将bash
    -c
    放在命令的
    args
    列表中,为所有任务使用bash,以便我们可以运行任意命令。
    echo
    语句只是示例,但可以是从bash终端执行的任何内容
  • args
    数组将包含要传递给
    bash-c
    的单个字符串(单独的项将被视为bash命令的多个参数,而不是与
    -c
    arg关联的命令)
  • suppressTaskName
    用于将
    taskName
    排除在混合之外
  • 第二个命令显示了如果您需要
    ~/.bash\u配置文件提供的任何内容,如别名、环境变量等,如何加载该配置文件
  • 第三个命令显示了如何使用您提到的Python命令
这不会给您任何类型的文件扩展名检测,但您至少可以使用cmd+p,然后键入“task”来获取任务列表。您始终可以使用
isBuildCommand
isTestCommand
标记两个最常用的命令,分别通过cmd+shift+b或cmd+shift+t运行它们

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

我编写了这个脚本

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


可以使用复合任务来运行多个命令
最简单的方法是添加它们,用
分隔在shell中:

{
“版本”:“2.0.0”,
“任务”:[
{
“标签”:“测试”,
“类型”:“外壳”,
“命令”:“cd~/dev/xxxx;source~/dev/yyyy;ls”,
}
]
}

您可以使用CTRL+D来关闭IDLE。我回到这里,我非常喜欢u
{
    "version": "0.1.0",             
    "command": "/usr/bin/idle",
    "isShellCommand": false,
    "showOutput": "never",
    "args": ["-r","${file}"]    
}