From e9f8b4df749eb793db744e7ba379de9f5bd89377 Mon Sep 17 00:00:00 2001 From: ducksan cho Date: Thu, 19 Nov 2015 17:15:51 +1300 Subject: [PATCH] Add Windows support to Utils.safe_glob --- lib/jekyll/cleaner.rb | 2 +- lib/jekyll/collection.rb | 2 +- lib/jekyll/utils.rb | 14 +++++++++----- test/test_utils.rb | 5 +++++ 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/jekyll/cleaner.rb b/lib/jekyll/cleaner.rb index 9146e7de..ca8a0ca9 100644 --- a/lib/jekyll/cleaner.rb +++ b/lib/jekyll/cleaner.rb @@ -40,7 +40,7 @@ module Jekyll regex = keep_file_regex dirs = keep_dirs - Utils.safe_glob(site.in_dest_dir, "**/*", File::FNM_DOTMATCH).each do |file| + Utils.safe_glob(site.in_dest_dir, ["**", "*"], File::FNM_DOTMATCH).each do |file| next if file =~ HIDDEN_FILE_REGEX || file =~ regex || dirs.include?(file) files << file end diff --git a/lib/jekyll/collection.rb b/lib/jekyll/collection.rb index e4725718..00906352 100644 --- a/lib/jekyll/collection.rb +++ b/lib/jekyll/collection.rb @@ -74,7 +74,7 @@ module Jekyll def entries return Array.new unless exists? @entries ||= - Utils.safe_glob(collection_dir, "**/*.*").map do |entry| + Utils.safe_glob(collection_dir, ["**", "*.*"]).map do |entry| entry["#{collection_dir}/"] = ''; entry end end diff --git a/lib/jekyll/utils.rb b/lib/jekyll/utils.rb index ab516767..54f400f8 100644 --- a/lib/jekyll/utils.rb +++ b/lib/jekyll/utils.rb @@ -222,15 +222,19 @@ module Jekyll # safe_glob("path", "*", File::FNM_DOTMATCH) # # => ["path/.", "path/..", "path/file1"] # - # dir - the dir where glob will be executed under + # safe_glob("path", ["**", "*"]) + # # => ["path[/file1", "path[/folder/file2"] + # + # dir - the dir where glob will be executed under # (the dir will be included to each result) - # pattern - the pattern which will be applied under the dir - # flags - the flags which will be applied to the pattern + # patterns - the patterns (or the pattern) which will be applied under the dir + # flags - the flags which will be applied to the pattern # # Returns matched pathes - def safe_glob(dir, pattern, flags = 0) + def safe_glob(dir, patterns, flags = 0) return [] unless Dir.exist?(dir) - return [dir] if pattern.nil? || pattern.empty? + pattern = File.join(Array patterns) + return [dir] if pattern.empty? Dir.chdir(dir) do Dir.glob(pattern, flags).map { |f| File.join(dir, f) } end diff --git a/test/test_utils.rb b/test/test_utils.rb index 02ebb09a..133aba0a 100644 --- a/test/test_utils.rb +++ b/test/test_utils.rb @@ -212,6 +212,11 @@ class TestUtils < JekyllUnitTest assert_equal Dir.glob(dir + "/*", File::FNM_DOTMATCH), Utils.safe_glob(dir, "*", File::FNM_DOTMATCH) end + + should "support pattern as an array to support windows" do + dir = "test" + assert_equal Dir.glob(dir + "/**/*"), Utils.safe_glob(dir, ["**", "*"]) + end end end