Module: LiveReload

Defined in:
lib/livereload.rb

Defined Under Namespace

Classes: Config, Project

Constant Summary collapse

GEM_VERSION =
"1.3"
API_VERSION =
"1.3"
PROJECT_CONFIG_FILE_TEMPLATE =
<<-END.strip.split("\n").collect { |line| line.strip + "\n" }.join("")
# Lines starting with pound sign (#) are ignored.

# additional extensions to monitor
#config.exts << 'haml'

# exclude files with NAMES matching this mask
#config.exclusions << '~*'
# exclude files with PATHS matching this mask (if the mask contains a slash)
#config.exclusions << '/excluded_dir/*'
# exclude files with PATHS matching this REGEXP
#config.exclusions << /somedir.*(ab){2,4}\.(css|js)$/

# reload the whole page when .js changes
#config.apply_js_live = false
# reload the whole page when .css changes
#config.apply_css_live = false
END
DEFAULT_CONFIG =
Config.new do |config|
  config.debug = false
  config.host  = '0.0.0.0'
  config.port  = 10083
  config.exts  = %w/html css js png gif jpg php php5 py rb erb/
  config.exclusions = %w!*/.git/* */.svn/* */.hg/*!
  config.apply_js_live  = true
  config.apply_css_live = true
end
USER_CONFIG_FILE =
File.expand_path("~/.livereload")
USER_CONFIG =
Config.load_from(USER_CONFIG_FILE)

Class Method Summary collapse

Class Method Details

.configure {|Config| ... } ⇒ Object

Yields:



189
190
191
# File 'lib/livereload.rb', line 189

def self.configure
  yield Config
end

.run(directories, explicit_config) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/livereload.rb', line 193

def self.run(directories, explicit_config)
  # EventMachine needs to run kqueue for the watching API to work
  EM.kqueue = true if EM.kqueue?

  web_sockets = []

  # for host and port
  global_config = Config.merge(DEFAULT_CONFIG, USER_CONFIG, explicit_config)
  directories = directories.collect { |directory| File.expand_path(directory) }
  projects = directories.collect { |directory| Project.new(directory, explicit_config) }

  puts
  puts "Version:  #{GEM_VERSION}  (compatible with browser extension versions #{API_VERSION}.x)"
  puts "Port:     #{global_config.port}"
  projects.each { |project| project.print_config }

  EventMachine.run do
    projects.each do |project|
      project.when_changes_detected do |event, modified_file|
        case event
        when :config_changed
          puts
          puts ">> Configuration change: " + modified_file
          puts
          EventMachine.next_tick do
            projects.each { |project| project.read_config; project.print_config }
            puts
            projects.each { |project| project.restart_watching }
          end
        when :excluded
          puts "Excluded: #{File.basename(modified_file)}"
        when :modified
          puts "Modified: #{File.basename(modified_file)}"
          data = ['refresh', { :path => modified_file,
              :apply_js_live  => project.config.apply_js_live,
              :apply_css_live => project.config.apply_css_live }].to_json
          puts data if global_config.debug
          web_sockets.each do |ws|
            ws.send data
          end
        end
      end
    end

    projects.each { |project| project.restart_watching }

    puts
    puts "LiveReload is waiting for browser to connect."
    EventMachine::WebSocket.start(:host => global_config.host, :port => global_config.port, :debug => global_config.debug) do |ws|
      ws.onopen do
        begin
          puts "Browser connected."; ws.send "!!ver:#{API_VERSION}"; web_sockets << ws
        rescue
          puts $!
          puts $!.backtrace
        end
      end
      ws.onmessage do |msg|
        puts "Browser URL: #{msg}"
      end
      ws.onclose do
        web_sockets.delete ws
        puts "Browser disconnected."
      end
    end
  end
end