lib/sinatra/assetpack/options.rb in sinatra-assetpack-0.0.9 vs lib/sinatra/assetpack/options.rb in sinatra-assetpack-0.0.10
- old
+ new
@@ -47,17 +47,22 @@
@output_path = app.public
@js_compression_options = Hash.new
@css_compression_options = Hash.new
+ @ignored = Array.new
+
reset!
# Defaults!
serve '/css', :from => 'app/css'
serve '/js', :from => 'app/js'
serve '/images', :from => 'app/images'
+ ignore '.*'
+ ignore '_*'
+
blk.arity <= 0 ? instance_eval(&blk) : yield(self) if block_given?
end
# =====================================================================
# DSL methods
@@ -73,10 +78,31 @@
def reset!
@served = Hash.new
@packages = Hash.new
end
+ # Ignores a given path spec.
+ def ignore(spec)
+ if spec[0] == '/'
+ @ignored << "#{spec}"
+ @ignored << "#{spec}/**"
+ else
+ @ignored << "**/#{spec}"
+ @ignored << "**/#{spec}/**"
+ end
+ end
+
+ # Makes nothing ignored. Use this if you don't want to ignore dotfiles and underscore files.
+ def clear_ignores!
+ @ignored = Array.new
+ end
+
+ # Checks if a given path is ignored.
+ def ignored?(fn)
+ @ignored.any? { |spec| File.fnmatch spec, fn }
+ end
+
# Adds some JS packages.
#
# js :foo, [ '/js/vendor/jquery.*.js', ... ]
# js :foo, '/js/foo.js', [ '/js/vendor/jquery.*.js', ... ]
#
@@ -120,10 +146,12 @@
attrib :output_path # '/public'
attrib :js_compression_options # Hash
attrib :css_compression_options # Hash
+ attrib :prebuild # Bool
+
def js_compression(name=nil, options=nil)
@js_compression = name unless name.nil?
@js_compression_options = options if options.is_a?(Hash)
@js_compression
end
@@ -140,24 +168,46 @@
attr_reader :served
def build!(&blk)
session = Rack::Test::Session.new app
+ get = lambda { |path|
+ response = session.get(path)
+ out = response.body
+ mtime = Time.parse(response.headers['Last-Modified']) if response.headers['Last-Modified']
+
+ [ out, mtime ]
+ }
+
packages.each { |_, pack|
- out = session.get(pack.path).body
+ out, mtime = get[pack.path]
- write pack.path, out, &blk
- write pack.production_path, out, &blk
+ write pack.path, out, mtime, &blk
+ write pack.production_path, out, mtime, &blk
}
files.each { |path, local|
- out = session.get(path).body
- write path, out, &blk
- write BusterHelpers.add_cache_buster(path, local), out, &blk
+ out, mtime = get[path]
+
+ write path, out, mtime, &blk
+ write BusterHelpers.add_cache_buster(path, local), out, mtime, &blk
}
end
+ # Caches the packages.
+ def cache!(&blk)
+ return false if app.reload_templates
+
+ session = Rack::Test::Session.new app
+ packages.each { |_, pack|
+ yield pack.path if block_given?
+ session.get(pack.path)
+ }
+
+ true
+ end
+
def served?(path)
!! local_file_for(path)
end
# Returns the local file for a given URI path.
@@ -187,17 +237,21 @@
Dir[File.join(app.root, from, "#{file}.*")].first
end
# Writes `public/#{path}` based on contents of `output`.
- def write(path, output)
+ def write(path, output, mtime=nil)
require 'fileutils'
path = File.join(@output_path, path)
yield path if block_given?
FileUtils.mkdir_p File.dirname(path)
File.open(path, 'w') { |f| f.write output }
+
+ if mtime
+ File.utime mtime, mtime, path
+ end
end
# Returns the files as a hash.
def files(match=nil)
# All