Class: Middleman::Watcher

Inherits:
Object
  • Object
show all
Defined in:
middleman-core/lib/middleman-core/watcher.rb

Class Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Watcher) initialize(options)

A new instance of Watcher



35
36
37
38
# File 'middleman-core/lib/middleman-core/watcher.rb', line 35

def initialize(options)
  @options = options
  register_signal_handlers unless ::Middleman::WINDOWS
end

Class Attribute Details

+ (Object) singleton

Returns the value of attribute singleton



12
13
14
# File 'middleman-core/lib/middleman-core/watcher.rb', line 12

def singleton
  @singleton
end

Class Method Details

+ (Object) ignore_list



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'middleman-core/lib/middleman-core/watcher.rb', line 20

def ignore_list
  [
    /^\.sass-cache\//,
    /^\.git\//,
    /^\.gitignore$/,
    /^\.DS_Store$/,
    /^build\//,
    /^\.rbenv-.*$/,
    /^Gemfile$/,
    /^Gemfile\.lock$/,
    /~$/
  ]
end

+ (Object) start(options)



14
15
16
17
18
# File 'middleman-core/lib/middleman-core/watcher.rb', line 14

def start(options)
  self.singleton = new(options)
  self.singleton.watch! unless options[:disable-watcher"]
  self.singleton.start
end

Instance Method Details

- (void) reload

This method returns an undefined value.

Simply stop, then start



92
93
94
95
# File 'middleman-core/lib/middleman-core/watcher.rb', line 92

def reload
  stop
  start
end

- (void) run_on_change(paths)

This method returns an undefined value.

What to do on file change

Parameters:

  • paths (Array<String>)

    Array of paths that changed



100
101
102
103
104
105
106
107
108
# File 'middleman-core/lib/middleman-core/watcher.rb', line 100

def run_on_change(paths)
  # See if the changed file is config.rb or lib/*.rb
  return reload if needs_to_reload?(paths)
  
  # Otherwise forward to Middleman
  paths.each do |path|
    tell_server(:change => path) unless self.class.ignore_list.any? { |r| path.match(r) }
  end
end

- (void) run_on_deletion(paths)

This method returns an undefined value.

What to do on file deletion

Parameters:

  • paths (Array<String>)

    Array of paths that were removed



113
114
115
116
117
118
119
120
121
# File 'middleman-core/lib/middleman-core/watcher.rb', line 113

def run_on_deletion(paths)
  # See if the changed file is config.rb or lib/*.rb
  return reload if needs_to_reload?(paths)
  
  # Otherwise forward to Middleman
  paths.each do |path|
    tell_server(:delete => path) unless self.class.ignore_list.any? { |r| path.match(r) }
  end
end

- (void) start

This method returns an undefined value.

Start an instance of Middleman::Application



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'middleman-core/lib/middleman-core/watcher.rb', line 64

def start
  env = (@options[:environment] || "development").to_sym
  is_logging = @options.has_key?(:debug) && @options[:debug]
  
  app = ::Middleman.server.inst do
    set :environment, env
    set :logging, is_logging
  end
  
  app_rack = app.class.to_rack_app
  
  opts = @options.dup
  opts[:app] = app_rack
  opts[:logging] = is_logging
  puts "== The Middleman is standing watch on port #{opts[:port]||4567}"
  ::Middleman.start_server(opts)
end

- (void) stop

This method returns an undefined value.

Stop the forked Middleman



84
85
86
87
88
# File 'middleman-core/lib/middleman-core/watcher.rb', line 84

def stop
  puts "== The Middleman is shutting down"
  # TODO: Figure out some way to actually unload the whole thing
  #       or maybe just re-exec this same thing
end

- (Object) watch!



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'middleman-core/lib/middleman-core/watcher.rb', line 40

def watch!
  local = self

  # Watcher Library
  require "listen"
  
  listener = Listen.to(Dir.pwd, :relative_paths => true)
  listener.change do |modified, added, removed|
    added_and_modified = modified + added

    if added_and_modified.length > 0
      local.run_on_change(added_and_modified)
    end
    
    if removed.length > 0
      local.run_on_deletion(removed)
    end
  end
  # Don't block this thread
  listener.start(false)
end