This repository has been archived by the owner on Jun 10, 2018. It is now read-only.
forked from rails/sprockets
/
trail.rb
90 lines (82 loc) · 2.45 KB
/
trail.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
require 'sprockets/errors'
require 'pathname'
module Sprockets
# `Trail` is an internal mixin whose public methods are exposed on
# the `Environment` and `Index` classes.
module Trail
# Returns `Environment` root.
#
# All relative paths are expanded with root as its base. To be
# useful set this to your applications root directory. (`Rails.root`)
def root
trail.root.dup
end
# Returns an `Array` of path `String`s.
#
# These paths will be used for asset logical path lookups.
#
# Note that a copy of the `Array` is returned so mutating will
# have no affect on the environment. See `append_path`,
# `prepend_path`, and `clear_paths`.
def paths
trail.paths.dup
end
# Prepend a `path` to the `paths` list.
#
# Paths at the end of the `Array` have the least priority.
def prepend_path(path)
expire_index!
@trail.prepend_path(path)
end
# Append a `path` to the `paths` list.
#
# Paths at the beginning of the `Array` have a higher priority.
def append_path(path)
expire_index!
@trail.append_path(path)
end
# Clear all paths and start fresh.
#
# There is no mechanism for reordering paths, so its best to
# completely wipe the paths list and reappend them in the order
# you want.
def clear_paths
expire_index!
@trail.paths.dup.each { |path| @trail.remove_path(path) }
end
# Returns an `Array` of extensions.
#
# These extensions maybe omitted from logical path searches.
#
# # => [".js", ".css", ".coffee", ".sass", ...]
#
def extensions
trail.extensions.dup
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|
yield Pathname.new(path)
end
else
resolve(logical_path, options) do |pathname|
return pathname
end
raise FileNotFound, "couldn't find file '#{logical_path}'"
end
end
protected
def trail
@trail
end
end
end