Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VScode: Defining own variables in tasks.json

I've set up a tasks.json file for building a project on multiple platforms. All platforms see the same content of the project repository. This is done either via disk sharing, because of running another platform in a VM, or via sync with the Git repository. So far so good, they all see the same task.json. However some command lines are rather long and those long lines are identical for most part. for example:

"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 are there for the different platforms. The configure part is always the same for the different platforms, so it would be nice to factor out this common part. Thus the question is if it is possible to define your own variables, so you can use them similar to ${workspaceRoot}.

Thus 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, then the tasks.json file needs only editing at one place, instead of many.

Perhaps it is already possible but I'm unable to find out how. I tried to do something with the declares block, but that seems to be hard tied to problemMatcher. You can find some examples, but I could not find clear documentation of of the elements of the tasks.json file and how they interact.

Perhaps I'm missing something, please educate me!

like image 592
Jimmy Venema Avatar asked Jun 01 '17 09:06

Jimmy Venema


People also ask

How do you change environment variables in VS Code?

If you open the . vscode/launch. json file in your workspace or select Debug > Open Configurations then you should see a set of launch configurations for debugging your code. You can then add to it an env property with a dictionary of string:string.

How do I create an .ENV file in Visual Studio Code?

Once you have opened the folder, click on the Explorer icon on the top left corner of the VSCode (or press Ctrl+Shift+E) to open the explorer panel. In the explorer panel, click on the New File button as shown in the following screenshot: Then simply type in the new file name . env ...

How do you input values in VS Code?

Go to settings (ctrl+,) -> Search settings -> : Code-runner : Run in terminal - Check this and you will be able to run the code directly in the terminal which takes input. :) Show activity on this post.


2 Answers

The other answer won't work because, at least on windows, the shell will not substitute environment variables given as arguments. ${env:...} variables as suggested in a comment on that answer won't be substituted using environment variables set in tasks.json itself, only preexisting ones. You can however add custom settings in settings.json, and reference those in tasks.json using ${config:...}.

e.g. settings.json:

{
    "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"
}

in tasks.json:

{
    "tasks": [
        {
            "taskName": "configure",
            "command": "bash",
            "windows": {
                "args": ["-c", "rm -rf ${config:win_dir}; mkdir ${config:win_dir}; cd ${config:win_dir}; ${config:configure}"]
            },
            "linux": {
                "args": ["-c", "rm -rf ${config:linux_dir}; mkdir ${config:linux_dir}; cd ${config:linux_dir}; ${config:configure}"]
            },
            "osx": {
                "args": ["-c", "rm -rf ${config:osx_dir}; mkdir ${config:osx_dir}; cd ${config:osx_dir}; ${config:configure}"]
            },
            "isBuildCommand": true,
            "problemMatcher": "$make-compile"
        },
        // ... other tasks using the variables
    ]
}
like image 192
patstew Avatar answered Sep 28 '22 10:09

patstew


Thus the question is if it is possible to define your own variables, so you can use them similar to ${workspaceRoot}.

You could 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 that, you could then also use the environment matching to refer to the relevant environment variables.

like image 25
Adam Parkin Avatar answered Sep 28 '22 11:09

Adam Parkin