augustocdias / vscode-shell-command

A task helper to use system commands as input
MIT License
53 stars 20 forks source link

Tasks: Shell Input

This extension aims to extend the possibilities of input in task execution. Currently, VSCode supports 3 types of inputs for your tasks:

None of them allows to get an input from a system command for example. This extension executes a shell command in your OS and each line of the output will be used as a possible input for your task.

Usage example:

Extension Demo

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Echo Project File",
      "type": "shell",
      "command": "echo ${input:inputTest}",
      "problemMatcher": []
    }
  ],
  "inputs": [
    {
      "id": "inputTest",
      "type": "command",
      "command": "shellCommand.execute",
      "args": {
          "command": "cat ${file}",
          "cwd": "${workspaceFolder}",
          "env": {
              "WORKSPACE": "${workspaceFolder[0]}",
              "FILE": "${file}",
              "PROJECT": "${workspaceFolderBasename}"
          }
      }
    }
  ]
}

By default the extension returns the exact string value that was produced by the shell command and then shown and selected in 'Quick Pick' dialog. However, sometimes it is useful to show more descriptive information than the internal string value that is returned. This can be done by specifying a fieldSeparator and making the shell command return lines containing multiple fields separated by that value. Supported fields are:

<value>|<label>|<description>|<detail>

Here, <value> is what is returned as input variable and is not shown in the UI. Instead, <label> and <description> are shown on a single line and <detail> is rendered on a separate line. These fields can also include icons such as $(git-merge). All fields except <value> are optional and can be omitted. fieldSeparator can be any string value.

Next example shows a process picker very similar to the built-in ${command:pickProcess}:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Echo Process ID",
      "type": "shell",
      "command": "echo ${input:processID}",
      "problemMatcher": []
    }
  ],
  "inputs": [
    {
      "id": "processID",
      "type": "command",
      "command": "shellCommand.execute",
      "args": {
          "command": "ps axww --no-headers k comm -o '%p|%c|%p|%a' | sed -e 's/^\\s*//' -e 's/\\s*|\\s*/|/g'",
          "fieldSeparator": "|",
          "description": "Select the process to attach to"
      }
    }
  ]
}

VSCode renders it like this:

Process Picker

Arguments for the extension:

As of today, the extension supports variable substitution for:

For a complete vscode variables documentation please refer to vscode variables.

Dependent Input Variables Usage example:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Nested input",
            "command": "ls ${input:rootDir}/${input:childDir}",
            "type": "shell",
            "problemMatcher": []
        }
    ],
    "inputs": [
        {
            "id": "rootDir",
            "type": "command",
            "command": "shellCommand.execute",
            "args": {
                "command": "ls -1a"
            }
        },
        {
            "id": "childDir",
            "type": "command",
            "command": "shellCommand.execute",
            "args": {
                "command": "ls -1a ${input:rootDir}"
            }
        }
    ]
}

Example with commandArgs:

{
    "tasks": {
        "version": "2.0.0",
        "tasks": [
            {
                "label": "Example with commandArgs",
                "command": "echo ${input:testInput}",
                "type": "shell"
            }
        ],
        "inputs": [
            {
                "id": "testInput",
                "type": "command",
                "command": "shellCommand.execute",
                "args": {
                    "command": "C:\\Program Files\\CMake\\bin\\cmake.exe",
                    "commandArgs": ["-E", "cat", "test-environment"]
                }
            }
        ]
    }
}

There are a few limitations to be aware of:

Developing and contributing

Please see ./CONTRIBUTING.md for documentation on developing this extension.

Misc

Icon created by Eucalyp - Flaticon