smghmw
Last Updated: February 25, 2016
·
2.131K
· mlafeldt
2190d7a468f51fa3be5eabfc9397a28b

Running a Rake task once at exit

While Rake makes it easy to define that a task has one or more prerequisites, I
learned that it's hard to define a task that is executed once after one or more
tasks have finished, e.g. running a cleanup task at the end of all test
tasks.

I fiddled around with Rake::Task#enhance and tried to add some intermediate
tasks. I didn't like the bloated result. I came up with this solution instead:

namespace :test do
  task :prepare do
    # Set up tests...

    # Run cleanup at exit unless an exception was raised.
    at_exit { Rake::Task['test:cleanup'].invoke if $!.nil? }
  end

  task :cleanup do
    # Clean up tests...
  end

  task :lint => :prepare do
    # Run lint checks...
  end

  task :spec => :prepare do
    # Run spec examples...
  end

  task :all => [:lint, :spec]
end

Here, test:cleanup is really only run once after test:lint, test:spec, or
test:all. No Rake::Task#enhance. No intermediate steps.

Say Thanks
Respond