Skip to content

Instantly share code, notes, and snippets.

@purplejacket
Last active September 6, 2018 17:11
Show Gist options
  • Select an option

  • Save purplejacket/7bfbfbb747be3e3b7ee4473e31fe86ef to your computer and use it in GitHub Desktop.

Select an option

Save purplejacket/7bfbfbb747be3e3b7ee4473e31fe86ef to your computer and use it in GitHub Desktop.
Skeleton of a rake task
# To test, type this on the command line:
# rake -T | grep foo
# To invoke:
# rake foo:first_task[1221]
namespace :foo do
desc 'Skeleton of a rake task'
task :first_task, [:num] => :environment do |_, args|
abort_reason = false
if abort_reason
puts "Sorry, something went wrong"
puts "Maybe you should do XYZ"
abort("aborting -- no action taken")
end
puts "Args times 10 gives: #{args.num.to_i * 10}"
bar "apple", "plum"
end
desc "skeleton of a simple rake task"
task :second_task => :environment do
end
end
# note, potential pitfall:
# this bar method is visible across ALL rake tasks in the repo.
# See here for alternative strategies:
# https://kevinjalbert.com/defined_methods-in-rake-tasks-you-re-gonna-have-a-bad-time/
def bar(p1, p2)
puts "p1=#{p1} p2=#{p2}"
end
__END__
expected output:
$ rake foo:first_task[1221]
Args times 10 gives: 12210
p1=apple p2=plum
$ rake -T | grep foo
rake foo:first_task[num] # Skeleton of a rake task
rake foo:second_task # skeleton of a simple rake task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment