运行 特定 file/folder 上的 VS 代码任务可通过下拉列表选择

Run VS Code task on specific file/folder selectable via a drop-down list

我正在尝试弄清楚如何 运行 在特定 folder/file 上执行 VS Code 任务,该任务 select 可以通过下拉列表进行。

示例:

  1. 打开命令面板 (Ctrl+Shift+P)
  2. 过滤 'tasks' -> select 'Tasks: Run Tasks'
  3. Select 运行 的任务,例如Cpplint
  4. 新部分:现在不是立即执行它,而是应该打开一个下拉列表 select 你想要 运行 任务的 folder/file 或者你可以 select 'all' 并且它像以前一样工作并且任务是 运行 在所有文件夹上,从根(此处: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 
        +-- ... 

有没有办法让它工作? 如果没有,是否有另一种方法可以使类似的工作起作用?感谢您的帮助。

更多问题:
我试图通过任务“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"
                    }
                }
            }
        }
    ]
}

您可以使用 ${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"
      ]
    }
  ]
}

编辑

使用扩展名 Command Variable 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"
          }
        }
      }
    }
  ]
}

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


编辑 2

在命令变量 v1.35.1 中:

  • 修复了 pickStringRemember 中原始值的存储。现在存储变量替换后的值。
  • 修复了 pickStringpickFile UI 的转义以中止任务。
  • 在示例中添加一个 属性 到 pickFile 以将选择的文件存储在一个名称下
  • pickString到use/remember之前的pickFile
  • 示例中添加一个选项
  • pickString 中的示例中添加 default 属性 以转义 UI