とある角度から

お腹いっぱいたべられる幸せ

rubyを実行するgruntプラグイン作るまで(1) - gruntpluginのテンプレートを実行

rubyのプログラムをgruntタスクの合間に挟みたかったので、gruntプラグインを自作することにしました。
物はこちら(※テストも書いてないし、npmに登録もしていないオレオレプラグイン汗)
github.com

gruntプラグイン用のテンプレートを持ってくる

まずはgruntpluginを作るときのテンプレートを作成してくれるgrunt-initモジュールをインストール

npm install -g grunt-init

使い方わからないのでヘルプを見る

grunt-init --help
grunt-init: Generate project scaffolding from a template. (v0.3.2)

Usage
 grunt-init [options] [template]

...

Available templates

(No templates found)

Templates that exist in the /Users/xxxxxx/.grunt-init directory may be
run with "grunt-init TEMPLATE". Templates that exist in another location may be
run with "grunt-init /path/to/TEMPLATE". A template is a directory that must
contain, at the very minimum, a template.js file.

For more information, see http://gruntjs.com/project-scaffolding

ふむふむ「有効なtemplatesはない」テンプレートは~/.grunt-initディレクトリの中に云々
詳しくは公式を見よと書いてある。
なので見てみると、いくつかテンプレートがあるみたいです。

今回はgruntplugin用のテンプレートを~/.grunt-initディレクトリにcloneしてきます。

mkdir ~/.grunt-init
git clone https://github.com/gruntjs/grunt-init-gruntplugin.git ~/.grunt-init/gruntplugin

で、適当なフォルダの中で、以下のコマンドを実行すると色々聞かれます。
(今回はすでにgruntを動かしているプロジェクト内のnode_moduleフォルダ内に、grunt-cclogdeleteという名前で空フォルダを作ってそこで実行しました)

grunt-init gruntplugin
Running "init:gruntplugin" (init) task
...

[?] Project name (grunt-cclogdelete) 
[?] Description (The best Grunt plugin ever.) 
...(ここ幾つか質問される)

Writing .gitignore...OK
Writing .jshintrc...OK
...

Done, without errors.

質問に全て答えると色々ファイルを作ってくれました。
githubリポジトリ指定まで聞いてくるので、とりあえず適当に答えてあとから修正しましたw
(ちなみにフォルダ内に何かファイルがあると怒られるので空にしておきましょう)

とりあえずこの状態で一旦githubリポジトリを作ってあげます。

git init
git add .
git commit -m 'first commit'
git remote add origin git@github.com:1010real/grunt-cclogdelete.git
git push -u origin master

grunt-cclogdeleteの初期コミット内容はこちら

テンプレートの中身は?

gruntプラグインでの実行ファイルはtasksフォルダ内に置かれます。
なのでtasks/cclogdelete.jsを眺めてみると、オプションを受け取ってデフォルト値とマージした後に、ファイルリストを受け取って読み込み→destに出力する部分が用意されてるぽい。感動。

とりあえずそのまま動かしてみる。プロジェクトのGrunffile.jsを編集
(gruntpluginテンプレートに含まれるGruntfile.jsでは無いです。分かりにくくてすみません。)

module.exports = function(grunt) {
  grunt.initConfig({

...

// 以下追記
    'cclogdelete': {
      build: {
        options: {
          param: 'test_param'
        },
        files: [{
          expand: true,
          cwd: 'input',
          src: '**/*.js',
          dest: 'output/',
          ext: '.js'
        }],
      }
    },

  ...

  }
});

// 追記
grunt.loadNpmTasks('grunt-cclogdelete');
...

実行してみる

grunt cclogdelete:build
Running "cclogdelete:build" (cclogdelete) task
File "output/src/lib/lodash.js" created.

...

Done, without errors.

動いてる動いてる。
ちなみにオプションがちゃんと渡ってるかどうかを確かめるため、tasks/cclogdelete.jsを編集

...

  grunt.registerMultiTask('cclogdelete', 'delete cclog() with ruby script.', function() {
    // Merge task-specific and/or target-specific options with these defaults.
    var options = this.options({
      punctuation: '.',
      separator: ', '
    });

    grunt.log.writeln(JSON.stringify(options)); // これ一行追記

    ...

実行すると以下のような出力が追加されている。
grunt.initConfigで追加したparam:test_paramが渡ってます。おけおけ

{"punctuation":".","separator":", ","param":"test_param}

長くなってきたので、記事を分けます。。。

とりあえず、grunt-init gruntpluginで作られるテンプレートでは、grunt.initConfigで指定されたファイルを読み込んで(expand対応)、それをそのままdestに出力するところまで含まれるようです。
内容をカンマ区切りで結合して、最後にピリオドを追加した結果をdestに出力するところまで含まれるようです。テスト書いてて気付きました(2016/03/12追記)

次の記事で、rubyを呼び出して実行するところをかきます。