Skip to content

Commit

Permalink
[INTERNAL] ProjectGraph: Refactor API
Browse files Browse the repository at this point in the history
Refactor some methods based on current usage.

* Internal: Use Maps and Sets instead of objects and arrays
* Rename several methods
* Traversal callbacks: Provide array of dependency names instead of a
  getDependency() function. Most consumers only need the names.
  Corresponding projects can be easily retrieved via getProject()
* Traversal callbacks: Callback can be synchronous. Before, we required
  a promise to be returned. Many consumers actually do not need to
  perform async tasks
* Add a method for retrieving the names of all projects in the graph.
  This is a common requirement
* Add a method for retrieving the number of projects in the graph.
  This is also a common requirement
* join: Fix taking over optional dependencies and correspoinding flag
* addProject: Remove option to ignore duplicates. This was never used
  • Loading branch information
RandomByte authored and d3xter666 committed Feb 6, 2023
1 parent 510ccd7 commit 277d590
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 144 deletions.
17 changes: 7 additions & 10 deletions lib/build/ProjectBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class ProjectBuilder {
});

// Count total number of projects to build based on input
const requestedProjects = this._graph.getAllProjects().map((p) => p.getName()).filter(function(projectName) {
const requestedProjects = this._graph.getProjectNames().filter(function(projectName) {
return filterProject(projectName);
});

Expand Down Expand Up @@ -264,18 +264,16 @@ class ProjectBuilder {
}

async _createRequiredBuildContexts(requestedProjects) {
const allProjects = this._graph.getAllProjects();
const requiredProjects = new Set(allProjects.filter((project) => {
return requestedProjects.includes(project.getName());
const requiredProjects = new Set(this._graph.getProjectNames().filter((projectName) => {
return requestedProjects.includes(projectName);
}));

const projectBuildContexts = new Map();

for (const project of requiredProjects) {
const projectName = project.getName();
for (const projectName of requiredProjects) {
log.verbose(`Creating build context for project ${projectName}...`);
const projectBuildContext = this._buildContext.createProjectContext({
project,
project: this._graph.getProject(projectName),
log
});

Expand All @@ -299,8 +297,7 @@ class ProjectBuilder {
return;
}
// Add dependency to list of projects to build
const depProject = this._graph.getProject(depName);
requiredProjects.add(depProject);
requiredProjects.add(depName);
});
}
}
Expand All @@ -322,7 +319,7 @@ class ProjectBuilder {
);

if (includedDependencies.length) {
if (includedDependencies.length === this._graph.getAllProjects().length - 1) {
if (includedDependencies.length === this._graph.getSize() - 1) {
log.info(` Including all dependencies`);
} else {
log.info(` Requested dependencies:`);
Expand Down
2 changes: 1 addition & 1 deletion lib/build/TaskRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ class TaskRunner {
// Add transitive dependencies to set of required dependencies
const requiredDependencies = new Set(requiredDirectDependencies);
for (const projectName of requiredDirectDependencies) {
this._graph.getAllDependencies(projectName).forEach((depName) => {
this._graph.getTransitiveDependencies(projectName).forEach((depName) => {
requiredDependencies.add(depName);
});
}
Expand Down
5 changes: 2 additions & 3 deletions lib/build/helpers/composeProjectList.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ async function getFlattenedDependencyTree(graph) {
const dependencyMap = Object.create(null);
const rootName = graph.getRoot().getName();

await graph.traverseDepthFirst(({project, getDependencies}) => {
await graph.traverseDepthFirst(({project, dependencies}) => {
if (project.getName() === rootName) {
// Skip root project
return;
}
const projectDeps = [];
getDependencies().forEach((dep) => {
const depName = dep.getName();
dependencies.forEach((depName) => {
projectDeps.push(depName);
if (dependencyMap[depName]) {
projectDeps.push(...dependencyMap[depName]);
Expand Down
Loading

0 comments on commit 277d590

Please sign in to comment.