From 60b8878f2176d7698bf789c5fdbbefc821b8ab05 Mon Sep 17 00:00:00 2001 From: apainintheneck Date: Wed, 28 Aug 2024 19:09:56 -0700 Subject: [PATCH] tap: share components of formula and cask file path regexes The goal here is to make things easier to read by better describing how different regexes are similar to each other. --- Library/Homebrew/tap.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/tap.rb b/Library/Homebrew/tap.rb index dec5485ee5..fa2faee250 100644 --- a/Library/Homebrew/tap.rb +++ b/Library/Homebrew/tap.rb @@ -753,15 +753,21 @@ class Tap end end + RUBY_FILE_NAME_REGEX = %r{[^/]+\.rb} + private_constant :RUBY_FILE_NAME_REGEX + + ZERO_OR_MORE_SUBDIRECTORIES_REGEX = %r{(?:[^/]+/)*} + private_constant :ZERO_OR_MORE_SUBDIRECTORIES_REGEX + sig { returns(Regexp) } def formula_file_regex @formula_file_regex ||= case formula_dir when path/"Formula" - %r{^Formula(/[^/]+)+\.rb$} + %r{^Formula/#{ZERO_OR_MORE_SUBDIRECTORIES_REGEX.source}#{RUBY_FILE_NAME_REGEX.source}$}o when path/"HomebrewFormula" - %r{^HomebrewFormula(/[^/]+)+\.rb$} + %r{^HomebrewFormula/#{ZERO_OR_MORE_SUBDIRECTORIES_REGEX.source}#{RUBY_FILE_NAME_REGEX.source}$}o when path - %r{^[^/]+\.rb$} + /^#{RUBY_FILE_NAME_REGEX.source}$/o else raise ArgumentError, "Unexpected formula_dir: #{formula_dir}" end @@ -774,7 +780,7 @@ class Tap file.match?(formula_file_regex) end - CASK_FILE_REGEX = %r{^Casks(/[^/]+)+\.rb$} + CASK_FILE_REGEX = %r{^Casks/#{ZERO_OR_MORE_SUBDIRECTORIES_REGEX.source}#{RUBY_FILE_NAME_REGEX.source}$} private_constant :CASK_FILE_REGEX # accepts the relative path of a file from {Tap}'s path