Skip to content

Instantly share code, notes, and snippets.

@ashmckenzie
Last active March 16, 2017 03:45
Show Gist options
  • Save ashmckenzie/61424ad16f8433c656c9f14c36f030c9 to your computer and use it in GitHub Desktop.
Save ashmckenzie/61424ad16f8433c656c9f14c36f030c9 to your computer and use it in GitHub Desktop.
before_all Rake hook
$ rake one
> always_run_before_one
> always_run_before_two
> one
$ rake nested:two
> always_run_before_one
> always_run_before_two
> two
$ rake three
> three
module Rake::DSL
def before_all(*args)
args.each do |pair|
matchers, task_names = pair.to_a.first
tasks = []
[ *matchers ].each do |matcher|
matcher = matcher.to_s if matcher.is_a?(Symbol)
tasks += Rake::Task.tasks.select { |task| !!task.name.match(matcher) }
end
tasks.each { |task| task.enhance([ *task_names ]) }
end
end
end
task :one do
puts '> one'
end
namespace :nested do
task :two do
puts '> two'
end
end
task :three do
puts '> three'
end
task :always_run_before_one do
puts '> always_run_before_one'
end
task :always_run_before_two do
puts '> always_run_before_two'
end
before_all [ /^one$/, 'nested:two' ] => [ 'always_run_before_one', 'always_run_before_two' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment