Class: Rackula::Command::Generate

Inherits:
Samovar::Command
  • Object
show all
Defined in:
lib/rackula/command/generate.rb

Overview

Server setup commands.

Instance Method Summary collapse

Instance Method Details

#callObject



111
112
113
114
115
116
117
118
119
120
# File 'lib/rackula/command/generate.rb', line 111

def call
  Variant.force!('static')
  
  endpoint = Async::IO::Endpoint.tcp("localhost", 0, reuse_port: true)
  
  # We bind to a socket to generate a temporary port:
  socket = Sync{endpoint.each.first.bind}
  
  run(socket.local_address, Pathname.new(parent.root))
end

#copy_and_fetch(port, root) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rackula/command/generate.rb', line 49

def copy_and_fetch(port, root)
  output_path = root + @options[:output_path]
  
  if output_path.exist?
    if @options[:force]
      # Delete any existing stuff:
      FileUtils.rm_rf(output_path.to_s)
    else
      # puts "Failing due to error..."
      raise Samovar::Failure.new("Output path already exists!")
    end
  end
  
  # Create output directory
  Dir.mkdir(output_path)
  
  # Copy all public assets:
  asset_pattern = root + @options[:public] + '*'
  Dir.glob(asset_pattern.to_s).each do |path|
    FileUtils.cp_r(path, File.join(output_path, "."))
  end
  
  # Generate HTML pages:
  unless system("wget", "--mirror", "--recursive", "--continue", "--convert-links", "--adjust-extension", "--no-host-directories", "--directory-prefix", output_path.to_s, "http://localhost:#{port}")
    raise "The wget command failed!"
  end
end

#run(address, root) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rackula/command/generate.rb', line 99

def run(address, root)
  endpoint = Async::HTTP::Endpoint.parse("http://localhost", port: address.ip_port, reuse_port: true)
  
  Console.logger.info(self) {"Setting up container to serve site on port #{address.ip_port}..."}
  container = serve(endpoint, root)
  
  # puts "Copy and fetch site to static..."
  copy_and_fetch(address.ip_port, root)
ensure
  container&.stop
end

#serve(endpoint, root) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rackula/command/generate.rb', line 77

def serve(endpoint, root)
  container = Async::Container.new
  
  config_path = root + @options[:config]
  
  container.run do |instance|
    Async do
      rack_app, _ = Rack::Builder.parse_file(config_path.to_s)
      app = ::Falcon::Server.middleware(rack_app, verbose: @options[:verbose])
      server = ::Falcon::Server.new(app, endpoint)
      
      instance.ready!
      
      server.run
    end
  end
  
  container.wait_until_ready
  
  return container
end