From fe8a9a0d106197bd9d619f331afdec102bfe9d24 Mon Sep 17 00:00:00 2001 From: Liehann Loots Date: Fri, 29 Jul 2011 13:28:56 +0200 Subject: [PATCH] Updated glob sorting to sort files in parent directories before files in subdirectories. eg: /a/b.js /a/a/a.js --- lib/jammit/packager.rb | 17 ++++++++++++++++- test/config/assets.yml | 2 ++ test/unit/test_packager.rb | 2 ++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/jammit/packager.rb b/lib/jammit/packager.rb index acce7a72..0f9c95b8 100644 --- a/lib/jammit/packager.rb +++ b/lib/jammit/packager.rb @@ -103,10 +103,25 @@ def package_for(package, extension) # Print a warning if no files were found that match the glob. def glob_files(glob) absolute = Pathname.new(glob).absolute? - paths = Dir[absolute ? glob : File.join(ASSET_ROOT, glob)].sort + paths = Dir[absolute ? glob : File.join(ASSET_ROOT, glob)].sort(&method(:glob_comparator)) Jammit.warn("No assets match '#{glob}'") if paths.empty? paths end + + # Compares two paths, sorting files in parent directories before + # files in their subdirectories. + # a/a.js + # a/b.js + # a/a/a.js + def glob_comparator(path1, path2) + dir1 = File.dirname(path1) + dir2 = File.dirname(path2) + + return path1 <=> path2 if dir1 == dir2 + return 1 if dir1.start_with?(dir2) + return -1 if dir2.start_with?(dir1) + dir1 <=> dir2 + end # Get the latest mtime of a list of files (plus the config path). def latest_mtime(paths) diff --git a/test/config/assets.yml b/test/config/assets.yml index 7d4df730..0141b35b 100644 --- a/test/config/assets.yml +++ b/test/config/assets.yml @@ -9,6 +9,8 @@ javascripts: js_test_nested: - *js_test - fixtures/src/nested/*.js + js_test_nested2: + - fixtures/src/**/*.js jst_test: &jst_test - fixtures/src/*.jst jst_test_nested: diff --git a/test/unit/test_packager.rb b/test/unit/test_packager.rb index 1f90f55c..81bb9eec 100644 --- a/test/unit/test_packager.rb +++ b/test/unit/test_packager.rb @@ -26,6 +26,8 @@ def test_fetching_lists_of_nested_urls assert urls == ['/fixtures/src/test1.css', '/fixtures/src/test2.css', '/fixtures/src/test_fonts.css', '/fixtures/src/nested/nested1.css', '/fixtures/src/nested/nested2.css'] urls = Jammit.packager.individual_urls(:js_test_nested, :js) assert urls == ['/fixtures/src/test1.js', '/fixtures/src/test2.js', '/fixtures/src/nested/nested1.js', '/fixtures/src/nested/nested2.js'] + urls = Jammit.packager.individual_urls(:js_test_nested2, :js) + assert urls == ['/fixtures/src/test1.js', '/fixtures/src/test2.js', '/fixtures/src/nested/nested1.js', '/fixtures/src/nested/nested2.js'] urls = Jammit.packager.individual_urls(:jst_test_nested, :js) assert urls == ['/assets/jst_test_nested.jst'] end