Check if rake task exists from Rakefile

I am looking for a way to check if a specific rake task from Rakefile exists. I have a dependency on a task that I want to include only in the dependency, if this task is available. In this particular case, the task is only available in the Rails project, but I want my rake tasks to work in a more general Ruby application environment (and not just in Rails).

I want to do something like this:

if tasks.includes?('assets:precompile') task :archive => [:clean, :vendor_deps, 'assets:precompile'] ... end else task :archive => [:clean, :vendor_deps] ... end end 

What is the best way to conditionally include a task dependency in a rake command?

+5
source share
2 answers

how about doing something like that? Invoke a task if it exists, as opposed to its explicit dependency?

  task :archive => [:clean, :vendor_deps] do Rake.application["assets:precompile"].invoke if Rake::Task.task_defined?('assets:precompile') .... end 

or even easier. Since specifying a task again allows you to add to it, it looks like it works.

 task :archive => [:clean, :vendor_deps] do ... end task :archive => "assets:precompile" if Rake::Task.task_defined?("assets:precompile") 

which conditionally adds asset dependency: precompilation, if defined.

+11
source

Can you use task_defined? :

 Rake::Task.task_defined?('assets:precompile') 
+1
source

All Articles