【VSCODE】ビルドタスクの実行時に開いてるBatchファイルを実行する

ネコニウム研究所

PCを利用したモノづくりに関連する情報や超個人的なナレッジを掲載するブログ

【VSCODE】ビルドタスクの実行時に開いてるBatchファイルを実行する

2022-2-21 | ,

VSCODEでビルドタスクの実行時に開いてるBatchファイルを実行したい!簡単にBatchファイルを実行したい!

概要

この記事では、VSCODEでビルドタスクの実行時に開いてるBatchファイルを実行する手順を掲載する。

仕様書

環境

  • Visual Studio Code 1.64.2
  • Windows 10 21H1(OSビルド19043.1526)

手順書

プロジェクトフォルダの中の.vscode/task.jsonを下記のように書く。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [{
        "label": "run",
        "type": "shell",
        "command": "${file}",
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "presentation": {
            "reveal": "always",
            "panel": "shared"
        }
    }]
}

ターミナル->ビルドタスクの実行からビルドタスクを実行すると開いてるBatchファイルが実行され、VSCODEの中のターミナルに出力が表示される。

解説

command${file}という変数を設定することで現在開いてるファイルをVSCODE上のターミナルで実行してる。
Batchファイル以外(例えばPowerShellとか)にもターミナルから実行できるファイルであれば、同じようにビルドタスクの実行時に実行できる。

おまけ

テストタスクで実行したい

プロジェクトフォルダの中の.vscode/task.jsonを下記のように書くとテストタスクの実行時に開いてるBatchファイルが実行されるようになる。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [{
        "label": "run",
        "type": "shell",
        "command": "${file}",
        "group": {
            "kind": "test",
            "isDefault": true
        },
        "presentation": {
            "reveal": "always",
            "panel": "shared"
        }
    }]
}

groupkindtestに変更する。

ビルドタスクでもテストタスクでも実行したい

プロジェクトフォルダの中の.vscode/task.jsonを下記のように書くとビルドタスク、または、テストタスクの実行時に開いてるBatchファイルが実行されるようになる。

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [{
        "label": "run",
        "type": "shell",
        "command": "${file}",
        "group": {
            "kind": "build",
            "isDefault": true
        },
        "presentation": {
            "reveal": "always",
            "panel": "shared"
        }
    },
        {
        "label": "run",
        "type": "shell",
        "command": "${file}",
        "group": {
            "kind": "test",
            "isDefault": true
        },
        "presentation": {
            "reveal": "always",
            "panel": "shared"
        }
    }]
}

まとめ(感想文)

Batchファイルのテストをするときに便利かもね!