Skip to content

Commit 0795cd4

Browse files
committed
Add cycle detection to celerity mapper
Summary: create CelerityResourceGraph, which extends AbstractDirectedGraph. since we've done a bunch of work already to load the resource graph into memory CelerityResourceGraph simply stores a copy and makes loadEdges work off that stored copy. Test Plan: made phabricator-prefab require herald-rule-editor ~/code/phabricator> ./scripts/celerity_mapper.php webroot Finding static resources... Processing 154 files.......................................................................................................................................................... [2011-11-22 11:28:29] EXCEPTION: (Exception) Cycle detected in resource graph: phabricator-prefab => herald-rule-editor => phabricator-prefab at [/Users/btrahan/Dropbox/code/phabricator/scripts/celerity_mapper.php:173] fixed phabricator-prefab requiring herald-rule-editor. re-ran celerity_mapper and no errors! Reviewers: epriestley Reviewed By: epriestley CC: aran, btrahan, epriestley Differential Revision: 1132
1 parent ec8dbfd commit 0795cd4

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

scripts/celerity_mapper.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@
130130
echo "\n";
131131

132132
$runtime_map = array();
133-
133+
$resource_graph = array();
134134
$hash_map = array();
135135

136136
$parser = new PhutilDocblockParser();
@@ -168,6 +168,8 @@
168168

169169
$hash_map[$provides] = $info['hash'];
170170

171+
$resource_graph[$provides] = $requires;
172+
171173
$runtime_map[$provides] = array(
172174
'uri' => $uri,
173175
'type' => $type,
@@ -176,6 +178,20 @@
176178
);
177179
}
178180

181+
$celerity_resource_graph = new CelerityResourceGraph();
182+
$celerity_resource_graph->addNodes($resource_graph);
183+
$celerity_resource_graph->setResourceGraph($resource_graph);
184+
$celerity_resource_graph->loadGraph();
185+
186+
foreach ($resource_graph as $provides => $requires) {
187+
$cycle = $celerity_resource_graph->detectCycles($provides);
188+
if ($cycle) {
189+
throw new Exception(
190+
"Cycle detected in resource graph: ". implode($cycle, " => ")
191+
);
192+
}
193+
}
194+
179195
$package_map = array();
180196
foreach ($package_spec as $name => $package) {
181197
$hashes = array();

src/__phutil_library_map__.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
'AphrontWriteGuard' => 'aphront/writeguard',
8484
'CelerityAPI' => 'infrastructure/celerity/api',
8585
'CelerityResourceController' => 'infrastructure/celerity/controller',
86+
'CelerityResourceGraph' => 'infrastructure/celerity/graph',
8687
'CelerityResourceMap' => 'infrastructure/celerity/map',
8788
'CelerityStaticResourceResponse' => 'infrastructure/celerity/response',
8889
'ConduitAPIMethod' => 'applications/conduit/method/base',
@@ -813,6 +814,7 @@
813814
'AphrontTypeaheadTemplateView' => 'AphrontView',
814815
'AphrontWebpageResponse' => 'AphrontResponse',
815816
'CelerityResourceController' => 'AphrontController',
817+
'CelerityResourceGraph' => 'AbstractDirectedGraph',
816818
'ConduitAPI_arcanist_Method' => 'ConduitAPIMethod',
817819
'ConduitAPI_arcanist_projectinfo_Method' => 'ConduitAPI_arcanist_Method',
818820
'ConduitAPI_conduit_connect_Method' => 'ConduitAPIMethod',
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2011 Facebook, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
final class CelerityResourceGraph extends AbstractDirectedGraph {
20+
21+
private $resourceGraph = array();
22+
private $graphSet = false;
23+
24+
protected function loadEdges(array $nodes) {
25+
if (!$this->graphSet) {
26+
throw new Exception(
27+
"Call setResourceGraph before loading the graph!"
28+
);
29+
}
30+
31+
$graph = $this->getResourceGraph();
32+
$edges = array();
33+
foreach ($nodes as $node) {
34+
$edges[$node] = idx($graph, $node, array());
35+
}
36+
return $edges;
37+
}
38+
39+
final public function setResourceGraph(array $graph) {
40+
$this->resourceGraph = $graph;
41+
$this->graphSet = true;
42+
}
43+
44+
private function getResourceGraph() {
45+
return $this->resourceGraph;
46+
}
47+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* This file is automatically generated. Lint this module to rebuild it.
4+
* @generated
5+
*/
6+
7+
8+
9+
phutil_require_module('phutil', 'utils');
10+
phutil_require_module('phutil', 'utils/abstractgraph');
11+
12+
13+
phutil_require_source('CelerityResourceGraph.php');

0 commit comments

Comments
 (0)