首页 > 解决方案 > 任务中的 VS CODE SaveAll

问题描述

我想从任务中执行SaveAll,例如从任务中调用workbench.action.files.saveAll

目标是确保在触发一系列其他任务之前保存所有文件。

tasks.json 看起来像这样

{   
    "version": "2.0.0",     
    "tasks": [
        {
             "command": "workbench.action.files.saveAll",
             "label": "SaveAllFiles",
         },
 
         {    
            "type": "grunt",            
             "task": "default",             
              "label": "Execute Grunt",
 
             "dependsOn": [
                 "SaveAllFiles"
             ],
             "problemMatcher": []
        }
   ] 
}

理论上,我只需要执行标记为 的任务Execute Grunt,它就会调用该SaveAllFiles任务。准确地说,我不想基于 Save 或 SaveAll 事件触发任务;有时我想保存文件,但不想触发其他任务。

标签: visual-studio-codevscode-tasks

解决方案


{
  "label": "SaveAllFiles",
  "command": "${command:workbench.action.files.saveAll}",
  "type": "shell",
  "problemMatcher": []
},

{    
  "type": "grunt",            
  "task": "default",             
  "label": "Execute Grunt",
 
  "dependsOn": [
    "SaveAllFiles"
  ],
  "dependsOrder": "sequence",   // need this, "parallel" is the default otherwise
  "problemMatcher": []
}

${command:workbench.action.files.saveAll}是调用vscode命令的通用形式,也可以通过这种方式调用扩展或宏命令。

这方面的信息在https://code.visualstudio.com/docs/editor/variables-reference#_command-variables中,这使得查找起来有点棘手,因为它不在Tasks文档中。


推荐阅读