Sha256: 1156061b29e69659935986633c9d6ef0f54fa9ee4a58384eaee69fea5bb56b99

Contents?: true

Size: 1.27 KB

Versions: 10

Compression:

Stored size: 1.27 KB

Contents

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([]) 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

        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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rake-funnel-0.22.2 lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.22.1 lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.22.0 lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.21.2 lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.21.1 lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.21.0 lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.20.2 lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.20.1 lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.20.0 lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.19.0 lib/rake/funnel/extensions/common_path.rb