Sha256: d093d5c3062361d197fb183639af545d2ccbb60fdddf81ae9ec9c0a422531399

Contents?: true

Size: 1.25 KB

Versions: 18

Compression:

Stored size: 1.25 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([]) { |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

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
rake-funnel-0.18.0 lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.17.0 lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.16.1 lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.16.0 lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.15.0.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.14.0.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.13.0.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.12.0.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.11.0.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.10.0.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.9.1.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.9.0.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.8.0.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.7.0.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.6.1.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.6.0.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.5.0.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.4.0.pre lib/rake/funnel/extensions/common_path.rb