Class: Bridgetown::Commands::Start

Inherits:
Thor::Group
  • Object
show all
Extended by:
BuildOptions, Summarizable
Includes:
ConfigurationOverridable, Utils::PidTracker
Defined in:
bridgetown-core/lib/bridgetown-core/commands/start.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BuildOptions

extended

Methods included from Summarizable

summary

Methods included from Utils::PidTracker

#add_pid, #create_pid_dir, #read_pidfile, #remove_pidfile

Methods included from ConfigurationOverridable

#configuration_with_overrides, included

Class Method Details



59
60
61
# File 'bridgetown-core/lib/bridgetown-core/commands/start.rb', line 59

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

Instance Method Details

#startObject

rubocop:todo Metrics/PerceivedComplexity



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
# File 'bridgetown-core/lib/bridgetown-core/commands/start.rb', line 64

def start # rubocop:todo Metrics/PerceivedComplexity
  Bridgetown.logger.writer.enable_prefix
  Bridgetown::Commands::Build.print_startup_message
  sleep 0.25

  options = Thor::CoreExt::HashWithIndifferentAccess.new(self.options)
  options[:start_command] = true

  # Load Bridgetown configuration into thread memory
  bt_options = configuration_with_overrides(options)

  # Set a local site URL in the config if one is not available
  if Bridgetown.env.development? && !options["url"]
    port = ENV["BRIDGETOWN_PORT"] || bt_options.port
    bt_options.url = "http://#{bt_options.bind}:#{port}"
  end

  server_uri = URI(bt_options.url)
  Bridgetown::Server.new({
    Host: server_uri.host,
    Port: server_uri.port,
    config: "config.ru",
  }).tap do |server|
    if server.serveable?
      create_pid_dir

      bt_options.skip_live_reload = !server.using_puma?

      build_args = ["-w"] + ARGV.reject { |arg| arg == "start" }
      build_pid = Process.fork { Bridgetown::Commands::Build.start(build_args) }
      add_pid(build_pid, file: :bridgetown)

      after_stop_callback = -> {
        say "Stopping Bridgetown server..."
        Bridgetown::Hooks.trigger :site, :server_shutdown
        Process.kill "SIGINT", build_pid
        remove_pidfile :bridgetown

        # Shut down the frontend bundler etc. if they're running
        unless Bridgetown.env.production? || bt_options[:skip_frontend]
          Bridgetown::Utils::Aux.kill_processes
        end
      }

      Bridgetown.logger.info ""
      Bridgetown.logger.info "Booting #{server.name} at:", server_uri.to_s.magenta
      Bridgetown.logger.info ""

      server.start(after_stop_callback)
    else
      say "Unable to find a Rack server."
    end
  end
end