Pretty much all my CLI utilities are written in Ruby now. Sometimes a Rakefile
is all I need, and as a programmer I love breaking things up into separate files. That is if I have a task named "say_hello" in my Rakefile I may actually want it in alib/tasks/say_hello.rake
file.
Coercing Rake to load these tasks outside of a Rails environment is actually surprisingly easy - though as a Ruby novice it took me some hunting to get the syntax correct.
#Rakefile $:.unshift 'lib' Dir[File.dirname(__FILE__) + '/lib/tasks/*.rake'].each { |file| load file }
$:
is shorthand for $LOADPATH and unshift prepends the lib
directory to the front of the $LOADPATH. Then we enumerate all of the *.rake
files found in ./lib/tasks
and load them (not require).