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.
This commit is contained in:
apainintheneck 2024-08-28 19:09:56 -07:00
parent 62a4734c90
commit 60b8878f21

View File

@ -753,15 +753,21 @@ class Tap
end end
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) } sig { returns(Regexp) }
def formula_file_regex def formula_file_regex
@formula_file_regex ||= case formula_dir @formula_file_regex ||= case formula_dir
when path/"Formula" when path/"Formula"
%r{^Formula(/[^/]+)+\.rb$} %r{^Formula/#{ZERO_OR_MORE_SUBDIRECTORIES_REGEX.source}#{RUBY_FILE_NAME_REGEX.source}$}o
when path/"HomebrewFormula" when path/"HomebrewFormula"
%r{^HomebrewFormula(/[^/]+)+\.rb$} %r{^HomebrewFormula/#{ZERO_OR_MORE_SUBDIRECTORIES_REGEX.source}#{RUBY_FILE_NAME_REGEX.source}$}o
when path when path
%r{^[^/]+\.rb$} /^#{RUBY_FILE_NAME_REGEX.source}$/o
else else
raise ArgumentError, "Unexpected formula_dir: #{formula_dir}" raise ArgumentError, "Unexpected formula_dir: #{formula_dir}"
end end
@ -774,7 +780,7 @@ class Tap
file.match?(formula_file_regex) file.match?(formula_file_regex)
end 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 private_constant :CASK_FILE_REGEX
# accepts the relative path of a file from {Tap}'s path # accepts the relative path of a file from {Tap}'s path