Search code examples
pythonvisual-studio-codepython-poetryvscode-tasks

Is there a way to get tasks in VSCode to automatically run when connecting to a new branch and not just when opening the project folder?


I've successfully automated the running of tasks (./.vscode/tasks.json) when I open the project folder. But is there a way to automatically run that script of tasks when connecting to a new branch? I'd like to update my Python virtual environment automatically each time I pull a version of the project from our branches.

Here's my current tasks.json ...

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Update pip",
            "type": "shell",
            "command": "python -m pip install --upgrade pip",
            "windows": {
                "command": "python -m pip install --upgrade pip"
            },
            "group": "build",
            "presentation": {
                "reveal": "always",
                "panel": "shared"
            },
            "runOptions": {
                "runOn": "folderOpen",
            }
        },
        {
            "label": "Update Poetry",
            "type": "shell",
            "command": "poetry self update",
            "windows": {
                "command": "poetry self update"
            },
            "group": "build",
            "presentation": {
                "reveal": "always",
                "panel": "shared"
            },
            "runOptions": {
                "runOn": "folderOpen",
            },
            "dependsOn": [
                "Update pip"
            ]
        },
        {
            "label": "Configure Poetry",
            "type": "shell",
            "command": "poetry config virtualenvs.in-project true",
            "windows": {
                "command": "poetry config virtualenvs.in-project true"
            },
            "group": "build",
            "presentation": {
                "reveal": "always",
                "panel": "shared"
            },
            "runOptions": {
                "runOn": "folderOpen",
            },
            "dependsOn": [
                "Update Poetry"
            ]
        },
        {
            "label": "Update Poetry Lock File",
            "type": "shell",
            "command": "poetry lock --no-update",
            "windows": {
                "command": "poetry lock --no-update"
            },
            "group": "build",
            "presentation": {
                "reveal": "always",
                "panel": "shared"
            },
            "runOptions": {
                "runOn": "folderOpen",
            },
            "dependsOn": [
                "Configure Poetry"
            ]
        },
        {
            "label": "Poetry Install",
            "type": "shell",
            "command": "poetry install --no-root",
            "windows": {
                "command": "poetry install --no-root"
            },
            "group": "build",
            "presentation": {
                "reveal": "always",
                "panel": "shared"
            },
            "runOptions": {
                "runOn": "folderOpen",
            },
            "dependsOn": [
                "Update Poetry Lock File"
            ]
        },
    ]
}

I've reviewed the Microsoft documentation that I could find about tasks in VSCode. Specifically focusing on the "runOptions" parameters that are allowed and can't find where it might allow this. I also know that you can manually run this task at any time. But I'm forgetful and may not always remember to keep things in sync as our .venv files do not travel with our project in version control.

Hoping that more experienced folks might have figured this out somehow or know of another tool/extension that can do this. Many thanks for any insights!


Solution

  • Not that I know of.

    If you're writing a VS Code extension, see Is there any command that gets triggered on change in git branch? (RepositoryState.onDidChange). Otherwise, git hooks sound appropriate for what you're looking for. For that, see also Is there a way to trigger a hook after a new branch has been checked out in Git?.