自分でRakeタスクを作る場合、Railsプロジェクトの場合はlib/tasks/
に.rake
ファイルを作ります。しかしHanamiの場合はそのようなディレクトリはありません。なので一工夫する必要があります。
まず、Rakefileに以下の行を足します。
Rakefile
Dir[File.expand_path('../tasks', __FILE__) << '/*.rake'].each { |file| load file }
tasks
ディレクトリを作り、そこにrakeファイルを置きます。
tasks/clear_users.rake
desc "Clear users"
task clear_users: :environment do
UserRepository.new.clear
end
:environment
をつけることでプロジェクトのエンティティなどを読み込んでくれます。
> bundle exec rake -T
rake create_user # Clear users
rake environment # Load the full project
rake spec # Run RSpec code examples
認識してくれました!
以上です。