Module: Berkshelf::FileSyncer

Extended by:
FileSyncer
Included in:
FileSyncer
Defined in:
lib/berkshelf/file_syncer.rb

Constant Summary collapse

IGNORED_FILES =

Files to be ignored during a directory globbing

%w(. ..).freeze

Instance Method Summary collapse

Instance Method Details

#glob(pattern) ⇒ Array<String>

Glob across the given pattern, accounting for dotfiles, removing Ruby’s dumb idea to include ‘.’ and ‘..’ as entries.

Parameters:

  • pattern (String)

    the path or glob pattern to get all files from

Returns:

  • (Array<String>)

    the list of all files



20
21
22
23
24
25
# File 'lib/berkshelf/file_syncer.rb', line 20

def glob(pattern)
  Dir.glob(pattern, File::FNM_DOTMATCH).sort.reject do |file|
    basename = File.basename(file)
    IGNORED_FILES.include?(basename)
  end
end

#sync(source, destination, options = {}) ⇒ true

Copy the files from source to destination, while removing any files in destination that are not present in source.

The method accepts an optional :exclude parameter to ignore files and folders that match the given pattern(s). Note the exclude pattern behaves on paths relative to the given source. If you want to exclude a nested directory, you will need to use something like **/directory.

Parameters:

  • source (String)

    the path on disk to sync from

  • destination (String)

    the path on disk to sync to

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :exclude (String, Array<String>)

    a file, folder, or globbing pattern of files to ignore when syncing

Returns:

  • (true)

Raises:

  • ArgumentError if the source parameter is not a directory



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/berkshelf/file_syncer.rb', line 49

def sync(source, destination, options = {})
  unless File.directory?(source)
    raise ArgumentError, "`source' must be a directory, but was a " \
      "`#{File.ftype(source)}'! If you just want to sync a file, use " \
      "the `copy' method instead."
  end

  # Reject any files that match the excludes pattern
  excludes = Array(options[:exclude]).map do |exclude|
    [exclude, "#{exclude}/*"]
  end.flatten

  source_files = glob(File.join(source, '**/*'))
  source_files = source_files.reject do |source_file|
    basename = relative_path_for(source_file, source)
    excludes.any? { |exclude| File.fnmatch?(exclude, basename, File::FNM_DOTMATCH) }
  end

  # Ensure the destination directory exists
  FileUtils.mkdir_p(destination) unless File.directory?(destination)

  # Copy over the filtered source files
  source_files.each do |source_file|
    relative_path = relative_path_for(source_file, source)

    # Create the parent directory
    parent = File.join(destination, File.dirname(relative_path))
    FileUtils.mkdir_p(parent) unless File.directory?(parent)

    case File.ftype(source_file).to_sym
    when :directory
      FileUtils.mkdir_p("#{destination}/#{relative_path}")
    when :link
      target = File.readlink(source_file)

      destination = File.expand_path(destination)
      Dir.chdir(destination) do
        FileUtils.ln_sf(target, "#{destination}/#{relative_path}")
      end
    when :file
      FileUtils.cp(source_file, "#{destination}/#{relative_path}")
    else
      type = File.ftype(source_file)
      raise RuntimeError, "Unknown file type: `#{type}' at " \
        "`#{source_file}'. Failed to sync `#{source_file}' to " \
        "`#{destination}/#{relative_path}'!"
    end
  end

  # Remove any files in the destination that are not in the source files
  destination_files = glob("#{destination}/**/*")

  # Calculate the relative paths of files so we can compare to the
  # source.
  relative_source_files = source_files.map do |file|
    relative_path_for(file, source)
  end
  relative_destination_files = destination_files.map do |file|
    relative_path_for(file, destination)
  end

  # Remove any extra files that are present in the destination, but are
  # not in the source list
  extra_files = relative_destination_files - relative_source_files
  extra_files.each do |file|
    FileUtils.rm_rf(File.join(destination, file))
  end

  true
end