lib/rake/funnel/extensions/common_path.rb in rake-funnel-0.3.2.pre vs lib/rake/funnel/extensions/common_path.rb in rake-funnel-0.4.0.pre

- old
+ new

@@ -1,52 +1,60 @@ -require 'abbrev' -require 'pathname' - -module Rake::Funnel::Extensions - module CommonPath - def common_path - list = self - .to_a - .compact - .map { |x| components(x) } - - min = list.min_by { |x| x.length } - - matches = list.map do |path| - longest_prefix = [] - - path.zip(min).each do |left, right| - if left != right - next - end - longest_prefix << right - end - - File.join(longest_prefix) - end - - matches.min_by { |x| x.length } || '' - end - - private - def components(path) - paths = [] - Pathname.new(path).descend do |p| - paths << p - end - - paths.inject([]) { |components, path| - relative = path.relative_path_from(components.last[:absolute]) if components.any? - - components << { absolute: path, relative: relative || path } - }.map { |component| component[:relative].to_s } - end - end -end - -class Array - include Rake::Funnel::Extensions::CommonPath -end - -class Rake::FileList - include Rake::Funnel::Extensions::CommonPath -end +require 'abbrev' +require 'pathname' + +module Rake + module Funnel + module Extensions + module CommonPath + def common_path + list = to_a + .compact + .map { |x| components(x) } + + min = list.min_by(&:length) + + matches = find_matches(list, min) + matches.min_by(&:length) || '' + end + + private + def components(path) + paths = [] + Pathname.new(path).descend do |p| + paths << p + end + + paths = paths.inject([]) { |components, p| + relative = p.relative_path_from(components.last[:absolute]) if components.any? + + components << { absolute: p, relative: relative || p } + } + + paths.map { |component| component[:relative].to_s } + end + + def find_matches(list, min) + list.map do |path| + longest_prefix = [] + + path.zip(min).each do |left, right| + next if left != right + longest_prefix << right + end + + File.join(longest_prefix) + end + end + end + end + end +end + +class Array + include Rake::Funnel::Extensions::CommonPath +end + +module Rake + class FileList + include Rake::Funnel::Extensions::CommonPath + end +end