VSCode generate a task.json entry when choosing a task
Asked Answered
P

1

6

I want to create a VSCode extension that generate a shell task, like shell task with additional generic properties.

Example:

  1. When user choose a task (my specific task that the extension is providing )

  2. A task.json is generated automatically like


   "tasks": [
       {
         "label": "mytask",
         "type": "shell",
         "group": {
           "kind": "build", 
           "isDefault": true
         },
         "command": "Invoke-Build -Task . -File './test'"
    },


I try to VSCode samples repo however I didn't manage to do it, The file is generated just if you click ctrl+shift+p and click on configure task button , which creates for you the tasks json. I want it be automatically with my json entry above. how I can do that ?

In the following link there is several of option that you can providing a task (manually...I need it generated from extension) https://code.visualstudio.com/docs/editor/tasks

I basically need the code which generate a new task in task.json file

If there is a reference where the code of creating a new tasks.json file maybe it can give a idea

https://github.com/microsoft/vscode/search?p=2&q=tasks.json&unscoped_q=tasks.json

There is another option which is to check if there is a tasks.json file , if not generate new one and add a new entry to the tasks array,(using my code not vscode api ) , but is it a good solution ?

Papal answered 24/3, 2020 at 11:4 Comment(0)
M
0

if yours tasks are shell commands or anything Can be run on terminal you do not need to create tasks.json you can just send the command to the terminal by executing

vscode.commands.executeCommand('workbench.action.terminal.new');
setTimeout(function () {
    const terminal = vscode.window.activeTerminal;
    terminal.sendText('you command here')
}, 1200);

if you really want to create some sort of file to run your tasks you can create custom json file mySuperTasks.json in the root directory in the user project and tell va code that he should auto execute Task auto-detection function on you file in this way you don't have to worry about the task.json file

in your package.json you will have somthing like

"contributes"
  "commands": [
        {
            "command": "extension.yourExtensionName.makeTaksFile",
            "title": "create mySuperTasks.json",
            "category": "your extention name"
        }
    ],
    "keybindings": [
        {
            "command": "extension.yourExtensionName.makeTaksFile",
            "key": "alt+L alt+C",
            "mac": "cmd+L cmd+C"
        }]

in you //extension-main-script ex: extinsion.js you will have

    let makeTaksFile = vscode.commands.registerCommand('extension.yourExtensionName.createTasksFile', function () {
    const rootFolder = vscode.workspace.getWorkspaceFolder();
    fs.writeFile(rootFolder.uri + '/mystasks.json', 'file content your json', (callback) => { });
    //then you create taskProvider via vs code api 
    // doc https://code.visualstudio.com/api/extension-guides/task-provider
});
context.subscriptions.push('makeTaksFile');

if that very important to create tasks.json file the option that you mentioned it is very helpful in this case. Hop that helps you Happy coding. I will add some code

Midshipmite answered 4/4, 2020 at 9:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.