Skip to content

Commit

Permalink
Use rule memory for compiling reps
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Dec 5, 2015
1 parent 754fcef commit 69802c4
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
18 changes: 12 additions & 6 deletions lib/nanoc/base/compilation/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,18 @@ def can_reuse_content_for_rep?(rep)
def recalculate_content_for_rep(rep)
executor = Nanoc::Int::Executor.new(self)

executor.snapshot(rep, :raw)
executor.snapshot(rep, :pre, final: false)
rules_collection.compilation_rule_for(rep)
.apply_to(rep, executor: executor, site: @site, view_context: create_view_context)
executor.snapshot(rep, :post) if rep.has_snapshot?(:post)
executor.snapshot(rep, :last)
rule_memory_calculator[rep].each do |action|
case action
when Nanoc::Int::RuleMemoryActions::Filter
executor.filter(rep, action.filter_name, action.params)
when Nanoc::Int::RuleMemoryActions::Layout
executor.layout(rep, action.layout_identifier, action.params)
when Nanoc::Int::RuleMemoryActions::Snapshot
executor.snapshot(rep, action.snapshot_name, final: action.final?, path: action.path)
else
raise "Internal inconsistency: unknown action #{action.inspect}"
end
end
end

# Clears the list of dependencies for items that will be recompiled.
Expand Down
4 changes: 4 additions & 0 deletions lib/nanoc/base/entities/rule_memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def snapshot_actions
@actions.select { |a| a.is_a?(Nanoc::Int::RuleMemoryActions::Snapshot) }
end

def any_layouts?
@actions.any? { |a| a.is_a?(Nanoc::Int::RuleMemoryActions::Layout) }
end

def serialize
map(&:serialize)
end
Expand Down
18 changes: 10 additions & 8 deletions lib/nanoc/base/services/rule_memory_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,21 @@ def snapshots_defs_for(rep)
#
# @return [Nanoc::Int::RuleMemory]
def new_rule_memory_for_rep(rep)
# FIXME: This is more-or-less duplicated from Compiler#recalculate_content_for_rep.
# Letting the compiler use the rule memory would fix this.

# FIXME: What if #compilation_rule_for returns nil?

executor = Nanoc::Int::RecordingExecutor.new(rep, @rules_collection, @site)
rule = @rules_collection.compilation_rule_for(rep)

executor.snapshot(rep, :raw)
executor.snapshot(rep, :pre, final: false)
@rules_collection
.compilation_rule_for(rep)
.apply_to(rep, executor: executor, site: @site, view_context: nil)
executor.snapshot(rep, :post) if rep.has_snapshot?(:post)
executor.snapshot(rep, :last) unless executor.rule_memory.snapshot_actions.any? { |sa| sa.snapshot_name == :last }
rule.apply_to(rep, executor: executor, site: @site, view_context: nil)
if executor.rule_memory.any_layouts?
executor.snapshot(rep, :post)
end
unless executor.rule_memory.snapshot_actions.any? { |sa| sa.snapshot_name == :last }
executor.snapshot(rep, :last)
end

executor.rule_memory
end

Expand Down
17 changes: 13 additions & 4 deletions spec/nanoc/base/services/rule_memory_calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
example do
subject

expect(subject.size).to eql(6)
expect(subject.size).to eql(7)

expect(subject[0]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[0].snapshot_name).to eql(:raw)
Expand All @@ -56,9 +56,14 @@
expect(subject[4].params).to eql({})

expect(subject[5]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[5].snapshot_name).to eql(:last)
expect(subject[5].snapshot_name).to eql(:post)
expect(subject[5]).to be_final
expect(subject[5].path).to be_nil

expect(subject[6]).to be_a(Nanoc::Int::RuleMemoryActions::Snapshot)
expect(subject[6].snapshot_name).to eql(:last)
expect(subject[6]).to be_final
expect(subject[6].path).to be_nil
end
end

Expand Down Expand Up @@ -115,7 +120,7 @@
end

example do
expect(subject.size).to eql(3)
expect(subject.size).to eql(4)

expect(subject[0]).to be_a(Nanoc::Int::SnapshotDef)
expect(subject[0].name).to eql(:raw)
Expand All @@ -126,8 +131,12 @@
expect(subject[1]).not_to be_final

expect(subject[2]).to be_a(Nanoc::Int::SnapshotDef)
expect(subject[2].name).to eql(:last)
expect(subject[2].name).to eql(:post)
expect(subject[2]).to be_final

expect(subject[3]).to be_a(Nanoc::Int::SnapshotDef)
expect(subject[3].name).to eql(:last)
expect(subject[3]).to be_final
end
end
end
2 changes: 1 addition & 1 deletion test/base/test_compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def test_compile_rep_should_write_proper_snapshots

# Create compiler
compiler = new_compiler(site)
compiler.rules_collection.expects(:compilation_rule_for).times(2).with(rep).returns(rule)
compiler.rules_collection.stubs(:compilation_rule_for).with(rep).returns(rule)
compiler.rules_collection.layout_filter_mapping[Nanoc::Int::Pattern.from(%r{^/blah/$})] = [:erb, {}]
site.stubs(:compiler).returns(compiler)

Expand Down

0 comments on commit 69802c4

Please sign in to comment.