Skip to content
Browse files

rescan if a filelist file is missing

git-svn-id: http://bundle-fu.googlecode.com/svn/trunk@4 1db77ec0-6337-0410-9320-454da9aca44f
  • Loading branch information...
1 parent 599f08b commit 21c501655ff294e9c4a912d5f6adc685c19185ce timcharper committed
Showing with 33 additions and 14 deletions.
  1. +2 −0 init.rb
  2. +8 −7 lib/bundle_fu.rb
  3. +4 −2 lib/bundle_fu/file_list.rb
  4. +18 −0 test/functional/bundle_fu_test.rb
  5. +1 −5 test/test_helper.rb
View
2 init.rb
@@ -2,3 +2,5 @@
for file in ["/lib/bundle_fu.rb", "/lib/bundle_fu/file_list.rb"]
require File.expand_path(File.join(File.dirname(__FILE__), file))
end
+
+ActionView::Base.send(:include, BundleFu::InstanceMethods)
View
15 lib/bundle_fu.rb
@@ -11,16 +11,15 @@ def bundle_files(filenames=[])
output = ""
filenames.each{|filename|
- output << "/* -------------- #{filename} -------------"
+ output << "/* -------------- #{filename} ------------- */ "
output << "\n"
- output << File.read(File.join(RAILS_ROOT, "public", filename))
+ output << (File.read(File.join(RAILS_ROOT, "public", filename)) rescue "/* FILE READ ERROR! */")
output << "\n"
}
output
end
end
-
self.init
module InstanceMethods
@@ -34,13 +33,16 @@ def bundle(options={}, &block)
:name => "bundle"
}.merge(options)
+ paths = { :css => options[:css_path], :js => options[:js_path] }
+
content = capture(&block)
content_changed = false
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 }
- # only rescan file list if content_changed
- unless content == BundleFu.content_store[options[:name]]
+ # 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])
BundleFu.content_store[options[:name]] = content
new_files = {:js => [], :css => []}
@@ -54,13 +56,12 @@ def bundle(options={}, &block)
}
end
- paths = { :css => options[:css_path], :js => options[:js_path] }
# abs_path = {}
# filelist = {}
[:css, :js].each { |filetype|
path = File.join(paths[filetype], "#{options[:name]}.#{filetype}")
abs_path = File.join(RAILS_ROOT, "public", path)
- abs_filelist_path = abs_path + ".filelist"
+ abs_filelist_path = abs_filelist_paths[filetype]
filelist = FileList.open( abs_filelist_path )
View
6 lib/bundle_fu/file_list.rb
@@ -26,13 +26,15 @@ def update_ctimes
end
def self.open(filename)
+ return nil unless File.exists?(filename)
b = new
- b.filelist = Marshal.load(File.read(filename)) if File.exists?(filename)
+ b.filelist = Marshal.load(File.read(filename))
b
end
# compares to see if one file list is exactly the same as another
def ==(compare)
+ return false if compare.nil?
throw "cant compare with #{compare.class}" unless self.class===compare
self.filelist == compare.filelist
@@ -40,7 +42,7 @@ def ==(compare)
def add_files(filenames=[])
filenames.each{|filename|
- self.filelist << [ filename, File.ctime(abs_location(filename)).to_i ]
+ self.filelist << [ filename, (File.ctime(abs_location(filename)).to_i rescue 0) ]
}
end
View
18 test/functional/bundle_fu_test.rb
@@ -108,6 +108,24 @@ def test__bundle__css_only__should_output_css_include_statement
end
+ def test__nonexisting_file__should_use_blank_file_created_at_0_ctime
+ @mock_view.bundle { %q{<script src="/javascripts/non_existing_file.js?1000" type="text/javascript"></script>} }
+
+ assert_public_files_match(cache_files("bundle").grep(/javascripts/), "FILE READ ERROR")
+
+ filelist = BundleFu::FileList.open(public_file("/javascripts/bundle.js.filelist"))
+ assert_equal(0, filelist.filelist[0][1], "ctime for first file should be 0")
+ end
+
+ def test__missing_cache_filelist__should_regenerate
+ @mock_view.bundle { @@content_include_some }
+ append_to_public_files(cache_files("bundle"), "BOGUS")
+
+ # now delete the cache files
+ Dir[ public_file("**/*.filelist")].each{|filename| FileUtils.rm_f filename }
+ @mock_view.bundle { @@content_include_some }
+ assert_public_files_no_match(cache_files("bundle"), "BOGUS", "Should have regenerated the file, but it didn't")
+ end
private
def public_file(filename)
File.join(::RAILS_ROOT, "public", filename)
View
6 test/test_helper.rb
@@ -2,20 +2,16 @@
require "rubygems"
require 'active_support'
-for file in ["../init.rb", "../lib/bundle_fu/file_list.rb", "mock_view.rb"]
+for file in ["../lib/bundle_fu.rb", "../lib/bundle_fu/file_list.rb", "../lib/bundle_fu/file_list.rb", "mock_view.rb"]
require File.expand_path(File.join(File.dirname(__FILE__), file))
end
-
-
def dbg
require 'ruby-debug'
Debugger.start
debugger
end
-
-
class Object
def to_regexp
is_a?(Regexp) ? self : Regexp.new(Regexp.escape(self.to_s))

0 comments on commit 21c5016

Please sign in to comment.
Something went wrong with that request. Please try again.