Bonus: Rake [0.75d]

Rake tasks automate recurring jobs in a Rails app, from database maintenance to custom scripts. This lesson covers how tasks are defined, how they depend on each other, and how to write your own.

Important

Work on this lesson in builder mode.

Research =========

  • What is rake good for?
  • Take a look at some of the Rake tasks that Rails gives you (rake -T within a Rails project)
  • Find the code that defines the rake stats task in the Rails gems
  • What are some ways how a Rake task can execute another task?
  • What does it mean if a Rake task "depends" on another task? E.g. understand what it means for a Rake task within a Rails app to depend on :environment.
  • Understand that Capistrano tasks are also defined using the Rake DSL, but a Capistrano task is not automatically a Rake task and vice versa.

Exercises =========

  • Write a Rake task rake list_app_files that lists all Ruby files and their file size within the app directory.
    • Hint: Use globbing to find those files (Dir.glob(File.join('root_directory', '**', '*.rb')))
    • Hint: You could use the number_to_human_size Show archive.org snapshot helper for better file size formatting
    • Hint: For better testability, write a service class with the functionality and only call it in your rake task
  • Use a namespace to rename your task to rake app:list_files
  • Give your task a nice description and notice how it appears in rake -T
Henning Koch