本帖最后由 lovzx 于 2024-10-4 11:48 编辑
本帖最后由 lovzx 于 2024-10-2 15:58 编辑
本帖最后由 lovzx 于 2024-9-30 13:59 编辑
10/4日task编译参数修改为-C 指定make工作目录方式
command variable 插件助力vscode 开发wb2/m61
command vaiable介绍
command vaiable是一个vscode的插件,支持在launch.json和tasks.json中执行command命令
command vaiable开源地址 https://github.com/rioj7/command-variable
使用的话在vscode 插件中搜索 command vaiable安装即可
command vaiable支持的命令
插件支持的命令很多,功能丰富,简单介绍几个命令
extension.commandvariable.pickStringRemember
:从给定的字符串数组中选择并保存到变量中
extension.commandvariable.promptStringRemember
:手动输入字符串并保存到变量中
extension.commandvariable.remember
:获取变量的内容,例如前面两个设置的变量
extension.commandvariable.number
:按照指定范围生成随机数
extension.commandvariable.getClipboard
:获取剪切板内容
等等具体的支持那些命令以及怎么使用请参考https://github.com/rioj7/command-variable/?tab=readme-ov-file#table-of-contents
利用插件配置wb2/m61的环境变量
之前有分享过通过配置task来实现编译/烧录,大致是通过设置cwd参数改变当前工作的目录,然后配置task任务,最后通过task buttons插件把status bar上面的按钮和task任务绑定,这样就实现了点击按钮可以编译/烧录功能,具体的教程可以参考之前的帖子:配置vscode纵享丝滑
之前的配置方式需要在tasks.json文件中设置cwd的参数来更改当前的工作目录,这个有点不友好
现在可以利用command variable插件来选择编译的目录,并且保存到变量中,然后通过cwd或者make -C的参数来实现更改编译项目,这种方式比手动更改要好的多
效果如下
配置思路如下
首先要安装command variable和 task buttons(这个可以参考之前的帖子)两个插件,安装好就可以配置了
- 通过命令extension.commandvariable.file.openDialog 命令设置打开选择文件夹窗口,把选择的文件保存到变量中
- 通过命令extension.commandvariable.remember拿到保存的值
具体的配置如下
tasks.json
{
"version": "2.0.0",
"presentation": {
"echo": false,
"reveal": "always",
"focus": true,
"panel": "shared",
"showReuseMessage": true,
"clear": false,
},
"options": {
// "cwd": "${workspaceFolder}/mqtt",
//"cwd": "${input:getWorkDir}"
},
"inputs": [
{
"id": "setWorkDir",
"type": "command",
"command": "extension.commandvariable.file.openDialog",
"args": {
"canSelect": "folders",
"defaultUri": "${workspaceFolder}",
"title": "请选择项目根目录",
"checkEscapedUI": true,
"keyRemember": "workDir"
},
},
{
"id": "getWorkDir",
"type": "command",
"command": "extension.commandvariable.remember",
"args": {
"key": "workDir",
"checkEscapedUI": true,
"options": {
"name": "dir-name",
"label": "dir"
},
}
}
],
"tasks": [
{
"type": "shell",
"label": "echo",
"command": "echo",
"args": [
"${input:getWorkDir}"
],
},
{
"type": "shell",
"label": "setcwd",
"command": "echo",
"args": [
"${input:setWorkDir}"
]
},
{
"type": "shell",
"group": "build",
"label": "make 编译",
"command": "make",
"detail": "make -j16",
"args": [
"-j16",
"-C",
"${input:getWorkDir}",
]
},
{
"type": "shell",
"group": "build",
"label": "make clean",
"command": "make",
"detail": "make clean",
"args": [
"-j16",
"-C",
"${input:getWorkDir}",
"clean"
]
},
{
"type": "shell",
"group": "build",
"label": "make flash",
"command": "make",
"detail": "make flash",
"args": [
"-j16",
"-C",
"${input:getWorkDir}",
"flash",
"p=COM6"
]
}
]
}
settings.json增加如下配置
"workbench.statusBar.visible": true,
"VsCodeTaskButtons.tasks": [
{
"label": "🛠️ make build",
"task": "make 编译",
"tooltip": "编译代码"
},
{
"label": "🧹make clean",
"task": "make clean",
"tooltip": "make clean"
},
{
"label": "⬇️ flash",
"task": "make flash",
"tooltip": "烧录"
},
{
"label": "👀echo",
"task": "echo",
"tooltip": "echo current work directory"
},
{
"label": "📂setcwd",
"task": "setcwd",
"tooltip": "set current work directory"
}
],
配置好了就可以正常使用了,如果想要改变编译的项目,点击下面的setcwd按钮选择要编译的项目,然后在点击make build、make clean或者make flash
避免了手动更改cwd目录,采用了点击选择编译的目录,这样方便了好多
完结撒花