lib/barista/compiler.rb in barista-1.1.0.pre1 vs lib/barista/compiler.rb in barista-1.1.0
- old
+ new
@@ -11,25 +11,42 @@
end
def js_path=(value)
CoffeeScript::Source.path = value
end
-
+
def bin_path
- CoffeeScript::Engines::Node.binary
+ if defined?(CoffeeScript::Engines::Node)
+ CoffeeScript::Engines::Node.binary
+ else
+ execjs_runtime_call :binary
+ end
end
-
+
def bin_path=(path)
- CoffeeScript::Engines::Node.binary = path
+ if defined?(CoffeeScript::Engines::Node)
+ CoffeeScript::Engines::Node.binary = path
+ else
+ execjs_runtime_call :binary=, path
+ end
end
-
+
+ def execjs_runtime_call(method, *args)
+ runtime = ExecJS.runtime
+ if runtime.respond_to?(method, true)
+ runtime.send method, *args
+ else
+ nil
+ end
+ end
+
def available?
CoffeeScript.engine && CoffeeScript.engine.supported?
end
def check_availability!(silence = false)
- available = available?
+ available = available?
if !available && Barista.exception_on_error? && !silence
raise CompilerUnavailableError, "No method of compiling cofffescript is currently available. Please install therubyracer or node."
end
available
end
@@ -41,16 +58,16 @@
def autocompile_file(file, force = false, silence_error = false)
# Expand the path from the framework.
origin_path, framework = Framework.full_path_for(file)
return if origin_path.nil?
destination_path = framework.output_path_for(file)
-
+
# read file directly if auto_compile is disabled
- if !Barista.auto_compile?
- if File.exist?(destination_path)
+ if !Barista.auto_compile?
+ if File.exist?(destination_path)
return File.read(destination_path)
- else
+ else
return nil
end
end
return File.read(destination_path) unless dirty?(origin_path, destination_path) || force
@@ -63,36 +80,36 @@
compiler = new(origin_path, :silence_error => silence_error, :output_path => destination_path)
content = compiler.to_js
compiler.save
content
end
-
+
def compile_as(file, type)
origin_path, framework = Framework.full_path_for(file)
return if origin_path.nil?
if type == :coffeescript
return File.read(origin_path), File.mtime(origin_path)
else
return autocompile_file(file), Time.now
end
end
-
+
def dirty?(from, to)
File.exist?(from) && (!File.exist?(to) || File.mtime(to) < File.mtime(from))
end
-
+
def setup_default_error_logger
Barista.on_compilation_error do |where, message|
if Barista.verbose?
Barista.debug "There was an error compiling coffeescript from #{where}:"
message.each_line { |line| Barista.debug line.rstrip }
end
end
end
-
+
end
-
+
def initialize(context, options = {})
@compiled = false
@options = options
setup_compiler_context context
end
@@ -106,15 +123,23 @@
def to_js
compile! unless defined?(@compiled) && @compiled
@compiled_content
end
-
+
+ def copyable?(location)
+ location != 'inline' && File.extname(location) == '.js'
+ end
+
def compile(script, where = 'inline')
- Barista.invoke_hook :before_compilation, where
- out = CoffeeScript.compile script, :bare => Barista.bare?
- Barista.invoke_hook :compiled, where
+ if copyable?(where)
+ out = script
+ else
+ Barista.invoke_hook :before_compilation, where
+ out = CoffeeScript.compile script, :bare => Barista.bare?
+ Barista.invoke_hook :compiled, where
+ end
out
rescue CoffeeScript::Error => e
Barista.invoke_hook :compilation_failed, where, e.message
if Barista.exception_on_error? && !@options[:silence]
if e.is_a?(CoffeeScript::CompilationError)
@@ -137,18 +162,19 @@
end
protected
def preamble(location)
- "/* DO NOT MODIFY. This file was compiled #{Time.now.httpdate} from\n * #{location.strip}\n */\n\n"
+ inner_message = copyable?(location) ? "copied" : "compiled"
+ "/* DO NOT MODIFY. This file was #{inner_message} #{Time.now.httpdate} from\n * #{location.strip}\n */\n\n"
end
-
+
def compilation_error_for(location, message)
details = "Compilation of '#{location}' failed:\n#{message}"
Barista.verbose? ? "alert(#{details.to_json});" : nil
end
-
+
def setup_compiler_context(context)
if context.respond_to?(:read)
@context = context.read
@type = :string
default_path = context.respond_to?(:path) ? context.path : 'inline'
@@ -161,8 +187,8 @@
@context = context.to_s
@type = :string
@options[:origin] ||= 'inline'
end
end
-
+
end
end