Skip to content

Commit

Permalink
Added support for a dynamic preamble.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Aug 30, 2011
1 parent 8208fe6 commit a9294b2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 16 additions & 6 deletions lib/barista.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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_boolean_options :verbose, :bare, :add_filter, :exception_on_error, :embedded_interpreter, :auto_compile, :add_preamble
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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lib/barista/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion lib/barista/hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ def invoke(name, *args)
nil
end

def has_hook?(name)
@callbacks.has_key?(name)
end

end
end
end
2 changes: 1 addition & 1 deletion spec/barista_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@

context 'compiling all'

end
end

0 comments on commit a9294b2

Please sign in to comment.