Sha256: 99fa3f13afb566d93cb32697fd7754c270ea8d98ced4dbc3331c90670e51d9b9

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

require 'yaml'

require_relative 'logger'
require_relative 'backup'
require_relative 'index'

module Flexdot
  class Installer
    def initialize(name, base_dir:, target_dir:)
      @name = name
      @base_dir = base_dir
      @target_dir = target_dir
      @backup = Backup.new
      @logger = Logger.new(@target_dir)
    end

    def install(index_file)
      index = Index.new(YAML.load_file(index_file.to_path))
      index.each do |dotfile_path:, target_path:|
        install_link(dotfile_path, target_path)
      end
    end

    private

    attr_reader :name, :base_dir, :target_dir, :backup, :logger

    def install_link(dotfile_path, target_path)
      dotfile = @base_dir.join(dotfile_path).expand_path
      target_file = @target_dir.join(target_path, dotfile.basename).expand_path

      logger.log(target_file) do |status|
        if target_file.symlink?
          if target_file.readlink == dotfile
            status.result = :already_linked
          else
            target_file.unlink
            target_file.make_symlink(dotfile.to_path)
            status.result = :link_updated
          end
        else
          if target_file.exist?
            backup.call(target_file)
            status.backuped = true
          elsif !target_file.dirname.exist?
            target_file.dirname.mkpath
          end

          target_file.make_symlink(dotfile.to_path)
          status.result = :link_created
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flexdot-1.0.2 lib/flexdot/installer.rb