Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

first commit

  • Loading branch information...
commit 40ec6b8e39923028e3e0f58985b6a4b71513ac4c 0 parents
@jasonLaster authored
1  .gitignore
@@ -0,0 +1 @@
+*.gem
21 LICENSE
@@ -0,0 +1,21 @@
+pry-remote - Copyright (c) 2012 - Mon ouïe
+
+This software is provided 'as-is', without any express or
+implied warranty. In no event will the authors be held
+liable for any damages arising from the use of this software.
+
+Permission is granted to anyone to use this software for any purpose,
+including commercial applications, and to alter it and redistribute
+it freely, subject to the following restrictions:
+
+1. The origin of this software must not be misrepresented;
+ you must not claim that you wrote the original software.
+ If you use this software in a product, an acknowledgment
+ in the product documentation would be appreciated but
+ is not required.
+
+2. Altered source versions must be plainly marked as such,
+ and must not be misrepresented as being the original software.
+
+3. This notice may not be removed or altered from any
+ source distribution.
13 README.md
@@ -0,0 +1,13 @@
+# What is it?
+
+A way to talk to Pry from another application. This is achieved by using DRb to proxy Pry's input and output.
+
+# Usage
+
+ shared_io = DRbObject.new nil, 'druby://:9000'
+ result = shared_io.pry_eval input
+
+# Thanks
+
+This project is heavily influenced by Mon Ouie's Pry Remote.
+
4 bin/pry-bot
@@ -0,0 +1,4 @@
+#!/usr/bin/env ruby
+
+require 'pry-bot'
+PryBot::CLI.new.run
19 lib/client.rb
@@ -0,0 +1,19 @@
+module PryBot
+ # A client is used to retrieve information from the client program.
+ Client = Struct.new :input, :output, :thread, :stdout, :stderr do
+ # Waits until both an input and output are set
+ def wait
+ sleep 0.01 until input and output and thread
+ end
+
+ # Tells the client the session is terminated
+ def kill
+ thread.run
+ end
+
+ # @return [InputProxy] Proxy for the input
+ def input_proxy
+ InputProxy.new input
+ end
+ end
+end
20 lib/core-ext.rb
@@ -0,0 +1,20 @@
+
+module PryBot
+ DefaultHost = "localhost"
+ DefaultPort = 9876
+end
+
+
+class Object
+ # Starts a remote Pry session
+ #
+ # @param [String] host Host of the server
+ # @param [Integer] port Port of the server
+ def remote_pry(host = PryBot::DefaultHost, port = PryBot::DefaultPort)
+ PryBot::Server.run(self)
+ end
+
+ # a handy alias as many people may think the method is named after the gem
+ # (pry-remote)
+ alias pry_remote remote_pry
+end
21 lib/input-proxy.rb
@@ -0,0 +1,21 @@
+module PryBot
+ # A class to represent an input object created from DRb. This is used because
+ # Pry checks for arity to know if a prompt should be passed to the object.
+ #
+ # @attr [#readline] input Object to proxy
+ InputProxy = Struct.new :input do
+ # Reads a line from the input
+ def readline(prompt)
+ case readline_arity
+ when 1 then input.readline(prompt)
+ else input.readline
+ end
+ end
+
+ def readline_arity
+ input.method_missing(:method, :readline).arity
+ rescue NameError
+ 0
+ end
+ end
+end
66 lib/io-string-proxy.rb
@@ -0,0 +1,66 @@
+module PryBot
+ # Class used to wrap modules so that they can be sent through DRb. This will
+ # mostly be used for Readline.
+ class StringIOProxy
+ include DRb::DRbUndumped
+
+ attr_accessor :obj
+ def initialize(obj)
+ @stdout = $stdout
+ @stdin = $stdin
+ @obj = obj
+ end
+
+ def completion_proc=(val)
+ if @obj.respond_to? :completion_proc=
+ @obj.completion_proc = val
+ end
+ end
+
+ attr_reader :completion_proc
+
+ def readline(prompt="")
+ begin
+ @obj.readline()
+ rescue EOFError
+ ""
+ end
+ end
+
+ def string()
+ @obj.string
+ end
+
+ def puts(*lines)
+ lines.map! {|line| line.end_with?("\n") ? line : line+"\n" }
+ # @stdout.puts "STDOUT: #{lines.inspect}"
+ lines.each { |l| self << l}
+ end
+
+ def print(*objs)
+ @obj.puts(*objs)
+ end
+
+ def write(data)
+ @obj.write data
+ end
+
+ def <<(data)
+ @obj.string << data
+ self
+ end
+
+ def tty?
+ false
+ end
+
+ def readlines
+ pos = @obj.pos
+ string = @obj.string[pos..-1]
+ @obj.pos = @obj.size
+ return string
+ end
+
+ end
+
+end
13 lib/pry-bot.rb
@@ -0,0 +1,13 @@
+require 'pry'
+require 'drb'
+require 'readline'
+require 'open3'
+
+require 'client'
+require 'core-ext'
+require 'shared-io'
+require 'input-proxy'
+require 'io-string-proxy'
+require 'server'
+require 'system'
+
20 lib/server.rb
@@ -0,0 +1,20 @@
+require 'pry-bot'
+require 'drb'
+
+module PryBot
+ class Server
+ def self.run(object)
+ shared_io = PryBot::SharedIO.new
+ DRb.start_service 'druby://:9000', shared_io
+
+ # Startup PRY
+ Pry.color = false
+ Pry.pager = false
+ pry = Pry.start(object, :input => shared_io.input_proxy, :output => shared_io.output_proxy)
+
+ # Kill PRY
+ DRb.thread.join
+ trap("INT") { DRb.stop_service }
+ end
+ end
+end
19 lib/shared-io.rb
@@ -0,0 +1,19 @@
+module PryBot
+ class SharedIO
+ attr_accessor :input_proxy, :output_proxy
+
+ def initialize
+ @input = StringIO.new
+ @output = StringIO.new
+
+ @input_proxy = PryBot::StringIOProxy.new(@input)
+ @output_proxy = PryBot::StringIOProxy.new(@output)
+ end
+
+ def pry_eval(string)
+ @input_proxy << string
+ sleep 0.1
+ @output_proxy.readlines
+ end
+ end
+end
25 lib/system.rb
@@ -0,0 +1,25 @@
+module PryBot
+ # Ensure that system (shell command) output is redirected for remote session.
+ System = proc do |output, cmd, _|
+
+ status = nil
+ Open3.popen3 cmd do |stdin, stdout, stderr, wait_thr|
+ stdin.close # Send EOF to the process
+
+ until stdout.eof? and stderr.eof?
+ if res = IO.select([stdout, stderr])
+ res[0].each do |io|
+ next if io.eof?
+ output.write io.read_nonblock(1024)
+ end
+ end
+ end
+
+ status = wait_thr.value
+ end
+
+ unless status.success?
+ output.puts "Error while executing command: #{cmd}"
+ end
+ end
+end
25 pry-bot.gemspec
@@ -0,0 +1,25 @@
+#!/usr/bin/env ruby
+# -*- coding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = "pry-bot"
+
+ s.version = "0.1"
+
+ s.summary = "Talk to Pry programatically"
+ s.description = "Connect to Pry remotely using DRb"
+ s.homepage = "http://github.com/JasonLaster/pry-bot"
+
+ s.email = "jason.laster.11@gmail.com"
+ s.authors = ["Jason Laster"]
+
+ s.files |= Dir["lib/**/*.rb"]
+ s.files |= Dir["*.md"]
+ s.files << "LICENSE"
+
+ s.require_paths = ["lib"]
+
+ s.add_dependency "pry", "~> 0.9"
+
+ s.executables = ["pry-bot"]
+end
Please sign in to comment.
Something went wrong with that request. Please try again.