Class: Nanoc::DependencyTracker Private

Inherits:
Store
  • Object
show all
Defined in:
lib/nanoc/base/compilation/dependency_tracker.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Responsible for remembering dependencies between items and layouts. It is used to speed up compilation by only letting an item be recompiled when it is outdated or any of its dependencies (or dependencies’ dependencies, etc) is outdated.

The dependencies tracked by the dependency tracker are not dependencies based on an item’s or a layout’s content. When one object uses an attribute of another object, then this is also treated as a dependency. While dependencies based on an item’s or layout’s content (handled in Compiler) cannot be mutually recursive, the more general dependencies in Nanoc::DependencyTracker can (e.g. item A can use an attribute of item B and vice versa without problems).

The dependency tracker remembers the dependency information between runs. Dependency information is stored in the tmp/dependencies file.

Instance Attribute Summary (collapse)

Attributes inherited from Store

#filename, #version

Instance Method Summary (collapse)

Methods inherited from Store

#load, #no_data_found, #store, #version_mismatch_detected

Constructor Details

- (DependencyTracker) initialize(objects)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a new dependency tracker for the given items and layouts.

Parameters:



36
37
38
39
40
41
42
# File 'lib/nanoc/base/compilation/dependency_tracker.rb', line 36

def initialize(objects)
  super('tmp/dependencies', 4)

  @objects = objects
  @graph   = Nanoc::DirectedGraph.new([ nil ] + @objects)
  @stack   = []
end

Instance Attribute Details

- (Nanoc::Compiler) compiler

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The compiler that corresponds to this dependency tracker

Returns:

  • (Nanoc::Compiler)

    The compiler that corresponds to this dependency tracker



30
31
32
# File 'lib/nanoc/base/compilation/dependency_tracker.rb', line 30

def compiler
  @compiler
end

- (Array<Nanoc::Item, Nanoc::Layout>) objects (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The list of items and layouts that are being tracked by the dependency tracker

Returns:



26
27
28
# File 'lib/nanoc/base/compilation/dependency_tracker.rb', line 26

def objects
  @objects
end

Instance Method Details

- (void) forget_dependencies_for(object)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Empties the list of dependencies for the given object. This is necessary before recompiling the given object, because otherwise old dependencies will stick around and new dependencies will appear twice. This function removes all incoming edges for the given vertex.

Parameters:



156
157
158
# File 'lib/nanoc/base/compilation/dependency_tracker.rb', line 156

def forget_dependencies_for(object)
  @graph.delete_edges_to(object)
end

- (Object) load_graph

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deprecated.

Use Store#load instead



166
167
168
# File 'lib/nanoc/base/compilation/dependency_tracker.rb', line 166

def load_graph
  load
end

- (Array<Nanoc::Item, Nanoc::Layout, nil>) objects_causing_outdatedness_of(object)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the direct dependencies for the given object.

The direct dependencies of the given object include the items and layouts that, when outdated will cause the given object to be marked as outdated. Indirect dependencies will not be returned (e.g. if A depends on B which depends on C, then the direct dependencies of A do not include C).

The direct predecessors can include nil, which indicates an item that is no longer present in the site.

predecessors of the given object

Parameters:

Returns:



108
109
110
# File 'lib/nanoc/base/compilation/dependency_tracker.rb', line 108

def objects_causing_outdatedness_of(object)
  @graph.direct_predecessors_of(object)
end

- (Array<Nanoc::Item, Nanoc::Layout>) objects_outdated_due_to(object)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the direct inverse dependencies for the given object.

The direct inverse dependencies of the given object include the objects that will be marked as outdated when the given object is outdated. Indirect dependencies will not be returned (e.g. if A depends on B which depends on C, then the direct inverse dependencies of C do not include A).

Parameters:

Returns:



125
126
127
# File 'lib/nanoc/base/compilation/dependency_tracker.rb', line 125

def objects_outdated_due_to(object)
  @graph.direct_successors_of(object).compact
end

- (void) record_dependency(src, dst)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Records a dependency from src to dst in the dependency graph. When dst is oudated, src will also become outdated.

Parameters:

  • src (Nanoc::Item, Nanoc::Layout)

    The source of the dependency, i.e. the object that will become outdated if dst is outdated

  • dst (Nanoc::Item, Nanoc::Layout)

    The destination of the dependency, i.e. the object that will cause the source to become outdated if the destination is outdated



140
141
142
143
# File 'lib/nanoc/base/compilation/dependency_tracker.rb', line 140

def record_dependency(src, dst)
  # Warning! dst and src are *reversed* here!
  @graph.add_edge(dst, src) unless src == dst
end

- (void) start

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Starts listening for dependency messages (:visit_started and :visit_ended) and start recording dependencies.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nanoc/base/compilation/dependency_tracker.rb', line 48

def start
  # Initialize dependency stack. An object will be pushed onto this stack
  # when it is visited. Therefore, an object on the stack always depends
  # on all objects pushed above it.
  @stack = []

  # Register start of visits
  Nanoc::NotificationCenter.on(:visit_started, self) do |obj|
    if !@stack.empty?
      Nanoc::NotificationCenter.post(:dependency_created, @stack.last, obj)
      record_dependency(@stack.last, obj)
    end
    @stack.push(obj)
  end

  # Register end of visits
  Nanoc::NotificationCenter.on(:visit_ended, self) do |obj|
    @stack.pop
  end
end

- (void) stop

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Stop listening for dependency messages and stop recording dependencies.



72
73
74
75
76
77
78
79
80
81
# File 'lib/nanoc/base/compilation/dependency_tracker.rb', line 72

def stop
  # Sanity check
  if !@stack.empty?
    raise 'Internal inconsistency: dependency tracker stack not empty at end of compilation'
  end

  # Unregister
  Nanoc::NotificationCenter.remove(:visit_started, self)
  Nanoc::NotificationCenter.remove(:visit_ended,   self)
end

- (Object) store_graph

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deprecated.

Use Store#store instead



161
162
163
# File 'lib/nanoc/base/compilation/dependency_tracker.rb', line 161

def store_graph
  store
end

- (Object) top

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns The topmost item on the stack, i.e. the one currently being compiled

Returns:

  • The topmost item on the stack, i.e. the one currently being compiled



87
88
89
# File 'lib/nanoc/base/compilation/dependency_tracker.rb', line 87

def top
  @stack.last
end

- (Object) unload

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

See Also:



171
172
173
# File 'lib/nanoc/base/compilation/dependency_tracker.rb', line 171

def unload
  @graph = Nanoc::DirectedGraph.new([ nil ] + @objects)
end