Hanami ships with default Rake tasks that can be used as prerequisites by developers to build their own tasks.

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

Environment

Use this as a Rake task prerequisite when we need to access project code (eg. entities, actions, views, etc..)

Example

Imagine we want to build a Rake task that is able to access project code (eg. a repository)

  1. # Rakefile
  2. task clear_users: :environment do
  3. UserRepository.new.clear
  4. end
  1. $ bundle exec rake clear_users

Test / Spec

This is the default Rake task, which runs the test suite

The following commands are equivalent:

  1. $ bundle exec rake
  1. $ bundle exec rake test

The :test (or :spec in case you generated the application with --test=rspec switch) Rake task is the default.

Ruby Server Hosting Ecosystem Compatibility

Many Software as a Service (SaaS) of the Ruby server hosting ecosystem are modeled after Ruby on Rails. For instance, Heroku expects to find the following Rake tasks in a Ruby application:

  • db:migrate
  • assets:precompile

For Heroku, there isn’t a way to customize the deploy, so we’re supporting these “standard” Rake tasks from Ruby on Rails.

If you are in control of your deployment, don’t rely on these Rake tasks, but please use hanami command line, instead.

Custom rake tasks

If you want to create a custom rake tasks you can create a rakelib folder in project root:

  1. $ mkdir rakelib/

And after that create *.rake file, export.rake for example:

  1. # rakelib/export.rake
  2. namespace :export do
  3. desc 'Export books to algolia service'
  4. task :books do
  5. ExportInteractor.new.call
  6. end
  7. end

Now you can see your custom rake task in the list:

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