lib/dassets.rb in dassets-0.2.0 vs lib/dassets.rb in dassets-0.3.0
- old
+ new
@@ -1,39 +1,86 @@
require 'pathname'
+require 'set'
require 'ns-options'
require 'dassets/version'
require 'dassets/root_path'
-require 'dassets/digests_file'
+require 'dassets/digests'
+require 'dassets/engine'
ENV['DASSETS_ASSETS_FILE'] ||= 'config/assets'
module Dassets
- def self.config; Config; end
- def self.configure(&block); Config.define(&block); end
+ def self.config; @config ||= Config.new; end
+ def self.sources; @sources ||= Set.new; end
+ def self.digests; @digests ||= NullDigests.new; end
- def self.init
- require self.config.assets_file
- @digests_file = DigestsFile.new(self.config.digests_file_path)
+ def self.configure(&block)
+ block.call(self.config)
end
def self.reset
- @digests_file = nil
+ @sources = @digests = nil
end
- def self.digests; @digests_file || NullDigestsFile.new; end
+ def self.init
+ require self.config.assets_file
+ @sources = SourceList.new(self.config)
+ @digests = Digests.new(self.config.digests_path)
+ end
+
def self.[](asset_path)
self.digests.asset_file(asset_path)
end
+ # Cmds
+
+ def self.digest_source_files(paths=nil)
+ require 'dassets/cmds/digest_cmd'
+ Cmds::DigestCmd.new(paths).run
+ end
+
class Config
include NsOptions::Proxy
- option :assets_file, Pathname, :default => ENV['DASSETS_ASSETS_FILE']
- option :root_path, Pathname, :required => true
- option :files_path, RootPath, :default => proc{ "app/assets/public" }
- option :digests_file_path, RootPath, :default => proc{ "app/assets/.digests" }
+ option :root_path, Pathname, :required => true
+ option :digests_path, Pathname, :required => true
+ option :output_path, RootPath, :required => true
+ option :assets_file, Pathname, :default => ENV['DASSETS_ASSETS_FILE']
+ option :source_path, RootPath, :default => proc{ "app/assets" }
+ option :source_filter, Proc, :default => proc{ |paths| paths }
+
+ attr_reader :engines
+
+ def initialize
+ super({
+ :digests_path => proc{ File.join(self.source_path, '.digests') },
+ :output_path => proc{ File.join(self.source_path, 'public') }
+ })
+ @engines = Hash.new{ |k,v| Dassets::NullEngine.new }
+ end
+
+ def source(path=nil, &filter)
+ self.source_path = path if path
+ self.source_filter = filter if filter
+ end
+
+ def engine(input_ext, engine_class, opts=nil)
+ @engines[input_ext.to_s] = engine_class.new(opts)
+ end
+ end
+
+ module SourceList
+ def self.new(config)
+ paths = Set.new
+ paths += Dir.glob(File.join(config.source_path, "**/*"))
+ paths.reject!{ |path| !File.file?(path) }
+ paths.reject!{ |path| path =~ /^#{config.output_path}/ }
+ paths.reject!{ |path| path =~ /^#{config.digests_path}/ }
+
+ config.source_filter.call(paths).sort
+ end
end
end