Is there a way to track a build time in VSCode and record it to a text file - visual-studio-code

I need to record the build time of the project to find out the average build time in VSCode.
For example, this is how it can be implemented in Xcode https://github.com/timroesner/BuildTimes.
I have already tried "preLaunchTask", "postDebugTask" in launch.json, but it's not exactly what I need.

You can use Compound tasks.
The tasks to perform are:
Record the start
Build
Record the end
{
"version": "2.0.0",
"tasks": [
{
"label": "Start Build",
"type": "shell",
"command": "BuildTimes -start",
},
{
"label": "End Build",
"type": "shell",
"command": "BuildTimes -end",
},
{
"label": "List BuildTime",
"type": "shell",
"command": "BuildTimes -list",
},
{
"label": "Build",
"command": "gulp",
"args": ["build"],
"options": {
"cwd": "${workspaceFolder}/server"
}
},
{
"label": "Record Build Time",
"dependsOrder": "sequence",
"dependsOn": ["Start Build", "Build", "End Build"]
}
]
}
Modify the Build task to what you need.
In your launch.json set the preLaunchTask to Record Build Time

Related

How to use the same input string across multiple chained tasks in vscode?

I have multiple tasks that depend on each other and that should all operate on the same folder.
My config looks something like this:
{
"version": "2.0.0",
"tasks": [
{
"label": "first task",
"type": "shell",
"command": "bash",
"args": ["do stuff in ${input:pickFolder}"],
"dependsOn": "second task"
},
{
"label": "second task",
"type": "shell",
"command": "bash",
"args": ["also do stuff in ${input:pickFolder}"]
}
],
"inputs": [
{
"type": "pickString",
"id": "pickFolder",
"options": ["path/to/folder", "path/to/other/folder"]
}
]
}
As you might imagine I want both tasks run in the same folder. Also, I don`t want to have to pick the folder twice. How can I do that?
You can use the extension Command Variable v0.9.
Use the commands:
extension.commandvariable.pickStringRemember
extension.commandvariable.rememberPick

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

How to run multiple tasks in VS Code on build?

Using tasks.json version 2.0.0, I have not been able to make it so that, when I build my application, multiple tasks are run at the same time. I'm using gulp for my SCSS compilation, and running my Compile/minify cms.scss task on its own works fine, so it's not a problem with the task itself, just VS Code's task runner. When I Run Build Task in VS Code, my gulp task is not being run, even though it has "group": "build" — only the dotnet one is.
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/HpsCoreWeb.csproj"
],
"problemMatcher": "$msCompile",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "Compile/minify cms.scss",
"type": "gulp",
"task": "cms.scss:cms.min.css",
"problemMatcher": "$node-sass",
"group": "build"
}
]
}
According to the VS Code Tasks documentation:
group: Defines to which group the task belongs. In the example, it belongs to the test group. Tasks that belong to the test group can be executed by running Run Test Task from the Command Palette.
The dotnet build task is succeeding, so shouldn't the other task, which is also part of the build group, be run as well? What am I doing wrong?
The problem is that "Run Test Task" and "Run Build Task" do not execute all tasks in that specific group. Usually you get a drop down selection so you can choose which task to execute. Since you have specified one of the tasks as default, the selection will be skipped and instead the default task is executed.
You can work around that by adding dependencies. Take the following example:
{
"version": "2.0.0",
"tasks": [
{
"label": "Echo 1",
"command": "echo",
"type": "shell",
"args": [ "echo1" ],
"group": {
"kind": "build",
"isDefault": true
},
"dependsOn":["Echo 2"]
},
{
"label": "Echo 2",
"type": "shell",
"command": "echo",
"args": [ "echo2" ],
"group": "build"
}
]
}
As Echo 1 depends on Echo 2, Echo 2 will be executed prior to executing Echo 1. Note that the definition is a list, so more than one task can be specified. In that case the tasks are executed in parallel.
In your case adding "dependsOn":["Compile/minify cms.scss"] to your main build task should execute both tasks.
You can use Compound Tasks.
The example below executes "Client Build" and "Server Build" tasks when "Build" task is called.
{
"version": "2.0.0",
"tasks": [
{
"label": "Client Build",
"command": "gulp",
"args": ["build"],
"options": {
"cwd": "${workspaceFolder}/client"
}
},
{
"label": "Server Build",
"command": "gulp",
"args": ["build"],
"options": {
"cwd": "${workspaceFolder}/server"
}
},
{
"label": "Build",
"dependsOn": ["Client Build", "Server Build"]
}
]
}
Add 'gulp-load-plugins' plugin in package.json
var gulpLoadPlugins = require('gulp-load-plugins'),
plugins = gulpLoadPlugins();
To read more about that plugin, see How To Build And Develop Websites With Gulp
Double check your settings are enabled for Gulp auto-detection:
"gulp.autoDetect": "on"

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.