module Sprockets class Secretary DEFAULT_OPTIONS = { :root => ".", :load_path => [], :source_files => [], :expand_paths => true } attr_reader :environment, :preprocessor def initialize(options = {}) @options = DEFAULT_OPTIONS.merge(options) @environment = Sprockets::Environment.new(@options[:root]) @preprocessor = Sprockets::Preprocessor.new(@environment) add_load_locations(@options[:load_path]) add_source_files(@options[:source_files]) end def add_load_location(load_location, options = {}) add_load_locations([load_location], options) end def add_load_locations(load_locations, options = {}) expand_paths(load_locations, options).each do |load_location| environment.register_load_location(load_location) end end def add_source_file(source_file, options = {}) add_source_files([source_file], options) end def add_source_files(source_files, options = {}) expand_paths(source_files, options).each do |source_file| if pathname = environment.find(source_file) preprocessor.require(pathname.source_file) else raise Sprockets::LoadError, "no such file `#{source_file}'" end end end def output_file preprocessor.output_file end protected def expand_paths(paths, options = {}) if options.has_key?(:expand_paths) ? options[:expand_paths] : @options[:expand_paths] paths.map { |path| Dir[from_root(path)].sort }.flatten.compact else paths.map { |path| from_root(path) } end end def from_root(path) if path[0, 1] == File::SEPARATOR path else File.join(@options[:root], path) end end end end