Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/perron/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def initialize

@config.metadata = ActiveSupport::OrderedOptions.new
@config.metadata.title_separator = " — "

@config.before_build = nil
@config.after_build = nil
end

def input = Rails.root.join("app", "content")
Expand Down
26 changes: 26 additions & 0 deletions lib/perron/site/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def initialize
end

def build
run_hook(:before_build)

if Perron.configuration.mode.standalone?
puts "🧹 Cleaning previous build…"

Expand All @@ -35,6 +37,10 @@ def build
output_preview_urls

puts "\n✅ Build complete"
rescue
raise
ensure
run_hook(:after_build)
end

private
Expand All @@ -58,6 +64,26 @@ def output_preview_urls
end
end
end

def run_hook(name)
return unless (hook = Perron.configuration.public_send(name))

context = Context.new(
output_path: @output_path.to_s,
mode: Perron.configuration.mode
)

hook.call(context)
end

class Context
attr_reader :output_path, :mode

def initialize(output_path:, mode:)
@output_path = output_path
@mode = mode
end
end
end
end
end
21 changes: 21 additions & 0 deletions test/perron/build_hooks_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "test_helper"

class Perron::BuildHooksTest < ActiveSupport::TestCase
test "configuration has before_build callback" do
assert_respond_to Perron.configuration, :before_build
end

test "configuration has after_build callback" do
assert_respond_to Perron.configuration, :after_build
end

test "before_build failure aborts build" do
Perron.configure do |config|
config.before_build = ->(context) { raise "pre-build validation failed" }
end

assert_raises(RuntimeError) do
Perron::Site.build
end
end
end
Loading