Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

自作ライブラリにRakeのタスクを付属する

f:id:tbpg:20161021234007p:plain

CLI の gem を提供する際などに、 gem のユーザーに対してRakeのタスクを付属提供したい場合に
Rake::TaskLib を利用することで実現できます。

サンプル

コードの行数をカウントする機能のタスクを作ります。
行数のカウントは loc_counter gem を利用します。

sample.gemspec

# 省略
spec.add_runtime_dependency 'loc_counter'
# 省略

lib/sample/rake_task.rb

Rake::TaskLib を利用して RakeTask を作成します。
これを Rakefile で require して呼び出すことで
gem の利用者が rake task を利用できるようになります。

require 'rake'
require 'rake/tasklib'

module Sample
  #
  # A rake task that runs LOCCounter
  #
  # This will create a task that can be run with:
  #
  #   rake loc_count
  #
  # Example:
  #
  #   require 'sample/rake_task'
  #
  #   Sample::RakeTask.new do |task|
  #     task.paths = FileList['lib/**/*.rb', 'spec/**/*.rb']
  #   end
  #
  class RakeTask < ::Rake::TaskLib
    attr_accessor :name
    attr_accessor :paths

    def initialize(name = :loc_count)
      @name    = name
      @paths   = FileList['.']

      yield self if block_given?
      define
    end

    private

    def define
      desc 'Run LOCCounter'
      task(name) { run_task }
    end

    def run_task
      puts `loc_counter #{paths.join(' ')}`
    end
  end
end

Rakefile

loc_count をタスクに追加します
(今回は自分自身のプロジェクト内で参照していますが、
実際は依存ライブラリとして参照した場合を想定しています)

require 'bundler/gem_tasks'
require 'sample/rake_task'

Sample::RakeTask.new do |task|
  task.paths = FileList['lib/**/*.rb', 'spec/**/*.rb']
end

実行結果

$ bundle exec rake -T
rake build            # Build sample-0.1.0.gem into the pkg directory
rake clean            # Remove any temporary products
rake clobber          # Remove any generated files
rake install          # Build and install sample-0.1.0.gem into system gems
rake install:local    # Build and install sample-0.1.0.gem into system gems without network access
rake loc_count        # Run LOCCounter
rake release[remote]  # Create tag v0.1.0 and build and push sample-0.1.0.gem to Rubygems

$ bundle exec rake loc_count
3 files processed
Total     53 lines
Empty     7 lines
Comments  18 lines
Code      28 lines

サンプルコード

Rake::TaskLib の参考コードです。

外部資料