いちごタルト

シュークリームはカスタードクリーム派

vimでサクッと CWL を validate する

  • この記事は Common Workflow Language (CWL) Advent Calendar 2018 5日目の記事です

qiita.com

みなさん、 CWL 書いてますか。 CWL のリファレンス実装である cwltool には validate モードがあり、入力した cwl ファイルの構文チェックを行うことができます。 この記事では vim プラグインを使ってすばやく CWL を validate する機能の設定方法を紹介します。

まずは CLI で実行した場合にどうなるのか、こちらの cwl ファイル(dajalendar を実行してテキストファイルに保存するだけ)で試してみましょう。

dajalendar.cwl

#!/usr/bin/env cwl-runner

cwlVersion: v1.0
class: CommandLineTool
baseCommand: dajalendar

hints:
  DockerRequirement:
    dockerPull: kubor/dajalendar
inputs: []
outputs:
    dajalendar:
        type: stdout
stdout:
    dajalendar.txt
$ cwltool --validata dajalendar.cwl
/usr/local/bin/cwltool 1.0.20181201184214
Resolved 'dajalendar.cwl' to 'file:///Users/ryuichi.kubo/src/github.com/kubor/cwl_handson/dajalendar/dajalendar.cwl'
dajalendar.cwl is valid CWL.

valid ですね。 間違いがあると警告を出してくれます。 試しに line:7 の hints:'hints に変更してわざと間違えてみます。

/usr/local/bin/cwltool 1.0.20181201184214
Resolved 'dajalendar.cwl' to 'file:///Users/ryuichi.kubo/src/github.com/kubor/cwl_handson/dajalendar/dajalendar.cwl'
I'm sorry, I couldn't load this CWL file, try again with --debug for more information.
The error was: while scanning a simple key
  in "file:///Users/ryuichi.kubo/src/github.com/kubor/cwl_handson/dajalendar/dajalendar.cwl", line 7, column 1
could not find expected ':'
  in "file:///Users/ryuichi.kubo/src/github.com/kubor/cwl_handson/dajalendar/dajalendar.cwl", line 8, column 20

便利ですね。 では、 Vim から操作してみます。 まずは、Vim 標準機能のコマンド実行方法を使ってみます。

:!cwltool --validate % で現在開いている Vim のバッファをコマンドの入力として、実行結果を得ることが可能です。

f:id:kuborn:20181208133202p:plain
Vim から cwltool を実行する

f:id:kuborn:20181208133553p:plain
Vim から cwltool を実行した結果

画面が切り替わりますし、実行結果が裏のコマンドラインに流れてしまうので確認しづらいですし、ちょっとめんどくさいですね。 thinca/vim-quickrun を使いましょう。

github.com

インストールは、 dein.vim を使っているなら toml ファイルにこんな感じで書いておきます。

[[plugins]]
repo = 'thinca/vim-quickrun'
hook_add = '''
let g:quickrun_config = {}
let g:quickrun_config.cwl = {
    \ 'command': 'cwltool',
    \ 'cmdopt': '--validate'
    \ }
'''

dein.vim を使っていない場合は是非使いましょう。 その他のプラグインマネージャーをご使用の場合はそれぞれで設定してみてください。

github.com

あとは、 .vimrc にファイルタイプの設定を追加しましょう。 この設定は、すでに vim-cwl をインストール済みの場合は不要です。 dein.vim の設定でファイルタイプ別の遅延ロード設定をしている場合はロード前にファイルタイプを指定できるようにするためこの設定を加えておくほうが良いです。

.vimrc

autocmd BufNewFile,BufRead *.cwl set filetype=cwl

設定が完了したら quickrun を実行してみましょう。 デフォルトのキーバインドでは prefix + r で、実行可能です。 もしくは :QuickRun を実行します。

f:id:kuborn:20181208172210p:plain
quickrun の実行結果

実行結果がウィンドウ分割した上で右側に表示されます。 さあどんどん活用しましょう。

おまけ

dajalendar.cwl の実行結果はこちらです。今日はすごく寒いですね。

$ cat dajalendar.txt
   December 2018
Mo Tu We Th Fr Sa Su
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
[       pip        ]
pythonモジュールのインストール?ぴっとしてぷっだよ、pipなら。