guard-spork 1.2.3 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/guard/spork.rb CHANGED
@@ -1,15 +1,13 @@
1
1
  require 'guard'
2
2
  require 'guard/guard'
3
- require 'sys/proctable'
4
3
  require 'childprocess'
5
- require 'rinda/ring'
6
- require 'guard/spork/rinda_ring_finger_patch'
7
4
 
8
5
  module Guard
9
6
  class Spork < Guard
10
7
 
11
8
  autoload :Runner, 'guard/spork/runner'
12
9
  autoload :SporkInstance, 'guard/spork/spork_instance'
10
+ autoload :SporkWindowsInstance, 'guard/spork/spork_windows_instance'
13
11
  attr_accessor :runner
14
12
 
15
13
  def initialize(watchers=[], options={})
@@ -42,37 +42,31 @@ module Guard
42
42
 
43
43
  def kill_global_sporks
44
44
  if options[:aggressive_kill]
45
- kill_pids ps_spork_pids
45
+ kill_pids self.class.spork_instance_class.spork_pids
46
46
  end
47
47
  end
48
48
 
49
+ def self.windows?
50
+ ENV['OS'] == 'Windows_NT'
51
+ end
52
+
49
53
  private
50
54
 
51
55
  def initialize_spork_instances
52
56
  @spork_instances = []
53
57
  [:rspec, :cucumber, :test_unit, :minitest].each do |type|
54
58
  port, env = options[:"#{type}_port"], options[:"#{type}_env"]
55
- spork_instances << SporkInstance.new(type, port, env, :bundler => should_use?(:bundler), :foreman => should_use?(:foreman), :quiet => should_use?(:quiet)) if should_use?(type)
59
+ spork_instances << self.class.spork_instance_class.new(type, port, env, :bundler => should_use?(:bundler), :foreman => should_use?(:foreman), :quiet => should_use?(:quiet)) if should_use?(type)
56
60
  end
57
61
  end
58
62
 
59
- def kill_pids(pids)
60
- UI.debug "Killing Spork servers with PID: #{pids.join(', ')}"
61
- pids.each { |pid| ::Process.kill("KILL", pid) }
63
+ def self.spork_instance_class
64
+ windows? ? SporkWindowsInstance : SporkInstance
62
65
  end
63
66
 
64
- def ps_spork_pids
65
- unless SporkInstance.windows?
66
- `ps aux | awk '/spork/&&!/awk/{print $2;}'`.split("\n").map { |pid| pid.to_i }
67
- else
68
- Sys::ProcTable.ps.reduce([]) do |spork_pids, process|
69
- spork_process = process.cmdline =~ /spork/ ||
70
- process.cmdline =~ /ring_server/ ||
71
- process.cmdline =~ /magazine_slave_provider/
72
- spork_pids << process.pid if spork_process
73
- spork_pids
74
- end
75
- end
67
+ def kill_pids(pids)
68
+ UI.debug "Killing Spork servers with PID: #{pids.join(', ')}"
69
+ pids.each { |pid| ::Process.kill("KILL", pid) rescue nil }
76
70
  end
77
71
 
78
72
  def find_instances(type = nil)
@@ -152,6 +146,7 @@ module Guard
152
146
  def detect_foreman
153
147
  File.exist?("Procfile")
154
148
  end
149
+
155
150
  end
156
151
  end
157
152
  end
@@ -26,11 +26,7 @@ module Guard
26
26
  end
27
27
 
28
28
  def start
29
- cmd = [command]
30
-
31
- if self.class.windows?
32
- cmd = ["cmd", "/C"] + cmd
33
- end
29
+ cmd = command
34
30
 
35
31
  ::Guard::UI.debug "guard-spork command execution: #{cmd}"
36
32
 
@@ -42,11 +38,7 @@ module Guard
42
38
  end
43
39
 
44
40
  def stop
45
- unless self.class.windows?
46
- process.stop
47
- else
48
- kill_all_child_processes
49
- end
41
+ process.stop
50
42
  end
51
43
 
52
44
  def alive?
@@ -56,19 +48,21 @@ module Guard
56
48
  def running?
57
49
  return false unless alive?
58
50
  TCPSocket.new('localhost', port).close
59
- if self.class.windows?
60
- running_on_windows?
61
- else
62
- true
63
- end
51
+ true
64
52
  rescue Errno::ECONNREFUSED
65
53
  false
66
54
  end
67
55
 
68
56
  def command
69
57
  parts = []
70
- parts << "bundle exec" if use_bundler?
71
- parts << "foreman run" if use_foreman?
58
+ if use_bundler?
59
+ parts << "bundle"
60
+ parts << "exec"
61
+ end
62
+ if use_foreman?
63
+ parts << "foreman"
64
+ parts << "run"
65
+ end
72
66
  parts << "spork"
73
67
 
74
68
  if type == :test_unit
@@ -79,13 +73,14 @@ module Guard
79
73
  parts << "minitest"
80
74
  end
81
75
 
82
- parts << "-p #{port}"
76
+ parts << "-p"
77
+ parts << port.to_s
83
78
  parts << "-q" if options[:quiet]
84
- parts.join(" ")
79
+ parts
85
80
  end
86
81
 
87
- def self.windows?
88
- RUBY_PLATFORM =~ /mswin|msys|mingw/
82
+ def self.spork_pids
83
+ `ps aux | awk '/spork/&&!/awk/{print $2;}'`.split("\n").map { |pid| pid.to_i }
89
84
  end
90
85
 
91
86
  private
@@ -98,31 +93,6 @@ module Guard
98
93
  options[:foreman]
99
94
  end
100
95
 
101
- def kill_all_child_processes
102
- all_pids_for(pid).each do |pid|
103
- Process.kill 9, pid
104
- end
105
- end
106
-
107
- def all_pids_for(parent_pid)
108
- pids = [parent_pid]
109
- Sys::ProcTable.ps do |process|
110
- pids += all_pids_for(process.pid) if process.ppid == parent_pid
111
- end
112
- pids
113
- end
114
-
115
- def running_on_windows?
116
- DRb.start_service
117
- # make sure that ringfinger is not taken from cache, because it won't
118
- # work after guard-spork has been restarted
119
- Rinda::RingFinger.class_variable_set :@@finger, nil
120
- ts = Rinda::RingFinger.primary
121
- ts.read_all([:name, :MagazineSlave, nil, nil]).size > 0
122
- rescue
123
- false
124
- end
125
-
126
96
  end
127
97
  end
128
98
  end
@@ -0,0 +1,62 @@
1
+ require 'rinda/ring'
2
+ require 'guard/spork/rinda_ring_finger_patch'
3
+
4
+ module Guard
5
+ class Spork
6
+ class SporkWindowsInstance < SporkInstance
7
+ def command
8
+ ["cmd", "/C"] + super
9
+ end
10
+
11
+ def stop
12
+ kill_all_spork_processes
13
+ end
14
+
15
+ def running?
16
+ super && drb_ready?
17
+ end
18
+
19
+ def self.spork_pids
20
+ spork_processes.map { |process| process[:pid] }
21
+ end
22
+
23
+ private
24
+
25
+ def drb_ready?
26
+ DRb.start_service
27
+ # make sure that ringfinger is not taken from cache, because it won't
28
+ # work after guard-spork has been restarted
29
+ Rinda::RingFinger.class_variable_set :@@finger, nil
30
+ ts = Rinda::RingFinger.primary
31
+ ts.read_all([:name, :MagazineSlave, nil, nil]).size > 0
32
+ rescue
33
+ false
34
+ end
35
+
36
+ def kill_all_spork_processes
37
+ all_pids_for(pid, self.class.spork_processes).each do |pid|
38
+ Process.kill 9, pid rescue nil
39
+ end
40
+ end
41
+
42
+ def all_pids_for(parent_pid, processes)
43
+ processes.inject([parent_pid]) do |memo, process|
44
+ memo += all_pids_for(process[:pid], processes) if process[:ppid] == parent_pid
45
+ memo
46
+ end
47
+ end
48
+
49
+ def self.spork_processes
50
+ require "win32ole"
51
+ WIN32OLE.connect("winmgmts://.").InstancesOf("win32_process").
52
+ each.
53
+ select do |p|
54
+ p.commandline =~ /spork|ring_server|magazine_slave_provider/ &&
55
+ File.basename(p.executablepath, File.extname(p.executablepath)) =~ /^(cmd|ruby)$/i
56
+ end.
57
+ map { |p| {:pid => p.processid, :ppid => p.parentprocessid} }
58
+ end
59
+
60
+ end
61
+ end
62
+ end
@@ -1,5 +1,5 @@
1
1
  module Guard
2
2
  module SporkVersion
3
- VERSION = "1.2.3"
3
+ VERSION = "1.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guard-spork
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-01 00:00:00.000000000 Z
12
+ date: 2012-11-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: guard
@@ -59,22 +59,6 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: 0.2.3
62
- - !ruby/object:Gem::Dependency
63
- name: sys-proctable
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - ! '>='
68
- - !ruby/object:Gem::Version
69
- version: '0'
70
- type: :runtime
71
- prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ! '>='
76
- - !ruby/object:Gem::Version
77
- version: '0'
78
62
  - !ruby/object:Gem::Dependency
79
63
  name: bundler
80
64
  requirement: !ruby/object:Gem::Requirement
@@ -133,6 +117,7 @@ files:
133
117
  - lib/guard/spork/rinda_ring_finger_patch.rb
134
118
  - lib/guard/spork/runner.rb
135
119
  - lib/guard/spork/spork_instance.rb
120
+ - lib/guard/spork/spork_windows_instance.rb
136
121
  - lib/guard/spork/templates/Guardfile
137
122
  - lib/guard/spork/version.rb
138
123
  - lib/guard/spork.rb