From d6227d8497223873255d1db143d0140281b3a635 Mon Sep 17 00:00:00 2001 From: David First Date: Sun, 9 Feb 2020 05:20:28 -0500 Subject: [PATCH] basic component graph with dependencies labeled (#2311) * basic component graph with dependencies labeled * change the graph to have the components as the content of the nodes, also change the parameter to pass BitId[] instead of Component[] --- src/scope/graph/components-graph.ts | 41 +++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/scope/graph/components-graph.ts b/src/scope/graph/components-graph.ts index c944f217660..fef95d863d5 100644 --- a/src/scope/graph/components-graph.ts +++ b/src/scope/graph/components-graph.ts @@ -1,11 +1,14 @@ import graphLib, { Graph } from 'graphlib'; import R from 'ramda'; import Component from '../../consumer/component/consumer-component'; -import Dependencies from '../../consumer/component/dependencies/dependencies'; +import Dependencies, { DEPENDENCIES_TYPES } from '../../consumer/component/dependencies/dependencies'; +import loadFlattenedDependenciesForCapsule from '../../consumer/component-ops/load-flattened-dependencies'; import ComponentWithDependencies from '../component-dependencies'; import GeneralError from '../../error/general-error'; import { ComponentsAndVersions } from '../scope'; -import { BitId } from '../../bit-id'; +import { BitId, BitIds } from '../../bit-id'; +import { Consumer } from '../../consumer'; +import { Dependency } from '../../consumer/component/dependencies'; export type AllDependenciesGraphs = { graphDeps: Graph; @@ -61,6 +64,40 @@ export function buildOneGraphForComponentsAndMultipleVersions(components: Compon return graph; } +/** + * returns one graph that includes all dependencies types. each edge has a label of the dependency + * type. the nodes content is the Component object. + */ +export async function buildOneGraphForComponents(ids: BitId[], consumer: Consumer): Promise { + const graph = new Graph(); + const { components } = await consumer.loadComponents(BitIds.fromArray(ids)); + const componentsWithDeps = await Promise.all( + components.map(component => loadFlattenedDependenciesForCapsule(consumer, component)) + ); + const allComponents: Component[] = R.flatten(componentsWithDeps.map(c => [c.component, ...c.allDependencies])); + + // set vertices + allComponents.forEach(component => { + const idStr = component.id.toStringWithoutVersion(); + if (!graph.hasNode(idStr)) graph.setNode(idStr, component); + }); + + // set edges + allComponents.forEach((component: Component) => { + DEPENDENCIES_TYPES.forEach(depType => { + component[depType].get().forEach((dependency: Dependency) => { + const depIdStr = dependency.id.toStringWithoutVersion(); + graph.setEdge(component.id.toStringWithoutVersion(), depIdStr, depType); + }); + }); + }); + + // uncomment to print the graph content + // console.log('graph', graphLib.json.write(graph)) + + return graph; +} + function _setGraphEdges(bitId: BitId, dependencies: Dependencies, graph: Graph) { const id = bitId.toString(); dependencies.get().forEach(dependency => {