├── .gitignore ├── .travis.yml ├── Gemfile ├── Gemfile.rake-0.8.7 ├── Gemfile.rake-0.9.2.2 ├── LICENSE ├── README.md ├── Rakefile ├── lib └── rake │ ├── hooks.rb │ └── hooks │ └── version.rb ├── rake-hooks.gemspec └── test └── test_rake_hooks.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | .bundle 3 | Gemfile.lock 4 | pkg/* 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 1.8.7 4 | - 1.9.2 5 | - 1.9.3 6 | - jruby-18mode # JRuby in 1.8 mode 7 | - jruby-19mode # JRuby in 1.9 mode 8 | - rbx-18mode 9 | script: rake test 10 | gemfile: 11 | - Gemfile.rake-0.8.7 12 | - Gemfile.rake-0.9.2.2 13 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | gemspec 4 | -------------------------------------------------------------------------------- /Gemfile.rake-0.8.7: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | gem "rake", "0.8.7" 4 | gemspec 5 | -------------------------------------------------------------------------------- /Gemfile.rake-0.9.2.2: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | gem "rake", "0.9.2.2" 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) <2010> 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rake-hooks 2 | 3 | Rake hooks let you add callbacks to rake: 4 | 5 | [![Build Status](https://secure.travis-ci.org/guillermo/rake-hooks.png)](http://travis-ci.org/guillermo/rake-hooks) 6 | [![endorse](http://api.coderwall.com/guillermo/endorsecount.png)](http://coderwall.com/guillermo) 7 | 8 | 9 | ## Usage 10 | 11 | For example in your Rakefile 12 | 13 | ```ruby 14 | require 'rake/hooks' 15 | 16 | task :say_hello do 17 | puts "Good Morning !" 18 | end 19 | 20 | after :say_hello do 21 | puts "GoodBye" 22 | end 23 | 24 | after :say_hello do 25 | puts "Now go to bed !" 26 | end 27 | 28 | before :say_hello do 29 | puts "Hi !" 30 | end 31 | ``` 32 | 33 | Now run with rake 34 | 35 | ```bash 36 | $ rake say_hello 37 | Hi ! 38 | Good Morning ! 39 | GoodBye 40 | Now go to bed ! 41 | ``` 42 | 43 | You can also pass more than one task and each task will be setup with the 44 | callback 45 | 46 | ```ruby 47 | before :say_hello, :say_goodbye do 48 | puts "Hi !" 49 | end 50 | ``` 51 | 52 | 53 | ## Installation 54 | 55 | With rubygems use: 56 | ```bash 57 | $ gem install rake-hooks 58 | ``` 59 | 60 | With other systems 61 | Add lib dir to load path. 62 | 63 | ## Dependencies 64 | 65 | * Rake 66 | 67 | ## Author 68 | 69 | * [Joel Moss](mailto:joel@developwithstyle.com) 70 | * [Guillermo Álvarez](mailto:guillermo@cientifico.net) 71 | 72 | ## Web 73 | 74 | http://github.com/guillermo/rake-hooks 75 | 76 | ## Date of writing 77 | 78 | 9 Sep 2011 79 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require 'rake/testtask' 3 | 4 | Rake::TestTask.new do |t| 5 | t.libs << "test" 6 | t.test_files = FileList['test/test*.rb'] 7 | t.verbose = true 8 | end 9 | 10 | 11 | task :default => :test -------------------------------------------------------------------------------- /lib/rake/hooks.rb: -------------------------------------------------------------------------------- 1 | module Rake::Hooks 2 | def before(*task_names, &new_task) 3 | task_names.each do |task_name| 4 | old_task = Rake.application.instance_variable_get('@tasks').delete(task_name.to_s) 5 | desc old_task.full_comment 6 | task task_name => old_task.prerequisites do 7 | new_task.call 8 | old_task.invoke 9 | end 10 | end 11 | end 12 | 13 | def after(*task_names, &new_task) 14 | options = task_names.last.is_a?(Hash) ? task_names.pop : {} 15 | ignore_exceptions = options.delete(:ignore_exceptions) || false 16 | 17 | task_names.each do |task_name| 18 | old_task = Rake.application.instance_variable_get('@tasks').delete(task_name.to_s) 19 | desc old_task.full_comment 20 | task task_name => old_task.prerequisites do 21 | begin 22 | old_task.invoke 23 | rescue 24 | raise unless ignore_exceptions 25 | end 26 | 27 | new_task.call 28 | end 29 | end 30 | end 31 | end 32 | 33 | Rake::DSL.send(:include, Rake::Hooks) if defined?(Rake::DSL) 34 | include Rake::Hooks unless self.class.included_modules.include?(Rake::Hooks) 35 | -------------------------------------------------------------------------------- /lib/rake/hooks/version.rb: -------------------------------------------------------------------------------- 1 | module Rake 2 | module Hooks 3 | VERSION = "1.2.3" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /rake-hooks.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $:.push File.expand_path("../lib", __FILE__) 3 | require "rake/hooks/version" 4 | 5 | Gem::Specification.new do |s| 6 | s.name = "rake-hooks" 7 | s.version = Rake::Hooks::VERSION 8 | s.email = ["guillermo@cientifico.net", "joel@developwithstyle.com"] 9 | s.authors = ["Guillermo Álvarez", "Joel Moss"] 10 | s.summary = "Add after and before hooks to rake tasks" 11 | s.description = "Add after and before hooks to rake tasks. You can use \"after :task do ... end\" and \"before :task do ... end\"." 12 | 13 | s.rubyforge_project = "rake-hooks" 14 | 15 | s.files = `git ls-files`.split("\n") 16 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 17 | s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } 18 | s.require_paths = ["lib"] 19 | 20 | s.add_runtime_dependency "rake" 21 | 22 | s.add_development_dependency "bundler" 23 | end 24 | 25 | 26 | -------------------------------------------------------------------------------- /test/test_rake_hooks.rb: -------------------------------------------------------------------------------- 1 | require 'test/unit' 2 | require 'rake' 3 | require 'rake/hooks' 4 | 5 | class Store 6 | def self.<<(string) 7 | @@data ||= "" 8 | @@data << string 9 | end 10 | 11 | def self.to_s 12 | @@data || "" 13 | end 14 | 15 | def self.clean 16 | @@data = "" 17 | end 18 | end 19 | 20 | 21 | class TestRakeHooks < Test::Unit::TestCase 22 | if defined?(Rake::DSL) 23 | include Rake::DSL 24 | end 25 | 26 | def setup 27 | if Rake::TaskManager.respond_to?(:record_task_metadata=) 28 | Rake::TaskManager.record_task_metadata = true 29 | end 30 | Rake::Task.clear 31 | Store.clean 32 | end 33 | 34 | def test_after 35 | task :task do Store << "wadus"; end 36 | after :task do Store << "way" ; end 37 | 38 | execute(:task) 39 | 40 | assert_equal "wadusway", Store.to_s 41 | end 42 | 43 | def test_after_multiple 44 | task :task_one do Store << "wadus"; end 45 | task :task_two do Store << "badus"; end 46 | after :task_one, :task_two do Store << "way" ; end 47 | 48 | execute(:task_one) 49 | assert_equal "wadusway", Store.to_s 50 | 51 | Store.clean 52 | 53 | execute(:task_two) 54 | assert_equal "badusway", Store.to_s 55 | end 56 | 57 | def test_before 58 | task :supertask do Store << "wadus" ; end 59 | before :supertask do Store << "super" ; end 60 | 61 | execute(:supertask) 62 | assert_equal "superwadus", Store.to_s 63 | end 64 | 65 | def test_after_and_before 66 | task :super_task do Store << "wadus" ; end 67 | before :super_task do Store << "love " ; end 68 | before :super_task do Store << "I " ; end 69 | after :super_task do Store << " way" ; end 70 | after :super_task do Store << "." ; end 71 | 72 | execute(:super_task) 73 | assert_equal "I love wadus way.", Store.to_s 74 | end 75 | 76 | def test_save_comment_on_after_tasks 77 | desc 'this is my task' 78 | task :my_task do ; end 79 | 80 | after :my_task do; end 81 | assert_equal "this is my task", Rake::Task[:my_task].full_comment 82 | end 83 | 84 | 85 | def test_save_comment_on_before_tasks 86 | desc 'this is my task' 87 | task :my_task2 do ; end 88 | 89 | before :my_task2 do; end 90 | assert_equal "this is my task", Rake::Task[:my_task2].full_comment 91 | end 92 | 93 | def test_save_prerequisites_with_after_tasks 94 | task :a do ; end 95 | task :b => :a do ; end 96 | 97 | assert_equal ['a'], Rake::Task[:b].prerequisites 98 | after :b do ; end 99 | assert_equal ['a'], Rake::Task[:b].prerequisites 100 | end 101 | 102 | def test_save_prerequisites_with_before_tasks 103 | task :a do ; end 104 | task :b => :a do ; end 105 | 106 | assert_equal ['a'], Rake::Task[:b].prerequisites 107 | before :b do ; end 108 | assert_equal ['a'], Rake::Task[:b].prerequisites 109 | end 110 | 111 | def test_prerequisites_run_before_before_tasks 112 | task :a do Store << "a" ; end 113 | task :b => :a do Store << "b" ; end 114 | before :b do Store << "-BEFORE-" ; end 115 | 116 | invoke(:b) 117 | assert_equal "a-BEFORE-b", Store.to_s 118 | end 119 | 120 | def test_prerequisites_run_before_after_tasks 121 | task :a do Store << "a" ; end 122 | task :b => :a do Store << "b" ; end 123 | after :b do Store << "-AFTER-" ; end 124 | 125 | invoke(:b) 126 | assert_equal "ab-AFTER-", Store.to_s 127 | end 128 | 129 | def test_raise_exceptions_on_after_task 130 | task :a do raise "ERROR" ; end 131 | after :a do Store << "-AFTER-" ; end 132 | 133 | assert_raise RuntimeError do 134 | invoke(:a) 135 | end 136 | 137 | assert_equal "", Store.to_s 138 | end 139 | 140 | def test_ignore_exceptions_on_after_task 141 | task :a do raise "ERROR" ; end 142 | after :a, :ignore_exceptions => true do Store << "-AFTER-"; end 143 | 144 | assert_nothing_raised RuntimeError do 145 | invoke(:a) 146 | end 147 | 148 | assert_equal "-AFTER-", Store.to_s 149 | end 150 | 151 | def execute(task_name) 152 | Rake::Task[task_name].execute 153 | end 154 | 155 | def invoke(task_name) 156 | Rake::Task[task_name].invoke 157 | end 158 | end 159 | --------------------------------------------------------------------------------