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, #growl?, #growlcmd, #icon

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

#dirObject



23
24
25
# File 'lib/rerun.rb', line 23

def dir
  @options[:dir] || "."
end

#git_head_changed?Boolean

Returns:

  • (Boolean)


116
117
118
119
120
# File 'lib/rerun.rb', line 116

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



92
93
94
# File 'lib/rerun.rb', line 92

def join
  @watcher.join
end

#notify(title, body) ⇒ Object



127
128
129
130
131
# File 'lib/rerun.rb', line 127

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

#read_git_headObject



122
123
124
125
# File 'lib/rerun.rb', line 122

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)


96
97
98
# File 'lib/rerun.rb', line 96

def running?
  signal(0)
end

#signal(signal) ⇒ Object



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

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

#startObject



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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rerun.rb', line 27

def start
  if windows?
    raise "Sorry, Rerun does not work on Windows."
  end

  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
    begin
      # Signal.trap("INT") { exit }
      exec(@run_command)
    rescue => e
      puts e
      exit
    end
  end
  Process.detach(@pid) # so if the child exits, it dies

  Signal.trap("INT") do  # INT = control-C
     stop # first stop the child
     exit
   end

  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 = mac? ? OSXWatcher : FSWatcher
    # watcher_class = FSWatcher
  
    watcher = watcher_class.new do
      restart unless @restarting
    end
    puts "Watching #{dir}"
    watcher.add_directory(dir, "**/*.rb")
    watcher.sleep_time = 1
    watcher.start
    @watcher = watcher
  end

end

#stopObject



107
108
109
110
111
112
113
114
# File 'lib/rerun.rb', line 107

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