Skip to content
This repository has been archived by the owner on Jun 10, 2018. It is now read-only.

Commit

Permalink
Merge branch 'master' into source-maps
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed Nov 17, 2013
2 parents d39b8e9 + 5b21cdd commit 1fbc896
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 118 deletions.
1 change: 1 addition & 0 deletions lib/sprockets/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ def digest
def cache=(cache)
expire_index!
@cache = cache
@cache_adapter = make_cache_adapter(cache)
end

def prepend_path(path)
Expand Down
65 changes: 36 additions & 29 deletions lib/sprockets/caching.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,18 @@ module Sprockets
# `Caching` is an internal mixin whose public methods are exposed on
# the `Environment` and `Index` classes.
module Caching
attr_reader :cache_adapter

# Low level cache getter for `key`. Checks a number of supported
# cache interfaces.
def cache_get(key)
# `Cache#get(key)` for Memcache
if cache.respond_to?(:get)
cache.get(key)

# `Cache#[key]` so `Hash` can be used
elsif cache.respond_to?(:[])
cache[key]

# `Cache#read(key)` for `ActiveSupport::Cache` support
elsif cache.respond_to?(:read)
cache.read(key)

else
nil
end
cache_adapter.get(key)
end

# Low level cache setter for `key`. Checks a number of supported
# cache interfaces.
def cache_set(key, value)
# `Cache#set(key, value)` for Memcache
if cache.respond_to?(:set)
cache.set(key, value)

# `Cache#[key]=value` so `Hash` can be used
elsif cache.respond_to?(:[]=)
cache[key] = value

# `Cache#write(key, value)` for `ActiveSupport::Cache` support
elsif cache.respond_to?(:write)
cache.write(key, value)
end

value
cache_adapter.set(key, value)
end

protected
Expand Down Expand Up @@ -92,5 +67,37 @@ def cache_set_hash(key, hash)
cache_set(expand_cache_key(key), hash)
hash
end

def make_cache_adapter(cache)
# `Cache#get(key)` for Memcache
if cache.respond_to?(:get)
cache

# `Cache#[key]` so `Hash` can be used
elsif cache.respond_to?(:[])
HashAdapter.new cache

# `Cache#read(key)` for `ActiveSupport::Cache` support
elsif cache.respond_to?(:read)
ReadWriteAdapter.new cache
else
NullAdapter.new
end
end

class HashAdapter < Struct.new(:cache)
def get(key); cache[key]; end
def set(key, value); cache[key] = value; end
end

class ReadWriteAdapter < Struct.new(:cache)
def get(key); cache.read(key); end
def set(key, value); cache.write(key, value); end
end

class NullAdapter
def get(key); nil; end
def set(key, value); value; end
end
end
end
2 changes: 1 addition & 1 deletion lib/sprockets/engines.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def register_engine(ext, klass)
private
def deep_copy_hash(hash)
initial = Hash.new { |h, k| h[k] = [] }
hash.inject(initial) { |h, (k, a)| h[k] = a.dup; h }
hash.each_with_object(initial) { |(k, a),h| h[k] = a.dup }
end
end
end
1 change: 1 addition & 0 deletions lib/sprockets/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def initialize(root = ".")
@trail.append_extension(ext)
end

self.cache = nil
expire_index!

yield self if block_given?
Expand Down
1 change: 1 addition & 0 deletions lib/sprockets/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def initialize(environment)
@logger = environment.logger
@context_class = environment.context_class
@cache = environment.cache
@cache_adapter = environment.cache_adapter
@trail = environment.trail.index
@digest = environment.digest
@digest_class = environment.digest_class
Expand Down
4 changes: 2 additions & 2 deletions lib/sprockets/manifest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def initialize(*args)
raise ArgumentError, "manifest requires output path"
end

data = nil
data = {}

begin
if File.exist?(@path)
Expand All @@ -68,7 +68,7 @@ def initialize(*args)
logger.error "#{@path} is invalid: #{e.class} #{e.message}"
end

@data = data.is_a?(Hash) ? data : {}
@data = data
end

# Returns internal assets mapping. Keys are logical paths which
Expand Down
22 changes: 8 additions & 14 deletions lib/sprockets/processed_asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ def initialize(environment, logical_path, pathname)

context = environment.context_class.new(environment, logical_path, pathname)
@source = context.evaluate(pathname)
@length = Rack::Utils.bytesize(source)
@length = source.bytesize
@digest = environment.digest.update(source).hexdigest

build_required_assets(environment, context)
build_dependency_paths(environment, context)
@dependency_paths = build_dependency_paths(environment, context)

@dependency_digest = compute_dependency_digest(environment)

Expand Down Expand Up @@ -122,25 +122,19 @@ def resolve_dependencies(environment, paths)
end

def build_dependency_paths(environment, context)
dependency_paths = {}

context._dependency_paths.each do |path|
dep = DependencyFile.new(path, environment.stat(path).mtime, environment.file_digest(path).hexdigest)
dependency_paths[dep] = true
paths = context._dependency_paths.map do |path|
DependencyFile.new(path, environment.stat(path).mtime, environment.file_digest(path).hexdigest)
end

context._dependency_assets.each do |path|
assets = context._dependency_assets.flat_map do |path|
if path == self.pathname.to_s
dep = DependencyFile.new(pathname, environment.stat(path).mtime, environment.file_digest(path).hexdigest)
dependency_paths[dep] = true
DependencyFile.new(pathname, environment.stat(path).mtime, environment.file_digest(path).hexdigest)
elsif asset = environment.find_asset(path, :bundle => false)
asset.dependency_paths.each do |d|
dependency_paths[d] = true
end
asset.dependency_paths
end
end

@dependency_paths = dependency_paths.keys
paths.concat(assets).uniq
end

def compute_dependency_digest(environment)
Expand Down
39 changes: 6 additions & 33 deletions lib/sprockets/processing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,7 @@ def register_processor(*args, &block)
# end
#
def register_preprocessor(mime_type, klass, &block)
if block_given?
name = klass.to_s
klass = Class.new(Processor) do
@name = name
@processor = block
end
end

@preprocessors[mime_type].push(klass)
@preprocessors[mime_type].push(Processor.make_processor(klass, &block))
end

# Registers a new Postprocessor `klass` for `mime_type`.
Expand All @@ -92,15 +84,7 @@ def register_preprocessor(mime_type, klass, &block)
# end
#
def register_postprocessor(mime_type, klass, &block)
if block_given?
name = klass.to_s
klass = Class.new(Processor) do
@name = name
@processor = block
end
end

@postprocessors[mime_type].push(klass)
@postprocessors[mime_type].push(Processor.make_processor(klass, &block))
end

# Deprecated alias for `unregister_preprocessor`.
Expand All @@ -115,8 +99,7 @@ def unregister_processor(*args)
def unregister_preprocessor(mime_type, klass)
if klass.is_a?(String) || klass.is_a?(Symbol)
klass = @preprocessors[mime_type].detect { |cls|
cls.respond_to?(:name) &&
cls.name == "Sprockets::Processor (#{klass})"
cls.name == "Sprockets::Processor (#{klass})"
}
end

Expand All @@ -130,8 +113,7 @@ def unregister_preprocessor(mime_type, klass)
def unregister_postprocessor(mime_type, klass)
if klass.is_a?(String) || klass.is_a?(Symbol)
klass = @postprocessors[mime_type].detect { |cls|
cls.respond_to?(:name) &&
cls.name == "Sprockets::Processor (#{klass})"
cls.name == "Sprockets::Processor (#{klass})"
}
end

Expand Down Expand Up @@ -166,15 +148,7 @@ def bundle_processors(mime_type = nil)
# end
#
def register_bundle_processor(mime_type, klass, &block)
if block_given?
name = klass.to_s
klass = Class.new(Processor) do
@name = name
@processor = block
end
end

@bundle_processors[mime_type].push(klass)
@bundle_processors[mime_type].push(Processor.make_processor(klass, &block))
end

# Remove Bundle Processor `klass` for `mime_type`.
Expand All @@ -184,8 +158,7 @@ def register_bundle_processor(mime_type, klass, &block)
def unregister_bundle_processor(mime_type, klass)
if klass.is_a?(String) || klass.is_a?(Symbol)
klass = @bundle_processors[mime_type].detect { |cls|
cls.respond_to?(:name) &&
cls.name == "Sprockets::Processor (#{klass})"
cls.name == "Sprockets::Processor (#{klass})"
}
end

Expand Down
10 changes: 10 additions & 0 deletions lib/sprockets/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ module Sprockets
# end
#
class Processor < Tilt::Template
def self.make_processor(klass, &block) # :nodoc:
return klass unless block_given?

name = klass.to_s
Class.new(Processor) do
@name = name
@processor = block
end
end

# `processor` is a lambda or block
def self.processor
@processor
Expand Down
54 changes: 15 additions & 39 deletions lib/sprockets/utils.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,25 @@
module Sprockets
# `Utils`, we didn't know where else to put it!
module Utils
# If theres encoding support (aka Ruby 1.9)
if "".respond_to?(:valid_encoding?)
# Define UTF-8 BOM pattern matcher.
# Avoid using a Regexp literal because it inheirts the files
# encoding and we want to avoid syntax errors in other interpreters.
UTF8_BOM_PATTERN = Regexp.new("\\A\uFEFF".encode('utf-8'))
# Define UTF-8 BOM pattern matcher.
# Avoid using a Regexp literal because it inheirts the files
# encoding and we want to avoid syntax errors in other interpreters.
UTF8_BOM_PATTERN = Regexp.new("\\A\uFEFF".encode('utf-8'))

def self.read_unicode(pathname, external_encoding = Encoding.default_external)
pathname.open("r:#{external_encoding}") do |f|
f.read.tap do |data|
# Eager validate the file's encoding. In most cases we
# expect it to be UTF-8 unless `default_external` is set to
# something else. An error is usually raised if the file is
# saved as UTF-16 when we expected UTF-8.
if !data.valid_encoding?
raise EncodingError, "#{pathname} has a invalid " +
"#{data.encoding} byte sequence"
def self.read_unicode(pathname, external_encoding = Encoding.default_external)
pathname.open("r:#{external_encoding}") do |f|
f.read.tap do |data|
# Eager validate the file's encoding. In most cases we
# expect it to be UTF-8 unless `default_external` is set to
# something else. An error is usually raised if the file is
# saved as UTF-16 when we expected UTF-8.
if !data.valid_encoding?
raise EncodingError, "#{pathname} has a invalid " +
"#{data.encoding} byte sequence"

# If the file is UTF-8 and theres a BOM, strip it for safe concatenation.
elsif data.encoding.name == "UTF-8" && data =~ UTF8_BOM_PATTERN
data.sub!(UTF8_BOM_PATTERN, "")
end
end
end
end

else
# Define UTF-8 and UTF-16 BOM pattern matchers.
# Avoid using a Regexp literal to prevent syntax errors in other interpreters.
UTF8_BOM_PATTERN = Regexp.new("\\A\\xEF\\xBB\\xBF")
UTF16_BOM_PATTERN = Regexp.new("\\A(\\xFE\\xFF|\\xFF\\xFE)")

def self.read_unicode(pathname)
pathname.read.tap do |data|
# If the file is UTF-8 and theres a BOM, strip it for safe concatenation.
if data =~ UTF8_BOM_PATTERN
elsif data.encoding.name == "UTF-8" && data =~ UTF8_BOM_PATTERN
data.sub!(UTF8_BOM_PATTERN, "")

# If we find a UTF-16 BOM, theres nothing we can do on
# 1.8. Only UTF-8 is supported.
elsif data =~ UTF16_BOM_PATTERN
raise EncodingError, "#{pathname} has a UTF-16 BOM. " +
"Resave the file as UTF-8 or upgrade to Ruby 1.9."
end
end
end
Expand Down

0 comments on commit 1fbc896

Please sign in to comment.