在可通过下拉列表选择的特定文件/文件夹上运行 VS Code 任务

Jul*_*ian 5 customization task visual-studio-code

我试图弄清楚如何在可通过下拉列表选择的特定文件夹/文件上运行 VS Code 任务。

例子:

  1. 打开命令面板(Ctrl+Shift+P)
  2. 过滤“任务”-> 选择“任务:运行任务”
  3. 选择要运行的任务,例如 Cpplint
  4. 新部分:现在不应立即执行它,而是应该打开一个下拉列表来选择要运行任务的文件夹/文件,或者您可以选择“全部”,它会像以前一样工作并运行任务在所有文件夹上,从根(此处:src)文件夹开始。

结构:

ws
|   
+-- .env
|   |  
|   +-- ...
|    
+-- src
    |  
    +-- dir1
    |   |
    |   +-- file 1.1
    |   +-- ... 
    |   +-- file 1.n
    +-- dir2
    |   |
    |   +-- file 2.1
    |   +-- ... 
    |   +-- file 2.n 
    |   +-- dir2.2
    |       |
    |       +-- file 2.2.1
    |       +-- ...
    |       +-- file 2.2.n 
    +-- ...
    |
    +-- dirn
        |
        +-- file n.1 
        +-- ... 
Run Code Online (Sandbox Code Playgroud)

有办法让这项工作发挥作用吗?如果没有,是否有其他方法可以使类似的工作正常进行?感谢您的帮助。

进一步的问题:
我试图通过任务“Lint all”同时运行多个 linter。此任务取决于另外两项任务。为了避免并行执行,“Lint all”具有“dependsOrder”:“sequence”,但不知何故,下拉列表仅出现在第一个子任务中。此外,仅执行第一个子任务,然后执行停止。现在我有两个问题:

  1. 有没有办法获取每个子任务的下拉列表?
  2. 有没有办法只获取第一个子任务的下拉列表,然后接下来的子任务记住输入并自动执行?
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Lint all",
            "detail": "Run all linters",
            "dependsOrder": "sequence",
            "dependsOn":["Cpplint", "Pylint"],
            "problemMatcher": []
        },
        {
            "label": "Cpplint",
            "type": "shell",
            "command": "cpplint --recursive ${input:selectDir}",
            "problemMatcher": []
        },
        {
            "label": "Pylint",
            "type": "shell",
            "command": "pylint ${input:selectDir}",
            "problemMatcher": []
        }
    ],
    "inputs": [
        {
            "id": "selectDir",
            "type": "command",
            "command": "extension.commandvariable.pickStringRemember",
            "args": {
                "description": "Which directory to Lint?",
                "options": [
                    ["All", "src/"],
                    ["Rerun task", "${remember:toRemember}"],
                    ["Pick directory", "${pickFile:directory}"]
                ],
                "default": null,
                "pickFile": {
                    "directory": {
                        "description": "Which directory?",
                        "include": "src/**",
                        "showDirs": true,
                        "keyRemember":"toRemember"
                    }
                }
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

rio*_*oV8 4

您可以使用一个${input}变量

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "cpp lint",
      "type": "shell",
      "command": "cpplint ${input:selectDir}"
    }
  ],
  "inputs": [
    {
      "id": "selectDir",
      "type": "pickString",
      "description": "What directory to lint?",
      "options": [
        {"label": "All", "value": "" },
        "dir1",
        "dir2",
        "dir3"
      ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

编辑

使用扩展命令变量v1.35.0,您可以获得动态目录列表。

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "cpp lint",
      "type": "shell",
      "command": "cpplint ${input:selectDir}"
    }
  ],
  "inputs": [
    {
      "id": "selectDir",
      "type": "command",
      "command": "extension.commandvariable.pickStringRemember",
      "args": {
        "description": "Which directory to Lint for C++?",
        "options": [
          ["Use previous directory", "${remember:srcSubDir}"],
          ["All", "all"],
          ["Pick directory", "${pickFile:srcSubDir}"]
        ],
        "default": null,
        "pickFile": {
          "srcSubDir": {
            "description": "Which directory?",
            "include": "src/**/*.{cpp,h}",
            "showDirs": true,
            "keyRemember": "srcSubDir"
          }
        }
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

如果您选择一个src子目录,您将获得该目录的完整路径。我创建了一个问题来获取与工作空间相关的结果。您可以使用该extension.commandvariable.transform命令删除工作区文件夹部分。


编辑2

在命令变量 v1.35.1 中:

  • 修复了原始值在 中的存储pickStringRemember。现在存储变量替换后的值。
  • pickString修复了和UI的转义pickFile以中止任务。
  • 在示例中添加一个属性以pickFile将所选文件存储在某个名称下
  • 在示例中添加一个选项以pickString使用/记住前面的选项pickFile
  • default向示例中添加属性pickString以转义 UI