Microsoft的Visual Studio代码编辑器相当不错,但它没有构建C++项目的默认支持.
如何配置它来执行此操作?
我已经看到可以在VSCode中定义任务.但我不确定如何在tasks.json
文件中定义多个任务.
我有一个本地文件夹,我用作多个小样本和玩具代码的便笺簿.我在这个目录中存储了一大堆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}"]
}
Run Code Online (Sandbox Code Playgroud)
此任务将使用python作为任务运行器,而不管我当前正在编辑的文件类型.
如何实现基于文件类型运行命令的任务(或在多个命令之间进行选择)?即如果我正在编辑C++文件,它将运行clang ++.
这是我的一个例子tasks.json
:
{
"version": "0.1.0",
"tasks": [
{
"taskName": "test",
"suppressTaskName": true,
"command": "python",
"args": [
"tests/brewer_tests.py"
],
"isTestCommand": true
}
]
}
Run Code Online (Sandbox Code Playgroud)
我可以用它来运行shift+cmd+alt+b
.我也可以用它来运行它alt+t
,然后从菜单中选择它.是否可以在该菜单中传递其他参数?例如
您可以将其构建到您的任务中,如下所示:
{
"version": "0.1.0",
"tasks": [
{
"taskName": "test",
"suppressTaskName": true,
"command": "python",
"args": [
"tests/brewer_tests.py",
$arg1 # would resolve to "ARG1"
],
"isTestCommand": true
}
]
}
Run Code Online (Sandbox Code Playgroud)
或类似的东西?
我按Ctrl+ Shift+ B开始Visual Studio代码中的构建(它被配置为只运行GNU Make),并且构建工具输出被写入终端窗口.
但是,它附加到上一个版本的输出,这是令人困惑的.
如何在开始新构建之前配置VS代码以清除终端窗口?
Visual Studio Code 任务的工作目录是什么?
根据官方文档,默认应该是工作空间目录,即 ,workspaceFolder
但是运行一个简单执行 的任务echo $PWD
,它显示的是 的父目录workspaceFolder
。
换句话说,给定,在引用(实际工作空间目录)时dir1/dir2/.vscode/tasks.json
打印以下简单任务。dir1
workspaceFolder
dir2
我应该怎么做才能从 运行任务workspaceFolder
?更改cwd
选项没有帮助。
tasks.json
:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Sample",
"command": "echo PWD=$PWD workspaceFolder=${workspaceFolder}",
"options": {
"cwd": "${workspaceFolder}"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
执行输出:
> Executing task: echo PWD=$PWD workspaceFolder=/home/ubuntu/dir1/dir2 <
PWD=/home/ubuntu/dir1 workspaceFolder=/home/ubuntu/dir1/dir2
Run Code Online (Sandbox Code Playgroud)
作为附加且可能相关的说明,我正在使用远程 - SSH扩展进行远程开发。
我使用以下命令创建了一个新的.NET Core应用程序:
dotnet new console -o test
Run Code Online (Sandbox Code Playgroud)
当我尝试在Visual Studio代码调试器中运行它时,我得到:
Could not find the preLaunchTask 'build'?
Run Code Online (Sandbox Code Playgroud)
Visual Studio Code为我生成了这些文件:
tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "dotnet",
"isShellCommand": true,
"args": [],
"tasks": [
{
"taskName": "build",
"args": [ ],
"isBuildCommand": true,
"showOutput": "silent",
"problemMatcher": "$msCompile"
}
]
}
Run Code Online (Sandbox Code Playgroud)
和
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/bin/Debug/<target-framework>/<project-name.dll>",
"args": [],
"cwd": …
Run Code Online (Sandbox Code Playgroud) 我一直在浏览Visual Studio Code的文档,以弄清楚如何将多个连续任务添加到tasks.json
文件中.
该tasks
数组仅允许为同一命令创建不同的参数.在这个例子中命令是echo
.
{
"version": "0.1.0",
"command": "echo",
"isShellCommand": true,
"args": [],
"showOutput": "always",
"echoCommand": true,
"suppressTaskName": true,
"tasks": [
{
"taskName": "hello",
"args": ["Hello World"]
},
{
"taskName": "bye",
"args": ["Good Bye"]
}
]
}
Run Code Online (Sandbox Code Playgroud)
tasks.json是否允许连续执行多个任务?例如,tsc
接下来是uglify
?
我在Visual Studio代码中有一个调试设置,我运行一个外部二进制文件,可以执行我的JS文件(使用duktape).调试适配器当前只支持附加请求(不启动),所以我必须在调试JS脚本之前运行二进制文件.
为了避免必须手动启动应用程序,我为它创建了一个任务,并在我的launch.json文件中设置它:
{
"version": "0.2.0",
"configurations": [{
"name": "Attach MGA",
"type": "duk",
"preLaunchTask": "debug mga",
"request": "attach",
"address": "localhost",
"port": 9091,
"localRoot": "${workspaceRoot}",
"stopOnEntry": false,
"debugLog": true
}]
}
Run Code Online (Sandbox Code Playgroud)
任务定义如下:
{
"version": "0.1.0",
"command": "<absolute path to>/mga",
"isShellCommand": false,
"showOutput": "always",
"suppressTaskName": true,
"tasks": [{
"taskName": "debug mga",
"args": ["--debugger", "main.json"]
}]
}
Run Code Online (Sandbox Code Playgroud)
现在问题是vscode等待预启动任务完成,而应用程序等待调试器附加.赶上22.
如何避免vscode等待预启动任务完成?
更新:
与此同时,我已经阅读了vscode任务页面,并提出了此任务配置.不过,它对我不起作用
{
"version": "2.0.0",
"tasks": [
{
"label": "launch-mga",
"type": "shell",
"command": "<absolute path to>/mga",
"args": [
"config/main.json",
"--debugger"
], …
Run Code Online (Sandbox Code Playgroud) 我尝试为将在 Windows 子系统 Linux 中运行的 Visual Studio Code 任务设置环境变量。然而,它似乎不起作用。这是我的tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "test env",
"type": "shell",
"command": "echo",
"args": [
"$test"
],
"options": {
"env": {
"test": "test_string"
}
}
},
]
}
Run Code Online (Sandbox Code Playgroud)
输出是:
> Executing task in folder ex12-test: echo $test <
Terminal will be reused by tasks, press any key to close it.
Run Code Online (Sandbox Code Playgroud)
请注意,默认情况下 shell 已手动修改为C:\WINDOWS\SysNative\bash.exe
WSL,如此处和此处的建议。