Skip to content

Commit 0ff93cc

Browse files
authored
Merge pull request #51 from devsecopsmaturitymodel/feat/51-generate-dependency-tree
Generate dependency tree as markdown
2 parents 48bb644 + 40fde98 commit 0ff93cc

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

yaml-generation/functions.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,16 @@ function getReferences($references) {
319319

320320
}
321321

322+
/**
323+
* Push item to array at key, creating array if needed.
324+
*/
325+
function array_push_item_to(array &$array, $key, $value) {
326+
if (!isset($array[$key])) {
327+
$array[$key] = [];
328+
}
329+
array_push($array[$key], $value);
330+
}
331+
322332

323333
// TODO create testcases
324334

yaml-generation/generateDimensions.php

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
$dimensionsAggregated = $dimensions;
5252
}
5353

54+
$dependencies = array();
55+
$activityIndex = array();
5456
foreach ($dimensionsAggregated as $dimension => $subdimensions) {
5557
ksort($subdimensions);
5658
foreach ($subdimensions as $subdimension => $elements) {
@@ -68,7 +70,7 @@
6870
array_push($errorMsg,"Missing 'level' attribute in activity: $activityName");
6971
}
7072

71-
echo "$subdimension | $activityName\n";
73+
// echo "$subdimension | $activityName\n";
7274
if (!array_key_exists("uuid", $activity)) {
7375
array_push($errorMsg, "$activityName is missing an uuid in $dimension");
7476
} else {
@@ -147,6 +149,16 @@
147149
array_push($errorMsg,"DependsOn non-existing activity: '$dependsOn' (in activity: $activityName)");
148150
}
149151
}
152+
153+
// Build dependency graph
154+
if (!array_key_exists($activityName, $activityIndex)) {
155+
$activityIndex[$activityName] = count($activityIndex);
156+
}
157+
if (!array_key_exists($dependsOn, $activityIndex)) {
158+
$activityIndex[$dependsOn] = count($activityIndex);
159+
}
160+
array_push_item_to($dependencies, $activityIndex[$dependsOn], $activityIndex[$activityName]);
161+
150162
}
151163
}
152164
}
@@ -188,6 +200,48 @@
188200
file_put_contents($targetGeneratedFile, $dimensionsString);
189201

190202

203+
// Store dependency graph
204+
$graphFilename = getcwd() . "/src/assets/YAML/generated/dependency-tree.md";
205+
$graphFile = fopen($graphFilename, "w");
206+
fwrite($graphFile, "```mermaid\n\n");
207+
fwrite($graphFile, "graph LR\n\n");
208+
// List all nodes
209+
foreach ($activityIndex as $activityName => $key) {
210+
$level = getActivityByActivityName($activityName, $dimensionsAggregated)["level"];
211+
$activityName = "L$level $activityName";
212+
$activityName = str_replace('(', '', $activityName);
213+
$activityName = str_replace(')', '', $activityName);
214+
fwrite($graphFile, "$key($activityName)\n");
215+
}
216+
// Add links between nodes
217+
fwrite($graphFile, "\n\n");
218+
foreach ($dependencies as $dad => $children) {
219+
foreach ($children as $child) {
220+
fwrite($graphFile, "$dad --> $child\n");
221+
}
222+
}
223+
// Tie all orphans to a common start node
224+
fwrite($graphFile, "\n");
225+
foreach ($activityIndex as $activityName => $key) {
226+
$isOrphan = true;
227+
foreach ($dependencies as $dad => $children) {
228+
if ($dad == $key) continue;
229+
if (in_array($key, $children)) {
230+
$isOrphan = false;
231+
break;
232+
}
233+
}
234+
if ($isOrphan) {
235+
fwrite($graphFile, "O --> $key\n");
236+
}
237+
}
238+
239+
// Close the file
240+
fwrite($graphFile, "```\n");
241+
fclose($graphFile);
242+
echo "\nSaved dependency graph '$graphFilename'\n\n";
243+
244+
191245

192246
function assertUniqueRefs($all_references, &$errorMsg) {
193247
foreach ($all_references as $references) {

0 commit comments

Comments
 (0)