How to Get a List of All Available Rake Tasks in a Namespace

Is it possible to get a list of all available rake tasks in a namespace?

I've found out the answer:


tasks = Rake.application.tasks

This will return an array of Rake::Task objects that can be examined. Further details at http://rake.rubyforge.org/

Run all rake tasks?

Here's one way:

namespace :hot_tasks do |hot_tasks_namespace|
task :task1 do
puts 1
end
task :task2 do
puts 2
end
task :all do
hot_tasks_namespace.tasks.each do |task|
Rake::Task[task].invoke
end
end
end

running it:

# bundle exec rake hot_tasks:all
1
2

More (not necessarily better) ideas at this question, especially if you're in a rails app.

Run all rake tasks inside a shared namespace in Rails?

It should be:

desc "Run all tasks"
task run_all: :environment do
Rake::Task['get_ready:check_weather'].execute
Rake::Task['get_ready:make_lunch'].execute
Rake::Task['get_ready:start_car'].execute
end

The namespace is get_ready and check_weather, make_lunch, start_car are task in that namespace.

More elegant solution is:

desc "Run all tasks"
task run_all_elegantly: [:environment, 'get_ready:check_weather', 'get_ready:make_lunch', 'get_ready:start_car']

List Rake Tasks WIthout Namespace

If your tasks have preceding descs, the are to be listed in rake -T:

desc "Lists all the tasks"
task :list do
Rake.application.tasks.each do |task|
puts "#{task.name} \# #{task.comment}"
end
end

Tasks w/o preceding desc are omitted in rake -T output by design. If you still want to use your :list task, simply add

Rake::TaskManager.record_task_metadata = true

to the very top of your main Rakefile.

Rake: how to output a list of tasks from inside a task?

Invoking rake -T from within tasks is a bit more complicated in newer versions of rake. The options that need to be set may be derived from rake/lib/application.rb in method standard_rake_options. Basically this boils down to

Rake::TaskManager.record_task_metadata = true

task :default do
Rake::application.options.show_tasks = :tasks # this solves sidewaysmilk problem
Rake::application.options.show_task_pattern = //
Rake::application.display_tasks_and_comments
end

Note that record_task_metadata cannot be set from within the default task, as it will already be too late when the task is executed (descriptions won’t have been collected, thus those are nil and therefore no task matches the pattern). Trying to reload the Rakefile from within a task will lead to a closed loop. I assume there are performance trade ofs when always collecting metadata. If that’s an issue

task :default do
system("rake -sT") # s for silent
end

might be more suitable.

Both work for me using rake 0.9.2.2.

Is it possible to include a module in rake tasks without polluting the global scope?

Here's what worked for me:

module MyRakeHelpers
def helper
puts 'foo'
end
end

module MyRakeTasks
extend Rake::DSL
extend MyRakeHelpers

task :some_task do
helper
end
end

In short, you can use the Rake DSL in a different scope by including (or extending) Rake::DSL. From the source:

DSL is a module that provides #task, #desc, #namespace, etc. Use this when you'd like to use rake outside the top level scope. For a Rakefile you run from the command line this module is automatically included.

task uses Rake::Task#define_task under the hood, which you could also use to write your own DSL.

Thanks to How To Build Custom Rake Tasks; The Right Way for the tip about define_task.

rake tasks dry up namespace with namespace

Unfortunately I was advised against this strategy and in the process of discovery I found out my helper methods were not setup correctly. So here goes.

I created a new modules folder in lib/modules with the new helper_functions.rb file inside that directory. Here are my helpers:

module HelperFunctions

    # ------------------------------------------------------------------------------------
# Permitted environments
# ------------------------------------------------------------------------------------
def environments(arg)
arg = arg || "development"
environments = ["development", "test", "production"]
if environments.include?(arg)
puts
msg("Environment parameter is valid")
return arg
else
error("Invalid environment parameter")
exit
end
end
# ------------------------------------------------------------------------------------
# Console message handler
# ------------------------------------------------------------------------------------
def msg(txt, periods: "yes", new_line: "yes")
txt = txt + "..." if periods == "yes"
puts "===> " + txt
puts if new_line == "yes"
end
def error(reason)
puts "**** ERROR! ABORTING: " + reason + "!"
end
# ------------------------------------------------------------------------------------
# Execute Shell Commands
# ------------------------------------------------------------------------------------
def shell(cmd, step: nil)
msg("Starting step #{step}", new_line: "no") if step.present?
if ENV['TRY']
puts "-->> " + cmd
else
sh %{#{cmd}}
end
msg("#{step} completed!", periods: "no")
end
end

Then in the Rakefile add:

# Shared Ruby functions used in rake tasks
require File.expand_path('../lib/modules/helper_functions', __FILE__)
include HelperFunctions

Rails.application.load_tasks

# Do not add other tasks to this file, make files in the primary lib/tasks dir ending in .rake
# All placewise tasks should be under the lib/tasks/placewise folder and end in .rake
desc 'List all available placewise rake tasks for this application.'
task :tasks do
result = %x[rake -T | sed -n '/placewise:/{/grep/!p;}']
result.each_line do |task|
puts task
end
end

And finally my .rake tasks look like:

namespace :placewise do
# ------------------------------------------------------------------------------------
namespace :db do
# ------------------------------------------------------------------------------------
desc "Drop and create the current database, the argument [env] = environment."
task :recreate, [:env] do |t,args|
env = HelperFunctions::environments(args.env)
HelperFunctions::msg("Dropping the #{env} database")
HelperFunctions::shell("RAILS_ENV=#{env} rake db:drop", step: "1/3")
HelperFunctions::msg("Creating the #{env} database")
HelperFunctions::shell("RAILS_ENV=#{env} rake db:create", step: "2/3")
HelperFunctions::msg("Running the #{env} database migrations")
HelperFunctions::shell("RAILS_ENV=#{env} rake db:migrate", step: "3/3")
end
# ------------------------------------------------------------------------------------
end
# ------------------------------------------------------------------------------------
end


Related Topics



Leave a reply



Submit