Permalink
Browse files

Merge pull request #54 from dblock/barista-preamble-nospec

Support for a custom preamble
  • Loading branch information...
2 parents d7f5084 + 48339ee commit 930db883e941eafd52b7098f2404b26f3c31bf2f @Sutto committed Sep 3, 2011
Showing with 34 additions and 8 deletions.
  1. +8 −0 README.md
  2. +15 −5 lib/barista.rb
  3. +6 −2 lib/barista/compiler.rb
  4. +5 −1 lib/barista/hooks.rb
View
@@ -154,6 +154,14 @@ and `#option!` (to set the value to true):
* `bin_path` – The path to the `node` executable if non-standard and not using `therubyracer`.
* All of the hook methods mentioned below.
+### Custom Preamble
+
+You can generate a custom preamble using a code block. For example, you can replace the location of the original `.coffee` file by a relative one to `Rails.root`.
+
+ Barista.add_preamble do |location|
+ "/* : DO NOT MODIFY - compiled from #{Pathname.new(location).relative_path_from(Rails.root).to_s}\n\n"
+ end
+
## Frameworks
One of the other main features Barista adds (over other tools) is frameworks similar
View
@@ -46,6 +46,10 @@ def on_hook(name, *args, &blk)
def invoke_hook(name, *args)
hooks.invoke(name, *args)
end
+
+ def has_hook?(name)
+ hooks.has_hook?(name)
+ end
has_hook_method :on_compilation_error => :compilation_failed,
:on_compilation => :compiled,
@@ -54,14 +58,24 @@ def invoke_hook(name, *args)
:before_full_compilation => :before_full_compilation,
:before_compilation => :before_compilation
-
# Configuration - Tweak how you use Barista.
has_boolean_options :verbose, :bare, :add_filter, :add_preamble, :exception_on_error, :embedded_interpreter, :auto_compile
has_delegate_methods Compiler, :bin_path, :bin_path=, :js_path, :js_path=
has_delegate_methods Framework, :register
has_deprecated_methods :compiler, :compiler=, :compiler_klass, :compiler_klass=
+ def add_preamble(&blk)
+ self.add_preamble = true
+ if block_given?
+ @preamble = blk
+ end
+ end
+
+ def preamble
+ @preamble
+ end
+
def configure
yield self if block_given?
end
@@ -159,10 +173,6 @@ def default_for_add_filter
local_env?
end
- def default_for_add_preamble
- local_env?
- end
-
def default_for_exception_on_error
true
end
View
@@ -114,7 +114,7 @@ def compile!
@compiled_content = preamble(location) + @compiled_content if location != 'inline' && Barista.add_preamble?
@compiled = true
end
-
+
def to_js
compile! unless defined?(@compiled) && @compiled
@compiled_content
@@ -159,7 +159,11 @@ def save(path = @options[:output_path])
def preamble(location)
inner_message = copyable?(location) ? "copied" : "compiled"
- "/* DO NOT MODIFY. This file was #{inner_message} #{Time.now.httpdate} from\n * #{location.strip}\n */\n\n"
+ if Barista.preamble
+ Barista.preamble.call(location)
+ else
+ "/* DO NOT MODIFY. This file was #{inner_message} #{Time.now.httpdate} from\n * #{location.strip}\n */\n\n"
+ end
end
def compilation_error_for(location, message)
View
@@ -16,5 +16,9 @@ def invoke(name, *args)
nil
end
+ def has_hook?(name)
+ @callbacks.has_key?(name)
+ end
+
end
-end
+end

0 comments on commit 930db88

Please sign in to comment.