Class: Jekyll::Commands::Serve

Inherits:
Jekyll::Command show all
Defined in:
lib/jekyll/commands/serve.rb,
lib/jekyll/commands/serve/servlet.rb,
lib/jekyll/commands/serve/websockets.rb,
lib/jekyll/commands/serve/live_reload_reactor.rb

Defined Under Namespace

Classes: BodyProcessor, HttpAwareConnection, LiveReloadReactor, Servlet, SkipAnalyzer

Constant Summary collapse

COMMAND_OPTIONS =
{
  "ssl_cert"             => ["--ssl-cert [CERT]", "X.509 (SSL) certificate."],
  "host"                 => ["host", "-H", "--host [HOST]", "Host to bind to"],
  "open_url"             => ["-o", "--open-url", "Launch your site in a browser"],
  "detach"               => ["-B", "--detach",
    "Run the server in the background",],
  "ssl_key"              => ["--ssl-key [KEY]", "X.509 (SSL) Private Key."],
  "port"                 => ["-P", "--port [PORT]", "Port to listen on"],
  "show_dir_listing"     => ["--show-dir-listing",
    "Show a directory listing instead of loading your index file.",],
  "skip_initial_build"   => ["skip_initial_build", "--skip-initial-build",
    "Skips the initial site build which occurs before the server is started.",],
  "livereload"           => ["-l", "--livereload",
    "Use LiveReload to automatically refresh browsers",],
  "livereload_ignore"    => ["--livereload-ignore ignore GLOB1[,GLOB2[,...]]",
    Array,
    "Files for LiveReload to ignore. Remember to quote the values so your shell "\
    "won't expand them",],
  "livereload_min_delay" => ["--livereload-min-delay [SECONDS]",
    "Minimum reload delay",],
  "livereload_max_delay" => ["--livereload-max-delay [SECONDS]",
    "Maximum reload delay",],
  "livereload_port"      => ["--livereload-port [PORT]", Integer,
    "Port for LiveReload to listen on",],
}.freeze
DIRECTORY_INDEX =
%w(
  index.htm
  index.html
  index.rhtml
  index.cgi
  index.xml
  index.json
).freeze
LIVERELOAD_PORT =
35_729
LIVERELOAD_DIR =
File.join(__dir__, "serve", "livereload_assets")

Class Attribute Summary collapse

Class Method Summary collapse

Methods inherited from Jekyll::Command

add_build_options, configuration_from_options, inherited, process_site, subclasses

Class Attribute Details

.mutexObject (readonly)

Returns the value of attribute mutex.



55
56
57
# File 'lib/jekyll/commands/serve.rb', line 55

def mutex
  @mutex
end

.run_condObject (readonly)

Returns the value of attribute run_cond.



55
56
57
# File 'lib/jekyll/commands/serve.rb', line 55

def run_cond
  @run_cond
end

.runningObject (readonly) Also known as: running?

Returns the value of attribute running.



55
56
57
# File 'lib/jekyll/commands/serve.rb', line 55

def running
  @running
end

Class Method Details

.init_with_program(prog) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/jekyll/commands/serve.rb', line 58

def init_with_program(prog)
  prog.command(:serve) do |cmd|
    cmd.description "Serve your site locally"
    cmd.syntax "serve [options]"
    cmd.alias :server
    cmd.alias :s

    add_build_options(cmd)
    COMMAND_OPTIONS.each do |key, val|
      cmd.option key, *val
    end

    cmd.action do |_, opts|
      opts["livereload_port"] ||= LIVERELOAD_PORT
      opts["serving"] = true
      opts["watch"]   = true unless opts.key?("watch")

      start(opts)
    end
  end
end

.process(opts) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/jekyll/commands/serve.rb', line 98

def process(opts)
  opts = configuration_from_options(opts)
  destination = opts["destination"]
  register_reload_hooks(opts) if opts["livereload"]
  setup(destination)

  start_up_webrick(opts, destination)
end

.shutdownObject



107
108
109
# File 'lib/jekyll/commands/serve.rb', line 107

def shutdown
  @server.shutdown if running?
end

.start(opts) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/jekyll/commands/serve.rb', line 82

def start(opts)
  # Set the reactor to nil so any old reactor will be GCed.
  # We can't unregister a hook so in testing when Serve.start is
  # called multiple times we don't want to inadvertently keep using
  # a reactor created by a previous test when our test might not
  @reload_reactor = nil

  config = configuration_from_options(opts)
  if Jekyll.env == "development"
    config["url"] = default_url(config)
  end
  [Build, Serve].each { |klass| klass.process(config) }
end