Method: Inspec::Shell#configure_pry

Defined in:
lib/inspec/shell.rb

#configure_pryObject

rubocop:disable Metrics/AbcSize



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/inspec/shell.rb', line 33

def configure_pry # rubocop:disable Metrics/AbcSize
  # Remove all hooks and checks
  Pry.hooks.clear_all
  that = self

  # Add the help command
  Pry::Commands.block_command 'help', 'Show examples' do |resource|
    that.help(resource)
  end

  # configure pry shell prompt
  Pry.config.prompt_name = 'inspec'
  Pry.prompt = [proc { "#{readline_ignore("\e[0;32m")}#{Pry.config.prompt_name}> #{readline_ignore("\e[0m")}" }]

  # Add a help menu as the default intro
  Pry.hooks.add_hook(:before_session, 'inspec_intro') do
    intro
  end

  # Track the rules currently registered and what their merge count is.
  Pry.hooks.add_hook(:before_eval, 'inspec_before_eval') do
    @current_eval_rules = @ctx.rules.each_with_object({}) do |(rule_id, rule), h|
      h[rule_id] = Inspec::Rule.merge_count(rule)
    end
    @runner.reset
  end

  # After pry has evaluated a commanding within the binding context of a
  # test file, register all the rules it discovered.
  Pry.hooks.add_hook(:after_eval, 'inspec_after_eval') do
    @current_eval_new_tests =
      @runner.register_rules(@ctx) do |rule_id, rule|
        @current_eval_rules[rule_id] != Inspec::Rule.merge_count(rule)
      end
    @runner.run if @current_eval_new_tests
  end

  # Don't print out control class inspection when the user uses DSL methods.
  # Instead produce a result of evaluating their control.
  Pry.config.print = proc do |_output_, value, pry|
    next if @current_eval_new_tests
    pry.pager.open do |pager|
      pager.print pry.config.output_prefix
      Pry::ColorPrinter.pp(value, pager, Pry::Terminal.width! - 1)
    end
  end
end