runtime_command 1.0.3 → 1.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f8e242795477317ba019e209a85a6ab44af589ae
4
- data.tar.gz: 3a0fcbcd6a519ee5f15c38860b6b8dc0bc1db1ac
3
+ metadata.gz: 324ec53b7ebeb61e18c83725f0c66963c63eddac
4
+ data.tar.gz: dbdb3070c12068ee56e7ca78f6d767d7c39cf346
5
5
  SHA512:
6
- metadata.gz: b0189a198f5d1f0adef08e7b776427d445a33bb30b2919fdd8d1c99fe9d1f2dba3f3fe8f4e7d2d956883cbb008a7dc6bbd1b925d59bb68b0dcd50d022c200f98
7
- data.tar.gz: 7320be85b803f7124d623745b8903555fd1eafe28c00025ecc3ad5e3b938379a25559e2bc3ca3a3322bcf2cc0d12534044ca7a8701a2240997dafe5129c5d03b
6
+ metadata.gz: 0c0c214d899135f7f0ed29d14842d80eaa8cc4e9326c7fcb8e481dff5bb10e4184606e5a2f93a56d1e4650b2808571a4d0f39cf28444a81f76e6a348ffefc74f
7
+ data.tar.gz: f7495faa2ce138ddeb4517e73f65aba83a25867b0ecac22a4c688cb86f99841a1388dfdeb4522e1e93db70c10adde34672dc87a41966b916c7d2b51504774935
data/.gitignore CHANGED
@@ -9,3 +9,5 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+
13
+ *.log
data/example/sample.rb CHANGED
@@ -1,12 +1,19 @@
1
1
  require 'bundler/setup'
2
2
  require 'runtime_command'
3
3
 
4
- command = RuntimeCommand::Builder.new
5
- # command.colors = :none
4
+ logger = Logger.new('result.log')
5
+ logger.formatter = proc do |severity, datetime, progname, msg|
6
+ "#{msg}\n"
7
+ end
8
+
9
+ command = RuntimeCommand::Builder.new(logger: logger)
10
+ command.exec('echo hello')
6
11
  command.exec('echo "wait 3" sec; sleep 3; echo "hello world!"')
7
12
 
8
- # command.output = false
9
13
  # puts command.exec('ls -la').buffered_stdout
10
14
 
11
- # command.colors[:stdout] = HighLine::Style.rgb(255, 0, 0)
15
+ # colors = {
16
+ # stdout: HighLine::Style.rgb(255, 0, 0)
17
+ # }
18
+ # command = RuntimeCommand::Builder.new(colors: colors)
12
19
  # command.exec('ls -la')
@@ -1,29 +1,37 @@
1
+ require 'logger'
1
2
  require 'open3'
2
3
  require 'runtime_command/logger'
3
4
 
4
5
  module RuntimeCommand
5
6
  class Builder
6
7
  attr_reader :buffered_log
7
- attr_accessor :stdin_prefix, :colors, :output
8
8
 
9
- # @param [String] base_dir
9
+ # @param [Hash] options
10
+ # @option options [String] base_dir
11
+ # @option options [Hash] colors
12
+ # @option options [Logger] logger
13
+ # @option options [Boolean] output
14
+ # @option options [String] stdin_prefix
10
15
  # @return [RuntimeCommand::Builder]
11
- def initialize(base_dir = '.')
12
- @base_dir = base_dir
13
- @output = true
16
+ def initialize(options = {})
17
+ options[:base_dir] ||= '.'
18
+ options[:colors] ||= {}
19
+ options[:logger] ||= nil
20
+ options[:output] ||= true
21
+ options[:stdin_prefix] ||= '>'
22
+
23
+ @options = options
14
24
  @buffered_log = ''
15
- @stdin_prefix = '>'
16
- @colors = {}
17
25
  end
18
26
 
19
27
  # @param [String] command
20
28
  # @param [String] chdir
21
29
  # @return [RuntimeCommand::Logger]
22
30
  def exec(command, chdir = nil)
23
- chdir ||= @base_dir
31
+ chdir ||= @options[:base_dir]
24
32
 
25
- logger = Logger.new(@output, @colors)
26
- logger.stdin(@stdin_prefix + ' ' + command)
33
+ logger = Logger.new(output: @options[:output], colors: @options[:colors], logger: @options[:logger])
34
+ logger.stdin(@options[:stdin_prefix] + ' ' + command)
27
35
 
28
36
  invoke_command(logger) do
29
37
  Open3.popen3(command, chdir: chdir) do |stdin, stdout, stderr|
@@ -46,19 +54,13 @@ module RuntimeCommand
46
54
  # @param [String] chdir
47
55
  # @return [RuntimeCommand::Logger]
48
56
  def exec_capture(command, chdir = nil)
49
- chdir ||= @base_dir
57
+ chdir ||= @options[:base_dir]
50
58
 
51
- logger = Logger.new(@output, @colors)
59
+ logger = Logger.new(output: @options[:output], colors: @options[:colors], logger: @options[:logger])
52
60
  invoke_command(logger) do
53
61
  stdout, stderr = Open3.capture3(command, chdir: chdir)
54
-
55
- unless stdout.empty?
56
- logger.stdout(stdout)
57
- end
58
-
59
- unless stderr.empty?
60
- logger.stderr(stderr)
61
- end
62
+ logger.stdout(stdout) unless stdout.empty?
63
+ logger.stderr(stderr) unless stderr.empty?
62
64
  end
63
65
 
64
66
  logger
@@ -67,7 +69,7 @@ module RuntimeCommand
67
69
  # @param [String] message
68
70
  # @return [RuntimeCommand::Logger]
69
71
  def puts(message)
70
- logger = Logger.new(@output, @colors)
72
+ logger = Logger.new(output: @options[:output], colors: @options[:colors], logger: @options[:logger])
71
73
  logger.stdout(message) unless message.nil?
72
74
 
73
75
  @buffered_log << logger.buffered_log + "\n"
@@ -77,7 +79,7 @@ module RuntimeCommand
77
79
  # @param [String] message
78
80
  # @return [RuntimeCommand::Logger]
79
81
  def puts_error(message)
80
- logger = Logger.new(@output, @colors)
82
+ logger = Logger.new(output: @options[:output], colors: @options[:colors], logger: @options[:logger])
81
83
  logger.stderr(message) unless message.nil?
82
84
 
83
85
  @buffered_log << logger.buffered_log + "\n"
@@ -4,17 +4,18 @@ module RuntimeCommand
4
4
  class Logger
5
5
  attr_reader :buffered_log, :buffered_stdout, :buffered_stderr
6
6
 
7
- # @param [Boolean] output
8
- # @param [Hash] colors
7
+ # @param [Hash] options
8
+ # @option options [Hash] colors
9
+ # @option options [Boolean] output
9
10
  # @return RuntimeCommand::Logger
10
- def initialize(output = true, colors = {})
11
- @output = output
12
- @has_color = colors != :none
11
+ def initialize(options = {})
12
+ @options = options
13
+ @has_color = options[:colors] != :none
13
14
 
14
15
  if @has_color
15
- @stdin_color = colors[:stdin] || HighLine::Style.rgb(204, 204, 0)
16
- @stdout_color = colors[:stdout] || HighLine::Style.rgb(64, 64, 64)
17
- @stderr_color = colors[:stderr] || HighLine::Style.rgb(255, 51, 51)
16
+ @stdin_color = options.dig(:colors, :stdin) || HighLine::Style.rgb(204, 204, 0)
17
+ @stdout_color = options.dig(:colors, :stdout) || HighLine::Style.rgb(64, 64, 64)
18
+ @stderr_color = options.dig(:colors, :stderr) || HighLine::Style.rgb(255, 51, 51)
18
19
  end
19
20
 
20
21
  flash
@@ -22,9 +23,9 @@ module RuntimeCommand
22
23
 
23
24
  # @param [String] line
24
25
  def stdin(line)
25
- puts HighLine.color(line, @stdin_color) if @output && @has_color
26
- puts line if @output && !@has_color
26
+ puts @has_color ? HighLine.color(line, @stdin_color) : line if @options[:output]
27
27
 
28
+ @options[:logger].info(line) if @options[:logger]
28
29
  @buffered_log << line + "\n"
29
30
 
30
31
  nil
@@ -37,9 +38,10 @@ module RuntimeCommand
37
38
 
38
39
  # @param [String] line
39
40
  def stdout(line)
40
- puts HighLine.color(line.chomp, @stdout_color) if @output && @has_color
41
- puts line if @output && !@has_color
41
+ trim_line = line.chomp
42
+ puts @has_color ? HighLine.color(trim_line, @stdout_color) : line if @options[:output]
42
43
 
44
+ @options[:logger].info(trim_line) if @options[:logger]
43
45
  @buffered_log << line
44
46
  @buffered_stdout << line
45
47
 
@@ -53,9 +55,10 @@ module RuntimeCommand
53
55
 
54
56
  # @param [String] line
55
57
  def stderr(line)
56
- puts HighLine.color(line.chomp, @stderr_color) if @output && @has_color
57
- puts line if @output && !@has_color
58
+ trim_line = line.chomp
59
+ puts @has_color ? HighLine.color(trim_line, @stderr_color) : line if @options[:output]
58
60
 
61
+ @options[:logger].error(trim_line) if @options[:logger]
59
62
  @buffered_log << line
60
63
  @buffered_stderr << line
61
64
 
@@ -1,3 +1,3 @@
1
1
  module RuntimeCommand
2
- VERSION = '1.0.3'.freeze
2
+ VERSION = '1.0.4'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: runtime_command
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - naomichi-y
@@ -149,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
149
  version: '0'
150
150
  requirements: []
151
151
  rubyforge_project:
152
- rubygems_version: 2.6.10
152
+ rubygems_version: 2.5.1
153
153
  signing_key:
154
154
  specification_version: 4
155
155
  summary: Execute external command from Ruby.