Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Updated glob sorting to sort files in parent directories ahead of files in sub-directories. #195

Open
wants to merge 1 commit into from

1 participant

@liehann

Previously Dir[].sort was called, which would result in the following sort order:
/a/a/a.js
/a/b.js

The patch changes the sort order to the following:
/a/b.js
/a/a/a.js

My motivation for the patch was having the following structure:
views/views.js
views/model/action.js

view.js defined a views namespace. action.js used it. Of course there are other ways to fix this, but the updated sort order seemed to make sense anyway.

I added one test to test the behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 29, 2011
  1. @liehann

    Updated glob sorting to sort files in parent directories before

    liehann authored
    files in subdirectories. eg:
    /a/b.js
    /a/a/a.js
This page is out of date. Refresh to see the latest.
View
17 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)
View
2  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:
View
2  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
Something went wrong with that request. Please try again.