module Rake::Funnel::Extensions::CommonPath

Public Instance Methods

common_path() click to toggle source
# File lib/rake/funnel/extensions/common_path.rb, line 10
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 Instance Methods

components(path) click to toggle source
# File lib/rake/funnel/extensions/common_path.rb, line 23
def components(path)
  paths = []
  Pathname.new(path).descend do |p|
    paths << p
  end

  paths = paths.inject([]) do |components, p|
    relative = p.relative_path_from(components.last[:absolute]) if components.any?

    components << { absolute: p, relative: relative || p }
  end

  paths.map { |component| component[:relative].to_s }
end
find_matches(list, min) click to toggle source
# File lib/rake/funnel/extensions/common_path.rb, line 38
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