ak47 0.2.1 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -26,4 +26,13 @@ and enter your command, use `--` to seperate options from your command. For exam
26
26
 
27
27
  ak47 -i2 test -- rake test
28
28
 
29
- This will watch your `test` directory and wait two seconds between restarts.
29
+ This will watch your `test` directory and wait two seconds between restarts.
30
+
31
+ ### Programmatic Usage
32
+
33
+ You can use Ak47 within a Ruby program in the following way.
34
+
35
+ require 'ak47'
36
+ Ak47 {
37
+ puts "Reloading!"
38
+ }
@@ -17,7 +17,7 @@ module Ak47
17
17
  [[], argv]
18
18
  end
19
19
 
20
- interval, maximum, error_time = 0.01, nil, 5
20
+ interval, maximum, error_time = nil, nil, 5
21
21
  optparse = OptionParser.new do |opts|
22
22
  opts.banner = "Usage: ak47 [cmd] / ak47 [options] -- [cmd]"
23
23
  opts.on( '-i', '--interval [FLOAT]', 'Interval before restarting' ) do |i|
@@ -48,9 +48,14 @@ module Ak47
48
48
  puts
49
49
  raise "No command supplied"
50
50
  end
51
- Runner.new(watch_dirs, command, maximum, interval, error_time).start
51
+ Runner.new(:watch_dirs => watch_dirs, :maximum => maximum, :interval => interval, :error_time => error_time, :command => command) { exec(command) }.start
52
52
  rescue
53
53
  puts $!.message.red
54
+ exit 1
54
55
  end
55
56
  end
57
+ end
58
+
59
+ def Ak47(opts = nil, &blk)
60
+ Ak47::Runner.new(opts, &blk).start
56
61
  end
@@ -1,9 +1,14 @@
1
1
  module Ak47
2
2
  class Runner
3
- attr_reader :watch_dirs, :command, :maximum, :interval, :error_time
3
+ attr_reader :watch_dirs, :maximum, :interval, :error_time, :command
4
4
 
5
- def initialize(watch_dirs, command, maximum, interval, error_time)
6
- @watch_dirs, @command, @maximum, @interval, @error_time = watch_dirs, command, maximum, interval, error_time
5
+ def initialize(opts = nil, &blk)
6
+ @watch_dirs = Array(opts && opts[:watch_dirs] || Dir.pwd)
7
+ @maximum = opts && opts[:maximum]
8
+ @interval = opts && opts[:interval] || 0.01
9
+ @error_time = opts && opts[:error_time] || 5
10
+ @command = opts && opts[:command]
11
+ @blk = blk
7
12
  end
8
13
 
9
14
  def start
@@ -19,13 +24,11 @@ module Ak47
19
24
  loop do
20
25
  begin
21
26
  puts "[Running... #{Time.new.to_s}]".yellow
22
- puts "# #{command}"
27
+ puts "# #{command}" if command
23
28
  if maximum
24
29
  @thread = Thread.new { sleep maximum; Thread.main.raise Reload, "Cancelled due to maximum time" }
25
30
  end
26
- @pid = fork {
27
- exec(command)
28
- }
31
+ @pid = fork(&@blk)
29
32
  _, status = Process.waitpid2(@pid)
30
33
  @thread.kill if @thread
31
34
  if status.success?
@@ -1,3 +1,3 @@
1
1
  module Ak47
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
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: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 1
10
- version: 0.2.1
9
+ - 2
10
+ version: 0.2.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Josh Hull
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-01-06 00:00:00 Z
18
+ date: 2012-01-08 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: guard