diff --git a/README.md b/README.md index 03235630..fca94070 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ TODO: - [x] basic 'capistrano/deploy' noop example - [x] don't care too much about testing at this point (rspec included for my reference) - - [ ] before/after task hooks + - [x] before/after task hooks - [ ] consider requiring default tasks via configuration (strategy?) rather than Capfile - [ ] write more default tasks - [ ] handle multiple stage file generation @@ -40,3 +40,30 @@ Capify: $ cap -vT $ cap deploy + +## Before / After + +Where calling on the same task name, executed in order of inclusion + + + # call an existing task + before :starting, :ensure_user + + after :finishing, :notify + + + # or define in block + before :starting, :ensure_user do + # + end + + after :finishing, :notify do + # + end + + + + + + + diff --git a/lib/capistrano/dsl.rb b/lib/capistrano/dsl.rb index b2de527f..ba6b7f70 100644 --- a/lib/capistrano/dsl.rb +++ b/lib/capistrano/dsl.rb @@ -1,8 +1,20 @@ module Capistrano module DSL + def before(task, prerequisite, *args, &block) + rerequisite = Rake::Task.define_task(prerequisite, *args, &block) if block_given? + Rake::Task[task].enhance [prerequisite] + end + + def after(task, post_task, *args, &block) + post_task = Rake::Task.define_task(post_task, *args, &block) if block_given? + Rake::Task[task].enhance do + invoke(post_task) + end + end + def invoke(task) - ::Rake::Task["deploy:#{task}"].invoke + Rake::Task[task].invoke end def fetch(key) diff --git a/lib/capistrano/tasks/deploy.rake b/lib/capistrano/tasks/deploy.rake index a504e84e..8dd98ae6 100644 --- a/lib/capistrano/tasks/deploy.rake +++ b/lib/capistrano/tasks/deploy.rake @@ -45,7 +45,7 @@ end desc "Deploy" task :deploy do %w{starting start update finalize restart finishing finished}.each do |stage| - invoke stage + invoke "deploy:#{stage}" end end task default: :deploy