class Prez::Start

Private Class Methods

source_root() click to toggle source
# File lib/prez/start.rb, line 104
def source_root
  File.absolute_path File.expand_path("../../../templates", __FILE__)
end

Public Instance Methods

check_file!() click to toggle source
# File lib/prez/start.rb, line 13
def check_file!
  if File.exists? prez_name
    @filename = prez_name
  elsif File.exists? "#{prez_name}.prez"
    @filename = "#{prez_name}.prez"
  else
    raise Prez::Error.new("Missing prez file '#{prez_name}'")
  end

  if filename =~ /\.html$/
    raise Prez::Error.new("Prez file cannot be an html file: '#{prez_name}'")
  end
end
generate_html() click to toggle source
# File lib/prez/start.rb, line 27
def generate_html
  return if options[:server]
  @html = build_html filename
end
start_server() click to toggle source
# File lib/prez/start.rb, line 32
def start_server
  say "Starting server..."
  @server = WEBrick::HTTPServer.new Port: 0, Logger: Prez::Start::NoopLog.new, AccessLog: []
  port = @server.config[:Port]

  if options[:server]
    ["INT", "TERM"].each do |signal|
      trap signal do
        stop_server
      end
    end
  end

  @server.mount_proc "/" do |request, response|
    if request.path == "/"
      response.body = html

      unless options[:server]
        @server.stop
      end
    else
      say "Ignoring request: #{request.path}"
      response.status = 404
    end
  end

  begin
    Launchy.open "http://localhost:#{port}/"
    @server.start
  ensure
    unless options[:server]
      stop_server
    end
  end
end

Private Instance Methods

filename() click to toggle source
# File lib/prez/start.rb, line 99
def filename
  @filename
end
html() click to toggle source
# File lib/prez/start.rb, line 91
def html
  if options[:server]
    build_html filename
  else
    @html
  end
end
only_existing_prez() click to toggle source
# File lib/prez/start.rb, line 74
def only_existing_prez
  results = Dir.glob "*.prez"

  if results.empty?
    raise Prez::Error.new("No .prez files found!")
  elsif results.size > 1
    raise Prez::Error.new("More than one .prez file found!\nPlease specify which one you want to start.")
  end

  results.first
end
prez_name() click to toggle source
# File lib/prez/start.rb, line 70
def prez_name
  @prez_name = name || only_existing_prez
end
stop_server() click to toggle source
# File lib/prez/start.rb, line 86
def stop_server
  say "Shutting down server..."
  @server.shutdown
end