Class: QueryPackwerk::Console

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/query_packwerk/console.rb

Overview

Console for QueryPackwerk

Class Method Summary collapse

Class Method Details

.start(directory = Dir.pwd) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
# File 'lib/query_packwerk/console.rb', line 13

def self.start(directory = Dir.pwd)
  puts "Loading packwerk context in #{directory}..."

  Dir.chdir(directory) do
    # Start IRB with current context
    # https://github.com/cucumber/aruba/blob/main/lib/aruba/console.rb
    ARGV.clear

    require 'irb'
    IRB.setup nil

    IRB.conf[:IRB_NAME] = 'query_packwerk'

    IRB.conf[:PROMPT] = {}
    IRB.conf[:PROMPT][:QUERY_PACKWERK] = {
      PROMPT_I: '%N:%03n:%i> ',
      PROMPT_S: '%N:%03n:%i%l ',
      PROMPT_C: '%N:%03n:%i* ',
      RETURN: "# => %s\n"
    }
    IRB.conf[:PROMPT_MODE] = :QUERY_PACKWERK

    IRB.conf[:RC] = false

    require 'irb/completion'
    IRB.conf[:READLINE] = true
    IRB.conf[:SAVE_HISTORY] = 1000
    IRB.conf[:HISTORY_FILE] = "#{directory}/.query_packwerk_history"

    context = Class.new do
      include QueryPackwerk
      include QueryPackwerk::ConsoleHelpers
      extend T::Sig
    end

    context_instance = context.new
    irb = IRB::Irb.new(IRB::WorkSpace.new(context_instance))
    IRB.conf[:MAIN_CONTEXT] = irb.context

    context_instance.welcome

    trap('SIGINT') do
      irb.signal_handle
    end

    catch(:IRB_EXIT) do
      irb.eval_input
    end
    Kernel.exit
  end
end