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 :)

2 Answers 2

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
8
  • I was toying with that, but what if you wanted to run that task prior or before the others fired? May 31, 2014 at 7: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. May 31, 2014 at 7: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? May 31, 2014 at 7: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. May 31, 2014 at 7: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? May 31, 2014 at 7:45
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.

1
  • Yep, commenting on my own post, if you use this approach please note that gym is rather outdated. May 31, 2014 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.