VScode: defining your own variables in tasks.json

I created a tasks.json file to build a project on multiple platforms. All platforms see the same contents of the project repository. This is done either through disk sharing, or by running a different platform in a virtual machine, or by syncing with a Git repository. So far so good, they all see the same challenge. However, some command lines are quite long, and these large lines are identical for the most part. eg:

"rm -rf build; mkdir build; cd build; ../configure --with-bash-malloc=no CFLAGS=\"-O3 -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free\" LDFLAGS=-L/usr/local/lib LIBS=\"-ltcmalloc -lcurl\" CC=clang

Similar lines exist for different platforms. The configuration part is always the same for different platforms, so it would be nice to see this common part. So the question is whether it is possible to define your own variables, so you can use them similar to ${workspaceRoot}

.

So define somewhere

"win_dir": "build_windows",
"linux_dir": "build",
"osx_dir": "build_osx",
"configure": "../configure --with-bash-malloc=no CFLAGS=\"-O3 -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free\" LDFLAGS=-L/usr/local/lib LIBS=\"-ltcmalloc -lcurl\" CC=clang"

      

And then write

"tasks": [
    {
        "taskName": "configure",
        "command": "bash",
        "windows": {
            "args": ["-c", "rm -rf ${win_dir}; mkdir ${win_dir}; cd ${win_dir}; ${configure}"]
        },
        "linux": {
            "args": ["-c", "rm -rf ${linux_dir}; mkdir ${linux_dir}; cd ${linux_dir}; ${configure}"]
        },
        "osx": {
            "args": ["-c", "rm -rf ${osx_dir}; mkdir ${osx_dir}; cd ${osx_dir}; ${configure}"]
        },
        "isBuildCommand": true,
        "problemMatcher": "$make-compile"
    },
    ... others tasks using the variables

      

When making changes to the build directory or arguments passed to configure, etc., the tasks.json file only needs to be edited in one place, not many.

It might already be possible, but I can't seem to find out how. I tried to do something with the declares block, but that seems to be tied hard to problemMatcher. You can find some examples, but I couldn't find clear documentation about the elements of the tasks.json file and how they interact.

Perhaps I am missing something, please enlighten me!

+3


source to share


1 answer


So the question is whether you can define your own variables, so you can use them similarly to $ {workspaceRoot}.

You can define environment variables in your tasks. json:



{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "options": { "env": { "win_dir": "build_windows", "linux_dir": "build", "osx_dir": "build_osx", "configure": "../configure --with-bash-malloc=no CFLAGS=\"-O3 -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free\" LDFLAGS=-L/usr/local/lib LIBS=\"-ltcmalloc -lcurl\" CC=clang" } }, "tasks": [ { "label": "Example", "type": "shell", "command": "echo win_dir is $win_dir" }, ] }

With this, you can also use environment mapping to reference the appropriate environment variables.

+1


source







All Articles