Class: VagrantPlugins::InstantRsyncAuto::RsyncHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-instant-rsync-auto/helper.rb

Overview

This is a helper that abstracts out the functionality of rsyncing folders so that it can be called from anywhere.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(machine, ssh_info, opts) ⇒ RsyncHelper

Returns a new instance of RsyncHelper.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/vagrant-instant-rsync-auto/helper.rb', line 9

def initialize(machine, ssh_info, opts)
  @machine = machine

  @opts = normalize_opts(opts, ssh_info)

  @guestpath = guestpath
  @hostpath = hostpath
  @excludes = excludes

  @rsync_command = rsync_command(ssh_info)
  @rsync_command_opts = rsync_command_opts

  @first_sync_done = false
end

Class Method Details

.exclude_to_regexp(path, exclude) ⇒ Object

This converts an rsync exclude pattern to a regular expression we can send to Listen.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/vagrant-instant-rsync-auto/helper.rb', line 59

def self.exclude_to_regexp(path, exclude)
  start_anchor = false

  if exclude.start_with?("/")
    start_anchor = true
    exclude      = exclude[1..-1]
  end

  path   = "#{path}/" if !path.end_with?("/")
  regexp = "^#{Regexp.escape(path)}"
  regexp += ".*" if !start_anchor

  # This is REALLY ghetto, but its a start. We can improve and
  # keep unit tests passing in the future.
  exclude = exclude.gsub("**", "|||GLOBAL|||")
  exclude = exclude.gsub("*", "|||PATH|||")
  exclude = exclude.gsub("|||PATH|||", "[^/]*")
  exclude = exclude.gsub("|||GLOBAL|||", ".*")
  regexp += exclude

  Regexp.new(regexp)
end

.rsync_single(machine, ssh_info, opts) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/vagrant-instant-rsync-auto/helper.rb', line 82

def self.rsync_single(machine, ssh_info, opts)
  machine.ui.warn("WARNING: `VagrantPlugins::SyncedFolderRSync::RsyncHelper.rsync_single`")
  machine.ui.warn("is a deprecated internal API. Please use an instance of that class instead.")

  instance = new(machine, ssh_info, opts)
  instance.rsync_single
end

Instance Method Details

#rsync_singleObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vagrant-instant-rsync-auto/helper.rb', line 24

def rsync_single
  log_info

  # If we have tasks to do before rsyncing, do those.
  if !skip_rsync_pre? && @machine.guest.capability?(:rsync_pre)
    @machine.guest.capability(:rsync_pre, @opts)
  end

  subprocess = if verbose?
    Vagrant::Util::Subprocess.execute(*(@rsync_command + [@rsync_command_opts])) {
      |io_name,data| data.each_line { |line|
        @machine.ui.info("rsync[#{io_name}] -> #{line}") }
    }
  else
    Vagrant::Util::Subprocess.execute(*(@rsync_command + [@rsync_command_opts]))
  end

  if subprocess.exit_code != 0
    raise Vagrant::Errors::RSyncError,
      command: @rsync_command.join(" "),
      guestpath: @guestpath,
      hostpath: @hostpath,
      stderr: subprocess.stderr
  end

  # If we have tasks to do after rsyncing, do those.
  if !skip_rsync_post? && @machine.guest.capability?(:rsync_post)
    @machine.guest.capability(:rsync_post, @opts)
  end

  @first_sync_done = true
end