Permalink
Browse files

kill-pry-rescue (for guard, etc)

  • Loading branch information...
rking authored and rking@sharpsaw.org committed Dec 26, 2012
1 parent dcdc273 commit eb49e14791ceb35c59fa9c911354835abd5c5eb0
Showing with 53 additions and 6 deletions.
  1. +37 −0 bin/kill-pry-rescue
  2. +16 −6 lib/pry-rescue.rb
View
@@ -0,0 +1,37 @@
+#!/usr/bin/env ruby
+
+def find_pry_rescuers extra_id = ''
+ pattern = 'pry-rescue @.*' + extra_id
+ pids = nil
+ IO.popen ['pgrep', '-f', pattern] do |io|
+ pids = io.readlines.map &:to_i
+ end
+ pids
+end
+
+if %w(-h --help').include? ARGV[0]
+ warn %{
+#$0 - Stops a pry-rescuer
+
+By default gets all of them, but if you provide an arg, it can filter by the
+directory name.
+
+Uses SIGINT by default, unless you supply the -9 option.
+
+Currently running pry-rescue pids: #{find_pry_rescuers.join ' '}}
+ exit 1
+end
+
+signal =
+ if ARGV.delete '-9'
+ :KILL
+ else
+ :INT
+ end
+
+pids = find_pry_rescuers(ARGV.join ' ')
+warn '(No processes found.)' if pids.size.zero?
+pids.each do |pid|
+ warn "Killing #{pid} with SIG#{signal}."
+ Process.kill signal, pid
+end
View
@@ -48,12 +48,14 @@ def enter_exception_context(raised)
exception, bindings = raised.last
bindings = without_duplicates(bindings)
- if defined?(PryStackExplorer)
- pry :call_stack => bindings,
- :hooks => pry_hooks(exception, raised),
- :initial_frame => initial_frame(bindings)
- else
- Pry.start bindings.first, :hooks => pry_hooks(exception, raised)
+ with_program_name "#$PROGRAM_NAME [in pry-rescue @ #{Dir.pwd}]" do
+ if defined?(PryStackExplorer)
+ pry :call_stack => bindings,
+ :hooks => pry_hooks(exception, raised),
+ :initial_frame => initial_frame(bindings)
+ else
+ Pry.start bindings.first, :hooks => pry_hooks(exception, raised)
+ end
end
ensure
@exception_context_depth -= 1
@@ -164,5 +166,13 @@ def pry_hooks(ex, raised)
hooks
end
+
+ def with_program_name name
+ before = $PROGRAM_NAME
+ $PROGRAM_NAME = name
+ yield
+ ensure
+ $PROGRAM_NAME = before
+ end
end
end

0 comments on commit eb49e14

Please sign in to comment.