Class: Bridgetown::Commands::Console

Inherits:
Bridgetown::Command show all
Includes:
ConfigurationOverridable
Defined in:
bridgetown-core/lib/bridgetown-core/commands/console.rb

Instance Method Summary collapse

Methods included from ConfigurationOverridable

#configuration_with_overrides, include_options

Methods inherited from Bridgetown::Command

command_line, start, summary

Instance Method Details

#callObject

rubocop:disable Metrics



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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'bridgetown-core/lib/bridgetown-core/commands/console.rb', line 44

def call # rubocop:disable Metrics
  require "irb"
  new_history_behavior = false
  begin
    require "irb/ext/save-history"
  rescue LoadError
    # Code path for Ruby 3.3+
    new_history_behavior = true
  end
  require "amazing_print" unless options[:bypass_ap]

  Bridgetown.logger.adjust_verbosity(**options)

  Bridgetown.logger.info "Starting:", "Bridgetown v#{Bridgetown::VERSION.magenta} " \
                                      "(codename \"#{Bridgetown::CODE_NAME.yellow}\") " \
                                      "console…"
  Bridgetown.logger.info "Environment:", Bridgetown.environment.cyan

  config_options = configuration_with_overrides(options)

  if options[:server_config]
    require "bridgetown-core/rack/boot"
    Bridgetown::Rack.boot
    begin
      require "rack/test"
      IRB::ExtendCommandBundle.include ::Rack::Test::Methods
      ConsoleMethods.module_eval do
        def app = Roda.subclasses[0].app
      end
      @rack_test_installed = true
    rescue LoadError # rubocop:disable Lint/SuppressedException
    end
  else
    config_options.run_initializers! context: :console
  end
  site = Bridgetown::Site.new(config_options)

  ConsoleMethods.site_reset(site) unless options[:blank]

  IRB::ExtendCommandBundle.include ConsoleMethods
  IRB.setup(nil)
  workspace = IRB::WorkSpace.new
  workspace.main.define_singleton_method(:site) { Bridgetown::Current.site }
  workspace.main.define_singleton_method(:collections) { site.collections }
  workspace.main.define_singleton_method(:helpers) do
    Bridgetown::TemplateView::Helpers.new
  end
  irb = IRB::Irb.new(workspace)
  IRB.conf[:IRB_RC]&.call(irb.context)
  IRB.conf[:MAIN_CONTEXT] = irb.context
  irb.context.io.load_history if new_history_behavior
  Bridgetown.logger.info "Console:", "Your site is now available as #{"site".cyan}."
  if options[:server_config]
    Bridgetown.logger.info "",
                           "Your Roda app is available as #{Roda.subclasses[0].to_s.cyan}."
    if @rack_test_installed
      Bridgetown.logger.info "", "You can use #{"Rack::Test".magenta} methods like #{"get".cyan}, #{"post".cyan}, and #{"last_response".cyan} to inspect" # rubocop:disable Layout/LineLength
      Bridgetown.logger.info "", "  static & dynamic routes in your application."
    end
  end
  Bridgetown.logger.info "",
                         "You can also access #{"collections".cyan} or perform a " \
                         "#{"reload!".cyan}"

  trap("SIGINT") do
    irb.signal_handle
  end

  begin
    catch(:IRB_EXIT) do
      unless options[:bypass_ap]
        AmazingPrint.defaults = {
          indent: 2,
        }
        AmazingPrint.irb!
      end
      irb.eval_input
    end
  ensure
    IRB.conf[:AT_EXIT].each(&:call)
    irb.context.io.save_history if new_history_behavior
  end
end