Sha256: 4c3c966fc63d709a5bf1d729d44ef72be8af4afafd57fe00ee3da61101e2b405

Contents?: true

Size: 1.4 KB

Versions: 11

Compression:

Stored size: 1.4 KB

Contents

require 'dply/helper'
module Dply
  class Linker

    include Helper

    def initialize(src_dir, dest_dir, map: {})
      verify_absolute src_dir, dest_dir
      @src_dir = src_dir
      @dest_dir = dest_dir
      @map = map
      validate_map!
    end

    def create_symlinks
      link_pairs.each do |src, dest|
        relative_source = src_relative_to_dest(src, dest)
        logger.debug "linking #{dest} -> #{src}" if logger.debug?
        error "source #{src} doesn't exist" if not src.exist?
        symlink(relative_source, dest)
      end
    end

    private

    def link_pairs
      Enumerator.new do |y|
        @map.each do |dest, src|
          src_path = Pathname.new "#{@src_dir}/#{src}"
          dest_path = Pathname.new "#{@dest_dir}/#{dest}"
          y.yield [src_path, dest_path]
        end
      end
    end

    def src_relative_to_dest(src, dest)
      src.relative_path_from dest.parent
    end

    def verify_absolute(*paths)
      paths.each do |path|
        absolute = Pathname.new(path).absolute?
        raise Error, "#{path} not absolute" if not absolute
      end
    end

    def validate_map!
      @map.each do |dest, src|
        dest_path = Pathname.new dest
        src_path = Pathname.new src
        raise Error, "dest path #{dest_path} not relative" if not dest_path.relative?
        raise Error, "src path #{src_path} not relative" if not src_path.relative?
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
dply-0.3.10 lib/dply/linker.rb
dply-0.3.9 lib/dply/linker.rb
dply-0.3.8 lib/dply/linker.rb
dply-0.3.7 lib/dply/linker.rb
dply-0.3.6 lib/dply/linker.rb
dply-0.3.5 lib/dply/linker.rb
dply-0.3.4 lib/dply/linker.rb
dply-0.3.3 lib/dply/linker.rb
dply-0.3.2 lib/dply/linker.rb
dply-0.3.1 lib/dply/linker.rb
dply-0.3.0 lib/dply/linker.rb