ak47 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/ak47.rb +25 -20
  2. data/lib/ak47/version.rb +1 -1
  3. metadata +3 -3
@@ -3,32 +3,37 @@ require 'shell_tools'
3
3
  require "ak47/version"
4
4
 
5
5
  module Ak47
6
+ Reload = Class.new(RuntimeError)
7
+
6
8
  class << self
7
9
  def run(*argv)
8
10
  command = ShellTools.escape(argv)
9
- @listener_exited = true
10
- raise "No command was given" if command.empty?
11
- kill_subprocess = proc do
12
- begin
13
- Process.kill("INT", @pid) if @pid
14
- rescue Errno::ESRCH
15
- end
16
- end
17
- trap("INT", &kill_subprocess)
18
- at_exit(&kill_subprocess)
19
- listener = Guard::Listener.select_and_init(:watchdir => Dir.pwd, :watch_all_modifications => true)
11
+ watch_dir = Dir.pwd
12
+ listener = Guard::Listener.select_and_init(:watchdir => watch_dir, :watch_all_modifications => true)
20
13
  listener.on_change {
21
- @listener_exited = true
22
- Process.kill("INT", @pid) if @pid
14
+ Thread.main.raise Reload
23
15
  }
24
- listener_thread = Thread.new { listener.start }
25
- listener_thread.abort_on_exception = true
26
- while @listener_exited do
27
- @listener_exited = false
28
- @pid = fork do
29
- exec(command.to_s)
16
+ Thread.new { listener.start }
17
+
18
+ at_exit { Process.kill("INT", @pid) rescue nil if @pid }
19
+
20
+ puts "[Starting ak47 #{VERSION} in #{watch_dir.inspect}]"
21
+ loop do
22
+ begin
23
+ puts "[Running... #{Time.new.to_s}]"
24
+ puts "# #{command}"
25
+ @pid = fork { exec(command) }
26
+ _, status = Process.waitpid2(@pid)
27
+ if status.success?
28
+ puts "[Terminated, waiting for file system change]"
29
+ sleep
30
+ else
31
+ puts "[Terminated abnormally (#{status.inspect}), retrying in 5s]"
32
+ sleep 5
33
+ end
34
+ rescue Reload
35
+ puts "[Reloading... #{Time.new.to_s}]"
30
36
  end
31
- Process.waitpid(@pid)
32
37
  end
33
38
  end
34
39
  end
@@ -1,3 +1,3 @@
1
1
  module Ak47
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ak47
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 1
10
- version: 0.0.1
9
+ - 0
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Josh Hull