Class: VagrantPlugins::Fsevents::Main

Inherits:
Object
  • Object
show all
Includes:
Vagrant::Action::Builtin::MixinSyncedFolders
Defined in:
lib/vagrant-fsevents/main.rb

Overview

Class containing the main plugin activities

Constant Summary collapse

LOGGING_CONTEXT =
'vagrant::commands::fsevents'.freeze
ADAPTER =
Listen::Adapter.select.inspect.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.synopsisObject



43
44
45
# File 'lib/vagrant-fsevents/main.rb', line 43

def self.synopsis
  'forwards filesystem events to virtual machine'
end

Instance Method Details

#command_line_argvObject

This parses the command line arguments passed by the user



91
92
93
94
95
96
# File 'lib/vagrant-fsevents/main.rb', line 91

def command_line_argv
  parse_options OptionParser.new do |o|
    o.banner = 'Usage: vagrant fsevents [vm-name]'
    o.separator ''
  end
end

#create_interrupt_callback(listener) ⇒ Object

Create the callback that lets us know when we’ve been interrupted



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/vagrant-fsevents/main.rb', line 71

def create_interrupt_callback(listener)
  queue    = Queue.new
  callback = lambda do
    # This needs to execute in another thread because Thread
    # synchronization can't happen in a trap context.
    Thread.new { queue << true }
  end

  # Run the listener in a busy block so that we can cleanly
  # exit once we receive an interrupt.
  Vagrant::Util::Busy.busy(callback) do
    listener.start
    queue.pop
    listener.stop if listener.state != :stopped
  end

  0
end

#executeObject

Main method for constructing the plugin



14
15
16
17
18
19
20
# File 'lib/vagrant-fsevents/main.rb', line 14

def execute
  init_supporting_classes
  init_path_data
  log_listening_start_event
  print_watch_paths_to_console
  start_listening
end

#init_path_dataObject

Build list of paths to watch and ignore

Raises:



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vagrant-fsevents/main.rb', line 31

def init_path_data
  with_target_vms(command_line_argv) do |machine|
    unless machine.communicate.ready?
      raise Vagrant::Errors::VMNotCreatedError
    end

    @paths.process(machine, synced_folders(machine))
  end

  raise NothingToSyncError.new if @paths.watch.empty?
end

#init_supporting_classesObject

Instantiate supporting classes



23
24
25
26
27
28
# File 'lib/vagrant-fsevents/main.rb', line 23

def init_supporting_classes
  @logger = VagrantPlugins::Fsevents::Logger.new LOGGING_CONTEXT
  @alerter = VagrantPlugins::Fsevents::Alerter.new
  @responder = VagrantPlugins::Fsevents::Responder.new @logger, @alerter
  @paths = VagrantPlugins::Fsevents::Paths.new
end

#log_listening_start_eventObject

Log that we’ve started listening



48
49
50
# File 'lib/vagrant-fsevents/main.rb', line 48

def log_listening_start_event
  @logger.listening_with_adapter(ADAPTER, @paths.watch, @paths.ignore)
end

Alert the user to the paths we’re watching



53
54
55
56
57
# File 'lib/vagrant-fsevents/main.rb', line 53

def print_watch_paths_to_console
  @paths.watch.each do |path, data|
    @alerter.watching(data[:machine], path)
  end
end

#start_listeningObject

Start the actual listener that will respond to file changes



60
61
62
63
64
65
66
67
68
# File 'lib/vagrant-fsevents/main.rb', line 60

def start_listening
  listener = Listen.to(
    *@paths.watch.keys,
    ignore: @paths.ignore,
    &@responder.callback_proc.curry[@paths.watch]
  )

  create_interrupt_callback(listener)
end