Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get a pickString dynamically populated in a VS Code task?

Tags:

vscode-tasks

I'd like to provide a list of strings as a pickString for a task. The list of strings will be a list of folder names, which I can get from PowerShell, but I'm not sure how to display this list in a task.

How can I set up my task and input so this list can be populated?

{
  "version": "2.0.0", 
  "tasks": [
    {
      "label": "Test Task", 
      "type":  "shell", 
      "windows": {
        "command":  "echo",
          "args": [
            "-opt",
            "${input:optionsList}"
          ]
      }
    }
  ], 
  "inputs": [
    "id": "optionsList", 
    "type": "pickString", 
    "options": [<insert something here>]
  ]
}

I want the user to see the list of folders while the task is running.

like image 679
user1668958 Avatar asked Sep 17 '19 15:09

user1668958


People also ask

How do I auto compile in Visual Studio code?

Press Ctrl+Shift+B to open a list of tasks in VS Code and select tsc: watch - tsconfig. json . Done! Your project is recompiled on every file save.

What is workspaceFolder in VS Code?

${workspaceFolder} - the path of the folder opened in VS Code. ${workspaceFolderBasename} - the name of the folder opened in VS Code without any slashes (/) ${file} - the current opened file.

How do you show variables in VS Code?

Set a breakpoint in your code, and start debugging by pressing F5 or selecting Debug > Start Debugging. When paused at the breakpoint, hover over any variable in the current scope. A data tip appears, showing the name and current value of the variable.


1 Answers

Problem

Actually VSCode tasks doesn't have this functionality built-in. The enhancement request was closed probably because there is no plans to develop it in the near future.

Alternative Solution

You can install the augustocdias.tasks-shell-input and configure an input variable with type command calling this extension for populating the pick list.

Follow the following steps:

  1. Install the extension with:
    1. Search in the Extensions Sidebar (Ctrl+Shift+X) for augustocdias.tasks-shell-input and install or
    2. Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter:
    • ext install augustocdias.tasks-shell-input
  2. Configure your task.json with:
    1. A task with the command you want to execute
    2. Add to the task args configuration entry one or more references to input variables like ${input:my_variable}.
    3. Configure an entry in the inputs section with:
      • id: use the same variable name defined in task args like my_variable for ${input:my_variable}
      • type: command
      • command: shellCommand.execute
      • args: add an config with the properties: command, cwd, env
  3. See the example task.json file bellow.

Example

Example task.json file using augustocdias.tasks-shell-input extension:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Dynamically Populated Task",
            "type": "shell",
            "command": "echo",
            "args": [
                "'${input:my_dynamic_input_variable}'"
            ],
            "problemMatcher": []
        }
    ],
    "inputs": [
        {
            "id": "my_dynamic_input_variable",
            "type": "command",
            "command": "shellCommand.execute",
            "args": {
                "command": "ls -1 *.*",
                "cwd": "${workspaceFolder}",
                "env": {
                    "WORKSPACE": "${workspaceFolder[0]}",
                    "FILE": "${file}",
                    "PROJECT": "${workspaceFolderBasename}"
                }
            },
        }
    ]
}

like image 156
Juarez Rudsatz Avatar answered Sep 22 '22 13:09

Juarez Rudsatz