runtime_command 0.1.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9d7ce4bf7b8f4efe1f92298a681da61acba42c93
4
+ data.tar.gz: c54942bd0bc7e82beb5460dd7f2a795e7840756a
5
+ SHA512:
6
+ metadata.gz: f453423dee2fbe12c87bdbd81a2955a169b438a637621c2666c69a51341392f32fbc0dbe0335e3bb075cb14c4787acbe4033b41bc904581a75985c46c4d4bcaa
7
+ data.tar.gz: ae2d203045aacb213dc5fdd01d2557e711a77a7e95b6885829a2b96a9e5184b88cecb5f0f2fe2c2d3194eb37083e3ddd6042e86f413b61bb0f6e17112143e218
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # RuntimeCommand
2
+
3
+ Execute external command and retrive STDIN / STDOUT in real time.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'runtime_command'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install runtime_command
22
+
23
+ ## Usage
24
+
25
+ Command results are show in real time on STDOUT.
26
+
27
+ ```
28
+ require 'runtime_command'
29
+
30
+ command = RuntimeCommand::Builder.new
31
+ command.exec('echo wait; sleep 3; echo hello')
32
+ ```
33
+
34
+ Output contents can be get as character string.
35
+
36
+ ```
37
+ command.output = false
38
+ logger = command.exec('echo wait; sleep 3; echo hello')
39
+
40
+ puts logger.buffered_log
41
+ puts logger.buffered_stdout
42
+ puts logger.buffered_stderr
43
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "runtime_command"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/example/sample.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler/setup'
2
+ require 'runtime_command'
3
+
4
+ command = RuntimeCommand::Builder.new
5
+ command.exec('echo "wait 3" sec; sleep 3; echo "hello world!"')
@@ -0,0 +1,45 @@
1
+ require 'open3'
2
+ require 'highline'
3
+
4
+ module RuntimeCommand
5
+ class Builder
6
+ attr_reader :buffered_log
7
+ attr_accessor :stdin_prefix, :colors, :output
8
+
9
+ def initialize(base_dir = '.')
10
+ @base_dir = base_dir
11
+ @output = true
12
+ @buffered_log = ''
13
+ @stdin_prefix = '>'
14
+ end
15
+
16
+ def exec(command, chdir = nil)
17
+ chdir ||= @base_dir
18
+
19
+ logger = Logger.new(@output, @colors)
20
+ logger.stdin(@stdin_prefix + ' ' + command)
21
+
22
+ begin
23
+ Open3.popen3(command, chdir: chdir) do |stdin, stdout, stderr, wait_thr|
24
+ stdin.close
25
+
26
+ stdout.each do |line|
27
+ logger.stdout(line)
28
+ end
29
+
30
+ stderr.each do |line|
31
+ logger.stderr(line)
32
+ end
33
+ end
34
+
35
+ @buffered_log << logger.buffered_log
36
+
37
+ rescue => e
38
+ logger.stderr(e.to_s)
39
+ @buffered_log << logger.buffered_log
40
+ end
41
+
42
+ logger
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,58 @@
1
+ module RuntimeCommand
2
+ class Logger
3
+ attr_reader :buffered_log, :buffered_stdout, :buffered_stderr
4
+
5
+ def initialize(output = true, colors = nil)
6
+ @output = output
7
+
8
+ stdin_rgb = [204, 204, 0]
9
+ stdout_rgb = [64, 64, 64]
10
+ stderr_rgb = [255, 51, 51]
11
+
12
+ if colors
13
+ stdin_rgb = colors[:stdin] if colors.has_key?(:stdin)
14
+ stdout_rgb = colors[:stdout] if colors.has_key?(:stdout)
15
+ stderr_rgb = colors[:stderr] if colors.has_key?(:stderr)
16
+ end
17
+
18
+ @stdin_color = HighLine::Style.rgb(*stdin_rgb)
19
+ @stdout_color = HighLine::Style.rgb(*stdout_rgb)
20
+ @stderr_color = HighLine::Style.rgb(*stderr_rgb)
21
+
22
+ flash
23
+ end
24
+
25
+ def stdin(line)
26
+ puts HighLine.color(line, @stdin_color) if @output
27
+ @buffered_log << line + "\n"
28
+
29
+ return
30
+ end
31
+
32
+ def stdout(line)
33
+ puts HighLine.color(line.chomp, @stdout_color) if @output
34
+
35
+ @buffered_log << line
36
+ @buffered_stdout << line
37
+
38
+ return
39
+ end
40
+
41
+ def stderr(line)
42
+ puts HighLine.color(line.chomp, @stderr_color) if @output
43
+
44
+ @buffered_log << line
45
+ @buffered_stderr << line
46
+
47
+ return
48
+ end
49
+
50
+ def flash
51
+ @buffered_log = ''
52
+ @buffered_stdout = ''
53
+ @buffered_stderr = ''
54
+
55
+ return
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module RuntimeCommand
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,10 @@
1
+ require 'runtime_command/version'
2
+
3
+ require 'runtime_command/builder'
4
+ require 'runtime_command/logger'
5
+
6
+ module RuntimeCommand
7
+ def self.builder
8
+ RuntimeCommand::Builder.new
9
+ end
10
+ end
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'runtime_command/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "runtime_command"
8
+ spec.version = RuntimeCommand::VERSION
9
+ spec.authors = ["naomichi-y"]
10
+ spec.email = ["n.yamakita@gmail.com"]
11
+
12
+ spec.summary = %q{Execute external command from Ruby.}
13
+ spec.description = %q{Execute external command and retrive STDIN / STDOUT in real time.}
14
+ spec.homepage = ""
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ # spec.metadata['allowed_push_host'] = ""
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_dependency 'highline'
33
+ spec.add_development_dependency "bundler", "~> 1.13"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: runtime_command
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - naomichi-y
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-02-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: highline
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Execute external command and retrive STDIN / STDOUT in real time.
56
+ email:
57
+ - n.yamakita@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - example/sample.rb
70
+ - lib/runtime_command.rb
71
+ - lib/runtime_command/builder.rb
72
+ - lib/runtime_command/logger.rb
73
+ - lib/runtime_command/version.rb
74
+ - runtime_command.gemspec
75
+ homepage: ''
76
+ licenses: []
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.6.10
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Execute external command from Ruby.
98
+ test_files: []