Permalink
Browse files

Update to support hooks

  • Loading branch information...
1 parent 91a269d commit fd82e0f935ea70ef51161ca68770679bb6696b5a @Sutto committed Jul 18, 2010
Showing with 42 additions and 15 deletions.
  1. +2 −0 README.md
  2. +1 −0 Rakefile
  3. +5 −1 barista.gemspec
  4. +10 −6 lib/barista.rb
  5. +16 −7 lib/barista/compiler.rb
  6. +8 −1 lib/generators/barista_install/templates/initializer.rb
View
@@ -66,8 +66,10 @@ To hook into these hooks, you can use like so:
* `Barista.before_compilation { |path| puts "Barista: Compiling #{path}" }`
* `Barista.on_compilation { |path| puts "Barista: Successfully compiled #{path}" }`
+* `Barista.on_compilation_with_warning { |path, output| puts "Barista: Compilation of #{path} had a warning:\n#{output}" }`
* `Barista.on_compilation_error { |path, output| puts "Barista: Compilation of #{path} failed with:\n#{output}" }`
+
These allow you to do things such as notify on compilation, automatically
perform compression post compilation and a variety of other cool things.
View
@@ -13,6 +13,7 @@ begin
gem.homepage = "http://github.com/Sutto/barista"
gem.version = Barista::Version::STRING
gem.authors = ["Darcy Laycock"]
+ gem.add_dependency 'open4'
end
Jeweler::GemcutterTasks.new
rescue LoadError
View
@@ -5,7 +5,7 @@
Gem::Specification.new do |s|
s.name = %q{barista}
- s.version = "0.4.3"
+ s.version = "0.5.0"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Darcy Laycock"]
@@ -29,6 +29,7 @@ Gem::Specification.new do |s|
"lib/barista/compiler.rb",
"lib/barista/filter.rb",
"lib/barista/framework.rb",
+ "lib/barista/hooks.rb",
"lib/barista/tasks/barista.rake",
"lib/barista/version.rb",
"lib/generators/barista_install/USAGE",
@@ -46,9 +47,12 @@ Gem::Specification.new do |s|
s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<open4>, [">= 0"])
else
+ s.add_dependency(%q<open4>, [">= 0"])
end
else
+ s.add_dependency(%q<open4>, [">= 0"])
end
end
View
@@ -18,24 +18,28 @@ def hooks
@hooks ||= Hooks.new
end
- def on_hook!(name, *args, &blk)
+ def on_hook(name, *args, &blk)
hooks.on(name, *args, &blk)
end
- def invoke_hook!(name, *args)
- hooks.invoke!(name, *args)
+ def invoke_hook(name, *args)
+ hooks.invoke(name, *args)
end
def on_compilation_error(&blk)
- on_hook! :compilation_failed, &blk
+ on_hook :compilation_failed, &blk
end
def on_compilation(&blk)
- on_hook! :compiled, &blk
+ on_hook :compiled, &blk
+ end
+
+ def on_compilation_with_warning(&blk)
+ on_hook :compiled_with_warning, &blk
end
def before_compilation(&blk)
- on_hook! :before_compilation, &blk
+ on_hook :before_compilation, &blk
end
def configure
@@ -1,4 +1,5 @@
require 'digest/sha2'
+require 'open4'
module Barista
class Compiler
@@ -53,19 +54,27 @@ def coffee_options
def invoke_coffee(path)
command = "#{self.class.bin_path} #{coffee_options} '#{path}'".squeeze(' ')
- Barista.invoke_hook! :before_compilation, path
- result = %x(#{command}).to_s
- if $?.success?
- Barista.invoke_hook! :compiled, path
+ Barista.invoke_hook :before_compilation, path
+ pid, stdin, stdout, stderr = Open4.popen4(command)
+ stdin.close
+ _, status = Process.waitpid2(pid)
+ out = stdout.read.strip
+ err = stderr.read.strip
+ if status.success?
+ if err.blank?
+ Barista.invoke_hook :compiled, path
+ else
+ Barista.invoke_hook :compiled_with_warning, path, err
+ end
else
- Barista.invoke_hook! :compilation_failed, path, result
+ Barista.invoke_hook :compilation_failed, path, err
if Barista.exception_on_error? && !@options[:silence]
raise CompilationError, "\"#{command}\" exited with a non-zero status."
else
- result = nil
+ out = nil
end
end
- result
+ out
end
end
@@ -24,6 +24,13 @@
# or, prefix the path for the app files:
- # c.change_output_prefix! :default, 'my-app-name'
+ # c.change_output_prefix! :default, 'my-app-name'
+
+ # or, hook into the compilation:
+
+ # c.before_compilation { |path| puts "Barista: Compiling #{path}" }
+ # c.on_compilation { |path| puts "Barista: Successfully compiled #{path}" }
+ # c.on_compilation_error { |path, output| puts "Barista: Compilation of #{path} failed with:\n#{output}" }
+ # c.on_compilation_with_warning { |path, output| puts "Barista: Compilation of #{path} had a warning:\n#{output}" }
end

0 comments on commit fd82e0f

Please sign in to comment.