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



108
109
110
111
112
113
114
115
116
117
# File 'lib/rackula/command/generate.rb', line 108

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



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

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:
	system("wget", "--mirror", "--recursive", "--continue", "--convert-links", "--adjust-extension", "--no-host-directories", "--directory-prefix", output_path.to_s, "http://localhost:#{port}")
end

#run(address, root) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rackula/command/generate.rb', line 96

def run(address, root)
	endpoint = Async::HTTP::Endpoint.parse("http://localhost", port: address.ip_port, reuse_port: true)
	
	Async.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



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

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