guard-mozrepl 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -2,3 +2,5 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ *~
6
+ \#*\#
@@ -42,19 +42,42 @@ module Guard
42
42
  # Called on file(s) modifications
43
43
  def run_on_change(paths)
44
44
  return true if @serious_issue
45
- puts "Reload tab via MozRepl for #{paths * ' '}" if @options[:verbose]
46
- reload_current_tab!
45
+ reload_tab = false
46
+ paths.each do |path|
47
+ case path
48
+ when /\.css$/ then reload_css(path)
49
+ when /\.js$/ then reload_js(path)
50
+ else reload_tab = true
51
+ end
52
+ end
53
+ reload_current_tab(paths) if reload_tab
47
54
  end
48
55
 
49
56
  private
50
57
 
51
- def reload_current_tab!
52
- mozrepl.cmd('content.location.reload();')
58
+ def reload_css(path)
59
+ log "Reload css via MozRepl for #{path}"
60
+ invoke "reload.css('#{path}');"
61
+ end
62
+
63
+ def reload_js(path)
64
+ log "Reload js via MozRepl for #{path}"
65
+ invoke "reload.js('#{path}');"
66
+ end
67
+
68
+ def reload_current_tab(paths)
69
+ log "Reload tab via MozRepl for #{paths * ' '}"
70
+ invoke 'content.location.reload();'
71
+ end
72
+
73
+ def invoke(cmd)
74
+ log "invoking: #{cmd}"
75
+ log mozrepl.cmd cmd
53
76
  rescue
54
- warn "Error sending command to MozRepl. Attempting to reconnect..." if @options[:verbose]
77
+ log "Error sending command to MozRepl. Attempting to reconnect..."
55
78
  @mozrepl = nil
56
79
  # retry
57
- mozrepl.cmd('content.location.reload();')
80
+ log mozrepl.cmd cmd
58
81
  end
59
82
 
60
83
  def mozrepl
@@ -62,13 +85,22 @@ module Guard
62
85
  puts "Guard connecting to MozRepl at #{@options[:host]}:#{@options[:port]}"
63
86
  @mozrepl = Net::Telnet::new("Host" => @options[:host],
64
87
  "Port" => @options[:port])
88
+ log @mozrepl.cmd(reload_code)
65
89
  rescue
66
90
  warn "Not able to connect to MozRepl. Install/start MozRepl, or simply ignore this message."
67
91
  @serious_issue = true
68
92
  end
69
93
 
70
94
  def close!
71
- @mozrepl.close
95
+ @mozrepl.close if @mozrepl
96
+ end
97
+
98
+ def reload_code
99
+ File.read(File.expand_path(File.join(%w(.. reload.js)), __FILE__))
100
+ end
101
+
102
+ def log(msg)
103
+ puts msg if @options[:verbose]
72
104
  end
73
105
 
74
106
  end
@@ -1,8 +1,8 @@
1
1
  guard 'mozrepl' do
2
- watch(%r{app/.+\.(erb|haml)})
3
- watch(%r{app/helpers/.+\.rb})
4
- watch(%r{public/.+\.(css|js|html)})
5
- watch(%r{config/locales/.+\.yml})
2
+ watch(/app\/.+\.(erb|haml)/)
3
+ watch(/app\/helpers\/.+\.rb/)
4
+ watch(/public\/(.+\.(css|js|html))/) { |m| m[1] }
5
+ watch(/config\/locales\/.+\.yml/)
6
6
  end
7
7
 
8
8
  ### or with options...
@@ -1,5 +1,5 @@
1
1
  module Guard
2
2
  module Mozrepl
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -0,0 +1,37 @@
1
+ var reload = {
2
+ document:function() {
3
+ for ( var i = 0; i < window.length; i++ ) {
4
+ if ( content.location == window[i].location ) {
5
+ return window[i].document;
6
+ }
7
+ }
8
+ },
9
+ css:function(filename) {
10
+ var doc = reload.document();
11
+ var l = doc.getElementsByTagName('link');
12
+ for ( var i = 0; i < l.length; i++ ) {
13
+ var ss = l[i];
14
+ if ( ss.href && ss.href.indexOf(filename) != -1 ) {
15
+ repl.print("reloading: " + ss.href);
16
+ try {
17
+ ss.href = (ss.href.split("?")[0]) + "?" + Math.random();
18
+ } catch (e) {
19
+ repl.print(e);
20
+ }
21
+ }
22
+ }
23
+ },
24
+ js:function(filename) {
25
+ var xpath = '//script[contains(@src, "'+ filename +'")]';
26
+ var doc = reload.document();
27
+ var node = doc.evaluate(xpath, document, null, XPathResult.ANY_TYPE, null).iterateNext();
28
+ if ( node ) {
29
+ repl.print("reloading: " + filename);
30
+ var newScript = doc.createElement('script');
31
+ newScript.src = node.src.split("?")[0] + "?" + Math.random();
32
+ document.getElementsByTagName('head')[0].appendChild(newScript);
33
+ } else {
34
+ rpl.print("not found: " + filename);
35
+ }
36
+ }
37
+ };
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guard-mozrepl
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Phil Hofmann
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-17 00:00:00 +02:00
18
+ date: 2011-05-19 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -37,6 +37,7 @@ files:
37
37
  - lib/guard/mozrepl.rb
38
38
  - lib/guard/mozrepl/templates/Guardfile
39
39
  - lib/guard/mozrepl/version.rb
40
+ - lib/guard/reload.js
40
41
  has_rdoc: true
41
42
  homepage: http://branch14.org/guard-mozrepl
42
43
  licenses: []