require 'pry' require "pico/test_runner" module Pico module PryContext extend self attr :commands_defined def start! if Pico.application.booted? raise Error, "cannot start pry context if application has booted" end define_commands unless commands_defined Pico.boot! Pico.application.mod.pry end def define_commands define_reload_command define_rake_command define_tmux_command if ENV['TMUX'] @commands_defined = true end def define_reload_command Pry::Commands.block_command "reload!", "Reload #{Pico.application.name}" do puts "Reloading #{Pico.application.name}..." Pico.application.reload! _pry_.binding_stack.push Pico.application.mod.__binding__ _pry_.binding_stack.shift until _pry_.binding_stack.size == 1 end end def define_rake_command Pry::Commands.create_command "rake", keep_retval: true do description "Run the test suite" def process build_passed = Pico::TestRunner.run! operator = args.shift command = args.shift evaluate_hook(build_passed, operator, command, args) if operator build_passed end def evaluate_hook(build_passed, operator, command, args) if operator == '&&' run(command, args) if build_passed elsif operator == '||' run(command, args) unless build_passed else raise ArgumentError, "Must supply either '&&' or '||' operators, followed by a pry command" end end end def define_tmux_command Pry::Commands.create_command "tmux" do description "Run a tmux command" def process system 'tmux', *args end end end end end end