Skip to content

Generate VSCode Tasks

Roman Kuzmin edited this page Jun 3, 2016 · 13 revisions

The script New-VSCodeTask.ps1 generates VSCode tasks bound to a specified or default build script and Invoke-Build.ps1 as the task runner.

The default task becomes a so called VSCode build task which is invoked by [Ctrl-Shift-B]. The default task is "." if it exists, otherwise it is the first task.

In order to invoke another task from VSCode use [Ctrl-P] and type task. Then type a task name or select it from the opened list of all tasks. Or bind a key to "command": "workbench.action.tasks.runTask", see File \ Preferences \ Keyboard Shortcuts.

Only tasks with certain names are included. They contain alphanumeric characters, '_', '.', and '-', with the first character other than '-'.

In order to invoke some tasks in a console host outside VSCode specify the tag #ConsoleHost in a comment preceding the task definition. Note that all parent tasks in the task trees get this tag automatically.


Handy output links

Build errors are shown in the VSCode output window with links to the problem and related task locations. Use [Ctrl-Click] to open the source.

This output window feature can be used for something else. For example this function in a build script outputs the name and location of every invoked task.

function Enter-BuildTask {
    "INFO: Task $($Task.Name) - $($Task.InvocationInfo.ScriptName):$($Task.InvocationInfo.ScriptLineNumber)"
}

How to use the script

Step 1. Change the current location to the VSCode workspace directory. The new files to be created are tasks.cmd and tasks.json in the directory .vscode. The existing files are replaced.

Step 2. Invoke New-VSCodeTask.ps1.

If the default build script is in the workspace directory, Invoke-Build.ps1 is in the workspace or subdirectory or in the path then parameters are not needed:

New-VSCodeTask

Otherwise specify the parameter BuildFile or InvokeBuild or both. If the workspace directory is not going to be moved then absolute paths may be used. Otherwise paths relative to the workspace should be be used. For example:

New-VSCodeTask .\Scripts\Build.ps1 .\Tools\Invoke-Build\Invoke-Build.ps1

Note that in the above example the second argument makes sense only if there are several versions of Invoke-Build.ps1 in the workspace for some reasons. Otherwise Invoke-Build.ps1 will be discovered, the argument is not needed.

NOTES

When you generate VSCode tasks make sure you do not have anything important in tasks.cmd and tasks.json in .vscode. The files are going to be replaced.

Do not edit the generated files. Modify the build script instead. VSCode tasks do not have to regenerated on every change. This is needed only when tasks are added, removed, renamed, reordered, their tags change, or the Invoke-Build.ps1 location in the workspace changes.