Module: Middleman::Dependencies

Includes:
Contracts
Defined in:
middleman-core/lib/middleman-core/dependencies.rb,
middleman-core/lib/middleman-core/dependencies/edge.rb,
middleman-core/lib/middleman-core/dependencies/graph.rb,
middleman-core/lib/middleman-core/dependencies/vertices.rb,
middleman-core/lib/middleman-core/dependencies/vertices/vertex.rb,
middleman-core/lib/middleman-core/dependencies/vertices/file_vertex.rb,
middleman-core/lib/middleman-core/dependencies/vertices/data_collection_vertex.rb,
middleman-core/lib/middleman-core/dependencies/vertices/data_collection_path_vertex.rb

Defined Under Namespace

Classes: ChangedDepth, DataCollectionPathVertex, DataCollectionVertex, DirectedAdjacencyGraph, Edge, FileVertex, Graph, InvalidDepsYAML, InvalidatedGlobalFiles, Vertex

Constant Summary collapse

DEFAULT_FILE_PATH =
'deps.yml'.freeze
GLOBAL_FILES =
['config.rb', 'lib/**/*.rb', 'helpers/**/*.rb', 'Gemfile.lock'].freeze
VERTICES_BY_TYPE =
{
  DataCollectionVertex::TYPE_ID => DataCollectionVertex,
  DataCollectionPathVertex::TYPE_ID => DataCollectionPathVertex,
  FileVertex::TYPE_ID => FileVertex
}.freeze

Constants included from Contracts

Contracts::ImmutableSetOf, Contracts::ImmutableSortedSetOf, Contracts::OldResourceList, Contracts::PATH_MATCHER, Contracts::ResourceList, Contracts::VectorOf

Class Method Summary collapse

Methods included from Contracts

#Contract

Class Method Details

.deserialize_vertex(app, data) ⇒ Object


13
14
15
16
# File 'middleman-core/lib/middleman-core/dependencies/vertices.rb', line 13

def self.deserialize_vertex(app, data)
  vertex_class = VERTICES_BY_TYPE[data['type'].to_sym]
  vertex_class.deserialize(app, data['key'].to_sym, data['attrs'])
end

.invalidated_global(app, known_files) ⇒ Object


60
61
62
63
64
65
# File 'middleman-core/lib/middleman-core/dependencies.rb', line 60

def invalidated_global(app, known_files)
  known_files.keys.reject do |key|
    p = File.expand_path(key, app.root)
    !(File.exist? p) || known_files[key] == ::Middleman::Util.hash_file(p)
  end
end

.load_and_deserialize(app, file_path = DEFAULT_FILE_PATH) ⇒ Object


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'middleman-core/lib/middleman-core/dependencies.rb', line 84

def load_and_deserialize(app, file_path = DEFAULT_FILE_PATH)
  return Graph.new unless File.exist?(file_path)

  data = parse_yaml(file_path)

  global = data['global']

  raise ChangedDepth if data['data_depth'] != app.config[:data_collection_depth]

  unless (invalidated = invalidated_global(app, global)).empty?
    raise InvalidatedGlobalFiles, invalidated
  end

  # Pre-existing vertices (from config.rb)
  preexisting_vertices = app.data.vertices.each_with_object({}) do |vertex, sum|
    sum[vertex.key] = vertex
  end

  vertices = data['vertices'].each_with_object(preexisting_vertices) do |(type, verts), sum|
    verts.each do |(k, h)|
      vertex_class = VERTICES_BY_TYPE[type.to_sym]
      vertex = vertex_class.deserialize(app, k.to_sym, 'hash' => h)
      if sum[vertex.key.to_sym]
        sum[vertex.key.to_sym].merge!(vertex)
      else
        sum[vertex.key.to_sym] = vertex
      end
    end
  end

  graph = Graph.new
  vertices.values.each { |v| graph.add_vertex(v) }

  data['edges'].each do |k, deps|
    deps.each do |d|
      graph.add_edge_by_key(d.to_sym, k.to_sym)
    end
  end

  # require 'rgl/dot'
  # graph.graph.write_to_graphic_file('jpg', 'valid')

  graph
rescue StandardError
  raise InvalidDepsYAML
end

.parse_yaml(file_path) ⇒ Object


53
54
55
56
57
# File 'middleman-core/lib/middleman-core/dependencies.rb', line 53

def parse_yaml(file_path)
  ::YAML.load_file(file_path)
rescue StandardError, ::Psych::SyntaxError => error
  warn "YAML Exception parsing dependency graph: #{error.message}"
end

.serialize(app, graph) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'middleman-core/lib/middleman-core/dependencies.rb', line 34

def serialize(app, graph)
  serialized = graph.serialize

  global = Dir.glob(GLOBAL_FILES)
              .sort
              .each_with_object({}) do |file, sum|
    p = Pathname(File.expand_path(file)).relative_path_from(app.root_path).to_s
    sum[p] = ::Middleman::Util.hash_file(file)
  end

  ::YAML.dump(
    {
      'data_depth' => app.config[:data_collection_depth],
      'global' => global
    }.merge(serialized)
  )
end

.serialize_and_save(app, graph, file_path = DEFAULT_FILE_PATH) ⇒ Object


19
20
21
22
23
24
25
# File 'middleman-core/lib/middleman-core/dependencies.rb', line 19

def serialize_and_save(app, graph, file_path = DEFAULT_FILE_PATH)
  new_output = serialize(app, graph)

  File.open(file_path, 'w') do |file|
    file.write new_output
  end
end

.visualize_graph(_app, graph) ⇒ Object


28
29
30
31
# File 'middleman-core/lib/middleman-core/dependencies.rb', line 28

def visualize_graph(_app, graph)
  require 'rgl/dot'
  graph.graph.write_to_graphic_file('jpg', 'graph')
end