Rakefiles
- Created at
- 23 April 2025
- Last modified
- 23 April 2025
- Status
- 🌿 sapling
Rakefiles are like makefiles, but for ruby. Makefiles basically set up a bunch of tasks that you use all the time in development.
The main advantage of rakefiles is if you use ruby all the time - it's easier to code things up in ruby than in shell script. And because my brain is influenced by what I did as a teenager, that makes it the easiest thing for me to quickly spin up build tooling in.
Notes and recipes
Give one task multiple names
Let's say I want rake edit
and rake show
to do the same thing. Or perhaps I want rake e
to be a shortcut for rake edit
.
If you don't care about arguments, you can do this with prerequisites:
task :edit do
# Whatever you need to do here
end
task :e => :edit
If you do care about arguments, it's a bit harder:
task :edit, [:par1, :par2] do |task, args|
# Do stuff with args here
end
task :e, [:par1, :par2] => :edit
And here's some code stolen from the above StackOverflow thread which compresses this into a method:
def alias_task(name, old_name)
t = Rake::Task[old_name]
desc t.full_comment if t.full_comment
task name, *t.arg_names do |_, args|
# values_at is broken on Rake::TaskArguments
args = t.arg_names.map { |a| args[a] }
t.invoke(args)
end
end
Namespaces
namespace "main" do
task :build do
# Build the program...
end
end
Can be accessed with rake main:build
.