From ebcc1d163ff742ee6ab1f243299b17d49407ab78 Mon Sep 17 00:00:00 2001 From: Pavel Skrylev Date: Wed, 9 Dec 2020 15:25:43 +0300 Subject: [PATCH] feat: Find all asset by a regexp filemask or a filepath + finding all asset by a regexp filemask, i.e. /\.(?:svg|eot|woff|woff2|gif|ttf)$/ what is required for example for the Foreman --- CHANGELOG.md | 1 + lib/sprockets/base.rb | 19 +++++++++++++++++++ lib/sprockets/manifest.rb | 2 +- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index efac5a1b0..8fbe623ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Get upgrade notes from Sprockets 3.x to 4.x at https://github.com/rails/sprocket ## Master - Remove remaining support for Ruby < 2.4.[#672](https://github.com/rails/sprockets/pull/672) +- Find all asset by a regexp filemask or a filepath.[#698](https://github.com/rails/sprockets/pull/698) ## 4.0.2 diff --git a/lib/sprockets/base.rb b/lib/sprockets/base.rb index b1137cb3c..ec0828227 100644 --- a/lib/sprockets/base.rb +++ b/lib/sprockets/base.rb @@ -129,6 +129,25 @@ def find_asset!(*args) end end + # Find all asset by a regexp filemask or a filepath. + def find_all_assets(*args, &block) + paths = config[:paths] + + args.each do |arg| + if arg.is_a?(Regexp) + paths.each do |path| + files = Dir["#{path}/*"].select {|file| arg =~ file } + + files.each do |file| + find_all_linked_assets(file, &block) + end + end + else + find_all_linked_assets(arg, &block) + end + end + end + # Pretty inspect def inspect "#<#{self.class}:0x#{object_id.to_s(16)} " + diff --git a/lib/sprockets/manifest.rb b/lib/sprockets/manifest.rb index 17026968a..456e466eb 100644 --- a/lib/sprockets/manifest.rb +++ b/lib/sprockets/manifest.rb @@ -122,7 +122,7 @@ def find(*args) environment = self.environment.cached promises = args.flatten.map do |path| Concurrent::Promise.execute(executor: executor) do - environment.find_all_linked_assets(path) do |asset| + environment.find_all_assets(path) do |asset| yield asset end end