Rake : conditional logic based on namespaces?

Hi all

I’ve got a Rakefile with 2 groups of commands (split into namespaces).

One namespace (‘rake show:foo’) prints a string, the other ‘backticks’
it (“rake run:foo”’).

The same functionality is duplicated in each namespace, so much that
the code smells
to high heaven - I’m in danger of ‘show’ and ‘do’ getting out of step.

An example might help:


require ‘rake’

ls = ‘ls /tmp’
date = ‘date’

show commands

namespace :show do
desc ‘show command to list /tmp’
task :ls do
puts ls
end

   desc 'show command to print date'
   task :date do
       puts date
   end

end

desc ‘show commands’
task :show => [‘show:ls’, ‘show:date’]

run command

namespace :run do
desc ‘list /tmp’
task :ls do
puts #{ls}
end

   desc 'print date'
   task :date do
       puts `#{date}`
   end

end

desc ‘run commands’
task :run => [‘run:ls’, ‘run:date’]


There must be someway to prevent duplicating the actual commands to run
(which are a lot more convoluted in reality) - any ideas how?

Thanks a lot.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs