Is there a possibility, to add multiple shell commands in sequence to a Visual Studio Code task with separate arguments and options? I managed to execute multiple commands using && to chain then together to a single command, as you could in any Linux shell. But i guess, there has to be a better way to do this.
I use Visual Studio Code on Ubuntu 18.04 LTS.
Here is the example of how i currently chained the commands for a build task in a task.json file to build a c++ project using cmake:
{
"tasks": [
{
"type": "shell",
"label": "build",
"command": "cd ${workspaceFolder}/build && cmake .. && make clean && make",
"group": {
"kind": "build",
"isDefault": true
}
}
],
"version": "2.0.0"
}
Update: -------------------------------------------------------------------
I tried to use the dependsOn: property to start the 4 separatly defined tasks. However, this resulted in all 4 commands being executed at the same time in different shell instances instead of in sequence as needed:
{
"tasks": [
{
"type": "shell",
"label": "build project",
"dependsOn": [
"Go to build folder",
"Cmake",
"make clean",
"make",
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "Go to build folder",
"type": "shell",
"command": "cd ${workspaceFolder}/build",
"presentation": {
"group": "cmake-complile"
}
},
{
"label": "Cmake",
"type": "shell",
"command": "cmake ..",
"presentation": {
"group": "cmake-complile"
}
},
{
"label": "make clean",
"type": "shell",
"command": "make clean",
"presentation": {
"group": "cmake-complile"
}
},
{
"label": "make",
"type": "shell",
"command": "make",
"presentation": {
"group": "cmake-complile"
}
}
],
"version": "2.0.0"
}
Thanks to the many comments i found a solution which works well by setting "dependsOrder" to "sequence":
{
"tasks": [
{
"type": "shell",
"label": "build project",
"dependsOrder": "sequence",
"dependsOn": [
"Cmake",
"make clean",
"make",
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "Cmake",
"type": "shell",
"command": "cmake ..",
"options": {
"cwd": "${workspaceFolder}/build",
},
},
{
"label": "make clean",
"type": "shell",
"command": "make clean",
"options": {
"cwd": "${workspaceFolder}/build",
},
},
{
"label": "make",
"type": "shell",
"command": "make",
"options": {
"cwd": "${workspaceFolder}/build",
},
}
],
"version": "2.0.0"
}
Related
I'm trying to configure my project in Visual Studio Code.
I am using CMake+make to build my project. CMake needs to know the project configuration (Debug/Release) and architecture (x64/x86) before it can generate the make files.
I could define 4 tasks and 4 launch configurations to achieve this (each hardcoding both options), but that is a lot of duplication.
Instead, I would like to use environment variables to basically "pass down" the parameters from the launch configuration to a single task.
However, VScode doesn't seem to forward what I define in the launch configuration to the task.
Here is what I have:
// launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug (x64)",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/debug/x64/executable",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [
{
"name": "proj_configuration",
"value": "Debug"
},
{
"name": "proj_platform",
"value": "x64"
}
],
// "variables": {
// "proj_configuration": "Debug",
// "proj_platform": "x64"
// },
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Make Project",
}
]
}
// tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Build Project",
"type": "shell",
"command": "cmake",
"args": [
"-B${workspaceFolder}/build",
"-H${workspaceFolder}",
"-DCMAKE_BUILD_TYPE=${proj_configuration}",
"-DCMAKE_GENERATOR_PLATFORM=${proj_platform}"
],
"group": {
"kind": "build",
"isDefault": false
}
},
{
"label": "Make Project",
"type": "shell",
"command": "make",
"group": {
"kind": "build",
"isDefault": true
},
"dependsOn": ["Build Project"],
}
]
}
When I try to run my project, I get
Executing task: cmake -B/home/user/project/build -H/home/user/project/ -DCMAKE_BUILD_TYPE=${proj_configuration} -DCMAKE_GENERATOR_PLATFORM=${proj_platform}
As you can see, the 2 variables are not replaced.
I've also tried using the property "variables", but no luck using that either.
So, is it possible to do what I described, or do I have to define multiple tasks instead?
with the extension Command Variable you can save a number of strings in a store and use these in launch.json and task.json. See example in the link
I am a newbie with Hasicorp Consul. I want to add to the Hashicorp Consul (v.1.7.1) watches config file the second service to be watched. How should it be done correctly?
I've tried several variants, but none of them is working. In this case it warks for service-2, but ignores service-1.
{
"watches": [{
"type": "service",
"service": "service-1",
"tag": [ "tag1","tag2","tag3"],
"args": ["./script.sh"],
"type": "service",
"service": "service-2",
"tag": [ "tag1","tag2","tag3"],
"args": ["./script.sh"]
}]
}
Watches is a list of watch specifications, so each individual watch needs to be a separate dict/hash item in the array.
The following config snippet should successfully fire a watch for each specified service.
{
"watches": [
{
"type": "service",
"service": "service-1",
"tag": [
"tag1",
"tag2",
"tag3"
],
"args": [
"./script.sh"
]
},
{
"type": "service",
"service": "service-2",
"tag": [
"tag1",
"tag2",
"tag3"
],
"args": [
"./script.sh"
]
}
]
}
Im using VS code with c++ extesion, on my task.json i have a debug task and runwithoutdebug task, this tasks have common arguments and i instead of have to add those arguments in both, i was trying to reference a array, and write those common argument in that array.
Is that possible? is there any alternative
Example: --->task.json
"version": "2.0.0",
"tasks": [
{ ----------------------> TASK 1
"label": "compilewithoutDeb",
"type": "shell",
"command": "g++",
"args": [
"main.cpp",
"header.cpp",
"-o",
"main.exe"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{ ----------------------> TASK 2
"label": "compileDeb",
"type": "shell",
"command": "g++",
"args": [
"-g",
"main.cpp",
"header.cpp",
"-o",
"main.exe"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Has you can see i have on task "compilewithoutDeb" and "compileDeb" this common arguments
"main.cpp",
"header.cpp",
"-o",
"main.exe"
Is there a away that i could do like:
"paramArg":[
"main.cpp",
"header.cpp",
"-o",
"main.exe"
]
"tasks": [
{
"label": "compilewithoutDeb",
"type": "shell",
"command": "g++",
"args": [
"${paramArg}" <-----------------
],
"group": {
"kind": "build",
"isDefault": true
}
}
You can create an input. It would be a promptString with the arguments as the default. For your other task you'd add the "-g" parameter before the input.
"tasks": [
{
"label": "compilewithoutDeb",
"type": "shell",
"command": "g++",
"args": [
"${input:paramArg}"
],
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "compileDeb",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${input:paramArg}"
],
"group": {
"kind": "build",
"isDefault": true
}
},
],
"inputs": [
{
"id": "paramArg",
"description": "Just hit the Enter key",
"type": "promptString",
"default": "main.cpp header.cpp -o main.exe"
},
]
Yes, there's an (ugly) way to do this. While tasks.json can only define tasks (that is what is is for) it can reference different kind of variables.
The two options that would be reasonable to use: env (if you want to set it outside of vscode which doesn't seem to be the case here) or config.
To apply it to your sample:
settings.json (either in your user's settings or in the workspace [folder .vscode])
{
"personalSettings.commonArgs": [
"-o",
"main.exe",
"header.cpp",
"main.cpp"
]
}
and then your tasks.json would be
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{ // ----------------------> TASK 1
"label": "compilewithoutDeb",
"type": "shell",
"command": "g++ ${config:personalSettings.commonArgs}",
"group": {
"kind": "build",
"isDefault": true
}
},
{ // ----------------------> TASK 2
"label": "compileDeb",
"type": "shell",
"command": "g++ ${config:personalSettings.commonArgs}",
"args": [
"-g"
],
"group": "build"
}
]
}
(additional changes: only one build task can be the default, used inline comments)
Note: to get the most out of your build tasks I'd suggest to add a problem matcher to your task definition, something like
// use this or similar when cpptools or a similar libre/free extension
// is installed providing an appropriate matcher
// "problemMatcher": "$gcc"
// remark: ms-vscode.cpptools is "gratis", but comes with telemetry + usage-restrictions
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceFolder}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
Note: I'd suggest to not hard-wire the file names, instead use either a "general task" which uses ${file} or change the tool you run to make where you have all the arguments defined clean, can build outside of vscode, too and because of dependency tracking only compile what is actually necessary.
I have an existing code base that builds with a makefile and I'd like to use Visual Studio Code to run it.
There are my launch.json and tasks.json.
{
"version": "0.2.0",
"configurations": [{
"name": "C Launch (GDB)",
"type": "cppdbg",
"request": "launch",
"launchOptionType": "Local",
"targetArchitecture": "x64",
"cwd": "${workspaceRoot}",
"program": "E:/code/c",
"miDebuggerPath": "E:/Program Files/TDM-GCC-64/bin/gdb64.exe",
"args": [""],
"stopAtEntry": false,
"externalConsole": true,
"preLaunchTask": "gcc"
}]
}
{
"version": "0.1.0",
"command": "gcc",
"args": ["${file}", "-o", "${fileBasenameNoExtension}.exe", "-g3", "-Og", "-Wall", "-static-libgcc", "-std=c11", "-fexec-charset=GBK", "-finput-charset=UTF-8"],
"showOutput": "always",
"problemMatcher": {
"owner": "c",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
}
}
then when I run Hello world,then will have some problems.
Try changing your paths to use \ instead of / on Windows. Since you have to escape \ in json strings, the actual value would be:
"program": "E:\\code\\c\\${fileBasenameNoExtension}.exe"
provided the built executable is actually at E:\code\c
I'm trying to map keyboard shortcuts for SublimeREPL plugin commands. Looking at SublimeREPL it looks like a menu item command is defined as:
Default.sublime-commands
{
"caption": "SublimeREPL: SBT for opened folder",
"command": "run_existing_window_command", "args":
{
"id": "repl_sbt",
"file": "config/Scala/Main.sublime-menu"
}
}
or in
Main.sublime-menu
{"command": "repl_open",
"caption": "SBT for opened folder",
"id": "repl_sbt",
"mnemonic": "b",
"args": {
"type": "subprocess",
"encoding": "utf8",
"external_id": "scala",
"cmd": {"linux": ["sbt"],
"osx": ["sbt"],
"windows": ["sbt"]},
"soft_quit": "\nexit\n",
"cwd": "$folder",
"cmd_postfix": "\n",
"extend_env": {"osx": {"EMACS": "1", "PATH": "{PATH}:/usr/local/bin"},
"linux": {"EMACS": "1", "PATH": "{PATH}:/usr/local/bin"},
"windows": {"EMACS": "1"}},
"suppress_echo": false,
"syntax": "Packages/Scala/Scala.tmLanguage"
}
}
I've tried making a keybinding in my SublimeREPL.sublime-settings to:
[{ "keys": ["super+shift+k"], "command": "run_existing_window_command", "args":
{
"id": "repl_sbt",
"file": "config/Scala/Main.sublime-menu"
}
}]
But when I try to use it the Sublime console just says:
no command for selector: noop:
Same if I map it to:
[{ "keys": ["super+shift+k"], "command": "repl_open",
"args": {
"type": "subprocess",
"encoding": "utf8",
"external_id": "scala",
"cmd": {"linux": ["sbt"],
"osx": ["sbt"],
"windows": ["sbt"]},
"soft_quit": "\nexit\n",
"cwd": "$folder",
"cmd_postfix": "\n",
"extend_env": {"osx": {"EMACS": "1", "PATH": "{PATH}:/usr/local/bin"},
"linux": {"EMACS": "1", "PATH": "{PATH}:/usr/local/bin"},
"windows": {"EMACS": "1"}},
"suppress_echo": false,
"syntax": "Packages/Scala/Scala.tmLanguage"
}
}]
Your first keybinding is correct and should work as expected. Place is in Preferences -> Key Bindings - User file.
[{ "keys": ["super+shift+k"], "command": "run_existing_window_command", "args":
{
"id": "repl_sbt",
"file": "config/Scala/Main.sublime-menu"
}
}]
based on your description, I suspect that some other command is hijacking super+shift+k.
>>> sublime.log_commands(True)
will let you see what's get called when.