Class: Prez::Start

Inherits:
Thor::Group
  • Object
show all
Includes:
Builder, Thor::Actions
Defined in:
lib/prez/start.rb

Defined Under Namespace

Classes: NoopLog

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



104
105
106
# File 'lib/prez/start.rb', line 104

def source_root
  File.absolute_path File.expand_path("../../../templates", __FILE__)
end

Instance Method Details

#check_file!Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# 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_htmlObject



27
28
29
30
# File 'lib/prez/start.rb', line 27

def generate_html
  return if options[:server]
  @html = build_html filename
end

#start_serverObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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