rb-process 0.1.1 → 0.2.1
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.
- checksums.yaml +4 -4
- data/.rubocop.yml +9 -0
- data/lib/rb/process/version.rb +1 -1
- data/lib/rb/process.rb +81 -16
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2211c2daaf9da3b8df7dab3cd4f948262c38486ee0c1c043de67335a17312f58
|
|
4
|
+
data.tar.gz: 5a407a0d39c42a07e4d32b30e28b69f60efb74e3bde87adbd168f21a4068979e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c3131329e1dd309bde627e95c3311d8a93aac6c07d68a4a12fb3123f84b507caad3755804de0387a6b0103ce69cb4eedd7417ecfe7b507d7a35146b039699b95
|
|
7
|
+
data.tar.gz: 2210d3454044e6080bc23927952d50b39d411a197a9747b6b6ff558594fd4b21619aa943dae95e5f0066146d66853e985fbb371589b0162e7afac8b711988a6b
|
data/.rubocop.yml
CHANGED
|
@@ -10,5 +10,14 @@ Metrics/BlockLength:
|
|
|
10
10
|
Metrics/AbcSize:
|
|
11
11
|
Enabled: false
|
|
12
12
|
|
|
13
|
+
Metrics/CyclomaticComplexity:
|
|
14
|
+
Enabled: false
|
|
15
|
+
|
|
16
|
+
Metrics/PerceivedComplexity:
|
|
17
|
+
Enabled: false
|
|
18
|
+
|
|
13
19
|
Style/ConditionalAssignment:
|
|
14
20
|
Enabled: false
|
|
21
|
+
|
|
22
|
+
Style/GlobalStdStream:
|
|
23
|
+
Enabled: false
|
data/lib/rb/process/version.rb
CHANGED
data/lib/rb/process.rb
CHANGED
|
@@ -1,31 +1,96 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "multi_io"
|
|
4
|
+
require "stringio"
|
|
4
5
|
require_relative "process/version"
|
|
5
6
|
|
|
6
7
|
module Process
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
class Pipe
|
|
9
|
+
attr_reader :input
|
|
10
|
+
attr_reader :output
|
|
11
|
+
attr_reader :error
|
|
12
|
+
|
|
13
|
+
def initialize(input, output, error)
|
|
14
|
+
@input = input
|
|
15
|
+
@output = output
|
|
16
|
+
@error = error
|
|
10
17
|
end
|
|
18
|
+
end
|
|
11
19
|
|
|
12
|
-
|
|
13
|
-
|
|
20
|
+
def self.run(*args, output: STDOUT, error: STDERR, **options, &block)
|
|
21
|
+
output_strio = StringIO.new
|
|
22
|
+
error_strio = StringIO.new
|
|
14
23
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
+
output_writter = if !output.is_a?(IO)
|
|
25
|
+
output
|
|
26
|
+
elsif output != STDOUT
|
|
27
|
+
MultiIO.new(STDOUT, output, output_strio)
|
|
28
|
+
else
|
|
29
|
+
MultiIO.new(STDOUT, output_strio)
|
|
30
|
+
end
|
|
31
|
+
error_writter = if !error.is_a?(IO)
|
|
32
|
+
error
|
|
33
|
+
elsif error != STDERR
|
|
34
|
+
MultiIO.new(STDERR, error, error_strio)
|
|
35
|
+
else
|
|
36
|
+
MultiIO.new(STDERR, error_strio)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# spawn don't support MultiIO nor StringIO
|
|
40
|
+
in_r, in_w = IO.pipe
|
|
41
|
+
out_r, out_w = IO.pipe
|
|
42
|
+
err_r, err_w = IO.pipe
|
|
43
|
+
|
|
44
|
+
# override the options, so put the option after the options
|
|
45
|
+
pid = spawn(*args, **options, in: in_r, out: out_w, err: err_w)
|
|
46
|
+
|
|
47
|
+
if block_given?
|
|
48
|
+
begin
|
|
49
|
+
case block.arity
|
|
50
|
+
when 1
|
|
51
|
+
yield Pipe.new(in_w, out_r, err_r)
|
|
52
|
+
when 2
|
|
53
|
+
yield in_w, out_r
|
|
54
|
+
when 3
|
|
55
|
+
yield in_w, out_r, err_r
|
|
56
|
+
else
|
|
57
|
+
raise ArgumentError.new("block must take 1 to 3 arguments")
|
|
58
|
+
end
|
|
59
|
+
rescue StandardError => e
|
|
60
|
+
Process.detach(pid)
|
|
61
|
+
raise e
|
|
24
62
|
end
|
|
25
63
|
end
|
|
26
64
|
|
|
27
|
-
|
|
28
|
-
|
|
65
|
+
in_w.close unless in_w.closed?
|
|
66
|
+
out_w.close
|
|
67
|
+
err_w.close
|
|
68
|
+
|
|
69
|
+
out_r.each_line do |line|
|
|
70
|
+
output_writter.write(line)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
err_r.each_line do |line|
|
|
74
|
+
error_writter.write(line)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
in_r.close
|
|
78
|
+
out_r.close
|
|
79
|
+
err_r.close
|
|
80
|
+
|
|
81
|
+
pid, status = Process.wait2(pid)
|
|
82
|
+
|
|
83
|
+
output.close unless !output.is_a?(IO) || output == STDOUT
|
|
84
|
+
error.close unless !error.is_a?(IO) || error == STDERR
|
|
85
|
+
output_strio.close
|
|
86
|
+
error_strio.close
|
|
87
|
+
|
|
88
|
+
case status.success?
|
|
89
|
+
when true
|
|
90
|
+
output_strio.string
|
|
91
|
+
else
|
|
92
|
+
[output_strio.string, error_strio.string, status]
|
|
93
|
+
end
|
|
29
94
|
end
|
|
30
95
|
|
|
31
96
|
def self.output(...)
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: rb-process
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- initdc
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-02-
|
|
11
|
+
date: 2026-02-19 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: Add the missing methods to the Ruby Process, you can use it with method
|
|
14
14
|
chaining
|