Define multiple tasks in VSCode

后端 未结 13 1978
旧时难觅i
旧时难觅i 2020-11-28 05:40

I have seen that it is possible to define a task in the VSCode. But I am not sure how to define multiple tasks in the tasks.json file.

相关标签:
13条回答
  • 2020-11-28 06:04

    Try this

    {
        "version": "0.1.0",
        "command": "cmd",
        "isShellCommand": true,
        "args": ["/C"],
        "tasks": [
            {
                "taskName": "install",
                "args": ["npm install"]
            },
            {
                "taskName": "build",
                "args": ["gulp build"],
                "isBuildCommand": true,
                "problemMatcher": "$gulp-tsc"
            }
        ]
    }
    
    0 讨论(0)
  • 2020-11-28 06:08

    This functionality was added in Visual Studio Code v1.9 (January 2017). Example and text come from the release notes:

    {
      "version": "0.1.0",
      "tasks": [
        {
          "taskName": "tsc",
          "command": "tsc",
          "args": ["-w"],
          "isShellCommand": true,
          "isBackground": true,
          "problemMatcher": "$tsc-watch"
        },
        {
          "taskName": "build",
          "command": "gulp",
          "args": ["build"],
          "isShellCommand": true
        }
      ]
    }
    

    Commands per task

    You can now define different commands per task (#981). This allows running different commands for different tasks without writing your own shell script. A tasks.json file using commands per task looks like [the above.]

    0 讨论(0)
  • 2020-11-28 06:09

    What helped me understand this better is the sequence of arguments passed to the command. It may be obvious to some but it is not clear in the documentation.

    Omitting some fields to only focus on the command being sent:

    { "command": "myCommand"
      "args": ["myCommandArguments"],
      "tasks" : [ 
        { "taskName": "myTask",
          "args": ["myTaskArguments"],
          "suppressTaskName": false,
        }
      ]
    }
    

    The above definition will result in the following command:

    myCommand myCommandArguments myTaskArguments myTask
    

    The task name myTask is always last. Since version 0.4 it can be omitted with "suppressTaskName": true.

    0 讨论(0)
  • 2020-11-28 06:09

    This works for me...

    I know there is a lot of different answers here but my approach was a little bit different again so I thought I would add my 2 pence worth.

    I am on windows and use an external batch file to run my commands. It’s similar to Jonathan's answer above but I don’t pipe any commands to it which means my “tasks.json” file is different.

    I might change this approach over time (for example I haven’t got around to playing with gulp just yet) but this method is working perfectly fine for me at the moment.

    I am using handlebars for html templating, babel so I can use ES6 code and a code linter to pick up errors. At the end, the batch file launches a browser with my start page (index.html)

    Here is my batch file named run_tasks.bat:

    @ECHO OFF
    @ECHO Startz!
    @ECHO Running Handlebars!
    
    call handlebars html_templates -e html -f dist/html_templates.js
    
    @ECHO Linting ES6 code
    
    call eslint -c eslint.json src
    
    @ECHO Running Babel ES6 to ES5
    
    call babel src --out-dir dist --source-maps
    
    @ECHO Now startzing page up in browser!
    index.html
    
    @ECHO Donezz it!
    

    And here is my tasks.json file:

    { 
        "version": "0.1.0",
        "command": "${workspaceRoot}/run_tasks.bat",
        "isShellCommand": true,
        "isWatching": true,
        "showOutput": "always",
    
        "args": [],
    
        "tasks": [
            {
                "taskName": "build",
                "isBuildCommand": true,
                "isWatching": true,
                "showOutput": "always"
            }
    }
    

    Then, in VSCode I press “CTRL + SHIFT + B” to run my batch file.

    0 讨论(0)
  • 2020-11-28 06:12

    I use the following tasks.json file to run multiple TypeScript build scenarios. I put a tsconfig.json file in each folder, so that allows me to tune each folder's output individually. Just make sure you suppress the task name, because it tries to put it in the command string.

    {
        "version": "0.1.0",
        "command": "tsc",
        "showOutput": "always",
        "isShellCommand": true,
        "args": [],
        "windows": {
            "command": "tsc",
            "showOutput": "always",
            "isShellCommand": true
        },
        "tasks": [
            {
                "taskName": "Build the examples",
                "suppressTaskName": true,
                "isBuildCommand": false,            
                "args": ["-p", "./source/examples", "--outDir", "./script/examples"],
                "problemMatcher": "$tsc"
            },
            {
                "taskName": "Build the solution",            
                "suppressTaskName": true,
                "isBuildCommand": false,        
                "args": ["-p", "./source/solution", "--outDir", "./script/solution"],
                "problemMatcher": "$tsc"
            }   
        ]
    }
    

    This is what the folder structure looks like, where /script is the output root and /source is the input root. Both folders reference type declarations in the /typingd folder and /typings folder. TypeScript is somewhat limited to using relative paths in external references, so it helps simplify things if these folder structures are similar.

    Oh yea, it makes it easier to launch them selectively if you mark them as non build and override the build key to select a specific task from a list, like so..

    // Place your key bindings in this file to overwrite the defaults
    [
        { "key": "ctrl+shift+b", "command": "workbench.action.tasks.runTask" }
    ]
    

    Update: You can always just go entirely rogue, if you want. There may be better ways to handle the args, but this works for me under OSX at the moment.

    {
      "version": "0.1.0",
      "isShellCommand": true,
      "linux": { "command": "sh", "args": ["-c"] },
      "osx": { "command": "sh", "args": ["-c"] },
      "windows": { "command": "powershell", "args": ["-Command"] },
      "tasks": [
        {
          "taskName": "build-models",
          "args": ["gulp build-models"],
          "suppressTaskName": true,
          "isBuildCommand": false,
          "isTestCommand": false
        },
        {
          "taskName": "run tests",
          "args": ["mocha ${workspaceRoot}/test"],
          "suppressTaskName": true,
          "isBuildCommand": false,
          "isTestCommand": false
        }
      ]
    }
    
    0 讨论(0)
  • 2020-11-28 06:15

    Just in case it helps someone... . If you don't have/want gulp/grunt/etc... or an extra shell script to proxy out your task commands , "npm run" is there already .

    this is for webpack and mocha as in "Build and Test" , Shift+Ctrl+B, Shift+Ctrl+T

    .vscode/tasks.json:

    {
      "name": "npmTask",
      //...
      "suppressTaskName": true,
      "command": "npm",
      "isShellCommand": true,
      "args": [
        "run"
      ],
      "tasks": [
        {
          //Build Task
          "taskName": "webpack",
          //Run On Shift+Ctrl+B
          "isBuildCommand": true,
          //Don't run when Shift+Ctrl+T
          "isTestCommand": false,
          // Show the output window if error any
          "showOutput": "silent",
          //Npm Task Name
          "args": [
            "webpack"
          ],
          // use 2 regex:
          // 1st the file, then the problem       
          "problemMatcher": {
            "owner": "webpack",
            "severity": "error",
            "fileLocation": "relative",
            "pattern": [
              {
                "regexp": "ERROR in (.*)",
                "file": 1
              },
              {
                "regexp": "\\((\\d+),(\\d+)\\):(.*)",
                "line": 1,
                "column": 2,
                "message": 3
              }
            ]
          }
        },
        {
          //Test Task   
          "taskName": "mocha",
          // Don't run on Shift+Ctrl+B
          "isBuildCommand": false,
          // Run on Shift+Ctrl+T
          "isTestCommand": true,
          "showOutput": "always",
          "args": [
            "mocha"
          ]
        }
      ]
    }
    

    package.json:

    {
      ...
      "scripts": {
        "webpack": "webpack",
        "mocha": "/usr/bin/mocha"
      },
      ...
    }
    
    0 讨论(0)
提交回复
热议问题