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.37.0 sub/avm/lib/avm/sync.rb
avm-0.48.0 lib/avm/sync.rb
eac_tools-0.36.1 sub/avm/lib/avm/sync.rb
avm-0.47.0 lib/avm/sync.rb
eac_tools-0.36.0 sub/avm/lib/avm/sync.rb
avm-0.46.0 lib/avm/sync.rb
eac_tools-0.35.0 sub/avm/lib/avm/sync.rb
eac_tools-0.34.0 sub/avm/lib/avm/sync.rb
eac_tools-0.33.0 sub/avm/lib/avm/sync.rb
avm-0.45.0 lib/avm/sync.rb
eac_tools-0.32.0 sub/avm/lib/avm/sync.rb
avm-0.44.0 lib/avm/sync.rb
eac_tools-0.31.1 sub/avm/lib/avm/sync.rb
eac_tools-0.31.0 sub/avm/lib/avm/sync.rb
eac_tools-0.30.0 sub/avm/lib/avm/sync.rb
avm-0.43.0 lib/avm/sync.rb
eac_tools-0.29.0 sub/avm/lib/avm/sync.rb
avm-0.42.0 lib/avm/sync.rb
eac_tools-0.28.0 sub/avm/lib/avm/sync.rb
eac_tools-0.27.2 sub/avm/lib/avm/sync.rb