trail.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. require 'sprockets/errors'
  2. require 'pathname'
  3. module Sprockets
  4. # `Trail` is an internal mixin whose public methods are exposed on
  5. # the `Environment` and `Index` classes.
  6. module Trail
  7. # Returns `Environment` root.
  8. #
  9. # All relative paths are expanded with root as its base. To be
  10. # useful set this to your applications root directory. (`Rails.root`)
  11. def root
  12. trail.root.dup
  13. end
  14. # Returns an `Array` of path `String`s.
  15. #
  16. # These paths will be used for asset logical path lookups.
  17. #
  18. # Note that a copy of the `Array` is returned so mutating will
  19. # have no affect on the environment. See `append_path`,
  20. # `prepend_path`, and `clear_paths`.
  21. def paths
  22. trail.paths.dup
  23. end
  24. # Prepend a `path` to the `paths` list.
  25. #
  26. # Paths at the end of the `Array` have the least priority.
  27. def prepend_path(path)
  28. expire_index!
  29. @trail.prepend_path(path)
  30. end
  31. # Append a `path` to the `paths` list.
  32. #
  33. # Paths at the beginning of the `Array` have a higher priority.
  34. def append_path(path)
  35. expire_index!
  36. @trail.append_path(path)
  37. end
  38. # Clear all paths and start fresh.
  39. #
  40. # There is no mechanism for reordering paths, so its best to
  41. # completely wipe the paths list and reappend them in the order
  42. # you want.
  43. def clear_paths
  44. expire_index!
  45. @trail.paths.dup.each { |path| @trail.remove_path(path) }
  46. end
  47. # Returns an `Array` of extensions.
  48. #
  49. # These extensions maybe omitted from logical path searches.
  50. #
  51. # # => [".js", ".css", ".coffee", ".sass", ...]
  52. #
  53. def extensions
  54. trail.extensions.dup
  55. end
  56. # Finds the expanded real path for a given logical path by
  57. # searching the environment's paths.
  58. #
  59. # resolve("application.js")
  60. # # => "/path/to/app/javascripts/application.js.coffee"
  61. #
  62. # A `FileNotFound` exception is raised if the file does not exist.
  63. def resolve(logical_path, options = {})
  64. # If a block is given, preform an iterable search
  65. if block_given?
  66. args = attributes_for(logical_path).search_paths + [options]
  67. trail.find(*args) do |path|
  68. yield Pathname.new(path)
  69. end
  70. else
  71. resolve(logical_path, options) do |pathname|
  72. return pathname
  73. end
  74. raise FileNotFound, "couldn't find file '#{logical_path}'"
  75. end
  76. end
  77. protected
  78. def trail
  79. @trail
  80. end
  81. end
  82. end