Class: LTools::Tool
- Inherits:
-
Object
- Object
- LTools::Tool
- Extended by:
- Forwardable
- Defined in:
- lib/core/opts.rb,
lib/core/tool.rb
Defined Under Namespace
Classes: ToolOpts
Instance Attribute Summary collapse
-
#cmd ⇒ Object
readonly
Returns the value of attribute cmd.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Class Method Summary collapse
Instance Method Summary collapse
- #command ⇒ Object
-
#initialize(*args) ⇒ Tool
constructor
A new instance of Tool.
- #io_inherit! ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #wait ⇒ Object
Constructor Details
#initialize(*args) ⇒ Tool
Returns a new instance of Tool.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/core/tool.rb', line 21 def initialize(*args) tool, *args = args # 检测参数类型进行变换,支持一下格式: # args = 'echo 1231' # args = ['echo', '1231'] # args = [['echo', '1231']] case tool when String if args.empty? and tool.include? ' ' tool, *args = Shellwords.split(tool) end when Array tool, *args = tool else raise end @cmd = args.unshift(tool).map(&:to_s) @process = ChildProcess.build(*@cmd) @stdout, @stdout_w = IO.pipe @process.io.stdout = @stdout_w @stderr, @stderr_w = IO.pipe @process.io.stderr = @stderr_w @verbose = true if LTools.verbose end |
Instance Attribute Details
#cmd ⇒ Object (readonly)
Returns the value of attribute cmd.
15 16 17 |
# File 'lib/core/tool.rb', line 15 def cmd @cmd end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
15 16 17 |
# File 'lib/core/tool.rb', line 15 def stderr @stderr end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
15 16 17 |
# File 'lib/core/tool.rb', line 15 def stdin @stdin end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
15 16 17 |
# File 'lib/core/tool.rb', line 15 def stdout @stdout end |
#verbose ⇒ Object
Returns the value of attribute verbose.
16 17 18 |
# File 'lib/core/tool.rb', line 16 def verbose @verbose end |
Class Method Details
.check? ⇒ Boolean
97 98 99 100 |
# File 'lib/core/tool.rb', line 97 def self.check? system "hash #{@name} 2>&-" $?.success? end |
Instance Method Details
#command ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/core/tool.rb', line 74 def command @cmd.map{|str| if str.include?(' ') if str.include? '"' "'#{str}'" else "\"#{str}\"" end else str end }.join ' ' end |
#io_inherit! ⇒ Object
88 89 90 91 |
# File 'lib/core/tool.rb', line 88 def io_inherit! @inherit = true @process.io.inherit! end |
#start ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/core/tool.rb', line 51 def start @process.duplex = true unless @inherit @process.start @stdin = @process.io.stdin unless @inherit if @verbose puts "[#{'+'.light_green.bold}] #{self.class.to_s.light_yellow} (pid: #{pid})" puts " #{?$.light_red.bold} #{command}" puts end end |
#stop ⇒ Object
68 69 70 71 72 |
# File 'lib/core/tool.rb', line 68 def stop @process.stop @stdout_w.close @stderr_w.close end |
#wait ⇒ Object
62 63 64 65 66 |
# File 'lib/core/tool.rb', line 62 def wait @process.wait @stdout_w.close @stderr_w.close end |