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, DependencyLoadError, DirectedAdjacencyGraph, Edge, FileVertex, Graph, InvalidDepsYAML, InvalidatedGlobalFiles, MissingDepsYAML, Vertex

Constant Summary collapse

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::PATH_MATCHER

Class Method Summary collapse

Methods included from Contracts

#Contract

Class Method Details

.deserialize_vertex(app, data) ⇒ Object


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

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


65
66
67
68
69
70
# File 'middleman-core/lib/middleman-core/dependencies.rb', line 65

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) ⇒ Object


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
130
131
132
133
134
135
136
137
138
139
140
141
# File 'middleman-core/lib/middleman-core/dependencies.rb', line 95

def load_and_deserialize(app, file_path)
  raise MissingDepsYAML 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.each_value { |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 => e
  new_error = e.is_a?(DependencyLoadError) ? e : InvalidDepsYAML
  raise new_error
end

.parse_yaml(file_path) ⇒ Object


58
59
60
61
62
# File 'middleman-core/lib/middleman-core/dependencies.rb', line 58

def parse_yaml(file_path)
  ::YAML.safe_load(File.read(file_path), permitted_classes: [Date, Time, DateTime, Symbol, Regexp], aliases: true)
rescue StandardError, ::Psych::SyntaxError => e
  warn "YAML Exception parsing dependency graph: #{e.message}"
end

.serialize(app, graph) ⇒ Object


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'middleman-core/lib/middleman-core/dependencies.rb', line 39

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) ⇒ Object


22
23
24
25
26
27
28
29
30
# File 'middleman-core/lib/middleman-core/dependencies.rb', line 22

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

  FileUtils.mkdir_p(File.dirname(file_path))

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

.visualize_graph(_app, graph) ⇒ Object


33
34
35
36
# File 'middleman-core/lib/middleman-core/dependencies.rb', line 33

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