LoginSignup
4
0

Github Actionsを利用したデータパックの構文チェック

Last updated at Posted at 2023-12-11

Minecraft Command Advent Calendar 2023 12日目の記事です。

はじめに

メモ帳やGithub上でコード修正したとき、マイクラを起動せずにデータパックの構文チェックを行いたい場面、ありますよね?
え?あんまりない?
そんな時に便利なのがGithub Actionsを利用した構文チェックです。

この記事はGithubやGithub Actionsを使ったことがある人向けです。

利用するAction

ChenCMDさんのdatapack-linterを利用します。

手順

1. Actionの設定

対象リポジトリのActionsタブからset up a workflow yourselfを選びmain.ymlの編集画面を開いてください。

そして、エディタに以下のコードを貼り付けてください。

name: lint-datapack
on:
  push:
  pull_request:
  workflow_dispatch:
jobs:
  lint:
    name: lint
    runs-on: ubuntu-latest
    steps:
      - name: checkout repository
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: lint
        uses: ChenCMD/datapack-linter@v2

引用元: README.md

2. Push

設定完了です。
後はコミットを行うと自動的にチェックが入ります。
構文チェックが問題なく完了するとコミットメッセージの後ろの方に緑色のチェックマークがつきます。

image.png

構文チェックの設定

要約して書こうと思ったのですがREADMEに書いてある文章とリストが読みやすかったのでそのまま引用させていただきます。

Datapack Linter は Datapack Helper Plus に使用されている言語サーバーを使用しており、リポジトリルートに配置された .vscode/setting.json を自動的に読み込み校閲ルールに適用します。
このファイルは VSCode でワークスペースの設定を変更すると生成されます。
また、後述の入力から configPath を指定することで、任意のコンフィグファイルを読み込むことが可能です。

引用元: README.md

名前 Name 想定する値 / Expect value 必須 / Require デフォルト / Default 概要 / About
lintDirectory path x "." チェックを行うディレクトリ
Directory to lint
configPath path x ".vscode/settings.json" 校閲ルールを記載したコンフィグファイルのパス
Path to the config file containing the lint rules
forcePass true x false チェックに失敗した Datapack ドキュメントが存在するときに step そのものを失敗させるか否か
Whether or not to fail the step itself when there is a Datapack document that fails the lint
muteSuccessResult true x false チェックに成功した Datapack ドキュメントのログを無効化するか否か
Whether to disable logging of Datapack documents that have been successfully linted
ignoreLintPathPattern Namespaced ID Pattern x "" チェックを行わない Datapack ドキュメントのリソースパスの名前空間 ID パターン
Namespaced ID Pattern of the resource path of the Datapack document whose lints are to be ignored
alwaysCheckAllFile true x false 常にすべての Datapack ドキュメントをチェックするか否か
Whether to always lint all Datapack documents

注: ignoreLintPathPattern は複数の文字列を入れることが可能です。方法は下記のサンプルを参照してください。

note: ignoreLintPathPattern can contain multiple character strings. Please refer to the sample below for the method.

         - name: lint
           uses: ChenCMD/datapack-linter@v2
           with:
             ignoreLintPathPattern: |
               ignore:**
               example:ignore/**
               example:data/ignore/**

引用元: README.md

役立つ場面

初めに書いた通りマイクラを起動せずに構文チェックを行うといった使い方もありますが、他にも用途はあります。

1. チーム開発

チームでの開発を行っている際、一部のmcfunctionがエラーを吐いていてもデータパック全体としては一見動いている、という状態でPushやPRをするとActionのチェックで引っ掛かるため本番環境にエラーを持ち込むことを未然に防げます。

2. CI/CD

CIの構文チェックに組み込みやすいです。
(他言語のLinterと比べると高機能とは言えませんが選択肢がほぼないのが現状です)

3. リファクタリング

リファクタリング等の大幅なコード変更を行った際に構文ミスを見落としていてもActionが見つけてくれます。

4. スマホデコーディング(!?)

半分ネタです。
ブラウザは使えるけどVSCodeが使えない環境でmcfunctionを書きたい!という場合は使えます。
Githubのリポジトリを開いてキーボードの.を押すか、urlをgithub.devに変更するとブラウザ内でVSCodeが開けます。
ですがこれは不完全なVSCodeなのでDHP(Datapack HelperPlus)等の便利な拡張機能が使えなかったりします。
色付けだけしてくれるlanguage-mcfunctionは使えるのでこれを入れましょう。
この状態でコードを書いてコミット&プッシュするとActionが構文チェックを行ってくれます!!

まとめ

ほぼdatapack-linterの紹介記事になってしまいました。
構文チェックは未然にエラーを防いでくれる手段の一つなのでGithubでデータパックを管理している方は是非使ってみてください。

4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0