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
avm-0.90.0 lib/avm/sync.rb
eac_tools-0.87.0 sub/avm/lib/avm/sync.rb
avm-0.89.0 lib/avm/sync.rb
eac_tools-0.86.12 sub/avm/lib/avm/sync.rb
avm-0.88.0 lib/avm/sync.rb
eac_tools-0.86.11 sub/avm/lib/avm/sync.rb
avm-0.87.0 lib/avm/sync.rb
eac_tools-0.86.8 sub/avm/lib/avm/sync.rb
avm-0.86.2 lib/avm/sync.rb
eac_tools-0.86.7 sub/avm/lib/avm/sync.rb
avm-0.86.1 lib/avm/sync.rb
eac_tools-0.86.5 sub/avm/lib/avm/sync.rb
avm-0.86.0 lib/avm/sync.rb
eac_tools-0.86.4 sub/avm/lib/avm/sync.rb
eac_tools-0.86.3 sub/avm/lib/avm/sync.rb
avm-0.85.0 lib/avm/sync.rb
eac_tools-0.86.2 sub/avm/lib/avm/sync.rb
avm-0.84.3 lib/avm/sync.rb
eac_tools-0.86.1 sub/avm/lib/avm/sync.rb
eac_tools-0.84.2 sub/avm/lib/avm/sync.rb