Class: Barista::Compiler
- Inherits:
-
Object
- Object
- Barista::Compiler
- Defined in:
- lib/barista/compiler.rb
Constant Summary collapse
- UNAVAILABLE_MESSAGE =
"No method of compiling coffee-script is currently available. Please see the ExecJS page (https://github.com/sstephenson/execjs) for details on how to set one up."
Class Method Summary collapse
- .autocompile_file(file, force = false, silence_error = false) ⇒ Object
- .available? ⇒ Boolean
- .bin_path ⇒ Object
- .bin_path=(path) ⇒ Object
- .check_availability!(silence = false) ⇒ Object
- .compile(content, options = {}) ⇒ Object
- .compile_as(file, type) ⇒ Object
- .dirty?(from, to) ⇒ Boolean
- .execjs_runtime_call(method, *args) ⇒ Object
- .js_path ⇒ Object
- .js_path=(value) ⇒ Object
- .setup_default_error_logger ⇒ Object
Instance Method Summary collapse
- #compile(script, where = 'inline') ⇒ Object
- #compile! ⇒ Object
- #copyable?(location) ⇒ Boolean
-
#initialize(context, options = {}) ⇒ Compiler
constructor
A new instance of Compiler.
- #save(path = @options[:output_path]) ⇒ Object
- #to_js ⇒ Object
Constructor Details
#initialize(context, options = {}) ⇒ Compiler
Returns a new instance of Compiler.
105 106 107 108 109 |
# File 'lib/barista/compiler.rb', line 105 def initialize(context, = {}) @compiled = false @options = setup_compiler_context context end |
Class Method Details
.autocompile_file(file, force = false, silence_error = false) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/barista/compiler.rb', line 52 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) return File.read(destination_path) else return nil end end return File.read(destination_path) unless dirty?(origin_path, destination_path) || force # Ensure we have a coffeescript compiler available. if !check_availability!(silence_error) Barista.debug UNAVAILABLE_MESSAGE return nil end Barista.debug "Compiling #{file} from framework '#{framework.name}'" compiler = new(origin_path, :silence_error => silence_error, :output_path => destination_path) content = compiler.to_js compiler.save content end |
.available? ⇒ Boolean
36 37 38 |
# File 'lib/barista/compiler.rb', line 36 def available? ExecJS.runtime and ExecJS.runtime.available? end |
.bin_path ⇒ Object
19 20 21 |
# File 'lib/barista/compiler.rb', line 19 def bin_path execjs_runtime_call :binary end |
.bin_path=(path) ⇒ Object
23 24 25 |
# File 'lib/barista/compiler.rb', line 23 def bin_path=(path) execjs_runtime_call :binary=, path end |
.check_availability!(silence = false) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/barista/compiler.rb', line 40 def check_availability!(silence = false) available = available? if !available && Barista.exception_on_error? && !silence raise CompilerUnavailableError, UNAVAILABLE_MESSAGE end available end |
.compile(content, options = {}) ⇒ Object
48 49 50 |
# File 'lib/barista/compiler.rb', line 48 def compile(content, = {}) self.new(content, ).to_js end |
.compile_as(file, type) ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/barista/compiler.rb', line 80 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 |
.dirty?(from, to) ⇒ Boolean
90 91 92 |
# File 'lib/barista/compiler.rb', line 90 def dirty?(from, to) File.exist?(from) && (!File.exist?(to) || File.mtime(to) < File.mtime(from)) end |
.execjs_runtime_call(method, *args) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/barista/compiler.rb', line 27 def execjs_runtime_call(method, *args) runtime = ExecJS.runtime if runtime.respond_to?(method, true) runtime.send method, *args else nil end end |
.js_path ⇒ Object
11 12 13 |
# File 'lib/barista/compiler.rb', line 11 def js_path CoffeeScript::Source.path end |
.js_path=(value) ⇒ Object
15 16 17 |
# File 'lib/barista/compiler.rb', line 15 def js_path=(value) CoffeeScript::Source.path = value end |
.setup_default_error_logger ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/barista/compiler.rb', line 94 def setup_default_error_logger Barista.on_compilation_error do |where, | if Barista.verbose? Barista.debug "There was an error compiling coffeescript from #{where}:" .each_line { |line| Barista.debug line.rstrip } end end end |
Instance Method Details
#compile(script, where = 'inline') ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/barista/compiler.rb', line 127 def compile(script, where = 'inline') if copyable?(where) out = script else Barista.invoke_hook :before_compilation, where out = CoffeeScript.compile script, :bare => Barista. Barista.invoke_hook :compiled, where end out rescue ExecJS::Error => e Barista.invoke_hook :compilation_failed, where, e. if Barista.exception_on_error? && !@options[:silence] if e.is_a?(ExecJS::ProgramError) where_within_app = where.sub(/#{Regexp.escape(Barista.app_root.to_s)}\/?/, '') raise CompilationError, "Error: In #{where_within_app}, #{e.}" else raise CompilationError, "CoffeeScript encountered an error compiling #{where}: #{e.}" end end compilation_error_for where, e. end |
#compile! ⇒ Object
111 112 113 114 115 116 |
# File 'lib/barista/compiler.rb', line 111 def compile! location = @options.fetch(:origin, 'inline') @compiled_content = compile(@context, location) @compiled_content = preamble(location) + @compiled_content if location != 'inline' && Barista.add_preamble? @compiled = true end |
#copyable?(location) ⇒ Boolean
123 124 125 |
# File 'lib/barista/compiler.rb', line 123 def copyable?(location) location != 'inline' && File.extname(location) == '.js' end |
#save(path = @options[:output_path]) ⇒ Object
149 150 151 152 153 154 155 156 |
# File 'lib/barista/compiler.rb', line 149 def save(path = @options[:output_path]) return false unless path.is_a?(String) && !to_js.nil? FileUtils.mkdir_p File.dirname(path) File.open(path, "w+") { |f| f.write @compiled_content } true rescue Errno::EACCES false end |
#to_js ⇒ Object
118 119 120 121 |
# File 'lib/barista/compiler.rb', line 118 def to_js compile! unless defined?(@compiled) && @compiled @compiled_content end |