lib/barista/compiler.rb in barista-0.5.1 vs lib/barista/compiler.rb in barista-0.6.0

- old
+ new

@@ -1,86 +1,51 @@ require 'digest/sha2' -require 'open4' module Barista class Compiler - class << self; attr_accessor :bin_path; end + class << self + attr_accessor :bin_path, :js_path + end self.bin_path ||= "coffee" + self.js_path ||= File.expand_path('../coffee-script/coffee-script-0.9.4.js', File.dirname(__FILE__)) - def self.available? - @coffee_available ||= system("which '#{self.bin_path}' >/dev/null 2>&1") + def self.compilers + [Compilers::Native, Compilers::Node] end - - def self.check_availability!(silence = false) - available?.tap do |available| - if !available && Barista.exception_on_error? && !silence - raise CompilerUnavailableError, "The coffeescript compiler '#{self.bin_path}' could not be found." - end - end + + def self.compiler=(value) + name = "Barista::Compilers::#{value.to_s.classify}".constantize + self.compiler_klass = name + rescue + self.compiler_klass = nil end - - def self.compile(path, options = {}) - new(path, options).to_js + + def self.compiler + compiler_klass.name.underscore.gsub("barista/compilers/", '').to_sym end - - def initialize(path, options = {}) - @compiled = false - @options = {} - @path = path + + def self.compiler_klass + @compiler_klass ||= compilers.detect(&:available?) end - - def compile! - # Compiler code thanks to bistro_car. - @compiled_content = invoke_coffee(@path) - @compiled = true + + def self.compiler_klass=(value) + @compiler_klass = value.present? ? value : nil end - def to_js - compile! unless @compiled - @compiled_content + def self.available? + compiler_klass.present? end - def self.dirty?(from, to) - File.exist?(from) && (!File.exist?(to) || File.mtime(to) < File.mtime(from)) - end - - protected - - def coffee_options - ["-p"].tap do |options| - options << "--no-wrap" if Barista.no_wrap? - end.join(" ") - end - - def invoke_coffee(path) - command = "#{self.class.bin_path} #{coffee_options} '#{path}'".squeeze(' ') - Barista.invoke_hook :before_compilation, path - - #jruby cannot use open4 because it uses fork. - #This should hopefully work for both jruby and ruby - popen_class = IO.respond_to?(:popen4) ? IO : Open4 - - pid, stdin, stdout, stderr = popen_class.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 + def self.check_availability!(silence = false) + available?.tap do |available| + if !available && Barista.exception_on_error? && !silence + raise CompilerUnavailableError, "A coffee script compiler is currently unavailable. Please install therubyracer or coffee-script via node" end - else - Barista.invoke_hook :compilation_failed, path, err - if Barista.exception_on_error? && !@options[:silence] - raise CompilationError, "\"#{command}\" exited with a non-zero status." - else - out = nil - end end - out end + def self.compile(path, options = {}) + compiler_klass.new(path, options).to_js + end end end