lib/bootstrap-email/sass_cache.rb in bootstrap-email-1.0.0.alpha2.1 vs lib/bootstrap-email/sass_cache.rb in bootstrap-email-1.0.0.alpha3

- old
+ new

@@ -1,44 +1,40 @@ module BootstrapEmail class SassCache - CACHE_DIR = File.expand_path('../../.sass-cache', __dir__) SASS_DIR = File.expand_path('../../core', __dir__) - def self.compile(type, config_path: nil, style: :compressed) - new(type, config_path, style).compile + def self.compile(type, style: :compressed) + new(type, style).compile end attr_accessor :type, :style, :file_path, :config_file, :checksum - def initialize(type, config_path, style) + def initialize(type, style) self.type = type self.style = style self.file_path = "#{SASS_DIR}/#{type}" - self.config_file = load_config(config_path) + self.config_file = load_config self.checksum = checksum_files end + def cache_dir + BootstrapEmail.config.sass_cache_location + end + def compile - cache_path = "#{CACHE_DIR}/#{checksum}/#{type}.css" + cache_path = "#{cache_dir}/#{checksum}/#{type}.css" unless cached?(cache_path) compile_and_cache_scss(cache_path) end File.read(cache_path) end private - def load_config(config_path) - lookup_locations = ["#{type}.config.scss", "app/assets/stylesheets/#{type}.config.scss"] - locations = lookup_locations.select { |location| File.exist?(File.expand_path(location, Dir.pwd)) } - if config_path && File.exist?(config_path) - # check if custom config was passed in - replace_config(File.read(config_path)) - elsif locations.any? - # look for common lookup locations of config - replace_config(File.read(File.expand_path(locations.first, Dir.pwd))) - end + def load_config + path = BootstrapEmail.config.sass_location_for(type: type) + replace_config(File.read(path)) if path end def replace_config(config_file) config_file.gsub("//= @import #{type};", "@import '#{file_path}';") end @@ -56,12 +52,13 @@ end def compile_and_cache_scss(cache_path) file = config_file || File.read("#{file_path}.scss") css = SassC::Engine.new(file, style: style).render - Dir.mkdir(CACHE_DIR) unless File.directory?(CACHE_DIR) - Dir.mkdir("#{CACHE_DIR}/#{checksum}") unless File.directory?("#{CACHE_DIR}/#{checksum}") + FileUtils.mkdir_p("#{cache_dir}/#{checksum}") unless File.directory?("#{cache_dir}/#{checksum}") File.write(cache_path, css) - puts "New css file cached for #{type}" + if BootstrapEmail.config.sass_log_enabled? + puts "New css file cached for #{type}" + end end end end