Skip to content

Commit 2fdde80

Browse files
Generalize static site generation.
Assisted-By: devx/b3414b50-d642-461b-95a8-8f773c4d087b
1 parent 8e4caea commit 2fdde80

1 file changed

Lines changed: 23 additions & 11 deletions

File tree

bake/utopia/static.rb

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,26 @@
33
# Released under the MIT License.
44
# Copyright, 2017-2025, by Samuel Williams.
55

6-
def generate(output_path: "static")
6+
# Generate a static copy of the application.
7+
# @parameter output_path [String] The output path for the generated site.
8+
# @parameter application_path [String] The application configuration path.
9+
# @parameter public_path [String] The public assets path.
10+
# @parameter force [Boolean] Remove the output directory before generating the site.
11+
def generate(output_path: "static", application_path: "config/application.rb", public_path: "public", force: true)
712
require "falcon/server"
8-
require "async/io"
913
require "async/http/endpoint"
1014
require "async/container"
15+
require "fileutils"
1116
require "utopia/application"
1217

13-
application_path = File.join(Dir.pwd, Utopia::Application::PATH)
18+
application_path = File.expand_path(application_path, Dir.pwd)
19+
public_path = File.expand_path(public_path, Dir.pwd)
1420
container_class = Async::Container::Threaded
1521
server_port = 9090
1622

1723
app = Utopia::Application.load(application_path)
1824

19-
container = container_class.run(count: 2) do
25+
container = container_class.run(count: 1) do
2026
Async do
2127
server = Falcon::Server.new(
2228
Falcon::Server.protocol_middleware(app),
@@ -29,17 +35,23 @@ def generate(output_path: "static")
2935

3036
output_path = File.expand_path(output_path, Dir.pwd)
3137

32-
# Delete any existing stuff:
33-
FileUtils.rm_rf(output_path)
38+
# Delete existing output when explicitly requested:
39+
if force
40+
FileUtils.rm_rf(output_path)
41+
end
3442

3543
# Copy all public assets:
3644
FileUtils::Verbose.mkpath(output_path)
37-
Dir.glob(File.join(Dir.pwd, "public/*")) do |path|
45+
Dir.glob(File.join(public_path, "*")) do |path|
3846
FileUtils::Verbose.cp_r(path, output_path)
3947
end
4048

41-
# Generate HTML pages:
42-
system("wget", "--mirror", "--recursive", "--continue", "--convert-links", "--adjust-extension", "--no-host-directories", "--directory-prefix", output_path.to_s, "http://localhost:#{server_port}")
43-
44-
container.stop
49+
begin
50+
# Generate HTML pages:
51+
unless system("wget", "--mirror", "--recursive", "--continue", "--convert-links", "--adjust-extension", "--no-host-directories", "--directory-prefix", output_path.to_s, "http://localhost:#{server_port}")
52+
raise "Static site generation failed!"
53+
end
54+
ensure
55+
container.stop
56+
end
4557
end

0 commit comments

Comments
 (0)