Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
908 views
in Technique[技术] by (71.8m points)

visual studio code - Using a shell command as VSCode task variable value

I am trying to define a VSCode task in tasks.json that would adapt to the specific architecture where VSCode runs. To do this, I want to get the architecture as uname --m (e.g. "aarch64" or "amd64"). My goal is to interpolate the output of uname into an environment variable like so

"version": "2.0.0",
"tasks": [
    {
        "label": "build",
        "type": "shell",
        "command": "cmake",
        "args": [
            "-DMYLIB_INCLUDE_DIR=$MYLIB/include",
            "-DMYLIB_LIBRARY=$MYLIB/lib"
        ],
        "options": {
            "env": {
                "MYLIB": "${workspaceFolder}/mylib/${command:get_arch}"
            }
        },
    }
]

In my case, I will have architecture-specific versions of mylib under mylib/aarch64, mylib/amd64, etc.

My attempt so far as been to define a second get_arch task used in the environment definition of MYLIB, that simply runs uname.

{
    "label": "get_arch",
    "type": "shell",
    "command": "uname --m"
}

Of course, this task is not a proper command and so it isn't detected by VSCode and my build task fails. I checked out the documentation on variable substition, but they don't mention if it's possible to substitute a shell command. I guess this would be possible from within an extension, but I want to keep things as simple as possible.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This extension provides a way to launch arbitrary shell commands as a VS Code command:

"tasks": [
    {
        "label": "test_arch",
        "type": "shell",
        "command": "echo",
        "args": [
            "${MYARCH}"
        ],
        "options": {
            "env": {
                "MYARCH": "${input:get_arch}"
            }
        },
        "problemMatcher": []
    },
],
"inputs": [
    {
        "id": "get_arch",
        "type": "command",
        "command": "shellCommand.execute",
        "args": {
            "command": "uname -m"
        }
    }
]

One disadvantage I discovered is that you have to press Enter once more when the result of the command is prompted in picker. Aside of this, this is the straightest way to implement what you want, and yet it can be utilized in many similar situations.

Alternatively, you can add pickString input with arch picker or create an extension that adds only single command GetArch.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...