Class: Rerun::Runner

Inherits:
Object
  • Object
show all
Includes:
System
Defined in:
lib/rerun.rb

Instance Method Summary collapse

Methods included from System

#app_name, #growl, #growlcmd, #has_growl?, #osx?

Constructor Details

#initialize(run_command, options = {}) ⇒ Runner

Returns a new instance of Runner.



12
13
14
# File 'lib/rerun.rb', line 12

def initialize(run_command, options = {})
  @run_command, @options = run_command, options
end

Instance Method Details

#git_head_changed?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
# File 'lib/rerun.rb', line 101

def git_head_changed?
  old_git_head = @git_head
  read_git_head
  @git_head and old_git_head and @git_head != old_git_head
end

#joinObject



77
78
79
# File 'lib/rerun.rb', line 77

def join
  @watcher.join
end

#notify(title, body) ⇒ Object



112
113
114
115
116
# File 'lib/rerun.rb', line 112

def notify(title, body)
  growl title, body if has_growl?
  puts
  puts "#{Time.now.strftime("%T")} - #{app_name} #{title}"
end

#read_git_headObject



107
108
109
110
# File 'lib/rerun.rb', line 107

def read_git_head
  git_head_file = File.join(dir, '.git', 'HEAD')
  @git_head = File.exists?(git_head_file) && File.read(git_head_file)
end

#restartObject



16
17
18
19
20
21
# File 'lib/rerun.rb', line 16

def restart
  @restarting = true
  stop
  start
  @restarting = false
end

#running?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/rerun.rb', line 81

def running?
  signal(0)
end

#signal(signal) ⇒ Object



85
86
87
88
89
90
# File 'lib/rerun.rb', line 85

def signal(signal)
  Process.kill(signal, @pid)
  true
rescue
  false
end

#startObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rerun.rb', line 23

def start
  if (!@already_running)
    taglines = [
      "To infinity... and beyond!",
      "Charge!",
      ]
    notify "Launched", taglines[rand(taglines.size)]
    @already_running = true
  else
    taglines = [
      "Here we go again!",
      "Once more unto the breach, dear friends, once more!", 
    ]
    notify "Restarted", taglines[rand(taglines.size)]
  end

  @pid = Kernel.fork do
    # Signal.trap("INT") { exit }
    exec(@run_command)
  end

  Signal.trap("INT") { stop; exit }

  # Process.detach(@pid)

  begin
    sleep 2
  rescue Interrupt => e
    # in case someone hits control-C immediately
    stop
    exit
  end

  unless running?
    notify "Launch Failed", "See console for error output"
    @already_running = false
  end

  unless @watcher
    watcher_class = osx? ? OSXWatcher : FSWatcher
    # watcher_class = FSWatcher
  
    watcher = watcher_class.new do
      restart unless @restarting
    end
    watcher.add_directory(".", "**/*.rb")
    watcher.sleep_time = 1
    watcher.start
    
    @watcher = watcher
  end

end

#stopObject



92
93
94
95
96
97
98
99
# File 'lib/rerun.rb', line 92

def stop
  if @pid && (@pid != 0)
    notify "Stopped", "All good things must come to an end." unless @restarting
    signal("KILL") && Process.wait(@pid)
  end
rescue => e
  false
end