0

I am tooling the following rake file:

namespace :build do
  desc 'Build development application to the build dist directory'
    task :default do
        Rake::Task[:delete].invoke
    end


    task :development do
        verbose(false) do
            puts "=> Building the frontend test build version of application!"

        end
    end
    desc 'Build production application to the build dist directory minified with no tests'
    task :production do
        verbose(false) do
            puts "=> Building the frontend production version of application!"

        end
    end
end

which has 2 simple tasks. Yes, I took them out during development for this ?. What I am trying to achieve is running that Rake::Task[:delete].invoke every time either the sub :development or :production tasks are invoked.

Also, I am not interested in setting up an :all either.

Thank you for your help :)

Chris Hough
  • 3,389
  • 3
  • 41
  • 80

2 Answers2

1

You could express the dependencies directly on the depending tasks, like

namespace :build do
  task :development => :delete do
    puts "development!"
  end
  task :production => :delete do
    puts "production!"
  end
end

task :delete do
  puts "delete!"
end
Bryan Stearns
  • 1,297
  • 12
  • 13
  • I was toying with that, but what if you wanted to run that task prior or before the others fired? – Chris Hough May 31 '14 at 07:30
  • 1
    In my example, :delete would get run before :development or :production (whichever you invoked).(I left out default -- if you wanted "rake build" to, say, default to doing :development [with :delete first!], you could add "task :default => :development" inside the namespace block. – Bryan Stearns May 31 '14 at 07:34
  • ok, I like this approach must better. Just one last trick, I need. How would you add a task that would run after? So the delete would fire first as you have shown, than it would run it's task, followed up by a shared post task running after? – Chris Hough May 31 '14 at 07:40
  • Spoke too soon about that default thing. If you wanted to pretend there was a default task in the build namespace (to be invoked by "rake build", you'd actually add a :build task *outside* the :build namespace. – Bryan Stearns May 31 '14 at 07:41
  • True. I am also thinking to add follow up tasks, is it a best practice to just toss in another invoke or dry it up with a method? – Chris Hough May 31 '14 at 07:45
  • I don't know of a better way to do after than to invoke the task using eg "Rake::Task[:delete].invoke" at the end of the body of whatever task you want to trigger it. Also, if you have two tasks that both end with invoking :delete that way, only the first will be run unless you precede the invokes with "Rake::Task[:delete].reenable" – Bryan Stearns May 31 '14 at 07:45
  • very true, thanks for you help man. I appreciate it. hope you have a great weekend. – Chris Hough May 31 '14 at 07:47
  • With something as specific as invoking another task this way, I think trying to DRY it up might obscure what's going on; I'm willing to pay a little duplication to get clarity. – Bryan Stearns May 31 '14 at 07:47
0

Using the rake-hooks project I was able to adjust this as follows:

namespace :build do
    desc 'Build development application to the build dist directory'
    task :development do
        verbose(false) do
            puts "=> Building the frontend test build version of application!"

        end
    end
    desc 'Build production application to the build dist directory minified with no tests'
    task :production do
        verbose(false) do
            puts "=> Building the frontend production version of application!"

        end
    end

end
before "build:development", "build:production" do
  Rake::Task[:delete].invoke
end

also make sure to have require 'rake/hooks' at the top of your rake file.

Chris Hough
  • 3,389
  • 3
  • 41
  • 80