Permalink
Browse files

Update to Guard 2.0 + some refactoring

  • Loading branch information...
1 parent 5304fd2 commit ea244f3264254309b9082265c6f20e80678e162f @thibaudgg thibaudgg committed Oct 6, 2013
View
@@ -1,22 +1,14 @@
language: ruby
bundler_args: --without development
rvm:
- - 1.8.7
- 1.9.3
- 2.0.0
- - ruby-head
- - ree
- - jruby-18mode
- jruby-19mode
- - jruby-head
- - rbx-18mode
- rbx-19mode
matrix:
allow_failures:
- - rvm: rbx-18mode
+ - jruby-19mode
- rvm: rbx-19mode
- - rvm: ruby-head
- - rvm: jruby-head
notifications:
recipients:
- thibaud@thibaud.me
View
@@ -11,5 +11,5 @@ end
group :test do
gem 'rspec'
- gem 'coveralls', :require => false
+ gem 'coveralls', require: false
end
View
@@ -1,5 +1,5 @@
-guard 'rspec', :cli => '-f doc' do
+guard :rspec do
watch(%r{spec/.+_spec.rb})
- watch(%r{lib/(.+)\.rb}) { |m| "spec/#{m[1]}_spec.rb" }
+ watch(%r{lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
end
View
@@ -1,4 +1,6 @@
-# Guard::LiveReload [![Gem Version](https://badge.fury.io/rb/guard-livereload.png)](http://badge.fury.io/rb/guard-livereload) [![Build Status](https://travis-ci.org/guard/guard-livereload.png?branch=master)](http://travis-ci.org/guard/guard-livereload) [![Dependency Status](https://gemnasium.com/guard/guard-livereload.png)](https://gemnasium.com/guard/guard-livereload) [![Code Climate](https://codeclimate.com/github/guard/guard-livereload.png)](https://codeclimate.com/github/guard/guard-livereload) [![Coverage Status](https://coveralls.io/repos/guard/guard-livereload/badge.png?branch=master)](https://coveralls.io/r/guard/guard-livereload)
+# Guard::LiveReload
+
+[![Gem Version](https://badge.fury.io/rb/guard-livereload.png)](http://badge.fury.io/rb/guard-livereload) [![Build Status](https://travis-ci.org/guard/guard-livereload.png?branch=master)](http://travis-ci.org/guard/guard-livereload) [![Dependency Status](https://gemnasium.com/guard/guard-livereload.png)](https://gemnasium.com/guard/guard-livereload) [![Code Climate](https://codeclimate.com/github/guard/guard-livereload.png)](https://codeclimate.com/github/guard/guard-livereload) [![Coverage Status](https://coveralls.io/repos/guard/guard-livereload/badge.png?branch=master)](https://coveralls.io/r/guard/guard-livereload)
LiveReload guard allows to automatically reload your browser when 'view' files are modified.
@@ -34,12 +36,12 @@ Use [rack-livereload](https://github.com/johnbintz/rack-livereload) or install [
## Usage
-Please read [Guard usage doc](http://github.com/guard/guard#readme) and [rack-livereload how it works readme section](https://github.com/johnbintz/rack-livereload#readme) or [LiveReload extension usage doc](http://github.com/mockko/livereload#readme)
+Please read [Guard usage doc](https://github.com/guard/guard#readme) and [rack-livereload how it works readme section](https://github.com/johnbintz/rack-livereload#readme) or [LiveReload extension usage doc](http://github.com/mockko/livereload#readme)
## Guardfile
You can adapt your 'view' files like you want.
-Please read [Guard doc](http://github.com/guard/guard#readme) for more info about Guardfile DSL.
+Please read [Guard doc](https://github.com/guard/guard#readme) for more info about Guardfile DSL.
``` ruby
guard 'livereload' do
@@ -65,14 +67,14 @@ end
Available options:
``` ruby
-:host => '127.3.3.1' # default '0.0.0.0'
-:port => '12345' # default '35729'
-:apply_css_live => false # default true
-:override_url => false # default false
-:grace_period => 0.5 # default 0 (seconds)
+host: '127.3.3.1' # default '0.0.0.0'
+port: '12345' # default '35729'
+apply_css_live: false # default true
+override_url: false # default false
+grace_period: 0.5 # default 0 (seconds)
```
-See [LiveReload configuration doc](http://github.com/mockko/livereload#readme) for more info about those options.
+See [LiveReload configuration doc](https://github.com/mockko/livereload#readme) for more info about those options.
## Development
View
@@ -1,21 +1,5 @@
-require 'bundler'
-Bundler::GemHelper.install_tasks
+require "bundler/gem_tasks"
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
task :default => :spec
-
-namespace(:spec) do
- desc "Run all specs on multiple ruby versions (requires rvm)"
- task(:portability) do
- %w[1.8.6 1.8.7 1.9.2 ree rbx jruby].each do |version|
- system <<-BASH
- bash -c 'source ~/.rvm/scripts/rvm;
- rvm #{version};
- echo "--------- version #{version} ----------\n";
- bundle install 1> /dev/null;
- rake spec'
- BASH
- end
- end
-end
View
@@ -1,27 +1,27 @@
-# -*- encoding: utf-8 -*-
-$:.push File.expand_path("../lib", __FILE__)
+# coding: utf-8
+lib = File.expand_path('../lib', __FILE__)
+$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'guard/livereload/version'
Gem::Specification.new do |s|
- s.name = "guard-livereload"
- s.version = Guard::LiveReloadVersion::VERSION
- s.platform = Gem::Platform::RUBY
- s.authors = ['Thibaud Guillaume-Gentil']
- s.email = ['thibaud@thibaud.me']
- s.homepage = 'http://rubygems.org/gems/guard-livereload'
- s.summary = 'Guard gem for livereload'
- s.description = "Guard::LiveReload automatically reloads your browser when 'view' files are modified."
+ s.name = "guard-livereload"
+ s.version = Guard::LiveReloadVersion::VERSION
+ s.author = 'Thibaud Guillaume-Gentil'
+ s.email = 'thibaud@thibaud.me'
+ s.summary = 'Guard plugin for livereload'
+ s.description = "Guard::LiveReload automatically reloads your browser when 'view' files are modified."
+ s.homepage = 'https://rubygems.org/gems/guard-livereload'
+ s.license = "MIT"
- s.required_rubygems_version = '>= 1.3.6'
- s.rubyforge_project = "guard-livereload"
+ s.files = `git ls-files`.split($/)
+ s.test_files = s.files.grep(%r{^spec/})
+ s.require_path = 'lib'
- s.add_dependency 'guard', '>= 1.8.0'
- s.add_dependency 'em-websocket', '>= 0.5.0'
- s.add_dependency 'multi_json', '~> 1.7'
+ s.add_dependency 'guard', '~> 2.0'
+ s.add_dependency 'em-websocket', '~> 0.5'
+ s.add_dependency 'multi_json', '~> 1.8'
- s.add_development_dependency 'bundler'
+ s.add_development_dependency 'bundler', '>= 1.3.5'
+ s.add_development_dependency 'rake'
s.add_development_dependency 'rspec'
-
- s.files = Dir.glob('{lib}/**/*') + %w[LICENSE README.md js/livereload.js]
- s.require_path = 'lib'
end
View
@@ -12,15 +12,15 @@ class LiveReload < Guard
# = Guard methods =
# =================
- def initialize(watchers = [], options = {})
+ def initialize(options = {})
super
@options = {
- :host => '0.0.0.0',
- :port => '35729',
- :apply_css_live => true,
- :override_url => false,
- :grace_period => 0
- }.update(options)
+ host: '0.0.0.0',
+ port: '35729',
+ apply_css_live: true,
+ override_url: false,
+ grace_period: 0
+ }.merge(options)
end
def start
@@ -8,7 +8,7 @@ class Reactor
def initialize(options)
@web_sockets = []
@options = options
- @thread = start_threaded_reactor(options)
+ @thread = Thread.new { _start_reactor }
end
def stop
@@ -18,58 +18,61 @@ def stop
def reload_browser(paths = [])
UI.info "Reloading browser: #{paths.join(' ')}"
paths.each do |path|
- data = {
- :command => 'reload',
- :path => "#{Dir.pwd}/#{path}",
- :liveCSS => @options[:apply_css_live]
- }
- if options[:override_url] && File.exist?(path)
- data[:overrideURL] = '/' + path
- end
- UI.debug data
+ data = _data(path)
+ UI.debug(data)
web_sockets.each { |ws| ws.send(MultiJson.encode(data)) }
end
end
private
- def start_threaded_reactor(options)
- Thread.new do
- EventMachine.epoll
- EventMachine.run do
- UI.info "LiveReload is waiting for a browser to connect."
- EventMachine.start_server(options[:host], options[:port], WebSocket, {}) do |ws|
- ws.onopen do
- begin
- UI.info "Browser connected."
- ws.send MultiJson.encode({
- :command => 'hello',
- :protocols => ['http://livereload.com/protocols/official-7'],
- :serverName => 'guard-livereload'
- })
- @web_sockets << ws
- rescue
- UI.errror $!
- UI.errror $!.backtrace
- end
- end
-
- ws.onmessage do |msg|
- msg = MultiJson.decode(msg)
- if msg['command'] == 'url'
- UI.info "Browser URL: #{msg['url']}"
- end
- end
+ def _data(path)
+ data = {
+ command: 'reload',
+ path: "#{Dir.pwd}/#{path}",
+ liveCSS: options[:apply_css_live]
+ }
+ if options[:override_url] && File.exist?(path)
+ data[:overrideURL] = '/' + path
+ end
+ data
+ end
- ws.onclose do
- @web_sockets.delete(ws)
- UI.info "Browser disconnected."
- end
- end
+ def _start_reactor
+ EventMachine.epoll
+ EventMachine.run do
+ UI.info "LiveReload is waiting for a browser to connect."
+ EventMachine.start_server(options[:host], options[:port], WebSocket) do |ws|
+ ws.onopen &_on_open
+ ws.onmessage &_on_message
+ ws.onmessage &_on_close
end
end
end
+ def _on_open
+ UI.info "Browser connected."
+ self.send MultiJson.encode(
+ command: 'hello',
+ protocols: ['http://livereload.com/protocols/official-7'],
+ serverName: 'guard-livereload'
+ )
+ @web_sockets << self
+ rescue
+ UI.errror $!
+ UI.errror $!.backtrace
+ end
+
+ def _on_message(message)
+ message = MultiJson.decode(message)
+ UI.info"Browser URL: #{message['url']}" if message['command'] == 'url'
+ end
+
+ def _on_close
+ UI.info "Browser disconnected."
+ @web_sockets.delete(self)
+ end
+
end
end
end
@@ -1,37 +1,47 @@
require 'eventmachine'
require 'em-websocket'
-require "http/parser"
+require 'http/parser'
module Guard
class LiveReload
class WebSocket < EventMachine::WebSocket::Connection
- def dispatch data
+
+ def dispatch(data)
parser = Http::Parser.new
parser << data
if parser.http_method != 'GET' || parser.upgrade?
super #pass the request to websocket
elsif parser.request_path == '/livereload.js'
- serve_file File.expand_path("../../../../js/livereload.js", __FILE__)
+ _serve_file(_livereload_js_file)
elsif File.exist?(parser.request_path[1..-1])
- serve_file parser.request_path[1..-1] # Strip leading slash
+ _serve_file(parser.request_path[1..-1]) # Strip leading slash
else
- send_data "HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\nContent-Length: 13\r\n\r\n404 Not Found"
+ send_data("HTTP/1.1 404 Not Found\r\nContent-Type: text/plain\r\nContent-Length: 13\r\n\r\n404 Not Found")
close_connection_after_writing
end
end
- def serve_file path
+ private
+
+ def _serve_file(path)
UI.debug "Serving file #{path}"
- content_type = case File.extname(path)
+ send_data "HTTP/1.1 200 OK\r\nContent-Type: #{_content_type(path)}\r\nContent-Length: #{File.size path}\r\n\r\n"
+ stream_file_data(path).callback { close_connection_after_writing }
+ end
+
+ def _content_type(path)
+ case File.extname(path)
when '.css' then 'text/css'
when '.js' then 'application/ecmascript'
when '.gif' then 'image/gif'
when '.jpeg', '.jpg' then 'image/jpeg'
when '.png' then 'image/png'
else; 'text/plain'
end
- send_data "HTTP/1.1 200 OK\r\nContent-Type: #{content_type}\r\nContent-Length: #{File.size path}\r\n\r\n"
- stream_file_data(path).callback { close_connection_after_writing }
+ end
+
+ def _livereload_js_file
+ File.expand_path("../../../../js/livereload.js", __FILE__)
end
end
@@ -2,28 +2,29 @@
describe Guard::LiveReload::Reactor do
let(:paths) { %w[stylesheets/layout.css stylesheets/style.css] }
+ before { Guard::UI.stub(:info) }
describe "#reload_browser(paths = [])" do
it "displays a message" do
- Guard::UI.should_receive(:info).with("Reloading browser: stylesheets/layout.css stylesheets/style.css")
+ expect(Guard::UI).to receive(:info).with("Reloading browser: stylesheets/layout.css stylesheets/style.css")
new_live_reactor.reload_browser(paths)
end
it "each web socket receives send with data containing default options for each path modified" do
reactor = new_live_reactor
paths.each do |path|
reactor.web_sockets.each do |ws|
- ws.should_receive(:send).with(MultiJson.encode(['refresh', { :path => "#{Dir.pwd}/#{path}", :apply_js_live => true, :apply_css_live => true }]))
+ expect(ws).to receive(:send).with(MultiJson.encode(['refresh', path: "#{Dir.pwd}/#{path}", apply_js_live: true, apply_css_live: true]))
end
end
reactor.reload_browser(paths)
end
it "each web socket receives send with data containing custom options for each path modified" do
- reactor = new_live_reactor(:apply_css_live => false, :apply_js_live => false)
+ reactor = new_live_reactor(apply_css_live: false, apply_js_live: false)
paths.each do |path|
reactor.web_sockets.each do |ws|
- ws.should_receive(:send).with(MultiJson.encode(['refresh', { :path => "#{Dir.pwd}/#{path}", :apply_js_live => false, :apply_css_live => false }]))
+ expect(ws).to receive(:send).with(MultiJson.encode(['refresh', path: "#{Dir.pwd}/#{path}", apply_js_live: false, apply_css_live: false]))
end
end
reactor.reload_browser(paths)
@@ -33,5 +34,5 @@
end
def new_live_reactor(options = {})
- Guard::LiveReload::Reactor.new({ :api_version => '1.6', :host => '0.0.0.0', :port => '35729', :apply_js_live => true, :apply_css_live => true, :grace_period => 0 }.merge(options))
+ Guard::LiveReload::Reactor.new({ api_version: '1.6', host: '0.0.0.0', port: '35729', apply_js_live: true, apply_css_live: true, grace_period: 0 }.merge(options))
end
Oops, something went wrong.

0 comments on commit ea244f3

Please sign in to comment.