Sha256: 0c62ec5a91399d2201568fb4cf84d279d45503eb77224e48592bebee19a0e687

Contents?: true

Size: 1.11 KB

Versions: 11

Compression:

Stored size: 1.11 KB

Contents

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rake-funnel-0.3.2.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.3.1.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.3.0.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.2.0.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.1.0.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.0.6.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.0.5.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.0.4.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.0.3.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.0.2.pre lib/rake/funnel/extensions/common_path.rb
rake-funnel-0.0.1.pre lib/rake/funnel/extensions/common_path.rb