Class: Brandish::Application::ServeCommand Private

Inherits:
Object
  • Object
show all
Includes:
Commander::Methods
Defined in:
lib/brandish/application/serve_command.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

The serve command. This builds, and then serves, an existing Brandish project. This watches the source. If it detects a change, it rebuilds.

Constant Summary collapse

COMMAND_DESCRIPTION =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The description for the serve command.

Returns:

  • (::String)
"Builds, and serves, an existing Brandish project."
DEFAULTS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

The default options for the serve command.

Returns:

  • ({::Symbol => ::Object})
{ only: :all, show_build: false }.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(application, options) ⇒ ServeCommand

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize the serve command.



52
53
54
55
56
# File 'lib/brandish/application/serve_command.rb', line 52

def initialize(application, options)
  @application = application
  @options = DEFAULTS.merge(options)
  @port = @options.fetch(:port, (ENV["PORT"] || "3000").to_i)
end

Class Method Details

.call(application, options) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Performs the serve command. This initializes the command, and calls #call.

Parameters:

  • application (Application)

    The application.

  • options ({::Symbol => ::Object})

    The options for the command.



39
40
41
42
# File 'lib/brandish/application/serve_command.rb', line 39

def self.call(application, options)
  puts "Running with options #{options.inspect}..."
  new(application, options).call
end

.define(application, command) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Defines the command on the given application. This sets the important data information for the command, for use for the help output.

Parameters:

  • application (Application)
  • command (Commander::Command)


22
23
24
25
26
27
28
29
30
# File 'lib/brandish/application/serve_command.rb', line 22

def self.define(application, command)
  command.syntax = "branish serve"
  command.description = COMMAND_DESCRIPTION
  command.option "-p", "--port PORT", ::Integer, "The port to listen on"
  command.option "-o", "--only NAMES", [::String], "The forms to build"
  command.option "--verbose", "Whether or not to be verbose in the output"

  command.action { |_, o| call(application, o.__hash__) }
end

Instance Method Details

#callvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Performs the serve command. It first loads the configuration file, then calls #start_webserver, followed by #start_buildserver. Once both servers are setup, it calls #wait_on_servers.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/brandish/application/serve_command.rb', line 63

def call
  @configuration = @application.load_configuration_file
  say "=> Beginning serve..."
  start_webserver
  start_buildserver
  color "\r=> Ready and waiting! ", :erase_line, :green
  wait_on_servers
rescue StandardError, ScriptError => e
  # Whenever we receive a general error, which only occurs while setup,
  # we complain, and pass up the exception.
  say_error "\n!> Received exception: #{e.class}: #{e.message}"
  e.backtrace.each { |l| say_warning "\t-> in #{l}" } if @options[:trace]
  fail
rescue SignalException, NoMemoryError, SystemExit, SystemStackError => e
  # Whenever we receive a signal, or an unrecoverable error, we kill
  # the servers and complain.  These exceptions occur on the main thread,
  # and so we handle them here.
  say_warning "\n!> Received exception: #{e.class}: #{e.message}"
  say_ok "\n-> Received termination, shutting down..."
  kill_webserver
  kill_buildserver
end