Sha256: 0a657eeeb112f2a9819a0196377b45ba82a17e84e9ba39150dc276d8286e0374

Contents?: true

Size: 1.57 KB

Versions: 78

Compression:

Stored size: 1.57 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(paths) do |logical_path|
        if asset = env.find_asset(logical_path)
          digest_path = write_asset(asset)
          manifest[asset.logical_path] = digest_path
          manifest[aliased_path_for(asset.logical_path)] = digest_path
        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 path_for(asset)
      @digest ? asset.digest_path : asset.logical_path
    end

    def aliased_path_for(logical_path)
      if File.basename(logical_path).start_with?('index')
        logical_path.sub(/\/index([^\/]+)$/, '\1')
      else
        logical_path.sub(/\.([^\/]+)$/, '/index.\1')
      end
    end
  end
end

Version data entries

78 entries across 63 versions & 16 rubygems

Version Path
actionpack-3.2.22.5 lib/sprockets/static_compiler.rb
actionpack-3.2.22.4 lib/sprockets/static_compiler.rb
actionpack-3.2.22.3 lib/sprockets/static_compiler.rb
actionpack-3.2.22.2 lib/sprockets/static_compiler.rb
actionpack-3.2.22.1 lib/sprockets/static_compiler.rb
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.12/lib/sprockets/static_compiler.rb
actionpack-3.2.22 lib/sprockets/static_compiler.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/actionpack-3.2.12/lib/sprockets/static_compiler.rb
actionpack-3.2.21 lib/sprockets/static_compiler.rb
actionpack-3.2.20 lib/sprockets/static_compiler.rb
actionpack-3.2.19 lib/sprockets/static_compiler.rb
actionpack-3.2.18 lib/sprockets/static_compiler.rb
actionpack-3.2.17 lib/sprockets/static_compiler.rb
actionpack-3.2.16 lib/sprockets/static_compiler.rb
actionpack-3.2.15 lib/sprockets/static_compiler.rb
actionpack-3.2.15.rc3 lib/sprockets/static_compiler.rb
actionpack-3.2.15.rc2 lib/sprockets/static_compiler.rb
actionpack-3.2.15.rc1 lib/sprockets/static_compiler.rb
actionpack-3.2.14 lib/sprockets/static_compiler.rb
actionpack-3.2.14.rc2 lib/sprockets/static_compiler.rb