File: paths.rb

package info (click to toggle)
ruby-sprockets 4.2.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,964 kB
  • sloc: ruby: 13,014; javascript: 157; makefile: 4
file content (82 lines) | stat: -rw-r--r-- 2,072 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true
require 'sprockets/path_utils'
require 'sprockets/utils'

module Sprockets
  module Paths
    include PathUtils, Utils

    # 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
      config[:root]
    end

    # Internal: Change Environment root.
    #
    # Only the initializer should change the root.
    def root=(path)
      self.config = hash_reassoc(config, :root) do
        File.expand_path(path)
      end
    end
    private :root=

    # Returns an `Array` of path `String`s.
    #
    # These paths will be used for asset logical path lookups.
    def paths
      config[:paths]
    end

    # Prepend a `path` to the `paths` list.
    #
    # Paths at the end of the `Array` have the least priority.
    def prepend_path(path)
      self.config = hash_reassoc(config, :paths) do |paths|
        path = File.expand_path(path, config[:root]).freeze
        paths.unshift(path)
      end
    end

    # Append a `path` to the `paths` list.
    #
    # Paths at the beginning of the `Array` have a higher priority.
    def append_path(path)
      self.config = hash_reassoc(config, :paths) do |paths|
        path = File.expand_path(path, config[:root]).freeze
        paths.push(path)
      end
    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
      self.config = hash_reassoc(config, :paths) do |paths|
        paths.clear
      end
    end

    # Public: Iterate over every file under all load paths.
    #
    # Returns Enumerator if no block is given.
    def each_file
      return to_enum(__method__) unless block_given?

      paths.each do |root|
        stat_tree(root).each do |filename, stat|
          if stat.file?
            yield filename
          end
        end
      end

      nil
    end
  end
end