runtime_command 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/example/sample.rb +11 -4
- data/lib/runtime_command/builder.rb +24 -22
- data/lib/runtime_command/logger.rb +17 -14
- data/lib/runtime_command/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 324ec53b7ebeb61e18c83725f0c66963c63eddac
|
4
|
+
data.tar.gz: dbdb3070c12068ee56e7ca78f6d767d7c39cf346
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0c0c214d899135f7f0ed29d14842d80eaa8cc4e9326c7fcb8e481dff5bb10e4184606e5a2f93a56d1e4650b2808571a4d0f39cf28444a81f76e6a348ffefc74f
|
7
|
+
data.tar.gz: f7495faa2ce138ddeb4517e73f65aba83a25867b0ecac22a4c688cb86f99841a1388dfdeb4522e1e93db70c10adde34672dc87a41966b916c7d2b51504774935
|
data/.gitignore
CHANGED
data/example/sample.rb
CHANGED
@@ -1,12 +1,19 @@
|
|
1
1
|
require 'bundler/setup'
|
2
2
|
require 'runtime_command'
|
3
3
|
|
4
|
-
|
5
|
-
|
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
|
-
#
|
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 [
|
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(
|
12
|
-
|
13
|
-
|
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
|
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 [
|
8
|
-
# @
|
7
|
+
# @param [Hash] options
|
8
|
+
# @option options [Hash] colors
|
9
|
+
# @option options [Boolean] output
|
9
10
|
# @return RuntimeCommand::Logger
|
10
|
-
def initialize(
|
11
|
-
@
|
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
|
16
|
-
@stdout_color = colors
|
17
|
-
@stderr_color = colors
|
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
|
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
|
-
|
41
|
-
puts line if @output
|
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
|
-
|
57
|
-
puts line if @output
|
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
|
|
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.
|
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.
|
152
|
+
rubygems_version: 2.5.1
|
153
153
|
signing_key:
|
154
154
|
specification_version: 4
|
155
155
|
summary: Execute external command from Ruby.
|