sprockets-vendor_gems/lib/sprockets-vendor_gems.rb

60 lines
1.2 KiB
Ruby
Raw Permalink Normal View History

2012-02-08 13:42:07 +00:00
require 'sprockets'
require 'rubygems'
2012-02-06 18:51:21 +00:00
module ::Sprockets
2012-08-02 16:50:20 +00:00
module VendorGems
class << self
attr_accessor :default_types
end
self.default_types = %w{javascripts stylesheets images}
end
2012-07-03 00:45:04 +00:00
def self.find_gem_vendor_paths(options = {})
options = { :paths => %w{vendor lib app} }.merge(options)
2012-02-06 18:51:21 +00:00
2012-08-02 16:50:20 +00:00
for_types = [ options[:for] || ::Sprockets::VendorGems.default_types ].flatten
2012-02-06 18:51:21 +00:00
2012-07-03 00:45:04 +00:00
paths = []
2012-02-06 18:51:21 +00:00
GemEnvironment.each do |gemspec|
2012-07-03 00:45:04 +00:00
options[:paths].product(for_types).each do |base_dir, type|
path = File.join(gemspec.gem_dir, base_dir, "assets", type.to_s)
2012-02-06 18:51:21 +00:00
2012-07-03 00:45:04 +00:00
paths << path if File.directory?(path)
end
2012-02-06 18:51:21 +00:00
end
2012-07-03 00:45:04 +00:00
paths
2012-02-06 18:51:21 +00:00
end
class EnvironmentWithVendoredGems < Environment
def initialize(*args)
super(*args)
2012-08-02 16:50:20 +00:00
extend_with_vendored_gems
end
end
class Environment
def extend_with_vendored_gems
2012-02-06 18:51:21 +00:00
Sprockets.find_gem_vendor_paths.each { |path| append_path(path) }
end
end
class GemEnvironment
extend Enumerable
def self.gemspecs
Gem.loaded_specs.map(&:last)
end
def self.each(&block)
return enum_for(:each) unless block_given?
gemspecs.each {|spec| yield spec }
end
end
2012-02-06 18:51:21 +00:00
end