Home > Software engineering >  How to perform operations on VSCode macros?
How to perform operations on VSCode macros?

Time:11-28

My launch.json specifies a list of arguments for a given debug session (For airflow debugging in this case):

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "airflow_debug",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/.venv/bin/airflow",
            "preLaunchTask": "activate-python-venv",
            "console": "integratedTerminal",
            "env": {
                "AIRFLOW_HOME": "${workspaceFolder}",
                "AIRFLOW__CORE__LOAD_EXAMPLES": "False",
                "AIRFLOW__CORE__DAGS_FOLDER": "${workspaceFolder}/dags",
                "AIRFLOW__CORE__EXECUTOR": "SequentialExecutor",
                "PYTHONPATH": "${workspaceFolder}/plugins:${workspaceFolder}/dags:${env:PYTHONPATH}"
            },
            "args": [
                "dags",
                "test",
                "migrate_dtm",
                "2022-11-24"
            ]
        }
    ]
}

In order to be able to debug my dags, I must specify the dag name in the args, here it is migrate_dtm. A more sophisticated way, would be to deduce the name of the dag from the name of the file My file's name is migrate_dtm_dag.py.

The goal here is not to change the launch.json and manually specify the dag name within every different debug launch; but only set a nomenclature rule for the devs to follow.

Something like this would help

"args":[
        ...
        "${file.split('/')[-1].split('_dag')'}",  // First split to get only the file name without the path, second split to extract the dag name.
        ...
]

Is there a way to achieve something like this in a VSCode macro? Or to perform operations on macros at all?

CodePudding user response:

you can use the extension Command Variable

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "airflow_debug",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/.venv/bin/airflow",
            "preLaunchTask": "activate-python-venv",
            "console": "integratedTerminal",
            "env": {
                "AIRFLOW_HOME": "${workspaceFolder}",
                "AIRFLOW__CORE__LOAD_EXAMPLES": "False",
                "AIRFLOW__CORE__DAGS_FOLDER": "${workspaceFolder}/dags",
                "AIRFLOW__CORE__EXECUTOR": "SequentialExecutor",
                "PYTHONPATH": "${workspaceFolder}/plugins:${workspaceFolder}/dags:${env:PYTHONPATH}"
            },
            "args": [
                "dags",
                "test",
                "${input:getdag}",
                "2022-11-24"
            ]
        }
    ],
    "inputs": [
        {
          "id": "getdag",
          "type": "command",
          "command": "extension.commandvariable.transform",
          "args": {
              "text": "${fileBasenameNoExtension}",
              "find": "_dag"
          }
        }
    ]
}

The default replace is "", so it removed the text _dag.

With the command extension.commandvariable.dateTime you can create a date string for today.

  • Related