Sha256: 96e7b76d802c995f3c9c1bc5730a444f2bd33969cea446f4212e3206b660711f

Contents?: true

Size: 1.33 KB

Versions: 34

Compression:

Stored size: 1.33 KB

Contents

require 'dply/helper'
module Dply
  class Linker

    include Helper

    attr_reader :src_dir, :dest_dir, :map

    def initialize(src_dir, dest_dir, map: {})
      verify_absolute src_dir, dest_dir
      @src_dir = src_dir
      @dest_dir = dest_dir
      @map = map
    end
    
    def create_symlinks
      mapped_files.each do |f|
        target = link_target(f)
        source = link_source(f)
        relative_source = link_relative_source(source, target)
        logger.debug "linking #{target} -> #{source}"
        error "source #{source} doesn't exist" if not File.exist? source
        symlink(relative_source, target)
      end
    end

    def link_target(relative_target)
      Pathname.new "#{dest_dir}/#{relative_target}"
    end

    def link_source(relative_target)
      relative_source = map[relative_target]
      Pathname.new "#{src_dir}/#{relative_source}"
    end

    def link_relative_source(source, target)
      source.relative_path_from target.parent
    end
    
    def mapped_files
      map.keys.collect do |k|
        path = Pathname.new k 
        raise "config map path cannot be absoulte" if path.absolute?
        k
      end
    end

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

  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
dply-0.2.19 lib/dply/linker.rb
dply-0.2.18 lib/dply/linker.rb
dply-0.2.17 lib/dply/linker.rb
dply-0.2.16 lib/dply/linker.rb
dply-0.2.15 lib/dply/linker.rb
dply-0.2.14 lib/dply/linker.rb
dply-0.2.13 lib/dply/linker.rb
dply-0.2.11 lib/dply/linker.rb
dply-0.2.10 lib/dply/linker.rb
dply-0.2.9 lib/dply/linker.rb
dply-0.2.8 lib/dply/linker.rb
dply-0.2.7 lib/dply/linker.rb
dply-0.2.6 lib/dply/linker.rb
dply-0.2.5 lib/dply/linker.rb
dply-0.2.4 lib/dply/linker.rb
dply-0.2.3 lib/dply/linker.rb
dply-0.2.2 lib/dply/linker.rb
dply-0.2.0 lib/dply/linker.rb
dply-0.1.19 lib/dply/linker.rb
dply-0.1.18 lib/dply/linker.rb