Skip to content

Commit

Permalink
Merge pull request #147 from guard/e2-js_erb_config
Browse files Browse the repository at this point in the history
Allow customizing livereload.js with ERB
  • Loading branch information
e2 committed Oct 19, 2015
2 parents ff474a1 + cbd86a6 commit 7f7e3d3
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 11 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,16 @@ port: '12345' # default '35729'
apply_css_live: false # default true
override_url: false # default false
grace_period: 0.5 # default 0 (seconds)
js_template: './my_livereload.js.erb' # default is livereload.js.erb from gem
```

Additional custom JS template options (see livereload.js.erb for details):
``` ruby
js_apple_webkit_extra_wait_time: 50 # default is 5 (see issue #123)
js_default_extra_wait_time: 100 # default is 200
```


`notify` uses Guard's [system notifications](https://github.com/guard/guard/wiki/System-notifications).
See [LiveReload configuration doc](https://github.com/mockko/livereload/blob/master/README-old.md) from version 1.x for more info about other options.

Expand Down
7 changes: 4 additions & 3 deletions js/livereload.js → js/livereload.js.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// <%= "guard-livereload options: #{options.inspect}" %>
(function() {
var __customevents = {}, __protocol = {}, __connector = {}, __timer = {}, __options = {}, __reloader = {}, __livereload = {}, __less = {}, __startup = {};

Expand Down Expand Up @@ -732,9 +733,9 @@
return this.waitUntilCssLoads(clone, function() {
var additionalWaitingTime;
if (/AppleWebKit/.test(navigator.userAgent)) {
additionalWaitingTime = 5;
additionalWaitingTime = <%= options[:js_apple_webkit_extra_wait_time] || 5 %>;
} else {
additionalWaitingTime = 200;
additionalWaitingTime = <%= options[:js_default_extra_wait_time] || 200 %>;
}
return _this.Timer.start(additionalWaitingTime, function() {
var _ref;
Expand Down Expand Up @@ -1052,4 +1053,4 @@
CustomEvents.bind(document, 'LiveReloadShutDown', function() {
return LiveReload.shutDown();
});
})();
})();
9 changes: 8 additions & 1 deletion lib/guard/livereload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,25 @@ module Guard
class LiveReload < Plugin
require 'guard/livereload/websocket'
require 'guard/livereload/reactor'
require 'guard/livereload/snippet'

attr_accessor :reactor, :options

def initialize(options = {})
super

js_path = File.expand_path('../../../js/livereload.js.erb', __FILE__)
@options = {
host: '0.0.0.0',
port: '35729',
apply_css_live: true,
override_url: false,
grace_period: 0
grace_period: 0,
js_template: js_path
}.merge(options)

js_path = @options[:js_template]
@options[:livereload_js_path] = Snippet.new(js_path, @options).path
end

def start
Expand Down
7 changes: 6 additions & 1 deletion lib/guard/livereload/reactor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ def _start_reactor
EventMachine.epoll if EventMachine.epoll?
EventMachine.kqueue if EventMachine.kqueue?
EventMachine.run do
EventMachine.start_server(options[:host], options[:port], WebSocket, {}) do |ws|
EventMachine.start_server(
options[:host],
options[:port],
WebSocket,
options
) do |ws|
ws.onopen { _connect(ws) }
ws.onclose { _disconnect(ws) }
ws.onmessage { |msg| _print_message(msg) }
Expand Down
23 changes: 23 additions & 0 deletions lib/guard/livereload/snippet.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'erb'
require 'tempfile'

require 'guard/compat/plugin'

module Guard
class LiveReload < Plugin
class Snippet
attr_reader :path
attr_reader :options

def initialize(template, options)
@options = options # for ERB context
tmpfile = Tempfile.new('livereload.js')
source = IO.read(template)
data = ERB.new(source).result(binding)
tmpfile.write(data)
tmpfile.close
@path = tmpfile.path
end
end
end
end
11 changes: 8 additions & 3 deletions lib/guard/livereload/websocket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
module Guard
class LiveReload
class WebSocket < EventMachine::WebSocket::Connection
def initialize(options)
@livereload_js_path = options[:livereload_js_path]
super
end

def dispatch(data)
parser = Http::Parser.new
parser << data
Expand Down Expand Up @@ -47,12 +52,12 @@ def _content_type(path)
end
end

def _livereload_js_file
File.expand_path('../../../../js/livereload.js', __FILE__)
def _livereload_js_path
@livereload_js_path
end

def _serve(path)
return _serve_file(_livereload_js_file) if path == './livereload.js'
return _serve_file(_livereload_js_path) if path == './livereload.js'
return _serve_file(path) if _readable_file(path)
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
Expand Down
45 changes: 45 additions & 0 deletions spec/lib/guard/livereload/snippet_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
RSpec.describe Guard::LiveReload::Snippet do
let(:options) { { foo: :bar } }

let(:template) { '/foo/livereload.js.erb' }
let(:contents) { '// <%= options[:foo] %>' }

subject { described_class.new(template, options) }

let(:tmpfile) { instance_double(Tempfile) }

before do
allow(File).to receive(:expand_path) do |*args|
fail "stub called for File.expand_path(#{args.map(&:inspect) * ','})"
end

allow(IO).to receive(:read) do |*args|
fail "stub called for IO.read(#{args.map(&:inspect) * ','})"
end

allow(IO).to receive(:read).with(template).and_return(contents)

allow(Tempfile).to receive(:new).and_return(tmpfile)
allow(tmpfile).to receive(:path).and_return('/tmp/livereload-123')
allow(tmpfile).to receive(:write)
allow(tmpfile).to receive(:close)
end

describe '#initialize' do
it 'evaluates the js snippet file' do
expect(tmpfile).to receive(:write).with('// bar')
subject
end

it 'closes the tmpfile' do
expect(tmpfile).to receive(:close)
subject
end
end

describe '#path' do
it 'is set to a tmpfile with the ERB result' do
expect(subject.path).to eq '/tmp/livereload-123'
end
end
end
58 changes: 55 additions & 3 deletions spec/lib/guard/livereload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,23 @@
RSpec.describe Guard::LiveReload do
let(:plugin) { Guard::LiveReload.new }
let(:reactor) { double(Guard::LiveReload::Reactor) }
before { allow(plugin).to receive(:reactor) { reactor } }

let(:tmp_path) { '/tmp/livereload-123' }
let(:snippet) { instance_double(Guard::LiveReload::Snippet, path: tmp_path) }

before do
allow(File).to receive(:expand_path) do |*args|
fail "stub called for File.expand_path(#{args.map(&:inspect) * ','})"
end

allow(File).to receive(:expand_path).
with('../../../js/livereload.js.erb', anything).
and_return('/foo/js/livereload.js.erb')

allow(Guard::LiveReload::Snippet).to receive(:new).and_return(snippet)

allow(plugin).to receive(:reactor) { reactor }
end

describe '#initialize' do
describe ':host option' do
Expand Down Expand Up @@ -65,6 +81,38 @@
expect(plugin.options[:grace_period]).to eq 0.5
end
end

describe ':js_template option' do
subject { described_class.new(*args) }

context 'when no value is provided' do
let(:args) { [] }

it 'is set to full path to default JS' do
expect(subject.options[:js_template]).to eq '/foo/js/livereload.js.erb'
end

it 'evalutes the default snippet' do
expect(Guard::LiveReload::Snippet).to receive(:new).
with('/foo/js/livereload.js.erb', anything).and_return(snippet)
subject
end
end

context 'with a custom path' do
let(:args) { [js_template: 'foo/bar.js.erb'] }

it 'is set to the given JS' do
expect(subject.options[:js_template]).to eq 'foo/bar.js.erb'
end

it 'evalutes the provided snippet' do
expect(Guard::LiveReload::Snippet).to receive(:new).
with('foo/bar.js.erb', anything).and_return(snippet)
subject
end
end
end
end

describe '#start' do
Expand All @@ -75,7 +123,9 @@
port: '35729',
apply_css_live: true,
override_url: false,
grace_period: 0
grace_period: 0,
js_template: '/foo/js/livereload.js.erb',
livereload_js_path: '/tmp/livereload-123'
)
plugin.start
end
Expand All @@ -87,7 +137,9 @@
port: '12345',
apply_css_live: false,
override_url: true,
grace_period: 1
grace_period: 1,
js_template: '/foo/js/livereload.js.erb',
livereload_js_path: '/tmp/livereload-123'
)
plugin.start
end
Expand Down

0 comments on commit 7f7e3d3

Please sign in to comment.