Dotnet cli is a great tool to work with the dotnet core apps in Mac/Linux. But, if you feel you want to be more productive by running the common tasks like building the app, running the tests at the stroke of a few key combinations, then VS Code is the way to go. You can create custom tasks in VS Code and that can be triggered by setting your favorite key combinations. For e.g., every .net developer working out of Visual Studio in Windows would love the combo ctrl + shift + B to build the solution, and that continues to exist in VS Code as well. In this post, lets see how we can create build and test tasks in VSCode and execute them without leaving the editor.

Step 1 : Configure a task runner

To configure any custom tasks, Press the keys cmd + P and then type >Task this should get the dropdown of task option. Click Configure Task Runner . Refer screenshot below

Screen Shot 2016-07-06 at 9.11.40 PM

Step 2 : Edit the Tasks.json with appropriate instructions

Tasks.json is the file which maintains the task related instructions and that will be created within .vscode\ folder. Upon clicking the option above, you should see a pre-populated build task which would look like below


{
 // 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": "always",
 "problemMatcher": "$msCompile"
 }
 ]
}

VS Code executes the tasks in this order. command taskName args so in this case, when this task is triggered, it will execute dotnet build as there are no arguments specified.

Setting isBuildCommand  to true enables ctrl + shift + B key combination to trigger the build.

Setting showOutput to always toggles the output window in VS Code editor when the task gets executed. The other options are silent and never

problemMatchers guide VS Code in highlighting errors/warnings upon running the task. Since this is a dotnet build task, the $msCompile matcher is used. For any other JS task like linting, a different problem matcher will be used.

Step 3 : Executing the Build task

There are two ways to execute this build task. Like I mentioned in the above step, since this is a build command, we can trigger the task by pressing ctrl + shift + B.

The other option is to open the tasks runner by pressing cmd + p and type > task  You can either select Run Build Task option or Run Task which will in turn list all the tasks present in the tasks.json. Select the build and it will trigger the build.

Step 4: Adding a Test task

Similar to build task, we can create the json structure for the Test task as well. After adding the task, the Project.json would like the one below.


{
 // 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": ["-c","release" ],
 "isBuildCommand": true,
 "showOutput": "always",
 "problemMatcher": "$msCompile"
 },
 {
 "taskName": "test",
 "args": [],
 "isTestCommand": true,
 "showOutput": "always",
 "problemMatcher": "$msCompile"
 }
 ]
}

Note the subtle change in the segment, instead of isBuildCommand we have to set isTestCommandto true.This is essential to trigger the test directly from the Task options as visible in the first screenshot

Step 5 : Adding key bindings/shortcuts for test task

Though Run Test Task option is present directly under the taskoption, it will be still nicer to associate a key binding to trigger the task. To do this, Open the Keybindings.json file and add the below set of lines.

Step 5.1 Open Code -> Preferences ->Keyboard shortcuts 

Screen Shot 2016-07-06 at 9.52.12 PM

Step 5.2 Define Key Bindings

Clicking on the Define Keybinding at the right bottom, will give you the option to key in the combination you want to set. In this case, I have associated cmd + shift + t to run the tests. Then select the command to which you want to associate the key binding from the left pane.

Screen Shot 2016-07-06 at 9.54.29 PM

The finished keybindings.json would look like the one below:


[{
 "key": "shift+cmd+t",
 "command": "workbench.action.tasks.test",
 "when": "editorTextFocus"
}]// Place your key bindings in this file to overwrite the defaults

That’s it. Now, as we keep building the project bigger and better, as we add more tests, we can simply hit cmd + shift + B and cmd + shift + T

Similarly, we can go ahead and create tasks for other dotnet cli commands as well and trigger all of them from within VS Code itself.

Advertisements