public
Rubygem
Description: Deploy scripting with Thor using remote Ruby execution
Clone URL: git://github.com/wesabe/robot-army.git
Add RobotArmy::IO to handle IO stuff.
Tue Jun 24 13:53:20 -0700 2008
commit  e2de68f9a9e219175d9e69a43c8846dbf79c0215
tree    32bd083f6efa9387fbe7564c844b99e8ea2f15bc
parent  8234a0a59f755c787b8745a8f50e2774f44ebeb9
...
52
53
54
55
 
56
57
58
...
52
53
54
 
55
56
57
58
0
@@ -52,7 +52,7 @@ module RobotArmy
0
   end
0
 end
0
 
0
-%w[loader dependency_loader
0
+%w[loader dependency_loader io
0
    officer_loader soldier officer
0
    messenger task_master proxy
0
    connection officer_connection
...
1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
3
4
...
8
9
10
11
 
12
13
14
15
16
17
 
 
 
18
19
20
21
22
23
24
 
25
26
27
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
30
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
...
51
52
53
 
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
0
@@ -1,4 +1,47 @@
0
 class RobotArmy::IO
0
+ attr_reader :name
0
+
0
+ # Starts capturing output of the named stream.
0
+ #
0
+ def start_capture
0
+ eval "$#{name} = self"
0
+ end
0
+
0
+ # Stops capturing output of the named stream.
0
+ #
0
+ def stop_capture
0
+ eval "$#{name} = #{name.upcase}"
0
+ end
0
+
0
+ def puts(*args) #:nodoc:
0
+ post capture(:puts, *args)
0
+ end
0
+
0
+ def print(*args) #:nodoc:
0
+ post capture(:print, *args)
0
+ end
0
+
0
+ def write(*args) #:nodoc:
0
+ post capture(:write, *args)
0
+ end
0
+
0
+private
0
+
0
+ def initialize(name)
0
+ @name = name.to_s
0
+ start_capture
0
+ end
0
+
0
+ def post(string)
0
+ RobotArmy.upstream.post(:status => 'output', :data => {:stream => name, :string => string})
0
+ end
0
+
0
+ def capture(*call)
0
+ stream = StringIO.new
0
+ stream.send(*call)
0
+ stream.string
0
+ end
0
+
0
   class <<self
0
     # Determines whether the given stream has any data to be read.
0
     #
0
@@ -8,23 +51,56 @@ class RobotArmy::IO
0
     # @return [Boolean]
0
     # +true+ if stream has data to be read, +false+ otherwise.
0
     #
0
- def self.has_data?(stream)
0
+ def has_data?(stream)
0
       selected, _ = IO.select([stream], nil, nil, 0.5)
0
       return selected && !selected.empty?
0
     end
0
 
0
     # Reads immediately available data from the given stream.
0
     #
0
+ # # echo foo | ruby test.rb
0
+ # RobotArmy::IO.read_data($stdin) # => "foo\n"
0
+ #
0
     # @param stream [IO]
0
     # The +IO+ stream to read from.
0
     #
0
     # @return [String]
0
     # The data read from the stream.
0
     #
0
- def self.read_data(stream)
0
+ def read_data(stream)
0
       data = []
0
       data << stream.readpartial(1024) while has_data?(stream)
0
       return data.join
0
     end
0
+
0
+ # Redirects the named stream to a +StringIO+.
0
+ #
0
+ # RobotArmy::IO.capture(:stdout) { puts "foo" } # => "foo\n"
0
+ #
0
+ # RobotArmy::IO.silence(:stderr) { system "rm non-existent-file" }
0
+ #
0
+ # @param stream [Symbol]
0
+ # The name of the stream to redirect (i.e. +:stderr+, +:stdout+).
0
+ #
0
+ # @yield
0
+ # The block whose output we should capture.
0
+ #
0
+ # @return [String]
0
+ # The string result of the output produced by the block.
0
+ #
0
+ def capture(stream)
0
+ begin
0
+ stream = stream.to_s
0
+ eval "$#{stream} = StringIO.new"
0
+ yield
0
+ result = eval("$#{stream}").string
0
+ ensure
0
+ eval("$#{stream} = #{stream.upcase}")
0
+ end
0
+
0
+ result
0
+ end
0
+
0
+ alias_method :silence, :capture
0
   end
0
 end

Comments

    No one has commented yet.