赞
踩
vscode是支持通过配置可以实现类似Visual C++等IDE开发工具使用菜单和快捷键直接进行程序编译构建的,这样构建的任务可以结合后续的调试配置进行IDE环境的程序调试,不过在之前必须先通过vscode的编译构建任务配置文件tasks.json进行相关设置,当然配置的任务不仅限于编译,还可以做很多后台处理,如老猿就定义了一个任务用于显示vscode中可以使用的预定义变量的值,详细介绍请参考老猿在CSDN的博文《vscode中tasks.json文件使用的预定义变量及国产统信操作系统UOS下配置一个任务显示相关预定义变量的案例》。
在tasks.json配置文件中,可以定义多个任务,每个任务都包含了一些属性,例如任务名称、任务类型、执行命令、命令参数、工作目录、输出等,这些任务还可以指定执行依赖关系。通过配置tasks.json文件,可以方便地在Visual Studio Code中执行这些任务,提高开发效率。
tasks.json最重要的配置当然就是任务,关于任务配置项官方定义请见vscode的官方文档。
任务又分为基本任务和可选的任务组,下面是基本任务的官方相关定义及注释:
interface BaseTaskConfiguration {
/**
* The type of a custom task. Tasks of type "shell" are executed
* inside a shell (e.g. bash, cmd, powershell, ...)
*/
type: 'shell' | 'process';
/**
* The command to be executed. Can be an external program or a shell
* command.
*/
command: string;
/**
* Specifies whether a global command is a background task.
*/
isBackground?: boolean;
/**
* The command options used when the command is executed. Can be omitted.
*/
options?: CommandOptions;
/**
* The arguments passed to the command. Can be omitted.
*/
args?: string[];
/**
* The presentation options.
*/
presentation?: PresentationOptions;
/**
* The problem matcher to be used if a global command is executed (e.g. no tasks
* are defined). A tasks.json file can either contain a global problemMatcher
* property or a tasks property but not both.
*/
problemMatcher?: string | ProblemMatcher | (string | ProblemMatcher)[];
/**
* The configuration of the available tasks. A tasks.json file can either
* contain a global problemMatcher property or a tasks property but not both.
*/
tasks?: TaskDescription[];
}
基本任务中的tasks就是可选任务组,可见可选任务组tasks是基本任务下的一个可选的配置项,其类型为TaskDescription类型的一个数组,下面是TaskDescription的官方定义。
/**
1. The description of a task.
*/
interface TaskDescription {
/**
2. The task's name
*/
label: string;
/**
3. The type of a custom task. Tasks of type "shell" are executed
4. inside a shell (e.g. bash, cmd, powershell, ...)
*/
type: 'shell' | 'process';
/**
5. The command to execute. If the type is "shell" it should be the full
6. command line including any additional arguments passed to the command.
*/
command: string;
/**
7. Whether the executed command is kept alive and runs in the background.
*/
isBackground?: boolean;
/**
8. Additional arguments passed to the command. Should be used if type
9. is "process".
*/
args?: string[];
/**
10. Defines the group to which this task belongs. Also supports to mark
11. a task as the default task in a group.
*/
group?: 'build' | 'test' | { kind: 'build' | 'test'; isDefault: boolean };
/**
12. The presentation options.
*/
presentation?: PresentationOptions;
/**
13. The problem matcher(s) to use to capture problems in the tasks
14. output.
*/
problemMatcher?: string | ProblemMatcher | (string | ProblemMatcher)[];
/**
15. Defines when and how a task is run.
*/
runOptions?: RunOptions;
}
本文主要介绍可选任务组tasks的相关配置,关于基本任务将在另外的博文中介绍。
从官方文档可见,一个典型的TaskDescription元素包含多种属性,下面逐一介绍:
下面是老猿配置的2个可选任务buildHello1和buildHello2的配置内容:
"tasks": [
{
"type": "cppbuild",
"label": "buildHello1",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"-Wall",
"-Wextra",
"-O0",
"${workspaceFolder}/hello.cpp",
"-o",
"${fileDirname}/$hello1"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": "$gcc",
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译器: /usr/bin/g++"
},
{
"type": "cppbuild",
"label": "buildHello2",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"-Wall",
"-Wextra",
"-O0",
"${workspaceFolder}/hello2.cpp",
"-o",
"${fileDirname}/$hello2"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": "$gcc",
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译器: /usr/bin/g++"
}]
配置完成后,在终端菜单选择“运行生成任务…”时可以看到两个任务供选择:
本文介绍了tasks.json中任务配置选项的可选任务组tasks子配置项,tasks的类型是TaskDescription,文章介绍了TaskDescription的详细内容和用途。tasks配置项配置的任务是大多数任务的配置方式,理解相关子配置项的内容对于更好地配置vscode的任务有重要作用。
如果阅读本文于您有所获,敬请点赞、评论、收藏,谢谢大家的支持!
更多关于vscode任务配置的内容请参考专栏《国产信创之光》的其他文章。
前两个专栏都适合有一定Python基础但无相关知识的小白读者学习,第三个专栏请大家结合《https://blog.csdn.net/laoyuanpython/category_9979286.html OpenCV-Python图形图像处理 》的学习使用。
对于缺乏Python基础的同仁,可以通过老猿的免费专栏《https://blog.csdn.net/laoyuanpython/category_9831699.html 专栏:Python基础教程目录)从零开始学习Python。
如果有兴趣也愿意支持老猿的读者,欢迎购买付费专栏。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。