Product and service reviews are conducted independently by our editorial team, but we sometimes make money when you click on links. Learn more.
 

Setting up Visual Studio Code Tasks

By - Source: Toms IT Pro
Tags :

Coders need an IDE that can be a text editor and integrate various tools, and Visual Studio Code could be that for you.

Credit: ShutterstockCredit: ShutterstockWhen working with code, we typically don't just write code. There are lots of other tasks that go in with the act of writing the code. There are lots of other tools that we use on a daily basis to help us test that code, deploy the code and to write better code. These tools work with us hand-in-hand. Because we need these other tools, text editors such as notepad won't do. We need an actual integrated development environment (IDE) that can not only be a text editor but can integrate all of these disparate tools together. One of those IDEs is Visual Studio Code and can integrate these tools together via a concept called tasks. These tasks are executed via task runners.

Tasks can be a lot of things. They can be as simple as executing a script to as complex as invoking and interactively working with an entire build system. Tasks are found by going to the Command Pallette. Tasks can be filtered by simply typing 'task' where you'll be presented with options to work with tasks.

To begin building tasks in Visual Studio Code let's use an example. For this example, I'm just going to execute an external command to send something to Visual Studio Code's Output panel. To do this, I'll Configure a Task Runner and choose Others. You'll see when I do this a tasks.json file is open. This is where all of your tasks will be stored. You'll see that an existing task has already been created for you.

This task simply executes an echo shell command to return Hello World to the Output panel.

Once the JSON file is created, you can now run this task by opening up the Command Palette again, typing task but this time choosing the Run Task command. You will then get prompted to choose which task. Since the command name inside of our configured task is called echo, you'll see in a drop-down box an option called echo.

Run this task, and you'll immediately see the Output panel appear at the bottom of your window to display "Hello World."

You've just created your first Visual Studio task. Let's now use a better example. I write a lot of code in the PowerShell language. PowerShell code is typically tested with an open source product called Pester. Pester is a PowerShell function itself and to run a test consists of calling a function called Invoke-Pester followed by the path to the test script. Before Visual Studio Code tasks, I had to stop what I was doing, run the test and come back. It took too much time. But with Visual Studio Code tasks, I can integrate this process directly into my editor using a task.

Because tasks are stored in a single JSON file, tasks can be easily shared. Here's a tasks file that does everything you need to execute Pester tests in the folder in which the currently opened file is a part of.

// A task runner that calls pester for powershell testing.

{

    "version": "0.1.0",



   "windows": {

        "command": "powershell"

    },

   "osx": {

         "command": "/usr/local/microsoft/powershell/6.0.0-alpha.15/powershell"

   },



    // The command is a shell script

    "isShellCommand": true,



   // Show the output window only if unrecognized errors occur.

    "showOutput": "always",



    "suppressTaskName": true,

    "tasks": [

        {

            "taskName": "Test",

            "isTestCommand": true,

            "args": [

                "-Command", "Invoke-Pester -Path ${fileDirname}"

            ],

            "problemMatcher": {

               "owner": "pester",

                "pattern": {

                    "regexp": "(.*);(.*);(.*)",

                    "file": 1,

                    "line": 2,

                    "message": 3

                }

            }

        }

    ]

}

You'll see that I have a Mac and Windows. The tasks allow you to differentiate between the two. Also notice that the variable ${fileDirName}. Visual Studio Code allows you to use placeholders to then pass to various tools. In this instance, I'm passing the current folder path to the Invoke-Pester PowerShell command but this can be anything.

As you can see Visual Studio Code tasks are extremely flexible. Using a number of different techniques, you are able to integrate just about any external tool you need.

Comments