This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
forked from rails/sprockets
-
Notifications
You must be signed in to change notification settings - Fork 24
/
asset_attributes.rb
137 lines (119 loc) · 3.91 KB
/
asset_attributes.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
require 'pathname'
module Sprockets
# `AssetAttributes` is a wrapper similar to `Pathname` that provides
# some helper accessors.
#
# These methods should be considered internalish.
class AssetAttributes
attr_reader :environment, :pathname
def initialize(environment, path)
@environment = environment
@pathname = path.is_a?(Pathname) ? path : Pathname.new(path.to_s)
end
# Returns paths search the load path for.
def search_paths
paths = [pathname.to_s]
extension = format_extension
path_without_extension = extension ?
pathname.sub(extension, '') :
pathname
# optimization: bower.json can only be nested one level deep
if !path_without_extension.to_s.index('/')
paths << path_without_extension.join(".bower.json").to_s
paths << path_without_extension.join("bower.json").to_s
# DEPRECATED bower configuration file
paths << path_without_extension.join("component.json").to_s
end
if pathname.basename(extension.to_s).to_s != 'index'
paths << path_without_extension.join("index#{extension}").to_s
end
paths
end
# Reverse guess logical path for fully expanded path.
#
# This has some known issues. For an example if a file is
# shaddowed in the path, but is required relatively, its logical
# path will be incorrect.
def logical_path
if root_path = environment.paths.detect { |path| pathname.to_s[path] }
path = pathname.to_s.sub("#{root_path}/", '')
path = pathname.relative_path_from(Pathname.new(root_path)).to_s
path = engine_extensions.inject(path) { |p, ext| p.sub(ext, '') }
path = "#{path}#{engine_format_extension}" unless format_extension
path
else
raise FileOutsidePaths, "#{pathname} isn't in paths: #{environment.paths.join(', ')}"
end
end
# Returns `Array` of extension `String`s.
#
# "foo.js.coffee"
# # => [".js", ".coffee"]
#
def extensions
@extensions ||= @pathname.basename.to_s.scan(/\.[^.]+/)
end
# Returns the format extension.
#
# "foo.js.coffee"
# # => ".js"
#
def format_extension
extensions.reverse.detect { |ext|
@environment.mime_types(ext) && !@environment.engines(ext)
}
end
# Returns an `Array` of engine extensions.
#
# "foo.js.coffee.erb"
# # => [".coffee", ".erb"]
#
def engine_extensions
exts = extensions
if offset = extensions.index(format_extension)
exts = extensions[offset+1..-1]
end
exts.select { |ext| @environment.engines(ext) }
end
# Returns engine classes.
def engines
engine_extensions.map { |ext| @environment.engines(ext) }
end
# Returns all processors to run on the path.
def processors
environment.preprocessors(content_type) +
engines.reverse +
environment.postprocessors(content_type)
end
# Returns the content type for the pathname. Falls back to `application/octet-stream`.
def content_type
@content_type ||= begin
if format_extension.nil?
engine_content_type || 'application/octet-stream'
else
@environment.mime_types(format_extension) ||
engine_content_type ||
'application/octet-stream'
end
end
end
private
# Returns implicit engine content type.
#
# `.coffee` files carry an implicit `application/javascript`
# content type.
def engine_content_type
engines.reverse.each do |engine|
if engine.respond_to?(:default_mime_type) && engine.default_mime_type
return engine.default_mime_type
end
end
nil
end
def engine_format_extension
if content_type = engine_content_type
environment.extension_for_mime_type(content_type)
end
end
end
end