lib/barista.rb in barista-0.1.5 vs lib/barista.rb in barista-0.2.0
- old
+ new
@@ -1,12 +1,13 @@
require 'active_support'
require 'pathname'
module Barista
- autoload :Compiler, 'barista/compiler'
- autoload :Filter, 'barista/filter'
+ autoload :Compiler, 'barista/compiler'
+ autoload :Filter, 'barista/filter'
+ autoload :Framework, 'barista/framework'
class << self
def configure
yield self if block_given?
@@ -16,49 +17,49 @@
@root ||= Rails.root.join("app", "coffeescripts")
end
def root=(value)
@root = Pathname(value.to_s)
+ Framework.default_framework = nil
end
def output_root
@output_root ||= Rails.root.join("public", "javascripts")
end
def output_root=(value)
@output_root = Pathname(value.to_s)
end
- def render_path(path)
- full_path = root.join("#{path.gsub(/(\A\/|\/\Z)/, '')}.coffee")
- return unless full_path.exist? && full_path.readable?
- Compiler.compile(full_path.read)
- rescue SystemCallError
- nil
- end
-
def compile_file!(file, force = false)
- file = file.to_s
- file = root.join(file).to_s unless file.include?(root.to_s)
- destination_path = file.gsub(/\.(coffee|js)\Z/, '').gsub(root.to_s, output_root.to_s) + ".js"
- return unless force || should_compile_file?(file, destination_path)
- Rails.logger.debug "[Barista] Compiling #{file} to #{destination_path}"
+ origin_path, framework = Framework.full_path_for(file)
+ return if origin_path.blank?
+ destination_path = self.output_path_for(file)
+ return unless force || Compiler.dirty?(origin_path, destination_path)
+ debug "Compiling #{file} from framework '#{framework.name}'"
FileUtils.mkdir_p File.dirname(destination_path)
File.open(destination_path, "w+") do |f|
- f.write Compiler.compile(File.read(file))
+ f.write Compiler.compile(File.read(origin_path))
end
true
rescue SystemCallError
false
end
- def should_compile_file?(from, to)
- File.exist?(from) && (!File.exist?(to) || File.mtime(to) < File.mtime(from))
- end
-
def compile_all!(force = false)
- Dir[root.join("**", "*.coffee")].each {|file| compile_file! file, force }
+ debug "Compiling all coffeescripts"
+ Framework.exposed_coffeescripts.each do |coffeescript|
+ compile_file! coffeescript, force
+ end
true
+ end
+
+ def output_path_for(file)
+ output_root.join(file.to_s.gsub(/^\/+/, '')).to_s.gsub(/\.coffee$/, '.js')
+ end
+
+ def debug(message)
+ Rails.logger.debug "[Barista] #{message}" if defined?(Rails.logger) && Rails.logger
end
# By default, only add it in dev / test
def add_filter?
Rails.env.test? || Rails.env.development?
\ No newline at end of file