rvm2-ui 0.0.1 → 0.9.0

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: 9b5f6425992ae9e1c4ed457d91464499d8feecd2
4
- data.tar.gz: a5278539ab6f31e28204f3a3ee1c5b0355d90b12
3
+ metadata.gz: e9d620b2ab7b9614bbe190c16c3621374597241b
4
+ data.tar.gz: 5189ea015f85edb6e0c3a77ec81721c43750686c
5
5
  SHA512:
6
- metadata.gz: 02f871b65ebe50095edfccf90e0e05ce66b3292a5bd806d2ac743bba372e4f448226c852dc06f1c87883459f155b5f239505f105f12593760c0c6269eded07e3
7
- data.tar.gz: 66415ddcf05e009ce9c3645711f70af90726a38a2f3d30e05e19134fc061fac50c9fe38b143a365ba548c51ef480d58b9414912d8ff298ed5001ff5babe5b988
6
+ metadata.gz: cbdcb4ba42ae9a28e1f4c1e0bdbafecaf7b6321504a5044630f317f64a0b9ccbf20f91e6ac4a61b2d0b85f47f6eed68f9dec3a342e2067d703f4d1d5f5756463
7
+ data.tar.gz: b616024af9297757e1d9ba388aa36d45c15fd87361ac9d764b771098ae88b969fca169950a88f1134283e8cd104610fbc5993e10fb00bf4fb7488ae1086002d5
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  Gemfile.lock
2
2
  coverage/
3
+ *.gem
@@ -9,3 +9,4 @@ notifications:
9
9
  recipients:
10
10
  - mpapis@gmail.com
11
11
  on_failure: change
12
+ cache: bundler
data/README.md CHANGED
@@ -5,5 +5,109 @@
5
5
  [![Coverage Status](https://coveralls.io/repos/rvm/rvm2-ui/badge.png?branch=master)](https://coveralls.io/r/rvm/rvm2-ui?branch=master)
6
6
  [![Build Status](https://travis-ci.org/rvm/rvm2-ui.png?branch=master)](https://travis-ci.org/rvm/rvm2-ui)
7
7
  [![Dependency Status](https://gemnasium.com/rvm/rvm2-ui.png)](https://gemnasium.com/rvm/rvm2-ui)
8
- [![Documentation](https://f.cloud.github.com/assets/23423/241231/303578dc-899b-11e2-84e7-50387dcc3abe.png)](http://rubydoc.info/gems/rvm2-ui/frames)
8
+ [![Documentation](http://b.repl.ca/v1/yard-docs-blue.png)](http://rubydoc.info/gems/rvm2-ui/frames)
9
9
 
10
+ ## Example output
11
+
12
+ Using `:console` handler:
13
+
14
+ [ ] Command 1
15
+ Log output 1
16
+ [x] Command 2
17
+ [ ] Command 3
18
+ Log output 2
19
+ [v] Command 3
20
+ [v] Command 1
21
+
22
+ ## Installation
23
+
24
+ Get the gem:
25
+
26
+ gem install rvm2-ui
27
+
28
+ Load it:
29
+
30
+ require 'rvm2/ui'
31
+
32
+ ## Usage
33
+
34
+ Different handlers can be loaded using `pluginator` group `rvm2` type `ui/output`:
35
+
36
+ Rvm2::Ui.get(type = :console, rvm2_plugins = nil, *args)
37
+
38
+ the `args` will be passed to the handler constructor.
39
+
40
+ To get the default console output:
41
+
42
+ @ui = Rvm2::Ui.get
43
+
44
+ ## Wrapping code blocks
45
+
46
+ The `command` will produce checklist like item:
47
+
48
+ @ui.command('display name') do
49
+ do_something
50
+ end
51
+
52
+ Example output with `:console` output - before finish:
53
+
54
+ [ ] Group 1
55
+
56
+ after finish:
57
+
58
+ [v] Group 1
59
+
60
+ ## Logging output
61
+
62
+ The `log` allows giving messages, warnings and errors to user:
63
+
64
+ log(message, type = :log)
65
+ Supported types are `:log`, `:warn`, `:warn_important`, `:error`, any other type might be supported
66
+ or should be handled as `:log` with the capitalized `type` as prefix.
67
+
68
+ Example:
69
+
70
+ @ui.log("something went wrong", :error)
71
+
72
+ Would produce with console:
73
+
74
+ Error: something went wrong
75
+
76
+ ## Handling extra outputs
77
+
78
+ In some cases like running shell commands an `stdout` and `stderr` objects are available with `IO`
79
+ interface allowing proper output handling (not injecting text in random places):
80
+
81
+ @ui.command("test") do
82
+ @ui.stderr.puts("debugging output")
83
+ end
84
+
85
+ would produce with `:console`:
86
+
87
+ [ ] test
88
+ debugging output
89
+ [v] test
90
+
91
+ ## Combining multiple outputs
92
+
93
+ In some cases it might be useful to send the same output to different targets like UI and log:
94
+
95
+ Rvm2::UI.multi(rvm2_plugins)
96
+
97
+ Example use:
98
+
99
+ @ui = Rvm2::UI.multi
100
+ @ui.add(:console)
101
+ @ui.add(:log, "my_app.log")
102
+ @ui.log("text")
103
+ @ui.remove # removes the last added logger
104
+
105
+ Example - temporarily use logger:
106
+
107
+ @ui = Rvm2::UI.multi
108
+ @ui.add(:console)
109
+ @ui.with(:log, "my_app.log") do
110
+ @ui.log("text")
111
+ end
112
+
113
+ In bot examples the output will be written to both standard output and log file.
@@ -0,0 +1,17 @@
1
+ module Rvm2
2
+ module Ui
3
+ module Output
4
+ class Log < Console
5
+ attr_reader :file_name
6
+ def initialize(file_name, flags = "w")
7
+ @file_name = file_name
8
+ @file = File.new(@file_name, flags)
9
+ super(@file, @file)
10
+ end
11
+ def finalize
12
+ @file.close
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,28 +1,13 @@
1
- require 'hooks'
2
- require 'hooks/instance_hooks'
3
-
4
1
  module Rvm2
5
2
  module Ui
6
- class Router
7
- define_hooks :on_start, :on_finish, :on_log
8
-
9
- # ui.command "bla" { do_something; }
10
- def command(name, &block)
11
- raise "No block given" unless block_given?
12
- run_hook(:on_start, name)
13
- status = block.call
14
- run_hook(:on_finish, status)
15
- status
16
- end
17
-
18
- # ui.log 'message'
19
- # ui.log 'message', :important
20
- # standard types => :log, :warn, :important, :error
21
- # in case unsupported type is used :log will be used
22
- def log(message, type = :log)
23
- run_hook(:on_log, message, type)
24
- end
25
-
3
+ def self.get(type = :console, rvm2_plugins = nil, *args)
4
+ Rvm2::Ui::Single.new(type, rvm2_plugins, *args)
5
+ end
6
+ def self.multi(rvm2_plugins = nil)
7
+ Rvm2::Ui::Multi.new(rvm2_plugins)
26
8
  end
27
9
  end
28
10
  end
11
+
12
+ require_relative 'ui/single'
13
+ require_relative 'ui/multi'
@@ -0,0 +1,55 @@
1
+ module Rvm2
2
+ module Ui
3
+ class Multi
4
+
5
+ attr_reader :handlers
6
+
7
+ def initialize(rvm2_plugins = nil)
8
+ @rvm2_plugins = rvm2_plugins || Pluginator.find("rvm2", extends: %i{first_class})
9
+ @handlers = []
10
+ end
11
+
12
+ def add(handler, *args)
13
+ @handlers << @rvm2_plugins.first_class!('ui/output', handler).new(*args)
14
+ end
15
+
16
+ def remove
17
+ @handlers.pop
18
+ end
19
+
20
+ def with(handler, *args, &block)
21
+ add(handler, *args)
22
+ block.call
23
+ remove
24
+ end
25
+
26
+ # ui.command "message" { do_something; }
27
+ def command(name, &block)
28
+ raise "No block given" unless block_given?
29
+ @handlers.each {|h| h.start(name) }
30
+ status = block.call
31
+ @handlers.each {|h| h.finish(status) }
32
+ status
33
+ end
34
+
35
+ # ui.log 'message'
36
+ # ui.log 'message', :important
37
+ # standard types => :log, :warn, :important, :error
38
+ # in case unsupported type is used :log will be used
39
+ def log(message, type = :log)
40
+ @handlers.each {|h| h.log(message, type) }
41
+ end
42
+
43
+ def stdout
44
+ @stdout ||= IoWriteRouter.new(self, :stdout)
45
+ end
46
+
47
+ def stderr
48
+ @stderr ||= IoWriteRouter.new(self, :stderr)
49
+ end
50
+
51
+ end
52
+ end
53
+ end
54
+
55
+ require_relative 'multi/io_write_router'
@@ -0,0 +1,32 @@
1
+ module Rvm2
2
+ module Ui
3
+ class Multi
4
+ class IoWriteRouter
5
+
6
+ def initialize(parent, type)
7
+ @parent = parent
8
+ @type = type
9
+ end
10
+ def write(*args)
11
+ @parent.handlers.each{|h| h.send(@type).write(*args) }
12
+ end
13
+ def <<(*args)
14
+ @parent.handlers.each{|h| h.send(@type).<<(*args) }
15
+ end
16
+ def print(*args)
17
+ @parent.handlers.each{|h| h.send(@type).print(*args) }
18
+ end
19
+ def printf(*args)
20
+ @parent.handlers.each{|h| h.send(@type).printf(*args) }
21
+ end
22
+ def puts(*args)
23
+ @parent.handlers.each{|h| h.send(@type).puts(*args) }
24
+ end
25
+ def write_nonblock(*args)
26
+ @parent.handlers.each{|h| h.send(@type).write_nonblock(*args) }
27
+ end
28
+
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,41 @@
1
+ require 'pluginator'
2
+
3
+ module Rvm2
4
+ module Ui
5
+ class Single
6
+
7
+ attr_reader :handler
8
+
9
+ def initialize(handler = :console, rvm2_plugins = nil, *args)
10
+ @rvm2_plugins = rvm2_plugins || Pluginator.find("rvm2", extends: %i{first_class})
11
+ @handler = @rvm2_plugins.first_class!('ui/output', handler).new(*args)
12
+ end
13
+
14
+ # ui.command "message" { do_something; }
15
+ def command(name, &block)
16
+ raise "No block given" unless block_given?
17
+ @handler.start(name)
18
+ status = block.call
19
+ @handler.finish(status)
20
+ status
21
+ end
22
+
23
+ # ui.log 'message'
24
+ # ui.log 'message', :important
25
+ # standard types => :log, :warn, :important, :error
26
+ # in case unsupported type is used :log will be used
27
+ def log(message, type = :log)
28
+ @handler.log(message, type)
29
+ end
30
+
31
+ def stdout
32
+ @handler.stdout
33
+ end
34
+
35
+ def stderr
36
+ @handler.stderr
37
+ end
38
+
39
+ end
40
+ end
41
+ end
@@ -1,5 +1,5 @@
1
1
  module Rvm2
2
2
  module Ui
3
- VERSION = '0.0.1'
3
+ VERSION = '0.9.0'
4
4
  end
5
5
  end
@@ -14,6 +14,7 @@ Gem::Specification.new do |s|
14
14
  s.files = `git ls-files`.split("\n")
15
15
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
16
  s.required_ruby_version = ">= 2.0.0"
17
+ s.add_dependency('pluginator')
17
18
  %w{rake minitest simplecov coveralls redcarpet}.each do |name|
18
19
  s.add_development_dependency(name)
19
20
  end
@@ -0,0 +1 @@
1
+ Note the `plugins_test` contains `_test` to avoid being discovered as `plugins` by `pluginator`.
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+ require 'plugins/rvm2/ui/output/log'
3
+ require 'tempfile'
4
+
5
+ describe Rvm2::Ui::Output::Log do
6
+ before do
7
+ @tempfile = Tempfile.new("rvm2-ui-output-log-test")
8
+ @tempfile.close
9
+ end
10
+ after do
11
+ @tempfile.unlink
12
+ end
13
+ subject do
14
+ Rvm2::Ui::Output::Log.new(@tempfile.path)
15
+ end
16
+
17
+ it "adds messages" do
18
+ subject.log("Test me 1")
19
+ subject.finalize
20
+ File.open(@tempfile.path, "r") do |file|
21
+ file.read.must_equal(<<-EXPECTED)
22
+ Test me 1
23
+ EXPECTED
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,57 @@
1
+ require 'test_helper'
2
+ require 'rvm2/ui/multi/io_write_router'
3
+ require 'plugins/rvm2/ui/output/fake'
4
+
5
+ class HandlersParent
6
+ attr_reader :handlers
7
+ def initialize
8
+ @handlers = [Rvm2::Ui::Output::Fake.new, Rvm2::Ui::Output::Fake.new]
9
+ end
10
+ end
11
+
12
+ describe Rvm2::Ui::Multi::IoWriteRouter do
13
+ before do
14
+ @parent = HandlersParent.new
15
+ end
16
+
17
+ subject do
18
+ Rvm2::Ui::Multi::IoWriteRouter.new(@parent, :stdout)
19
+ end
20
+
21
+ it "supports write" do
22
+ subject.write("test 1")
23
+ @parent.handlers[0].root.list.map(&:message).must_equal(["test 1"])
24
+ @parent.handlers[1].root.list.map(&:message).must_equal(["test 1"])
25
+ end
26
+
27
+ it "supports <<" do
28
+ subject << "test 1"
29
+ @parent.handlers[0].root.list.map(&:message).must_equal(["test 1"])
30
+ @parent.handlers[1].root.list.map(&:message).must_equal(["test 1"])
31
+ end
32
+
33
+ it "supports print" do
34
+ subject.print("test 1")
35
+ @parent.handlers[0].root.list.map(&:message).must_equal(["test 1"])
36
+ @parent.handlers[1].root.list.map(&:message).must_equal(["test 1"])
37
+ end
38
+
39
+ it "supports printf" do
40
+ subject.printf("test 1")
41
+ @parent.handlers[0].root.list.map(&:message).must_equal(["test 1"])
42
+ @parent.handlers[1].root.list.map(&:message).must_equal(["test 1"])
43
+ end
44
+
45
+ it "supports puts" do
46
+ subject.puts("test 1")
47
+ @parent.handlers[0].root.list.map(&:message).must_equal(["test 1", "\n"])
48
+ @parent.handlers[1].root.list.map(&:message).must_equal(["test 1", "\n"])
49
+ end
50
+
51
+ it "supports write_nonblock" do
52
+ subject.write_nonblock("test 1")
53
+ @parent.handlers[0].root.list.map(&:message).must_equal(["test 1"])
54
+ @parent.handlers[1].root.list.map(&:message).must_equal(["test 1"])
55
+ end
56
+
57
+ end
@@ -0,0 +1,62 @@
1
+ require 'test_helper'
2
+ require 'rvm2/ui/multi'
3
+
4
+ describe Rvm2::Ui::Multi do
5
+ before do
6
+ @stdout = StringIO.new
7
+ @stderr = StringIO.new
8
+ end
9
+
10
+ subject do
11
+ ui = Rvm2::Ui::Multi.new
12
+ ui.add(:console, @stdout, @stderr)
13
+ ui.add(:fake)
14
+ ui
15
+ end
16
+
17
+ it "removes handlers" do
18
+ subject.remove
19
+ subject.handlers.map{|h| h.class.name }.must_equal(["Rvm2::Ui::Output::Console"])
20
+ end
21
+
22
+ it "supports multiple outputs" do
23
+ subject.handlers.map{|h| h.class.name }.must_equal(["Rvm2::Ui::Output::Console", "Rvm2::Ui::Output::Fake"])
24
+ end
25
+
26
+ it "handles commands" do
27
+ subject.command("test true" ){ true }.must_equal(true)
28
+ subject.command("test false"){ false }.must_equal(false)
29
+ @stdout.string.must_equal(<<-EXPECTED)
30
+ [ ] test true\r[v] test true
31
+ [ ] test false\r[x] test false
32
+ EXPECTED
33
+ subject.handlers[1].root.list.map(&:message).must_equal(["test true", "test false"])
34
+ end
35
+
36
+ it "handles log" do
37
+ subject.log("test log")
38
+ @stdout.string.must_equal(<<-EXPECTED)
39
+ test log
40
+ EXPECTED
41
+ subject.handlers[1].root.list.map(&:message).must_equal(["test log"])
42
+ end
43
+
44
+ it "supports outputs" do
45
+ subject.stdout.write("test stdout")
46
+ subject.stderr.write("test stderr")
47
+ @stdout.string.must_equal("test stdout")
48
+ @stderr.string.must_equal("test stderr")
49
+ subject.handlers[1].root.list.map(&:message).must_equal(["test stdout", "test stderr"])
50
+ subject.handlers[1].root.list.map(&:type).must_equal([:stdout, :stderr])
51
+ end
52
+
53
+ it "handles temporary adding of a log" do
54
+ subject.handlers.count.must_equal(2)
55
+ subject.with(:fake) do
56
+ subject.handlers.count.must_equal(3)
57
+ subject.handlers[2].class.name.must_equal("Rvm2::Ui::Output::Fake")
58
+ end
59
+ subject.handlers.count.must_equal(2)
60
+ end
61
+
62
+ end
@@ -0,0 +1,34 @@
1
+ require 'test_helper'
2
+ require 'rvm2/ui/single'
3
+
4
+ describe Rvm2::Ui::Single do
5
+ subject do
6
+ Rvm2::Ui::Single
7
+ end
8
+
9
+ it "loads console by default" do
10
+ subject.new.handler.class.name.must_equal("Rvm2::Ui::Output::Console")
11
+ end
12
+
13
+ it "handles commands" do
14
+ @obj = subject.new(:fake)
15
+ @obj.command("test true" ){ true }.must_equal(true)
16
+ @obj.command("test false"){ false }.must_equal(false)
17
+ @obj.handler.root.list.map(&:message).must_equal(["test true", "test false"])
18
+ end
19
+
20
+ it "handles log" do
21
+ @obj = subject.new(:fake)
22
+ @obj.log("test log")
23
+ @obj.handler.root.list.map(&:message).must_equal(["test log"])
24
+ end
25
+
26
+ it "supports outputs" do
27
+ @obj = subject.new(:fake)
28
+ @obj.stdout.write("test stdout")
29
+ @obj.stderr.write("test stderr")
30
+ @obj.handler.root.list.map(&:message).must_equal(["test stdout", "test stderr"])
31
+ @obj.handler.root.list.map(&:type ).must_equal([:stdout, :stderr])
32
+ end
33
+
34
+ end
@@ -0,0 +1,11 @@
1
+ require 'test_helper'
2
+ require 'rvm2/ui'
3
+
4
+ describe Rvm2::Ui do
5
+ it "gets instance of single" do
6
+ Rvm2::Ui.get.class.name.must_equal("Rvm2::Ui::Single")
7
+ end
8
+ it "gets instance of multi" do
9
+ Rvm2::Ui.multi.class.name.must_equal("Rvm2::Ui::Multi")
10
+ end
11
+ end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rvm2-ui
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michal Papis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-05 00:00:00.000000000 Z
11
+ date: 2013-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pluginator
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'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rake
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -95,11 +109,21 @@ files:
95
109
  - Rakefile
96
110
  - lib/plugins/rvm2/ui/output/console.rb
97
111
  - lib/plugins/rvm2/ui/output/fake.rb
112
+ - lib/plugins/rvm2/ui/output/log.rb
98
113
  - lib/rvm2/ui.rb
114
+ - lib/rvm2/ui/multi.rb
115
+ - lib/rvm2/ui/multi/io_write_router.rb
116
+ - lib/rvm2/ui/single.rb
99
117
  - lib/rvm2/ui/version.rb
100
118
  - rvm2-ui.gemspec
101
- - test/plugins/rvm2/ui/output/console_test.rb
102
- - test/plugins/rvm2/ui/output/fake_test.rb
119
+ - test/README.md
120
+ - test/plugins_test/rvm2/ui/output/console_test.rb
121
+ - test/plugins_test/rvm2/ui/output/fake_test.rb
122
+ - test/plugins_test/rvm2/ui/output/log_test.rb
123
+ - test/rvm2/ui/multi/io_write_router_test.rb
124
+ - test/rvm2/ui/multi_test.rb
125
+ - test/rvm2/ui/single_test.rb
126
+ - test/rvm2/ui_test.rb
103
127
  - test/test_helper.rb
104
128
  homepage: https://github.com/rvm/rvm2-ui
105
129
  licenses:
@@ -126,6 +150,12 @@ signing_key:
126
150
  specification_version: 4
127
151
  summary: Abstract user interface handling in RVM2
128
152
  test_files:
129
- - test/plugins/rvm2/ui/output/console_test.rb
130
- - test/plugins/rvm2/ui/output/fake_test.rb
153
+ - test/README.md
154
+ - test/plugins_test/rvm2/ui/output/console_test.rb
155
+ - test/plugins_test/rvm2/ui/output/fake_test.rb
156
+ - test/plugins_test/rvm2/ui/output/log_test.rb
157
+ - test/rvm2/ui/multi/io_write_router_test.rb
158
+ - test/rvm2/ui/multi_test.rb
159
+ - test/rvm2/ui/single_test.rb
160
+ - test/rvm2/ui_test.rb
131
161
  - test/test_helper.rb