Sha256: 23db4a2c27137b6e3cf3832aa5b1bcaca0525cbf65f92969f4ada494ac8f8462

Contents?: true

Size: 1.73 KB

Versions: 37

Compression:

Stored size: 1.73 KB

Contents

require 'fileutils'

module Sprockets
  class StaticCompiler
    attr_accessor :env, :target, :paths

    def initialize(env, target, paths, options = {})
      @env = env
      @target = target
      @paths = paths
      @digest = options.key?(:digest) ? options.delete(:digest) : true
      @manifest = options.key?(:manifest) ? options.delete(:manifest) : true
      @manifest_path = options.delete(:manifest_path) || target
    end

    def compile
      manifest = {}
      env.each_logical_path do |logical_path|
        if File.basename(logical_path)[/[^\.]+/, 0] == 'index'
          logical_path.sub!(/\/index\./, '.')
        end
        next unless compile_path?(logical_path)
        if asset = env.find_asset(logical_path)
          manifest[logical_path] = write_asset(asset)
        end
      end
      write_manifest(manifest) if @manifest
    end

    def write_manifest(manifest)
      FileUtils.mkdir_p(@manifest_path)
      File.open("#{@manifest_path}/manifest.yml", 'wb') do |f|
        YAML.dump(manifest, f)
      end
    end

    def write_asset(asset)
      path_for(asset).tap do |path|
        filename = File.join(target, path)
        FileUtils.mkdir_p File.dirname(filename)
        asset.write_to(filename)
        asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/
      end
    end

    def compile_path?(logical_path)
      paths.each do |path|
        case path
        when Regexp
          return true if path.match(logical_path)
        when Proc
          return true if path.call(logical_path)
        else
          return true if File.fnmatch(path.to_s, logical_path)
        end
      end
      false
    end

    def path_for(asset)
      @digest ? asset.digest_path : asset.logical_path
    end
  end
end

Version data entries

37 entries across 31 versions & 5 rubygems

Version Path
actionpack-3.1.12 lib/sprockets/static_compiler.rb
actionpack-3.1.11 lib/sprockets/static_compiler.rb
actionpack-3.1.10 lib/sprockets/static_compiler.rb
actionpack-3.1.9 lib/sprockets/static_compiler.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/actionpack-3.2.8/lib/sprockets/static_compiler.rb
challah-rolls-0.2.0 vendor/bundle/gems/actionpack-3.2.7/lib/sprockets/static_compiler.rb
challah-rolls-0.2.0 vendor/bundle/gems/actionpack-3.2.8/lib/sprockets/static_compiler.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/actionpack-3.2.7/lib/sprockets/static_compiler.rb
challah-0.8.3 vendor/bundle/gems/actionpack-3.2.8/lib/sprockets/static_compiler.rb
challah-0.8.1 vendor/bundle/gems/actionpack-3.2.8/lib/sprockets/static_compiler.rb
challah-rolls-0.1.0 vendor/bundle/gems/challah-0.8.0.pre/vendor/bundle/gems/actionpack-3.2.7/lib/sprockets/static_compiler.rb
challah-rolls-0.1.0 vendor/bundle/gems/actionpack-3.2.8/lib/sprockets/static_compiler.rb
challah-rolls-0.1.0 vendor/bundle/gems/actionpack-3.2.7/lib/sprockets/static_compiler.rb
challah-0.8.0.pre vendor/bundle/gems/actionpack-3.2.7/lib/sprockets/static_compiler.rb
challah-0.7.1 vendor/bundle/gems/actionpack-3.2.7/lib/sprockets/static_compiler.rb
challah-0.7.0 vendor/bundle/gems/actionpack-3.2.7/lib/sprockets/static_compiler.rb
challah-0.7.0.pre2 vendor/bundle/gems/actionpack-3.2.7/lib/sprockets/static_compiler.rb
actionpack-3.2.8 lib/sprockets/static_compiler.rb
actionpack-3.1.8 lib/sprockets/static_compiler.rb
actionpack-3.2.8.rc2 lib/sprockets/static_compiler.rb