Class: RubyConsole
Overview
Wraps around a local or remote irb or merb (and probably rails) console, passing commands to it and returning their stdout output.
Constant Summary collapse
- CODE_SAMPLES =
%q< # Send something to a local irb console RubyConsole.run("puts 'hey'") # Send something to a merb console RubyConsole.register(:foo, 'merb -m /projects/foo -i') # Do this only once RubyConsole[:foo].run("y Account.first") >
- @@output =
""
- @@registered =
{}
Class Method Summary collapse
- .[](key) ⇒ Object
- .at(key, the_command) ⇒ Object
- .output ⇒ Object
- .register(key, console_command = 'irb', server = nil) ⇒ Object
- .run(the_command) ⇒ Object
Instance Method Summary collapse
- #connect ⇒ Object
- #console_command ⇒ Object
- #console_command=(to) ⇒ Object
-
#initialize(console_command = 'irb', server = nil) ⇒ RubyConsole
constructor
A new instance of RubyConsole.
- #open_channel ⇒ Object
- #run(the_command) ⇒ Object
-
#send_it(the_command) ⇒ Object
Send output.
- #session ⇒ Object
Constructor Details
#initialize(console_command = 'irb', server = nil) ⇒ RubyConsole
Returns a new instance of RubyConsole.
24 25 26 |
# File 'lib/xiki/ruby_console.rb', line 24 def initialize console_command='irb', server=nil @console_command, @server = console_command, server end |
Class Method Details
.[](key) ⇒ Object
143 144 145 |
# File 'lib/xiki/ruby_console.rb', line 143 def self.[] key @@registered[key] end |
.at(key, the_command) ⇒ Object
151 152 153 154 155 156 |
# File 'lib/xiki/ruby_console.rb', line 151 def self.at key, the_command key = key.to_sym if key.is_a? String console = self[key] raise "No console has been defined for key '#{key}'." unless console console.run the_command end |
.output ⇒ Object
9 |
# File 'lib/xiki/ruby_console.rb', line 9 def self.output; @@output; end |
.register(key, console_command = 'irb', server = nil) ⇒ Object
147 148 149 |
# File 'lib/xiki/ruby_console.rb', line 147 def self.register key, console_command='irb', server=nil @@registered[key] = RubyConsole.new(console_command, server) end |
.run(the_command) ⇒ Object
139 140 141 |
# File 'lib/xiki/ruby_console.rb', line 139 def self.run the_command self[:irb].run the_command end |
Instance Method Details
#connect ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/xiki/ruby_console.rb', line 28 def connect begin timeout(25) do open_channel # Connect # TODO: try to indent this send_it %Q[conf.echo = false # Temporary hack for nuby 1.8.4 $out_bufr = defined?(StringIO) ? StringIO.new : '' module Kernel def puts *args args.each { |a| $out_bufr << ( (a.class == Array) ? (a.join("\\n") + "\\n") : "\#{a}\\n" ) } nil end def p *args args.each { |a| $out_bufr << "\#{a.inspect}\n" } nil end end ] end#timeout rescue Timeout::Error=>e Ol << 'Timed out in RubyConsole!' end#begin end |
#console_command ⇒ Object
137 |
# File 'lib/xiki/ruby_console.rb', line 137 def console_command; @console_command; end |
#console_command=(to) ⇒ Object
136 |
# File 'lib/xiki/ruby_console.rb', line 136 def console_command= to; @console_command = to; end |
#open_channel ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/xiki/ruby_console.rb', line 92 def open_channel @session = if @server user, server, port = Remote.split_root @server Remote.new_connection(user, server, port) else Net::SSH.start('localhost', ENV['USER'] || ENV['USERNAME']) end @channel = @session.open_channel do |ch| ch.exec @console_command do |ch, success| raise "could not execute command" unless success # When console returns (prints) something ch.on_data do |c, data| # Use to debug problems / errors on server # Ol << "data: #{data.inspect}" RubyConsole.output << data #print data end # "on_extended_data" is called when the process writes something to stderr ch.on_extended_data do |c, type, data| print "error: #{data}" end ch.on_close { RubyConsole.output << "___CLOSED___" } end end end |
#run(the_command) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/xiki/ruby_console.rb', line 122 def run the_command connect unless @channel send_it the_command out = @@output @@output = "" # Remove up until beginning of output out.sub!(/.*^.*\$stdout\.print out\n/m, '') # Remove last line out.sub!(/^.?.?.?-eor\d+-\n.*/, '') out == "" ? "(no output)\n" : out end |
#send_it(the_command) ⇒ Object
Send output
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 |
# File 'lib/xiki/ruby_console.rb', line 63 def send_it the_command time_stamp = "-eor#{Time.now.usec}-" the_command = "\ # Temporary hack for nuby 1.8.4 $out_bufr = defined?(StringIO) ? StringIO.new : '' begin #{the_command} rescue Exception => e puts e.message end # Temporary hack for nuby 1.8.4 out = $out_bufr.respond_to?(:string) ? $out_bufr.string : $out_bufr puts '#{time_stamp}' $stdout.print out " #the_command.gsub!(/$/, " # input!") begin @channel.send_data(the_command) @session.loop 2 do |session| # Return true, unless found output @@output !~ /^#{time_stamp}$/ && @@output !~ /___CLOSED___/ end rescue Exception => e puts "too slow! #{e.}" end end |
#session ⇒ Object
158 159 160 161 |
# File 'lib/xiki/ruby_console.rb', line 158 def session connect unless @channel @session end |