Sha256: d9ebd4aace804d2bea49c9d2ba1b541057fc14d595094bf92f1ba0e5b5712c6e

Contents?: true

Size: 1.86 KB

Versions: 327

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

require 'eac_ruby_utils/core_ext'

module Avm
  class Sync
    attr_reader :excludes, :includes

    common_constructor :source_path, :target_path do
      self.source_path = source_path.to_pathname
      self.target_path = target_path.to_pathname
      @excludes = []
      @includes = []
    end

    def run
      clear_target
      copy
    end

    def add_exclude(exclude)
      excludes << exclude

      self
    end

    def add_excludes(*excludes)
      excludes.each { |exclude| add_exclude(exclude) }

      self
    end

    def add_include(include)
      includes << include

      self
    end

    def add_includes(*includes)
      includes.each { |include| add_include(include) }

      self
    end

    def move_mode(value)
      @move_mode = value

      self
    end

    def move_mode?
      @move_mode ? true : false
    end

    private

    def clear_target
      target_remove(target_path)
      target_path.children.each { |tchild| target_remove(tchild) }
    end

    def copy
      source_path.children.each do |schild|
        ::FileUtils.cp_r(schild.to_path, target_path.to_path)
        schild.rmtree if move_mode?
      end
    end

    def source_to_target_path(source_path)
      source_path.relative_path_from(self.source_path).expand_path(target_path)
    end

    def target_remove(tpath)
      return if skip_target_path?(tpath)

      if tpath.directory?
        tpath.children.each { |tchild| target_remove(tchild) }
        tpath.rmdir if tpath.children.empty?
      elsif tpath.file?
        tpath.unlink
      end
    end

    def skip_target_path?(tpath)
      skip_path?(tpath.relative_path_from(target_path))
    end

    def skip_path?(relpath)
      relpath = relpath.expand_path('/')
      excludes.any? { |exclude| relpath.fnmatch?(exclude) } &&
        includes.none? { |include| relpath.fnmatch?(include) }
    end
  end
end

Version data entries

327 entries across 327 versions & 3 rubygems

Version Path
eac_tools-0.60.0 sub/avm/lib/avm/sync.rb
eac_tools-0.59.0 sub/avm/lib/avm/sync.rb
avm-0.66.1 lib/avm/sync.rb
eac_tools-0.58.0 sub/avm/lib/avm/sync.rb
eac_tools-0.57.0 sub/avm/lib/avm/sync.rb
eac_tools-0.56.1 sub/avm/lib/avm/sync.rb
avm-0.66.0 lib/avm/sync.rb
eac_tools-0.56.0 sub/avm/lib/avm/sync.rb
avm-0.65.0 lib/avm/sync.rb
eac_tools-0.55.7 sub/avm/lib/avm/sync.rb
avm-0.64.2 lib/avm/sync.rb
eac_tools-0.55.6 sub/avm/lib/avm/sync.rb
avm-0.64.1 lib/avm/sync.rb
eac_tools-0.55.5 sub/avm/lib/avm/sync.rb
avm-0.64.0 lib/avm/sync.rb
eac_tools-0.55.4 sub/avm/lib/avm/sync.rb
avm-0.63.0 lib/avm/sync.rb
eac_tools-0.55.3 sub/avm/lib/avm/sync.rb
eac_tools-0.55.2 sub/avm/lib/avm/sync.rb
eac_tools-0.55.1 sub/avm/lib/avm/sync.rb