justrun 1.0.2 → 1.0.3

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/justrun.rb +34 -5
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e66d6b5814c68eb5fc90d70aafb51bd5448937a6
4
- data.tar.gz: d9da91e425e876f3f6c80f017b68b7905c99be35
3
+ metadata.gz: d05de7ed05646f8d9f0d48d01fa813f2eed91f90
4
+ data.tar.gz: a5c0fc3dba2c3f5d5f376add56935f5542f0d452
5
5
  SHA512:
6
- metadata.gz: 85a0d8372e61fb234d6afb1d4579e543ec1b28c25ac7127f45418720c4a4edf71c3003e58722a0ed6f9ca0d4a55f042c8ca71645f36368edf44fff22cde8a6d7
7
- data.tar.gz: f51d8c54df13a74149c09e372fcbe93bdc9d50f2b7a7a661b19ed211b38a6bd9d7dbdd62246622466542620593bce0a8784ccca4638027c7c55a59d78ab6ca9e
6
+ metadata.gz: 5ac33ff8029a8781beeb087242bc9fbea2c904a8433720e7da85d52cc3f7ecc772d38d8f2c75bdd36cb68e5c4a51dedf5056aef3894ecc1e4a274ed27230198a
7
+ data.tar.gz: 806ace9c477e2a83cfa35dfb6a39a39b31b7afa2a274547e1368813cc326b867d7e7760f718cfead837294da126cc44e4a38f8b8b38dbdcacd74cf0d8f6f3158
data/lib/justrun.rb CHANGED
@@ -3,12 +3,29 @@ require 'open3'
3
3
  #TODO: Run multiple commands at the same time
4
4
 
5
5
  class JustRun
6
- def self.command(command, block_size: 4096*4, init: ->(writer) {}, env: ENV, &block)
6
+ def self.command(command, block_size: 4096*4, buffer_output: false, init: ->(writer) {}, env: ENV, &block)
7
7
  ret_code = -1
8
+
9
+ buffers = {
10
+ stdout: [],
11
+ stderr: [],
12
+ all: []
13
+ }
14
+
8
15
  Open3.popen3 env, command do |stdin, stdout, stderr, wait_thr|
9
16
  writer = JustRun::Writer.new stdin
10
17
  init.call writer
11
18
 
19
+ line_handler = -> line, name {
20
+ if buffer_output
21
+ buffers[name].push line
22
+ buffers[:all].push line
23
+ end
24
+ if block
25
+ block.call line, name, writer
26
+ end
27
+ }
28
+
12
29
  fileno_lines = {}
13
30
  begin
14
31
  files = [stdout, stderr]
@@ -25,6 +42,7 @@ class JustRun
25
42
  fileno_lines[fileno] ||= []
26
43
  lines = fileno_lines[fileno]
27
44
  name = fileno2name[fileno]
45
+
28
46
  begin
29
47
  data = f.read_nonblock block_size
30
48
  lines_new = data.lines
@@ -33,7 +51,8 @@ class JustRun
33
51
  end
34
52
  lines.push(*lines_new)
35
53
  while lines[0] =~ /\n\r?/
36
- block.call lines.shift.chomp, name, writer
54
+ line = lines.shift.chomp
55
+ line_handler.call line, name
37
56
  end
38
57
  rescue EOFError => e
39
58
  # expected
@@ -48,7 +67,7 @@ class JustRun
48
67
  fileno_lines.each do |fileno, lines|
49
68
  name = fileno2name[fileno]
50
69
  if lines.length > 0
51
- block.call lines.shift.chomp, name, writer
70
+ line_handler.call lines.shift.chomp, name
52
71
  end
53
72
  end
54
73
  rescue IOError => e
@@ -56,7 +75,17 @@ class JustRun
56
75
  end
57
76
  ret_code = wait_thr.value
58
77
  end
59
- ret_code.exitstatus
78
+
79
+ if buffer_output
80
+ {
81
+ code: ret_code.exitstatus,
82
+ stderr: buffers[:stderr],
83
+ stdout: buffers[:stdout],
84
+ all: buffers[:all]
85
+ }
86
+ else
87
+ ret_code.exitstatus
88
+ end
60
89
  end
61
90
 
62
91
  private
@@ -75,7 +104,7 @@ class JustRun
75
104
  end
76
105
 
77
106
  def write str
78
- @buffer << str;
107
+ @buffer << str
79
108
  process
80
109
  end
81
110
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: justrun
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damian Kaczmarek
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-15 00:00:00.000000000 Z
11
+ date: 2015-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -82,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
82
  version: '0'
83
83
  requirements: []
84
84
  rubyforge_project:
85
- rubygems_version: 2.4.8
85
+ rubygems_version: 2.4.5
86
86
  signing_key:
87
87
  specification_version: 4
88
88
  summary: Run command and get live line by line callbacks