lib/sinatra/assetpack/options.rb in sundawg-sinatra-assetpack-fork-0.0.12.pre1 vs lib/sinatra/assetpack/options.rb in sundawg-sinatra-assetpack-fork-0.1.6

- old
+ new

@@ -151,16 +151,25 @@ # Key is URI path, value is local path attrib :js_compression # Symbol, compression method for JS attrib :css_compression # Symbol, compression method for CSS attrib :output_path # '/public' + attrib :asset_hosts # [ 'http://cdn0.example.org', 'http://cdn1.example.org' ] attrib :js_compression_options # Hash attrib :css_compression_options # Hash attrib :prebuild # Bool + def expires(*args) + if args.empty? + @expires + else + @expires = args + end + end + 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 @@ -236,31 +245,37 @@ # Returns the local file for a given URI path. (for dynamic files) # Returns nil if a file is not found. # TODO: consolidate with local_file_for def dyn_local_file_for(requested_file, from) - # Remove extension file = requested_file - extension = '' + extension = File.extname(requested_file) + # Remove extension + file.gsub!(/#{extension}$/, "") + # Remove cache-buster (/js/app.28389 => /js/app) + file.gsub!(/\.[0-9]+$/, "") + matches = Dir[File.join(app.root, from, "#{file}.*")] - file.sub(/^(.*)(\.[^\.]+)$/) { file, extension = $1, $2 } + # Fix for filenames with dots (can't do this with glob) + matches.select! { |f| f =~ /#{file}\.[^.]+$/ } - # Remove cache-buster (/js/app.28389.js => /js/app) - file = $1 if file =~ /^(.*)\.[0-9]+$/ - - Dir[File.join(app.root, from, "#{file}#{extension}")].first + # Sort static file match, weighting exact file extension matches + matches.sort! do |f, _| + (File.basename(f) == "#{file}#{extension}" || File.extname(f) == extension) ? -1 : 1 + end + matches.first end # Writes `public/#{path}` based on contents of `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 } + File.open(path, 'wb') { |f| f.write output } if mtime File.utime mtime, mtime, path end end @@ -303,10 +318,10 @@ }.flatten paths = paths.uniq tuples = paths.map { |key| [key, files[key]] } - HashArray[*tuples.flatten] + Hash[*tuples.flatten] end private # Returns a URI for a given file # path = '/projects/x/app/css'