diff options
author | danielsdeleo <dan@chef.io> | 2016-02-26 15:34:17 -0800 |
---|---|---|
committer | danielsdeleo <dan@chef.io> | 2016-02-26 17:42:30 -0800 |
commit | 4710acd2029d8a7a0417c994cc5a5f61ffbf8fc6 (patch) | |
tree | efb4e40972b84039fe7d22c6eca0a001d519edd8 /lib/chef | |
parent | 8701b3d18cd1e9df64bb4552467e06383878d7ea (diff) | |
download | chef-4710acd2029d8a7a0417c994cc5a5f61ffbf8fc6.tar.gz |
Load cookbook segements by fnmatch-ing from file list
Diffstat (limited to 'lib/chef')
-rw-r--r-- | lib/chef/cookbook/cookbook_version_loader.rb | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/lib/chef/cookbook/cookbook_version_loader.rb b/lib/chef/cookbook/cookbook_version_loader.rb index 74ef9f9fcb..b9fa5dfa29 100644 --- a/lib/chef/cookbook/cookbook_version_loader.rb +++ b/lib/chef/cookbook/cookbook_version_loader.rb @@ -253,8 +253,8 @@ class Chef end def load_recursively_as(category, category_dir, glob) - file_spec = File.join(Chef::Util::PathHelper.escape_glob(cookbook_path, category_dir), "**", glob) - Dir.glob(file_spec, File::FNM_DOTMATCH).each do |file| + glob_pattern = File.join(Chef::Util::PathHelper.escape_glob(cookbook_path, category_dir), "**", glob) + select_files_by_glob(glob_pattern, File::FNM_DOTMATCH).each do |file| file = Chef::Util::PathHelper.cleanpath(file) next if File.directory?(file) name = Chef::Util::PathHelper.relative_path_from(@cookbook_path, file) @@ -263,13 +263,31 @@ class Chef end def load_as(category, *path_glob) - Dir[File.join(Chef::Util::PathHelper.escape_glob(cookbook_path), *path_glob)].each do |file| + glob_pattern = File.join(Chef::Util::PathHelper.escape_glob(cookbook_path), *path_glob) + select_files_by_glob(glob_pattern).each do |file| file = Chef::Util::PathHelper.cleanpath(file) name = Chef::Util::PathHelper.relative_path_from(@cookbook_path, file) cookbook_settings[category][name] = file end end + # Mimic Dir.glob inside a cookbook by running `File.fnmatch?` against + # `cookbook_settings[:all_files]`. + # + # @param pattern [String] a glob string passed to `File.fnmatch?` + # @param option [Integer] Option flag to control globbing behavior. These + # are constants defined on `File`, such as `File::FNM_DOTMATCH`. + # `File.fnmatch?` and `Dir.glob` only take one option argument, if you + # need to combine options, you must `|` the constants together. To make + # `File.fnmatch?` behave like `Dir.glob`, `File::FNM_PATHNAME` is + # always enabled. + def select_files_by_glob(pattern, option = 0) + combined_opts = option | File::FNM_PATHNAME + cookbook_settings[:all_unignored_files].values.select do |path| + File.fnmatch?(pattern, path, combined_opts) + end + end + def remove_ignored_files cookbook_settings.each_value do |file_list| file_list.reject! do |relative_path, full_path| |