Skip to content

Commit

Permalink
Inherit generator priority from builder
Browse files Browse the repository at this point in the history
* BREAKING CHANGE (maybe?): the exact order multiple generators will run in may be subtly different than before. Use Builder priorities to resolve
  • Loading branch information
jaredcwhite committed May 17, 2022
1 parent f30ed8e commit d44e245
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 7 additions & 4 deletions bridgetown-builder/lib/bridgetown-builder/dsl/generators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ module Generators
def generator(method_name = nil, &block)
block = method(method_name) if method_name.is_a?(Symbol)
local_name = name # pull the name method into a local variable
builder_priority = self.class.instance_variable_get(:@priority)

new_gen = Class.new(Bridgetown::Generator) do
anon_generator = Class.new(Bridgetown::Generator) do
define_method(:_builder_block) { block }
define_singleton_method(:custom_name) { local_name }

attr_reader :site

priority builder_priority || :low

def inspect
"#<#{self.class.custom_name} (Generator)>"
end
Expand All @@ -23,10 +26,10 @@ def generate(_site)
end
end

first_low_priority_index = site.generators.find_index { |gen| gen.class.priority == :low }
site.generators.insert(first_low_priority_index || 0, new_gen.new(site.config))
site.generators << anon_generator.new(site.config)
site.generators.sort!

functions << { name: name, generator: new_gen }
functions << { name: name, generator: anon_generator }
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions bridgetown-builder/test/test_generators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def build
end

class GeneratorBuilder2 < Builder
priority :normal

def build
generator do
site.data[:site_metadata][:title] = "Test Title 2"
Expand Down

0 comments on commit d44e245

Please sign in to comment.