fix crlf issue

This commit is contained in:
Reinier Balt 2009-02-07 21:26:43 +01:00
parent 48fb00b24e
commit 374adbbd89

View file

@ -1,147 +1,147 @@
class BundleFu class BundleFu
class << self class << self
attr_accessor :content_store attr_accessor :content_store
def init def init
@content_store = {} @content_store = {}
end end
def bundle_files(filenames=[]) def bundle_files(filenames=[])
output = "" output = ""
filenames.each{ |filename| filenames.each{ |filename|
filename_no_root = filename.sub(/^#{ActionController::Base.relative_url_root}/, '') filename_no_root = filename.sub(/^#{ActionController::Base.relative_url_root}/, '')
output << "/* --------- #{filename} - #{filename_no_root} --- ------ */ " output << "/* --------- #{filename} - #{filename_no_root} --- ------ */ "
output << "\n" output << "\n"
begin begin
content = (File.read(File.join(RAILS_ROOT, "public", filename_no_root))) content = (File.read(File.join(RAILS_ROOT, "public", filename_no_root)))
rescue rescue
output << "/* FILE READ ERROR! */" output << "/* FILE READ ERROR! */"
next next
end end
output << (yield(filename, content)||"") output << (yield(filename, content)||"")
} }
output output
end end
def bundle_js_files(filenames=[], options={}) def bundle_js_files(filenames=[], options={})
output = output =
bundle_files(filenames) { |filename, content| bundle_files(filenames) { |filename, content|
if options[:compress] if options[:compress]
if Object.const_defined?("Packr") if Object.const_defined?("Packr")
content content
else else
JSMinimizer.minimize_content(content) JSMinimizer.minimize_content(content)
end end
else else
content content
end end
} }
if Object.const_defined?("Packr") if Object.const_defined?("Packr")
# use Packr plugin (http://blog.jcoglan.com/packr/) # use Packr plugin (http://blog.jcoglan.com/packr/)
Packr.new.pack(output, options[:packr_options] || {:shrink_vars => false, :base62 => false}) Packr.new.pack(output, options[:packr_options] || {:shrink_vars => false, :base62 => false})
else else
output output
end end
end end
def bundle_css_files(filenames=[], options = {}) def bundle_css_files(filenames=[], options = {})
bundle_files(filenames) { |filename, content| bundle_files(filenames) { |filename, content|
BundleFu::CSSUrlRewriter.rewrite_urls(filename, content) BundleFu::CSSUrlRewriter.rewrite_urls(filename, content)
} }
end end
end end
self.init self.init
module InstanceMethods module InstanceMethods
# valid options: # valid options:
# :name - The name of the css and js files you wish to output # :name - The name of the css and js files you wish to output
# returns true if a regen occured. False if not. # returns true if a regen occured. False if not.
def bundle(options={}, &block) def bundle(options={}, &block)
# allow bypassing via the querystring # allow bypassing via the querystring
session[:bundle_fu] = (params[:bundle_fu]=="true") if params.has_key?(:bundle_fu) session[:bundle_fu] = (params[:bundle_fu]=="true") if params.has_key?(:bundle_fu)
options = { options = {
:css_path => ($bundle_css_path || "/stylesheets/cache"), :css_path => ($bundle_css_path || "/stylesheets/cache"),
:js_path => ($bundle_js_path || "/javascripts/cache"), :js_path => ($bundle_js_path || "/javascripts/cache"),
:name => ($bundle_default_name || "bundle"), :name => ($bundle_default_name || "bundle"),
:compress => true, :compress => true,
:bundle_fu => ( session[:bundle_fu].nil? ? ($bundle_fu.nil? ? true : $bundle_fu) : session[:bundle_fu] ) :bundle_fu => ( session[:bundle_fu].nil? ? ($bundle_fu.nil? ? true : $bundle_fu) : session[:bundle_fu] )
}.merge(options) }.merge(options)
# allow them to bypass via parameter # allow them to bypass via parameter
options[:bundle_fu] = false if options[:bypass] options[:bundle_fu] = false if options[:bypass]
paths = { :css => options[:css_path], :js => options[:js_path] } paths = { :css => options[:css_path], :js => options[:js_path] }
content = capture(&block) content = capture(&block)
content_changed = false content_changed = false
new_files = nil new_files = nil
abs_filelist_paths = [:css, :js].inject({}) { | hash, filetype | hash[filetype] = File.join(RAILS_ROOT, "public", paths[filetype], "#{options[:name]}.#{filetype}.filelist"); hash } abs_filelist_paths = [:css, :js].inject({}) { | hash, filetype | hash[filetype] = File.join(RAILS_ROOT, "public", paths[filetype], "#{options[:name]}.#{filetype}.filelist"); hash }
# only rescan file list if content_changed, or if a filelist cache file is missing # only rescan file list if content_changed, or if a filelist cache file is missing
unless content == BundleFu.content_store[options[:name]] && File.exists?(abs_filelist_paths[:css]) && File.exists?(abs_filelist_paths[:js]) unless content == BundleFu.content_store[options[:name]] && File.exists?(abs_filelist_paths[:css]) && File.exists?(abs_filelist_paths[:js])
BundleFu.content_store[options[:name]] = content BundleFu.content_store[options[:name]] = content
new_files = {:js => [], :css => []} new_files = {:js => [], :css => []}
content.scan(/(href|src) *= *["']([^"^'^\?]+)/i).each{ |property, value| content.scan(/(href|src) *= *["']([^"^'^\?]+)/i).each{ |property, value|
case property case property
when "src" when "src"
new_files[:js] << value new_files[:js] << value
when "href" when "href"
new_files[:css] << value new_files[:css] << value
end end
} }
end end
[:css, :js].each { |filetype| [:css, :js].each { |filetype|
output_filename = File.join(paths[filetype], "#{options[:name]}.#{filetype}") output_filename = File.join(paths[filetype], "#{options[:name]}.#{filetype}")
abs_path = File.join(RAILS_ROOT, "public", output_filename) abs_path = File.join(RAILS_ROOT, "public", output_filename)
abs_filelist_path = abs_filelist_paths[filetype] abs_filelist_path = abs_filelist_paths[filetype]
filelist = FileList.open( abs_filelist_path ) filelist = FileList.open( abs_filelist_path )
# check against newly parsed filelist. If we didn't parse the filelist from the output, then check against the updated mtimes. # check against newly parsed filelist. If we didn't parse the filelist from the output, then check against the updated mtimes.
new_filelist = new_files ? BundleFu::FileList.new(new_files[filetype]) : filelist.clone.update_mtimes new_filelist = new_files ? BundleFu::FileList.new(new_files[filetype]) : filelist.clone.update_mtimes
unless new_filelist == filelist unless new_filelist == filelist
FileUtils.mkdir_p(File.join(RAILS_ROOT, "public", paths[filetype])) FileUtils.mkdir_p(File.join(RAILS_ROOT, "public", paths[filetype]))
# regenerate everything # regenerate everything
if new_filelist.filenames.empty? if new_filelist.filenames.empty?
# delete the javascript/css bundle file if it's empty, but keep the filelist cache # delete the javascript/css bundle file if it's empty, but keep the filelist cache
FileUtils.rm_f(abs_path) FileUtils.rm_f(abs_path)
else else
# call bundle_css_files or bundle_js_files to bundle all files listed. output it's contents to a file # call bundle_css_files or bundle_js_files to bundle all files listed. output it's contents to a file
output = BundleFu.send("bundle_#{filetype}_files", new_filelist.filenames, options) output = BundleFu.send("bundle_#{filetype}_files", new_filelist.filenames, options)
File.open( abs_path, "w") {|f| f.puts output } if output File.open( abs_path, "w") {|f| f.puts output } if output
end end
new_filelist.save_as(abs_filelist_path) new_filelist.save_as(abs_filelist_path)
end end
if File.exists?(abs_path) && options[:bundle_fu] if File.exists?(abs_path) && options[:bundle_fu]
tag = filetype==:css ? stylesheet_link_tag(output_filename) : javascript_include_tag(output_filename) tag = filetype==:css ? stylesheet_link_tag(output_filename) : javascript_include_tag(output_filename)
if Rails::version < "2.2.0" if Rails::version < "2.2.0"
concat( tag , block.binding) concat( tag , block.binding)
else else
#concat doesn't need block.binding in Rails >= 2.2.0 #concat doesn't need block.binding in Rails >= 2.2.0
concat( tag ) concat( tag )
end end
end end
} }
unless options[:bundle_fu] unless options[:bundle_fu]
if Rails::version < "2.2.0" if Rails::version < "2.2.0"
concat( content, block.binding ) concat( content, block.binding )
else else
#concat doesn't need block.binding in Rails >= 2.2.0 #concat doesn't need block.binding in Rails >= 2.2.0
concat( content ) concat( content )
end end
end end
end end
end end
end end