Class: Bridgetown::Commands::Console

Inherits:
Thor::Group
  • Object
show all
Extended by:
Summarizable
Includes:
ConfigurationOverridable
Defined in:
bridgetown-core/lib/bridgetown-core/commands/console.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Summarizable

summary

Methods included from ConfigurationOverridable

#configuration_with_overrides, included

Class Method Details



39
40
41
# File 'bridgetown-core/lib/bridgetown-core/commands/console.rb', line 39

def self.banner
  "bridgetown console [options]"
end

Instance Method Details

#consoleObject

rubocop:disable Metrics



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
127
128
# File 'bridgetown-core/lib/bridgetown-core/commands/console.rb', line 63

def console # 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 "puma"
    require "bridgetown-core/rack/boot"
    Bridgetown::Rack.boot
  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::RubyTemplateView::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}"
  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