lib/tap/root.rb in bahuvrihi-tap-0.10.4 vs lib/tap/root.rb in bahuvrihi-tap-0.10.5

- old
+ new

@@ -76,24 +76,24 @@ # use dir.length + 1 to remove a leading '/'. If dir.length + 1 >= expanded.length # as in: relative_filepath('/path', '/path') then the first arg returns nil, and an # empty string is returned expanded_path[( expanded_dir.chomp("/").length + 1)..-1] || "" - end - - # Generates a target filepath translated from the source_dir to - # the target_dir. Raises an error if the filepath is not relative - # to the source_dir. - # - # Root.translate("/path/to/file.txt", "/path", "/another/path") # => '/another/path/to/file.txt' - # - def translate(path, source_dir, target_dir) - unless relative_path = relative_filepath(source_dir, path) - raise ArgumentError, "\n#{path}\nis not relative to:\n#{source_dir}" - end - File.join(target_dir, relative_path) end + + # Generates a target filepath translated from the source_dir to + # the target_dir. Raises an error if the filepath is not relative + # to the source_dir. + # + # Root.translate("/path/to/file.txt", "/path", "/another/path") # => '/another/path/to/file.txt' + # + def translate(path, source_dir, target_dir) + unless relative_path = relative_filepath(source_dir, path) + raise ArgumentError, "\n#{path}\nis not relative to:\n#{source_dir}" + end + File.join(target_dir, relative_path) + end # Lists all unique paths matching the input glob patterns. def glob(*patterns) patterns.collect do |pattern| Dir.glob(pattern) @@ -291,24 +291,23 @@ # Tap::Root.minimal_match?('dir/file-0.1.0.txt', 'file') # => true # Tap::Root.minimal_match?('dir/file-0.1.0.txt', 'ile') # => false # Tap::Root.minimal_match?('dir/file-0.1.0.txt', 'r/file') # => true # def minimal_match?(path, mini_path) - extname = File.extname(mini_path) - extname = '' if extname =~ /^\.\d+$/ + extname = non_version_extname(mini_path) version = mini_path =~ /(-\d+(\.\d+)*)#{extname}$/ ? $1 : '' match_path = case when !extname.empty? # force full match path when !version.empty? # match up to version - path.chomp(File.extname(path)) + path.chomp(non_version_extname(path)) else # match up base - path.chomp(File.extname(path)).sub(/(-\d+(\.\d+)*)$/, '') + path.chomp(non_version_extname(path)).sub(/(-\d+(\.\d+)*)$/, '') end # key ends with pattern AND basenames of each are equal... # the last check ensures that a full path segment has # been specified @@ -396,10 +395,17 @@ end just_one end + # utility method for minimal_match -- returns a non-version + # extname, or an empty string if the path ends in a version. + def non_version_extname(path) # :nodoc: + extname = File.extname(path) + extname =~ /^\.\d+$/ ? '' : extname + end + end include Support::Versions include Support::Configurable @@ -538,10 +544,10 @@ # the aliased target_dir. Raises an error if the filepath is not relative # to the aliased source_dir. # # fp = r.filepath(:in, 'path/to/file.txt') # => '/root_dir/in/path/to/file.txt' # r.translate(fp, :in, :out) # => '/root_dir/out/path/to/file.txt' - def translate(filepath, source_dir, target_dir) + def translate(filepath, source_dir, target_dir) Root.translate(filepath, self[source_dir], self[target_dir]) end # Lists all files in the aliased dir matching the input patterns. Patterns # should be valid inputs for +Dir.glob+. If no patterns are specified, lists \ No newline at end of file