Sha256: 576195bff98b6240ab3b06f86f727ec6e4c1df26f8b25107289b08768ee97795

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

require "irb"

module Bridgetown
  module Commands
    class Console < Command
      class << self
        def init_with_program(prog)
          prog.command(:console) do |c|
            c.syntax "console"
            c.description "Invoke an IRB console with the site loaded"
            c.alias :c

            add_build_options(c)

            c.action do |_, options|
              Bridgetown::Commands::Console.process(options)
            end
          end
        end

        # TODO: is there a way to add a unit test for this command?
        # rubocop:disable Style/GlobalVars, Metrics/AbcSize, Metrics/MethodLength
        def process(options)
          Bridgetown.logger.info "Starting:", "Bridgetown v#{Bridgetown::VERSION.magenta}" \
                                      " (codename \"#{Bridgetown::CODE_NAME.yellow}\")" \
                                      " consoleā€¦"
          Bridgetown.logger.info "Environment:", Bridgetown.environment.cyan
          site = Bridgetown::Site.new(configuration_from_options(options))
          site.reset
          site.read
          site.generate

          $BRIDGETOWN_SITE = site
          IRB.setup(nil)
          workspace = IRB::WorkSpace.new
          irb = IRB::Irb.new(workspace)
          IRB.conf[:MAIN_CONTEXT] = irb.context
          eval("site = $BRIDGETOWN_SITE", workspace.binding, __FILE__, __LINE__)
          Bridgetown.logger.info "Console:", "Now loaded as " + "site".cyan + " variable."

          trap("SIGINT") do
            irb.signal_handle
          end

          begin
            catch(:IRB_EXIT) do
              irb.eval_input
            end
          end
        end
        # rubocop:enable Style/GlobalVars, Metrics/AbcSize, Metrics/MethodLength
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bridgetown-core-0.14.1 lib/bridgetown-core/commands/console.rb
bridgetown-core-0.14.0 lib/bridgetown-core/commands/console.rb