Class: VagrantPlugins::Fsevents::Paths

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-fsevents/paths.rb

Overview

Logic for paths to watch and ignore

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePaths

Returns a new instance of Paths.



8
9
10
11
# File 'lib/vagrant-fsevents/paths.rb', line 8

def initialize
  @watch = {}
  @ignore = []
end

Instance Attribute Details

#ignoreObject (readonly)

Returns the value of attribute ignore.



6
7
8
# File 'lib/vagrant-fsevents/paths.rb', line 6

def ignore
  @ignore
end

#watchObject (readonly)

Returns the value of attribute watch.



5
6
7
# File 'lib/vagrant-fsevents/paths.rb', line 5

def watch
  @watch
end

Instance Method Details

#active_for_folder?(options) ⇒ Boolean

checks to ensure that a path watch is not completely disabled

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/vagrant-fsevents/paths.rb', line 67

def active_for_folder?(options)
  (
    (options == true) || (
      options.respond_to?(:include?) &&
      (
        options.include?(:modified) ||
        options.include?(:added) ||
        options.include?(:removed)
      )
    )
  )
end

#add_folder_to_watch(id, opts, machine) ⇒ Object

Populates ‘@watch` attribute from a single watch



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

def add_folder_to_watch(id, opts, machine)
  get_paths(opts, machine).each do |paths|
    @watch[paths[:hostpath]] = {
      id: id,
      machine: machine,
      opts: opts,
      sync_path: paths[:syncpath],
      watch_path: paths[:hostpath]
    }
  end
end

#add_folders_to_ignore(folders) ⇒ Object

Populates ‘@ignore` attribute from a single VM instance



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/vagrant-fsevents/paths.rb', line 43

def add_folders_to_ignore(folders)
  folders.values.each do |folder|
    folder.values.each do |opts|
      next unless active_for_folder?(opts[:fsevents])

      Array(opts[:exclude]).each do |pattern|
        @ignore << exclude_to_regexp(pattern.to_s)
      end
    end
  end
end

#add_folders_to_watch(machine, folders) ⇒ Object

Populates ‘@watch` attribute from a single VM instance



19
20
21
22
23
24
25
26
27
# File 'lib/vagrant-fsevents/paths.rb', line 19

def add_folders_to_watch(machine, folders)
  folders.values.each do |folder|
    folder.each do |id, opts|
      next unless active_for_folder? opts[:fsevents]

      add_folder_to_watch(id, opts, machine)
    end
  end
end

#exclude_to_regexp(exclude) ⇒ Object

This is REALLY ghetto, but its a start. We can improve and keep unit tests passing in the future.



57
58
59
60
61
62
63
64
# File 'lib/vagrant-fsevents/paths.rb', line 57

def exclude_to_regexp(exclude)
  exclude = exclude.gsub('**', '|||GLOBAL|||')
  exclude = exclude.gsub('*', '|||PATH|||')
  exclude = exclude.gsub('|||PATH|||', '[^/]*')
  exclude = exclude.gsub('|||GLOBAL|||', '.*')

  Regexp.new(exclude)
end

#get_host_path(path, machine) ⇒ Object

Convert a path to a full path on the host machine



95
96
97
98
99
100
101
102
# File 'lib/vagrant-fsevents/paths.rb', line 95

def get_host_path(path, machine)
  hostpath = File.expand_path(path, machine.env.root_path)
  hostpath = Vagrant::Util::Platform.fs_real_path(hostpath).to_s

  # Avoid creating a nested directory
  hostpath += '/' unless hostpath.end_with?('/')
  hostpath
end

#get_paths(options, machine) ⇒ Object

Get the hostpath(s) for a given synced folder



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/vagrant-fsevents/paths.rb', line 81

def get_paths(options, machine)
  paths = (options[:include] || [options[:hostpath]])
  hostpaths = []
  syncpath = get_host_path(options[:hostpath], machine)
  paths.each do |path|
    hostpaths << {
      hostpath: get_host_path(path, machine),
      syncpath: syncpath
    }
  end
  hostpaths
end

#process(machine, folders) ⇒ Object



13
14
15
16
# File 'lib/vagrant-fsevents/paths.rb', line 13

def process(machine, folders)
  add_folders_to_watch(machine, folders)
  add_folders_to_ignore(folders)
end