Permalink
Browse files

Use Eventful to improve event publishing during a build. Also removin…

…g logging code, which should be done using event handlers.
  • Loading branch information...
1 parent 2959171 commit ef1c8dac5cb2108d031cdb7f898e402bfc61ed6a @jcoglan committed Jun 18, 2009
Showing with 14 additions and 38 deletions.
  1. +0 −1 Manifest.txt
  2. +5 −3 README.rdoc
  3. +1 −1 Rakefile
  4. +3 −2 lib/jake.rb
  5. +2 −9 lib/jake/build.rb
  6. +1 −4 lib/jake/buildable.rb
  7. +0 −16 lib/jake/observer.rb
  8. +2 −2 test/Jakefile
View
@@ -5,7 +5,6 @@ Rakefile
bin/jake
lib/jake.rb
lib/jake/helper.rb
-lib/jake/observer.rb
lib/jake/build.rb
lib/jake/buildable.rb
lib/jake/package.rb
View
@@ -231,10 +231,12 @@ name. When built, the output would contain the following:
The +Jakefile+ may also define event hooks that are fired during a build when
interesting things happen. This allows you to extend your build process using
-configuration data from Jake. We currently have two events:
+configuration data from Jake. All event callbacks are passed a +Build+ object
+as the first argument, and may receive additional arguments depending on the
+event type. We currently have two events:
+file_created+ is fired whenever a new build file is created. The callback is
-passed the +Buildable+ package object, the current build name (+src+ or +min+
+passed the +Buildable+ package object, the current build type (+src+ or +min+
using the above examples), and the full path to the newly created file.
The package object may contain metadata (set using the +meta+ option, see
above) which you can use for further code generation.
@@ -246,7 +248,7 @@ directory, etc.
$register = {}
- jake_hook :file_created do |pkg, build_name, path|
+ jake_hook :file_created do |build, pkg, build_type, path|
$register[path] = pkg.meta
end
View
@@ -7,7 +7,7 @@ require './lib/jake.rb'
Hoe.spec('jake') do |p|
# p.rubyforge_name = 'jakex' # if different than lowercase project name
p.developer('James Coglan', 'jcoglan@googlemail.com')
- p.extra_deps = %w(oyster packr)
+ p.extra_deps = %w(eventful packr oyster)
end
# vim: syntax=Ruby
View
@@ -4,6 +4,7 @@
require 'yaml'
require 'rubygems'
require 'packr'
+require 'eventful'
begin
require 'erubis'
@@ -54,7 +55,7 @@ def self.erb(template)
end
-%w(helper observer build buildable package bundle).each do |file|
+%w(helper build buildable package bundle).each do |file|
require File.dirname(__FILE__) + '/jake/' + file
end
@@ -68,6 +69,6 @@ def jake_helper(name, &block)
# Registers an event listener that will fire whenever a build is run.
def jake_hook(type, &block)
- Jake::Observer.new(type, &block)
+ Jake::Build.on(type, &block)
end
View
@@ -3,14 +3,7 @@ module Jake
# is responsible for running the build and provides access to any configuration
# data used to set up the build.
class Build
- extend Observable
-
- # Calls all registered +Observer+ instances with any arguments passed in. Note
- # that it is the +Build+ class that is +Observable+, not its instances.
- def self.notify_observers(*args)
- self.changed(true)
- super
- end
+ include Eventful
DEFAULT_LAYOUT = 'together'
@@ -71,7 +64,7 @@ def run!
FileUtils.cd(@dir) do
@packages.each { |name, pkg| pkg.write! }
@bundles.each { |name, pkg| pkg.write! }
- self.class.notify_observers(:build_complete, self)
+ fire(:build_complete)
end
end
View
@@ -79,8 +79,6 @@ def meta
# options. Files are only generated if they are out of date or the build has
# been forced.
def write!
- puts "Package #{@name}..."
-
@build.each do |name, settings|
next unless build_needed?(name)
@@ -89,11 +87,10 @@ def write!
FileUtils.mkdir_p(File.dirname(path))
File.open(path, 'wb') { |f| f.write( (header + "\n\n" + code(name)).strip ) }
- @build.class.notify_observers(:file_created, self, name, path)
+ @build.fire(:file_created, self, name, path)
size = (File.size(path)/1024.0).ceil
path = path.sub(@build.build_directory, '')
- puts " -- build '#{ name }' created #{ path }, #{ size } kb"
end
end
View
@@ -1,16 +0,0 @@
-module Jake
- # The +Observer+ class is used in conjunction with Ruby's +Observable+
- # module to provide typed event listeners. Use +jake_hook+ to register
- # new listeners in Jakefiles.
- class Observer
- def initialize(type, &block)
- @type, @block = type, block
- Build.add_observer(self)
- end
-
- def update(*args)
- @block[*args[1..-1]] if args.first == @type
- end
- end
-end
-
View
@@ -5,9 +5,9 @@ end
FILES = []
DEPS = {}
-jake_hook :file_created do |package, build, path|
+jake_hook :file_created do |build, package, build_type, path|
FILES << File.basename(path)
- DEPS[path] = package.meta if build == :min
+ DEPS[path] = package.meta if build_type == :min
end
jake_hook :build_complete do |build|

0 comments on commit ef1c8da

Please sign in to comment.