tasks.json的使用

Intro

tasks.json 的作用是什么呢?官方的解释是这样的:

Lots of tools exist to automate tasks like linting, building, packaging, testing, or deploying software systems.
Tasks in VS Code can be configured to run scripts and start processes so that many of these existing tools can be used from within VS Code without having to enter a command line or write new code.
linting: Linting is a tool that analyzes source code to flag programming errors, bugs, stylistic errors, and suspicious constructs > Integrate with External Tools via Tasks

简单来说,tasks.json 的作用就是可以在 VSCode 中编写一些脚本,用于执行一些任务,比如编译代码,运行代码,打包代码等等。

properties

The task’s properties have the following semantic:

  • label: The task’s label used in the user interface.
  • type: The task’s type. For a custom task, this can either be shell or process. If shell is specified, the command is interpreted as a shell command (for example: bash, cmd, or PowerShell). If process is specified, the command is interpreted as a process to execute.
  • command: The actual command to execute.
  • windows: Any Windows specific properties. Will be used instead of the default properties when the command is executed on the Windows operating system.
  • group: Defines to which group the task belongs. In the example, it belongs to the test group. Tasks that belong to the test group can be executed by running Run Test Task from the Command Palette.
  • presentation: Defines how the task output is handled in the user interface. In this example, the Integrated Terminal showing the output is always revealed and a new terminal is created on every task run.
  • options: Override the defaults for cwd (current working directory), env (environment variables), or shell (default shell). Options can be set per task but also globally or per platform. Environment variables configured here can only be referenced from within your task script or process and will not be resolved if they are part of your args, command, or other task attributes.
  • runOptions: Defines when and how a task is run.

示例

tasks.json的功能十分丰富,这里只列举一些常用的功能,更多的功能可以参考官方文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "compile c file(Windows)",
"command": "gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"group": "build"
},
{
"type": "shell",
"label": "compile c file(Linux)",
"command": "gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.out"
],
"group": "build"
},
{
"type": "shell",
"label": "run exe file(Windows)",
"command": "${fileDirname}\\${fileBasenameNoExtension}.exe"
},
{
"type": "shell",
"label": "compile and run c file(Windows)",
"command": "gcc -g ${file} -o ${fileDirname}\\${fileBasenameNoExtension}.exe && ${fileDirname}\\${fileBasenameNoExtension}.exe",
"problemMatcher": ["$gcc"]
}
]
}
  • compile c file(Windows)compile c file(Linux):用于编译 c 文件
  • run exe file(Windows)用于运行编译好的 exe 文件,代码没有改动时,可以直接运行 exe 文件,避免重新编译。
  • compile and run c file(Windows)用于编译并运行 c 文件。

ps: 为workbench.action.tasks.runTask设置快捷键,可显示 tasks 列表

Author

Efterklang

Posted on

2024-01-26

Updated on

2024-09-18

Licensed under

Comments