Class: PryRemoteReloaded::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/pry-remote-reloaded.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, host = DefaultHost, port = DefaultPort, options = {}) ⇒ Server

Returns a new instance of Server.



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/pry-remote-reloaded.rb', line 147

def initialize(object, host = DefaultHost, port = DefaultPort, options = {})
  @host    = host
  @port    = port

  @object  = object
  @options = options

  @client = PryRemoteReloaded::Client.new
  DRb.start_service uri, @client
rescue Errno::EADDRINUSE => e
  puts "[pry-remote] Port already in use: #{e.message}"
end

Instance Attribute Details

#clientPryServer::Client (readonly)

Returns Client connecting to the pry-remote server.

Returns:

  • (PryServer::Client)

    Client connecting to the pry-remote server



250
251
252
# File 'lib/pry-remote-reloaded.rb', line 250

def client
  @client
end

#hostString (readonly)

Returns Host of the server.

Returns:

  • (String)

    Host of the server



253
254
255
# File 'lib/pry-remote-reloaded.rb', line 253

def host
  @host
end

#objectObject (readonly)

Returns Object to enter into.

Returns:

  • Object to enter into



247
248
249
# File 'lib/pry-remote-reloaded.rb', line 247

def object
  @object
end

#portInteger (readonly)

Returns Port of the server.

Returns:

  • (Integer)

    Port of the server



256
257
258
# File 'lib/pry-remote-reloaded.rb', line 256

def port
  @port
end

Class Method Details

.run(object, host = DefaultHost, port = DefaultPort, options = {}) ⇒ Object



143
144
145
# File 'lib/pry-remote-reloaded.rb', line 143

def self.run(object, host = DefaultHost, port = DefaultPort, options = {})
  new(object, host, port, options).run
end

Instance Method Details

#capture_outputObject

Captures $stdout and $stderr if so requested by the client.



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/pry-remote-reloaded.rb', line 203

def capture_output
  @old_stdout, $stdout = if @client.stdout
                           [$stdout, @client.stdout]
                         else
                           [$stdout, $stdout]
                         end

  @old_stderr, $stderr = if @client.stderr
                           [$stderr, @client.stderr]
                         else
                           [$stderr, $stderr]
                         end
end

#editor_procObject



223
224
225
226
227
# File 'lib/pry-remote-reloaded.rb', line 223

def editor_proc
  proc do |file, line|
    File.write(file, @client.editor.call(File.read(file), line))
  end
end

#runObject

Actually runs pry-remote



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/pry-remote-reloaded.rb', line 230

def run
  puts "[pry-remote] Waiting for client on #{uri}"
  @client.wait

  puts "[pry-remote] Client received, starting remote session"
  setup

  Pry.start(@object, @options.merge(:input => client.input_proxy,
                                    :output => client.output,
                                    :hooks => @hooks))
rescue => e
  puts "[pry-remote] Error: #{e.message}"
ensure
  teardown
end

#setupObject

Code that has to be called for Pry-remote to work properly



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/pry-remote-reloaded.rb', line 161

def setup
  @hooks = Pry::Hooks.default

  @hooks.add_hook :before_eval, :pry_remote_capture do
    capture_output
  end

  @hooks.add_hook :after_eval, :pry_remote_uncapture do
    uncapture_output
  end

  # Before Pry starts, save the pager config.
  # We want to disable this because the pager won't do anything useful in
  # this case (it will run on the server).
  Pry.config.pager, @old_pager = false, Pry.config.pager

  # As above, but for system config
  Pry.config.system, @old_system = PryRemoteReloaded::System, Pry.config.system

  Pry.config.editor, @old_editor = editor_proc, Pry.config.editor
end

#teardownObject

Code that has to be called after setup to return to the initial state



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/pry-remote-reloaded.rb', line 184

def teardown
  # Reset config
  Pry.config.editor = @old_editor
  Pry.config.pager  = @old_pager
  Pry.config.system = @old_system

  puts "[pry-remote] Remote session terminated"

  begin
    @client.kill
  rescue DRb::DRbConnError, Errno::ECONNREFUSED
    puts "[pry-remote] Continuing to stop service"
  ensure
    puts "[pry-remote] Ensure stop service"
    DRb.stop_service
  end
end

#uncapture_outputObject

Resets $stdout and $stderr to their previous values.



218
219
220
221
# File 'lib/pry-remote-reloaded.rb', line 218

def uncapture_output
  $stdout = @old_stdout
  $stderr = @old_stderr
end

#uriString

Returns URI for DRb.

Returns:

  • (String)

    URI for DRb



259
260
261
# File 'lib/pry-remote-reloaded.rb', line 259

def uri
  "druby://#{host}:#{port}"
end