Sha256: 633f94cee24d3cd7ae8c46ed1f8240c71b2a3b7e311f292224635a4ea37dd0c1

Contents?: true

Size: 1.77 KB

Versions: 11

Compression:

Stored size: 1.77 KB

Contents

module BootstrapEmail
  class SassCache
    SASS_DIR = File.expand_path('../../core', __dir__)

    def self.compile(type, style: :compressed)
      new(type, style).compile
    end

    attr_accessor :type, :style, :file_path, :config_file, :checksum

    def initialize(type, style)
      self.type = type
      self.style = style
      self.file_path = "#{SASS_DIR}/#{type}"
      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"
      unless cached?(cache_path)
        compile_and_cache_scss(cache_path)
      end
      File.read(cache_path)
    end

    private

    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

    def checksum_files
      checksums = config_file.nil? ? [] : [Digest::SHA1.hexdigest(config_file)]
      Dir.glob('../../core/**/*.scss', base: __dir__).each do |path|
        checksums << Digest::SHA1.file(File.expand_path(path, __dir__)).hexdigest
      end
      Digest::SHA1.hexdigest(checksums.join)
    end

    def cached?(cache_path)
      File.file?(cache_path)
    end

    def compile_and_cache_scss(cache_path)
      file = config_file || File.read("#{file_path}.scss")
      css = SassC::Engine.new(file, style: style).render
      FileUtils.mkdir_p("#{cache_dir}/#{checksum}") unless File.directory?("#{cache_dir}/#{checksum}")
      File.write(cache_path, css)
      if BootstrapEmail.config.sass_log_enabled?
        puts "New css file cached for #{type}"
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
bootstrap-email-1.1.4 lib/bootstrap-email/sass_cache.rb
bootstrap-email-1.1.3 lib/bootstrap-email/sass_cache.rb
bootstrap-email-1.1.2 lib/bootstrap-email/sass_cache.rb
bootstrap-email-1.1.1 lib/bootstrap-email/sass_cache.rb
bootstrap-email-1.1.0 lib/bootstrap-email/sass_cache.rb
bootstrap-email-1.0.2 lib/bootstrap-email/sass_cache.rb
bootstrap-email-1.0.1 lib/bootstrap-email/sass_cache.rb
bootstrap-email-1.0.0 lib/bootstrap-email/sass_cache.rb
bootstrap-email-1.0.0.alpha4 lib/bootstrap-email/sass_cache.rb
bootstrap-email-1.0.0.alpha3.1 lib/bootstrap-email/sass_cache.rb
bootstrap-email-1.0.0.alpha3 lib/bootstrap-email/sass_cache.rb