Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prioritise recompiling dependent reps #1077

Merged
merged 3 commits into from Jan 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 29 additions & 6 deletions lib/nanoc/base/services/item_rep_selector.rb
Expand Up @@ -7,18 +7,21 @@ def initialize(reps)
@reps = reps
end

NONE = Object.new

def each
graph = Nanoc::Int::DirectedGraph.new(@reps)

prioritised = Set.new
loop do
break if graph.roots.empty?
rep = graph.roots.each { |e| break e }
rep = find(graph, prioritised)
break if NONE.equal?(rep)

begin
yield(rep)
graph.delete_vertex(rep)
rescue => e
handle_error(e, rep, graph)
handle_error(e, rep, graph, prioritised)
end
end

Expand All @@ -28,7 +31,26 @@ def each
end
end

def handle_error(e, rep, graph)
def find(graph, prioritised)
if graph.roots.empty?
NONE
elsif prioritised.any?
until prioritised.empty?
rep = prioritised.each { |e| break e }
if graph.roots.include?(rep)
return rep
else
prioritised.delete(rep)
end
end

find(graph, prioritised)
else
graph.roots.each { |e| break e }
end
end

def handle_error(e, rep, graph, prioritised)
actual_error =
if e.is_a?(Nanoc::Int::Errors::CompilationError)
e.unwrap
Expand All @@ -37,14 +59,15 @@ def handle_error(e, rep, graph)
end

if actual_error.is_a?(Nanoc::Int::Errors::UnmetDependency)
handle_dependency_error(actual_error, rep, graph)
handle_dependency_error(actual_error, rep, graph, prioritised)
else
raise(e)
end
end

def handle_dependency_error(e, rep, graph)
def handle_dependency_error(e, rep, graph, prioritised)
other_rep = e.rep
prioritised << other_rep
graph.add_edge(other_rep, rep)
unless graph.vertices.include?(other_rep)
graph.add_vertex(other_rep)
Expand Down
15 changes: 15 additions & 0 deletions spec/nanoc/base/services/item_rep_selector_spec.rb
Expand Up @@ -165,5 +165,20 @@
expect(tentatively_yielded).to eq [:a, :b, :a, :c, :a, :d, :a, :e, :a]
end
end

context 'unrelated roots' do
let(:dependencies) do
{
a: [:d],
b: [:e],
c: [],
}
end

it 'picks prioritised roots' do
expect(successfully_yielded).to eq [:d, :e, :c, :a, :b]
expect(tentatively_yielded).to eq [:a, :d, :b, :e, :c, :a, :b]
end
end
end
end