| name | rake |
| description | This skill should be used when the user asks about "Rake", "rake tasks", "Rakefile", "task dependencies", "rake namespace", "rake desc", "rake file tasks", "invoke", "execute", "task arguments", or needs guidance on creating and managing Rake tasks. |
| version | 1.0.0 |
Rake Task System
Guide to creating and managing Rake tasks for Ruby projects.
Basic Tasks
Defining Tasks
desc "Greet the user"
task :greet do
puts "Hello, World!"
end
Task with Description
desc "Generate the weekly report"
task :weekly_report do
Report.generate(:weekly)
end
Tasks Without Description
task :internal_cleanup do
Cleaner.run
end
Task Dependencies
Simple Dependencies
task :build => :compile do
puts "Building..."
end
task :compile do
puts "Compiling..."
end
Multiple Dependencies
task :deploy => [:test, :build, :upload] do
puts "Deployed!"
end
Default Task
task :default => [:test, :lint]
Namespaces
Organizing Tasks
namespace :db do
desc "Create the database"
task :create do
Database.create
end
desc "Run migrations"
task :migrate do
Database.migrate
end
desc "Seed the database"
task :seed => :migrate do
Database.seed
end
end
Nested Namespaces
namespace :db do
namespace :schema do
desc "Dump the schema"
task :dump do
Schema.dump
end
desc "Load the schema"
task :load do
Schema.load
end
end
end
Task Arguments
Simple Arguments
desc "Greet a person"
task :greet, [:name] do |t, args|
puts "Hello, #{args[:name]}!"
end
Multiple Arguments
desc "Create a user"
task :create_user, [:name, :email] do |t, args|
User.create(
name: args[:name],
email: args[:email]
)
end
Arguments with Defaults
task :greet, [:name] do |t, args|
args.with_defaults(name: "World")
puts "Hello, #{args[:name]}!"
end
Arguments with Dependencies
task :deploy, [:env] => :build do |t, args|
args.with_defaults(env: "staging")
Deploy.to(args[:env])
end
Environment and Configuration
Using Environment Variables
task :deploy do
env = ENV.fetch("DEPLOY_ENV", "staging")
Deploy.to(env)
end
Loading Application Environment
task :my_task => :environment do
process_items
end
File Tasks
Building Files
file "output.txt" => "input.txt" do |t|
File.write(t.name, File.read(t.prerequisites.first).upcase)
end
Directory Tasks
directory "tmp/cache"
task :cache_setup => "tmp/cache" do
puts "Cache directory ready"
end
File Lists
SRC = FileList["src/**/*.rb"]
TESTS = FileList["test/**/*_test.rb"]
task :test do
TESTS.each { |f| ruby f }
end
Rules
rule ".html" => ".md" do |t|
sh "pandoc #{t.source} -o #{t.name}"
end
Invoking Tasks
Invoke vs Execute
task :setup do
Rake::Task["db:create"].invoke
Rake::Task["db:migrate"].invoke
Rake::Task["db:seed"].execute
end
Re-enabling Tasks
task :multi_run do
3.times do
Rake::Task["process"].reenable
Rake::Task["process"].invoke
end
end
Passing Arguments to Invoked Tasks
task :deploy_all do
%w[staging production].each do |env|
Rake::Task["deploy"].reenable
Rake::Task["deploy"].invoke(env)
end
end
Testing Rake Tasks
With Minitest
require "test_helper"
require "rake"
class MyTaskTest < Minitest::Test
def setup
Rake.application.load_rakefile
end
def test_greet_task
output = capture_io do
Rake::Task["greet"].invoke("Alice")
end
assert_match(/Hello, Alice/, output.first)
ensure
Rake::Task["greet"].reenable
end
end
With RSpec
require "rails_helper"
require "rake"
RSpec.describe "greet task" do
before do
Rake.application.load_rakefile
end
after do
Rake::Task["greet"].reenable
end
it "greets the user" do
expect { Rake::Task["greet"].invoke("Alice") }
.to output(/Hello, Alice/).to_stdout
end
end
Best Practices
Task Organization
Error Handling
desc "Safe task with error handling"
task :safe_task do
begin
risky_operation
rescue StandardError => e
warn "Task failed: #{e.message}"
exit 1
end
end
Verbose Output
task :process do
items.each do |item|
puts "Processing #{item}..." if Rake.verbose
process(item)
end
end
Dry Run Support
task :delete_old_files do
old_files.each do |file|
if Rake.application.options.dryrun
puts "Would delete: #{file}"
else
FileUtils.rm(file)
end
end
end
Common Patterns
Progress Reporting
desc "Process all items"
task :process_all do
items = Item.all
total = items.count
items.each_with_index do |item, i|
print "\rProcessing #{i + 1}/#{total}..."
process(item)
end
puts "\nDone!"
end
Confirmation Prompt
desc "Dangerous operation"
task :dangerous do
print "Are you sure? (y/N) "
abort "Cancelled" unless $stdin.gets.chomp.downcase == "y"
perform_dangerous_operation
end
Parallel Execution
require "parallel"
task :parallel_process do
items = Item.all
Parallel.each(items, in_processes: 4) do |item|
process(item)
end
end