Projects: Rakeタスク


HanamiにはデフォルトのRakeタスクが同梱されています。これは開発者が独自のタスクを作成するための 前提条件 として使用できます。

$ bundle exec rake -T
rake environment # Load the full project
rake test        # Run tests (for Minitest)
rake spec        # Run tests (for RSpec)

環境

プロジェクトコード(エンティティ、アクション、ビューなど)にアクセスする必要がある場合は、これをRakeタスクの前提条件として使用します。

プロジェクトコード(リポジトリなど)にアクセスできるRakeタスクを作成したいとします。

# Rakefile

task clear_users: :environment do
  UserRepository.new.clear
end
$ bundle exec rake clear_users

テスト/スペック

これはテストスイートを実行するデフォルトのRakeタスクです。

以下のコマンドは同等です。

$ bundle exec rake
$ bundle exec rake test

:test (または--test=rspecスイッチでアプリケーションを生成した場合は:spec) Rakeタスクがデフォルトです。

Rubyサーバーホスティングエコシステムの互換性

Rubyサーバーホスティングエコシステムの多くのSoftware as a Service(SaaS)は、Ruby on Railsをモデルにしています。 たとえば、HerokuはRubyアプリケーションで次のRakeタスクを見つけることを期待しています:

  • db:migrate
  • assets:precompile

Herokuにはデプロイをカスタマイズする方法がないので、Ruby on Railsのこれらの「標準的な」Rakeタスクをサポートしています。

あなたがあなたのデプロイを管理しているなら、これらのRakeタスクに頼らないでください。代わりにhanamiコマンドラインを使ってください。

カスタムrakeタスク

カスタムrakeタスクを作成したい場合は、プロジェクトルートにrakelibフォルダを作成できます:

$ mkdir rakelib/

その後、*.rakeファイルを作成します。たとえばexport.rake:

# rakelib/export.rake

namespace :export do
  desc 'Export books to algolia service'
  task :books do
    ExportInteractor.new.call
  end
end

これで、カスタムrakeタスクがリストに表示されます: Now you can see your custom rake task in the list:

$ bundle exec rake -T
rake export:books  # Export books to algolia service
rake environment   # Load the full project
rake spec          # Run RSpec code examples