Class: Guard::Spork::SporkInstance

Inherits:
Object
  • Object
show all
Defined in:
lib/guard/spork/spork_instance.rb

Direct Known Subclasses

SporkWindowsInstance

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, port, env, options) ⇒ SporkInstance

Returns a new instance of SporkInstance.



8
9
10
11
12
13
# File 'lib/guard/spork/spork_instance.rb', line 8

def initialize(type, port, env, options)
  @type = type
  @port = port
  @env = env
  @options = options
end

Instance Attribute Details

#env ⇒ Object (readonly)

Returns the value of attribute env.



6
7
8
# File 'lib/guard/spork/spork_instance.rb', line 6

def env
  @env
end

#options ⇒ Object (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/guard/spork/spork_instance.rb', line 6

def options
  @options
end

#pid ⇒ Object (readonly)

Returns the value of attribute pid.



6
7
8
# File 'lib/guard/spork/spork_instance.rb', line 6

def pid
  @pid
end

#port ⇒ Object (readonly)

Returns the value of attribute port.



6
7
8
# File 'lib/guard/spork/spork_instance.rb', line 6

def port
  @port
end

#process ⇒ Object (readonly)

Returns the value of attribute process.



6
7
8
# File 'lib/guard/spork/spork_instance.rb', line 6

def process
  @process
end

#type ⇒ Object (readonly)

Returns the value of attribute type.



6
7
8
# File 'lib/guard/spork/spork_instance.rb', line 6

def type
  @type
end

Class Method Details

.spork_pids ⇒ Object



89
90
91
# File 'lib/guard/spork/spork_instance.rb', line 89

def self.spork_pids
  `ps aux | grep -v guard | awk '/spork/&&!/awk/{print $2;}'`.split("\n").map { |pid| pid.to_i }
end

Instance Method Details

#alive? ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/guard/spork/spork_instance.rb', line 46

def alive?
  pid && process.alive?
end

#command ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/guard/spork/spork_instance.rb', line 58

def command
  parts = []
  if use_bundler?
    parts << "bundle"
    parts << "exec"
  end
  if use_foreman?
    parts << "foreman"
    parts << "run"
  end
  parts << "spork"

  if type == :test_unit
    parts << "testunit"
  elsif type == :cucumber
    parts << "cu"
  elsif type == :minitest
    parts << "minitest"
  end

  parts << "-p"
  parts <<  port.to_s
  parts << "-q" if options[:quiet]

  if use_foreman?
    parts << "-e=#{options[:foreman].fetch(:env, '.env')}" if foreman_options?
  end

  parts
end

#running? ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
# File 'lib/guard/spork/spork_instance.rb', line 50

def running?
  return false unless alive?
  TCPSocket.new('127.0.0.1', port).close
  true
rescue Errno::ECONNREFUSED
  false
end

#start ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/guard/spork/spork_instance.rb', line 30

def start
  executable, *cmd = command

  Compat::UI.debug "guard-spork command execution: #{cmd}"

  @process = ChildProcess.build(executable, *cmd)
  @process.environment.merge!(env) unless env.empty?
  @process.io.inherit!
  @process.start
  @pid = @process.pid
end

#stop ⇒ Object



42
43
44
# File 'lib/guard/spork/spork_instance.rb', line 42

def stop
  process.stop
end

#to_s ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/guard/spork/spork_instance.rb', line 15

def to_s
  case type
  when :rspec
    "RSpec"
  when :cucumber
    "Cucumber"
  when :test_unit
    "Test::Unit"
  when :minitest
    "MiniTest"
  else
    type.to_s
  end
end