From 312d8eeebe50cb42c54a1379ff1861df4ecd351a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 4 Sep 2024 18:19:51 +0000 Subject: [PATCH] Extract common logic to a shared method --- lib/propshaft/asset.rb | 9 +++++++++ lib/propshaft/output_path.rb | 11 ++--------- lib/propshaft/server.rb | 4 +--- test/propshaft/output_path_test.rb | 5 ++++- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/lib/propshaft/asset.rb b/lib/propshaft/asset.rb index 1210871..58e81dd 100644 --- a/lib/propshaft/asset.rb +++ b/lib/propshaft/asset.rb @@ -4,6 +4,15 @@ class Propshaft::Asset attr_reader :path, :logical_path, :load_path + class << self + def extract_path_and_digest(digested_path) + digest = digested_path[/-([0-9a-zA-Z]{7,128})\.(?!digested)([^.]|.map)+\z/, 1] + path = digest ? digested_path.sub("-#{digest}", "") : digested_path + + [path, digest] + end + end + def initialize(path, logical_path:, load_path:) @path, @logical_path, @load_path = path, Pathname.new(logical_path), load_path end diff --git a/lib/propshaft/output_path.rb b/lib/propshaft/output_path.rb index df6687e..f063134 100644 --- a/lib/propshaft/output_path.rb +++ b/lib/propshaft/output_path.rb @@ -26,7 +26,7 @@ def files Hash.new.tap do |files| all_files_from_tree(path).each do |file| digested_path = file.relative_path_from(path) - logical_path, digest = extract_path_and_digest(digested_path) + logical_path, digest = Propshaft::Asset.extract_path_and_digest(digested_path.to_s) files[digested_path.to_s] = { logical_path: logical_path.to_s, @@ -42,7 +42,7 @@ def fresh_version_within_limit(mtime, count, expires_at:, limit:) modified_at = [ 0, Time.now - mtime ].max modified_at < expires_at || limit < count end - + def remove(path) FileUtils.rm(@path.join(path)) Propshaft.logger.info "Removed #{path}" @@ -51,11 +51,4 @@ def remove(path) def all_files_from_tree(path) path.children.flat_map { |child| child.directory? ? all_files_from_tree(child) : child } end - - def extract_path_and_digest(digested_path) - digest = digested_path.to_s[/-([0-9a-f]{7,128})\.(?!digested)/, 1] - path = digest ? digested_path.sub("-#{digest}", "") : digested_path - - [path, digest] - end end diff --git a/lib/propshaft/server.rb b/lib/propshaft/server.rb index 777ab1b..c50d7d9 100644 --- a/lib/propshaft/server.rb +++ b/lib/propshaft/server.rb @@ -35,10 +35,8 @@ def inspect private def extract_path_and_digest(env) full_path = Rack::Utils.unescape(env["PATH_INFO"].to_s.sub(/^\//, "")) - digest = full_path[/-([0-9a-zA-Z]{7,128})\.(?!digested)([^.]|.map)+\z/, 1] - path = digest ? full_path.sub("-#{digest}", "") : full_path - [ path, digest ] + Propshaft::Asset.extract_path_and_digest(full_path) end if Gem::Version.new(Rack::RELEASE) < Gem::Version.new("3") diff --git a/test/propshaft/output_path_test.rb b/test/propshaft/output_path_test.rb index 5210cf6..49f1c83 100644 --- a/test/propshaft/output_path_test.rb +++ b/test/propshaft/output_path_test.rb @@ -54,10 +54,13 @@ class Propshaft::OutputPathTest < ActiveSupport::TestCase old = output_asset("by_count.txt.map", "old", created_at: Time.now - 300) current = output_asset("by_count.txt.map", "current", created_at: Time.now - 180) + assert File.exist?(current) + assert File.exist?(old) + @output_path.clean(1, 0) assert File.exist?(current) - assert_not File.exist?(old) + assert_not File.exist?(old), "#{old} should not exist" ensure FileUtils.rm(old) if File.exist?(old) FileUtils.rm(current) if File.exist?(current)