How to add multiple build tasks in Visual Studio Code? - visual-studio-code

I am trying to use Visual Studio Code to build a Haxe project. I would like to have two build commands, one to build with the -debug option, and one to build without it. Here is my tasks.json file code:
{
"version": "2.0.0",
"tasks":
[
{
"type": "lime",
"command": "test",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "build: flash",
"command": "haxelib",
"group": {
"kind": "build",
"isDefault": true
},
"args": [
"run",
"lime",
"build",
"flash",
],
"problemMatcher": [
"$haxe-absolute",
"$haxe",
"$haxe-error",
"$haxe-trace"
]
},
{
"label": "debug: flash",
"command": "haxelib",
"args": [
"run",
"lime",
"build",
"flash",
"-debug",
"-Dfdb"
],
"problemMatcher": [
"$haxe-absolute",
"$haxe",
"$haxe-error",
"$haxe-trace"
]
}
]
}
This gives me two tasks named "lime: test flash -debug" and "build: flash". The "lime: test flash -debug" command works fine, but when I run "build:flash" nothing happens. The haxelib command shows up in the terminal, but nothing pops up. I've read both the tasks documentation here and the tasks.json schema here and I still can't figure out how to do this. Can anyone tell me how to do this? Thanks.

Related

Run instead of debugging in Visual Studio Code

I would like to run my simple program in Visual Studio Code without debugging but all programs run in debugging mode.
I used CTRL + F5 (Run without debugging) but automatically go to debugging mode.
Could you please help me?
Tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\MinGW\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\\MinGW\\bin\\g++.exe"
}
]
}

How to write VS Code tasks that does not waits for process to finish?

I want to make a task which will build a project and open resulting pdf-file in pdf viewer:
"tasks": [
{
"label": "Build",
"type": "shell",
"command": "make",
},
{
"label": "Build and View",
"type": "process",
"command": "evince",
"args": [
"_build/document.pdf"
],
"dependsOn": ["Build"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
But it makes VS Code wait until pdf viewer process exists. Which is not what I want. In addition when running task again I get

Visual Studio Code: running preLaunchTask with multiple tasks

I am trying to figure out how to run multiple tasks at once in the prelaunchtask of the launch.json file.
My code in the tasks.json is as follows:
"version": "2.0.0",
"tasks": [
{
"label": "CleanUp_Client",
"type": "shell",
"command": "rm",
"args": [
"-f",
"Client"
],
},
{
"label": "Client_Build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"client.cpp",
"-o",
"Client",
"-lssl",
"-lcrypto"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$gcc"
}
]
In the launch.json for the preLaunchTask parameter if I only put the build task it works, however I want to run multiple tasks, in this case is the CleanUp_Client and Client_Build.
I tried adding another preLaunchTask - However it looks like you can only use that parameter once, so then I tried:
"preLaunchTask": "build" + "clean",
"preLaunchTask": "build"; "clean",
"preLaunchTask": "build" & "clean",
"preLaunchTask": "build" && "clean",
All with no success, not the correct syntax.
Also as a second part to this I would like to know how the group part of this works, and what it means for "isDefault": true.
For your reference: https://code.visualstudio.com/docs/editor/tasks
Here is something that will work. Basically you make another task in which you include all the other tasks that you want to run on your preLaunchTask with the dependsOn keyword.
Code for reference:
"tasks": [
{
"label": "CleanUp_Client",
"type": "shell",
"command": "rm",
"args": [
"-f",
"Client"
]
},
{
"label": "Client_Build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"client.cpp",
"-o",
"Client",
"-lssl",
"-lcrypto"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$gcc"
},
{
"label": "Build",
"dependsOn": [
"CleanUp_Client",
"Client_Build"
]
}
]
In this case you would set your preLaunchTask to "Build" and it will run both tasks.
I am curious if anybody else knows an alternative or the correct syntax to just run several tasks from the launch.json preLaunchTask
I agree with #Revx0r answer, but there is important notice: you need to add to last task dependsOrder field, if you want to run it in sequence:
{
"label": "Build",
"dependsOrder": "sequence",
"dependsOn": [
"CleanUp_Client",
"Client_Build"
]
}

How to chain tasks in Visual Studio Code using only tasks.json?

I have been ploughing through the documentation of Visual Studio Code to figure out how to add multiple consecutive tasks to the tasks.json file.
The tasks array only allows for creating different arguments to the same command. In this example the command is 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"]
}
]
}
Does tasks.json allow several tasks to be executed consecutively? For example, tsc followed by uglify?
The dependsOn feature was shipped in version 1.10.0. For example, I am using this to compile and run single file scripts in TypeScript:
{
"version": "2.0.0",
"tasks": [
{
"command": "tsc -p ${cwd}/2017-play",
"label": "tsc-compile",
"type": "shell"
},
{
"command": "node ${cwd}/2017-play/build/${fileBasenameNoExtension}.js",
"label": "node-exec",
"type": "shell",
"dependsOn": [
"tsc-compile"
],
"problemMatcher": []
}
]
}
Here is a working example that runs the tcs build and copies the source to another folder using a shell script.
This is based on various posts on StackOverflow and the documentation found here:
https://code.visualstudio.com/updates/v1_10#_more-work-on-terminal-runner
One could also make a tasks.json with two tasks with the second having a dependsOn on the first one as shown in Ben Creasy post, the two tasks would get executed when the second one is called. I needed to be able to execute one, the other or both. Many thanks to Ben, I had a hard time finding a solution before hitting this post.
BTW, when including a shell file, the commands are run with reference to the project folder, not the one where the script is located.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"problemMatcher": [
"$tsc"
],
"group": "build",
"identifier": "build"
},
{
"label": "Copy files",
"type": "shell",
"command": "./scripts/copysrc.sh",
"windows": {
"command": ".\\scripts\\copysrc.cmd"
},
"group": "build",
"presentation": {
"reveal": "always"
},
"problemMatcher": [],
"dependsOn": "build"
},
{
"label": "Build and copy",
"dependsOn": [
"build",
"Copy files"
],
"group": "build",
"problemMatcher": []
}
]
}

How do I run multiple vscode tasks simultaneously?

For example, running a typescript watch task & a gulp task at the same time. (Without using the terminal)
I use a tasks.json like this to run two watch tasks simultaneously:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Watch all",
"dependsOn": [
"Watch package 'core'",
"Watch package 'ui'"
],
"dependsOrder": "parallel",
"group": "build",
"problemMatcher": [
"$tsc-watch"
],
"isBackground": true
},
{
"label": "Watch package 'core'",
"type": "typescript",
"tsconfig": "packages/core/tsconfig.json",
"option": "watch",
"problemMatcher": [
"$tsc-watch"
],
"group": "build"
},
{
"label": "Watch package 'ui'",
"type": "typescript",
"tsconfig": "packages/ui/tsconfig.json",
"option": "watch",
"problemMatcher": [
"$tsc-watch"
],
"group": "build"
}
]
}
When you open the build menu in vscode then you can choose to run the two separate watch tasks or the "Watch all" task which runs the other two tasks.
I guess you can easily replace one of the watch tasks with your gulp task.
See running multiple tasks. You can use the "dependsOn" key in a version 2.0.0 tasks.json. Example from above link:
{
"version": "2.0.0",
"tasks": [
{
"taskName": "Client Build",
"command": "gulp",
"args": ["build"],
"isShellCommand": true,
"options": {
"cwd": "${workspaceRoot}/client"
}
},
{
"taskName": "Server Build",
"command": "gulp",
"args": ["build"],
"isShellCommand": true,
"options": {
"cwd": "${workspaceRoot}/server"
}
},
{
"taskName": "Build",
"dependsOn": ["Client Build", "Server Build"]
}
]
}
Apparently, this is still preliminary? and so difficult to find documentation unless I am just missing it. But I tested it and it works. It was added in vscode 1.10.
VS Code has a built in Task Runner that you can configure with multiple tasks.
In VS Code, type Ctrl+Shift+P and search for "Tasks: Configure Task Runner." A tasks.json file will be created. Here's some sample code showing how to configure multiple tasks.
{
"version": "0.1.0",
"tasks": [
{
"taskName": "tsc",
"command": "tsc",
"args": ["-w"],
"isShellCommand": true,
"isBackground": true,
"problemMatcher": "$tsc-watch"
},
{
"taskName": "build",
"command": "gulp",
"args": ["build"],
"isShellCommand": true
}
]
}
Run the task by pressing Ctrl+Shift+P and searching for "Task: Run task."
See more documentation at https://code.visualstudio.com/docs/editor/tasks.