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

Do not use items/layouts (mutable identifier) as hash key #1184

Merged
merged 1 commit into from
Jun 13, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 60 additions & 25 deletions lib/nanoc/base/repos/dependency_store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ def initialize(items, layouts, site: nil)
@items = items
@layouts = layouts

@refs2objs = {}
items.each { |o| add_vertex_for(o) }
layouts.each { |o| add_vertex_for(o) }

@new_objects = []
@graph = Nanoc::Int::DirectedGraph.new([nil] + @items.to_a + @layouts.to_a)
@graph = Nanoc::Int::DirectedGraph.new([nil] + objs2refs(@items) + objs2refs(@layouts))
end

contract C::Or[Nanoc::Int::Item, Nanoc::Int::ItemRep, Nanoc::Int::Layout] => C::ArrayOf[Nanoc::Int::Dependency]
Expand All @@ -36,6 +40,16 @@ def dependencies_causing_outdatedness_of(object)
end
end

def items=(items)
@items = items
items.each { |o| @refs2objs[obj2ref(o)] = o }
end

def layouts=(layouts)
@layouts = layouts
layouts.each { |o| @refs2objs[obj2ref(o)] = o }
end

# Returns the direct dependencies for the given object.
#
# The direct dependencies of the given object include the items and
Expand All @@ -57,7 +71,7 @@ def objects_causing_outdatedness_of(object)
if @new_objects.any?
[@new_objects.first]
else
@graph.direct_predecessors_of(object)
refs2objs(@graph.direct_predecessors_of(obj2ref(object)))
end
end

Expand All @@ -76,11 +90,23 @@ def objects_causing_outdatedness_of(object)
#
# @return [void]
def record_dependency(src, dst, raw_content: false, attributes: false, compiled_content: false, path: false)
existing_props = Nanoc::Int::Props.new(@graph.props_for(dst, src) || {})
# TODO: do src == dst check first (faster)

add_vertex_for(src)
add_vertex_for(dst)

src_ref = obj2ref(src)
dst_ref = obj2ref(dst)

existing_props = Nanoc::Int::Props.new(@graph.props_for(dst_ref, src_ref) || {})
new_props = Nanoc::Int::Props.new(raw_content: raw_content, attributes: attributes, compiled_content: compiled_content, path: path)
props = existing_props.merge(new_props)

@graph.add_edge(dst, src, props: props.to_h) unless src == dst
@graph.add_edge(dst_ref, src_ref, props: props.to_h) unless src == dst
end

def add_vertex_for(o)
@refs2objs[obj2ref(o)] = o
end

# Empties the list of dependencies for the given object. This is necessary
Expand All @@ -93,13 +119,33 @@ def record_dependency(src, dst, raw_content: false, attributes: false, compiled_
#
# @return [void]
def forget_dependencies_for(object)
@graph.delete_edges_to(object)
@graph.delete_edges_to(obj2ref(object))
end

protected

def obj2ref(obj)
obj && obj.reference
end

def ref2obj(reference)
if reference
@refs2objs[reference]
else
nil
end
end

def objs2refs(objs)
objs.map { |o| obj2ref(o) }
end

def refs2objs(refs)
refs.map { |r| ref2obj(r) }
end

def props_for(a, b)
props = @graph.props_for(a, b) || {}
props = @graph.props_for(obj2ref(a), obj2ref(b)) || {}

if props.values.any? { |v| v }
props
Expand All @@ -111,37 +157,26 @@ def props_for(a, b)
def data
{
edges: @graph.edges,
vertices: @graph.vertices.map { |obj| obj && obj.reference },
vertices: @graph.vertices,
}
end

def data=(new_data)
objects = @items.to_a + @layouts.to_a
objects = Set.new(@items.to_a + @layouts.to_a)
refs = objs2refs(objects)

# Create new graph
@graph = Nanoc::Int::DirectedGraph.new([nil] + objects)
@graph = Nanoc::Int::DirectedGraph.new([nil] + refs)

# Load vertices
previous_objects = new_data[:vertices].map do |reference|
if reference
case reference[0]
when :item
@items.object_with_identifier(reference[1])
when :layout
@layouts.object_with_identifier(reference[1])
else
raise Nanoc::Int::Errors::InternalInconsistency, "unrecognised reference #{reference[0].inspect}"
end
else
nil
end
end
previous_refs = new_data[:vertices]
previous_objects = Set.new(refs2objs(previous_refs))

# Load edges
new_data[:edges].each do |edge|
from_index, to_index, props = *edge
from = from_index && previous_objects[from_index]
to = to_index && previous_objects[to_index]
from = from_index && previous_refs[from_index]
to = to_index && previous_refs[to_index]
@graph.add_edge(from, to, props: props)
end

Expand Down
22 changes: 22 additions & 0 deletions spec/nanoc/regressions/gh_1185_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

describe 'GH-1185', site: true, stdio: true do
before do
File.write('content/foo.html', 'stuff')

File.write('Rules', <<~EOS)
preprocess do
@items['/foo.*'].identifier = '/bar.html'
end

compile '/**/*' do
filter :erb
write ext: 'html'
end
EOS
end

it 'does not crash' do
Nanoc::CLI.run(%w[compile])
end
end