Sha256: aa4790dfe8e8da5c15e5c4c4482aa99ac202ff465c0707c5df8401828388c541

Contents?: true

Size: 1.81 KB

Versions: 2

Compression:

Stored size: 1.81 KB

Contents

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pico-0.1.0 lib/pico/pry_context.rb
pico-0.0.1 lib/pico/pry_context.rb