Skip to content

Commit 518b75d

Browse files
committed
feat: Process.run add log_file option
with gem multi_io
1 parent 08ed836 commit 518b75d

7 files changed

Lines changed: 40 additions & 7 deletions

File tree

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
runs-on: ubuntu-latest
1818
name: Ruby ${{ matrix.ruby }}
1919
strategy:
20+
fail-fast: false
2021
matrix:
2122
ruby:
2223
- "2.7.0"

.rubocop.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,12 @@ inherit_gem:
33

44
Metrics/MethodLength:
55
Enabled: false
6+
7+
Metrics/BlockLength:
8+
Enabled: false
9+
10+
Metrics/AbcSize:
11+
Enabled: false
12+
13+
Style/ConditionalAssignment:
14+
Enabled: false

Gemfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ gem "rspec", "~> 3.0"
1313

1414
gem "rubocop", "~> 1.21"
1515

16-
gem "rubocop-config-crystal"
16+
gem "rubocop-config-crystal", "~> 0.0.3"
17+
18+
gem "multi_io", "~> 0.0.1.pre2"

Gemfile.lock

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
rb-process (0.1.0)
4+
rb-process (0.1.1)
55

66
GEM
77
remote: https://rubygems.org/
@@ -11,6 +11,7 @@ GEM
1111
json (2.15.2)
1212
language_server-protocol (3.17.0.5)
1313
lint_roller (1.1.0)
14+
multi_io (0.0.1.pre2)
1415
parallel (1.27.0)
1516
parser (3.3.10.0)
1617
ast (~> 2.4.1)
@@ -47,7 +48,7 @@ GEM
4748
rubocop-ast (1.47.1)
4849
parser (>= 3.3.7.2)
4950
prism (~> 1.4)
50-
rubocop-config-crystal (0.0.1)
51+
rubocop-config-crystal (0.0.3)
5152
ruby-progressbar (1.13.0)
5253
unicode-display_width (3.2.0)
5354
unicode-emoji (~> 4.1)
@@ -58,11 +59,12 @@ PLATFORMS
5859

5960
DEPENDENCIES
6061
bundler (~> 2.4)
62+
multi_io (~> 0.0.1.pre2)
6163
rake (~> 13.0)
6264
rb-process!
6365
rspec (~> 3.0)
6466
rubocop (~> 1.21)
65-
rubocop-config-crystal
67+
rubocop-config-crystal (~> 0.0.3)
6668

6769
BUNDLED WITH
6870
2.4.22

lib/rb/process.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
# frozen_string_literal: true
22

3+
require "multi_io"
34
require_relative "process/version"
45

56
module Process
6-
def self.run(*args, **options)
7+
def self.run(*args, log_file: nil, **options)
8+
if log_file && !log_file.is_a?(File)
9+
raise ArgumentError.new("log_file must be a File with mode")
10+
end
11+
12+
mio = log_file ? MultiIO.new($stdout, log_file) : $stdout
713
result = String.new
14+
815
IO.popen(*args, **options) do |pipe|
916
if block_given?
1017
yield pipe
1118
pipe.close_write
1219
end
1320
while !pipe.eof
1421
line = pipe.gets
15-
print line
22+
mio.write(line)
1623
result << line
1724
end
1825
end
26+
27+
log_file.close if log_file
1928
result
2029
end
2130

lib/rb/process/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Process
4-
VERSION = "0.1.0"
4+
VERSION = "0.1.1"
55
end

spec/rb/process_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "tempfile"
4+
35
RSpec.describe Process do
46
it "has a version number" do
57
expect(Process::VERSION).not_to be nil
@@ -28,4 +30,12 @@
2830
it "answer with cmd with ruby style" do
2931
expect(Process.run("bash", "r+") { |pipe| pipe.puts "uname" }).to eq "Linux\n"
3032
end
33+
34+
it "print and also log to file" do
35+
tempfile = Tempfile.new(["test_", ".log"])
36+
Process.run("uname", log_file: File.open(tempfile.path, "w"))
37+
38+
expect(tempfile.readlines).to eq ["Linux\n"]
39+
tempfile.delete
40+
end
3141
end

0 commit comments

Comments
 (0)