Andres
Andres

Reputation: 11747

Define a custom rake task for each of the existing rake tasks

I need to create a custom task for each of the existing task I have. For example in /lib/tasks I have two files with tasks: update.rake,service.rake, etc. I have several tasks in each of these files. I have for example: rake update:users. Now I want to have rake custom_behaviour: for each of the tasks I have defined for example: rake custom_behaviour:update:users. I found the following code:

Rake::Task.tasks.each do |task|
  task "custom_behaviour:#{task.name}" do 
    puts "Custom Behaviour Task: rake #{task.name}"
  end
end

And this works properly if the file where I'm storing this code starts with "z", this is because it is the last file to be executed, but if I name the file as custom_behaviour.rake all the tasks defined in the files that are after the "c" are not "custom_behavioured" because the tasks are not loaded yet...

So my question is: What's the proper way of doing this? Where should I put the "custom_behaviour" code so when it's executed "all the tasks" are loaded?

Upvotes: 1

Views: 137

Answers (1)

William Hertling
William Hertling

Reputation: 675

I'd use the filename convention. This happens with other stuff that's order dependent, like Rails initializers.

The alternate that might allow you to accomplish what you need (I'm assuming you're looking have some custom behavior before/after the standard task) with is rake_hooks: https://github.com/guillermo/rake-hooks

Upvotes: 2

Related Questions