This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
forked from rails/sprockets
/
base.rb
338 lines (297 loc) · 9.63 KB
/
base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
require 'sprockets/asset_attributes'
require 'sprockets/bundled_asset'
require 'sprockets/caching'
require 'sprockets/errors'
require 'sprockets/processed_asset'
require 'sprockets/server'
require 'sprockets/static_asset'
require 'json'
require 'pathname'
module Sprockets
# `Base` class for `Environment` and `Index`.
class Base
include Caching, Paths, Mime, Processing, Compressing, Engines, Server
# Returns a `Digest` implementation class.
#
# Defaults to `Digest::MD5`.
attr_reader :digest_class
# Assign a `Digest` implementation class. This maybe any Ruby
# `Digest::` implementation such as `Digest::MD5` or
# `Digest::SHA1`.
#
# environment.digest_class = Digest::SHA1
#
def digest_class=(klass)
expire_index!
@digest_class = klass
end
# The `Environment#version` is a custom value used for manually
# expiring all asset caches.
#
# Sprockets is able to track most file and directory changes and
# will take care of expiring the cache for you. However, its
# impossible to know when any custom helpers change that you mix
# into the `Context`.
#
# It would be wise to increment this value anytime you make a
# configuration change to the `Environment` object.
attr_reader :version
# Assign an environment version.
#
# environment.version = '2.0'
#
def version=(version)
expire_index!
@version = version
end
# Returns a `Digest` instance for the `Environment`.
#
# This value serves two purposes. If two `Environment`s have the
# same digest value they can be treated as equal. This is more
# useful for comparing environment states between processes rather
# than in the same. Two equal `Environment`s can share the same
# cached assets.
#
# The value also provides a seed digest for all `Asset`
# digests. Any change in the environment digest will affect all of
# its assets.
def digest
# Compute the initial digest using the implementation class. The
# Sprockets release version and custom environment version are
# mixed in. So any new releases will affect all your assets.
@digest ||= digest_class.new.update(VERSION).update(version.to_s)
# Returned a dupped copy so the caller can safely mutate it with `.update`
@digest.dup
end
# Get and set `Logger` instance.
attr_accessor :logger
# Get `Context` class.
#
# This class maybe mutated and mixed in with custom helpers.
#
# environment.context_class.instance_eval do
# include MyHelpers
# def asset_url; end
# end
#
attr_reader :context_class
# Get persistent cache store
attr_reader :cache
# Set persistent cache store
#
# The cache store must implement a pair of getters and
# setters. Either `get(key)`/`set(key, value)`,
# `[key]`/`[key]=value`, `read(key)`/`write(key, value)`.
def cache=(cache)
expire_index!
@cache = cache
@cache_adapter = make_cache_adapter(cache)
end
def prepend_path(path)
# Overrides the global behavior to expire the index
expire_index!
super
end
def append_path(path)
# Overrides the global behavior to expire the index
expire_index!
super
end
def clear_paths
# Overrides the global behavior to expire the index
expire_index!
super
end
# Finds the expanded real path for a given logical path by
# searching the environment's paths.
#
# resolve("application.js")
# # => "/path/to/app/javascripts/application.js.coffee"
#
# A `FileNotFound` exception is raised if the file does not exist.
def resolve(logical_path, options = {})
# If a block is given, preform an iterable search
if block_given?
args = attributes_for(logical_path).search_paths + [options]
@trail.find(*args) do |path|
pathname = Pathname.new(path)
if pathname.basename.to_s == "bower.json"
bower = json_decode(pathname.read)
case bower['main']
when String
yield pathname.dirname.join(bower['main'])
when Array
extname = File.extname(logical_path)
bower['main'].each do |fn|
if extname == "" || extname == File.extname(fn)
yield pathname.dirname.join(fn)
end
end
end
else
yield pathname
end
end
else
resolve(logical_path, options) do |pathname|
return pathname
end
raise FileNotFound, "couldn't find file '#{logical_path}'"
end
end
# Register a new mime type.
def register_mime_type(mime_type, ext)
# Overrides the global behavior to expire the index
expire_index!
@trail.append_extension(ext)
super
end
# Registers a new Engine `klass` for `ext`.
def register_engine(ext, klass)
# Overrides the global behavior to expire the index
expire_index!
add_engine_to_trail(ext, klass)
super
end
def register_preprocessor(mime_type, klass, &block)
# Overrides the global behavior to expire the index
expire_index!
super
end
def unregister_preprocessor(mime_type, klass)
# Overrides the global behavior to expire the index
expire_index!
super
end
def register_postprocessor(mime_type, klass, &block)
# Overrides the global behavior to expire the index
expire_index!
super
end
def unregister_postprocessor(mime_type, klass)
# Overrides the global behavior to expire the index
expire_index!
super
end
def register_bundle_processor(mime_type, klass, &block)
# Overrides the global behavior to expire the index
expire_index!
super
end
def unregister_bundle_processor(mime_type, klass)
# Overrides the global behavior to expire the index
expire_index!
super
end
# Return an `Index`. Must be implemented by the subclass.
def index
raise NotImplementedError
end
if defined? Encoding.default_external
# Define `default_external_encoding` accessor on 1.9.
# Defaults to UTF-8.
attr_accessor :default_external_encoding
end
# Read and compute digest of filename.
#
# Subclasses may cache this method.
def file_digest(path)
if stat = self.stat(path)
# If its a file, digest the contents
if stat.file?
digest.file(path.to_s)
# If its a directive, digest the list of filenames
elsif stat.directory?
contents = self.entries(path).join(',')
digest.update(contents)
end
end
end
# Internal. Return a `AssetAttributes` for `path`.
def attributes_for(path)
AssetAttributes.new(self, path)
end
# Internal. Return content type of `path`.
def content_type_of(path)
attributes_for(path).content_type
end
# Find asset by logical path or expanded path.
def find_asset(path, options = {})
logical_path = path
pathname = Pathname.new(path)
if pathname.absolute?
return unless stat(pathname)
logical_path = attributes_for(pathname).logical_path
else
begin
pathname = resolve(logical_path)
# If logical path is missing a mime type extension, append
# the absolute path extname so it has one.
#
# Ensures some consistency between finding "foo/bar" vs
# "foo/bar.js".
if File.extname(logical_path) == ""
expanded_logical_path = attributes_for(pathname).logical_path
logical_path += File.extname(expanded_logical_path)
end
rescue FileNotFound
return nil
end
end
build_asset(logical_path, pathname, options)
end
# Preferred `find_asset` shorthand.
#
# environment['application.js']
#
def [](*args)
find_asset(*args)
end
# Pretty inspect
def inspect
"#<#{self.class}:0x#{object_id.to_s(16)} " +
"root=#{root.to_s.inspect}, " +
"paths=#{paths.inspect}, " +
"digest=#{digest.to_s.inspect}" +
">"
end
protected
# Clear index after mutating state. Must be implemented by the subclass.
def expire_index!
raise NotImplementedError
end
def build_asset(logical_path, pathname, options)
pathname = Pathname.new(pathname)
# If there are any processors to run on the pathname, use
# `BundledAsset`. Otherwise use `StaticAsset` and treat is as binary.
if attributes_for(pathname).processors.any?
if options[:bundle] == false
circular_call_protection(pathname.to_s) do
ProcessedAsset.new(index, logical_path, pathname)
end
else
BundledAsset.new(index, logical_path, pathname)
end
else
StaticAsset.new(index, logical_path, pathname)
end
end
def cache_key_for(path, options)
"#{path}:#{options[:bundle] ? '1' : '0'}"
end
def circular_call_protection(path)
reset = Thread.current[:sprockets_circular_calls].nil?
calls = Thread.current[:sprockets_circular_calls] ||= Set.new
if calls.include?(path)
raise CircularDependencyError, "#{path} has already been required"
end
calls << path
yield
ensure
Thread.current[:sprockets_circular_calls] = nil if reset
end
def json_decode(obj)
JSON.parse(obj, create_additions: false)
end
end
end