Skip to content

Commit

Permalink
feat(core): give custom hasher factory access to projectGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Feb 25, 2022
1 parent 61ed903 commit 7d38f86
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
26 changes: 14 additions & 12 deletions packages/linter/src/executors/eslint/hasher.ts
@@ -1,19 +1,20 @@
import { Task, TaskGraph } from '@nrwl/devkit';
import { ProjectGraph, Task, TaskGraph } from '@nrwl/devkit';
import { Hash, Hasher } from '@nrwl/workspace/src/core/hasher/hasher';
import { appRootPath } from '@nrwl/tao/src/utils/app-root';
import { Workspaces } from '@nrwl/tao/src/shared/workspace';

export default async function run(
task: Task,
taskGraph: TaskGraph,
hasher: Hasher
hasher: Hasher,
projectGraph: ProjectGraph
): Promise<Hash> {
if (task.overrides['hasTypeAwareRules'] === true) {
return hasher.hashTaskWithDepsAndContext(task);
}
const command = hasher.hashCommand(task);
const sources = await hasher.hashSource(task);
const deps = allDeps(task.id, taskGraph);
const deps = allDeps(task.id, taskGraph, projectGraph);
const workspace = new Workspaces(appRootPath).readWorkspaceConfiguration();
const tags = hasher.hashArray(
deps.map((d) => (workspace.projects[d].tags || []).join('|'))
Expand All @@ -36,13 +37,14 @@ export default async function run(
};
}

function allDeps(taskId: string, taskGraph: TaskGraph): string[] {
return [
...taskGraph.dependencies[taskId].map(
(task) => taskGraph.tasks[task].target.project
),
...taskGraph.dependencies[taskId].flatMap((task) =>
allDeps(task, taskGraph)
),
];
function allDeps(
taskId: string,
taskGraph: TaskGraph,
projectGraph: ProjectGraph
): string[] {
const project = taskGraph.tasks[taskId].target.project;
const dependencies = projectGraph.dependencies[project]
.filter((d) => !!projectGraph.nodes[d.target])
.map((d) => d.target);
return dependencies;
}
3 changes: 2 additions & 1 deletion packages/workspace/src/tasks-runner/task-orchestrator.ts
Expand Up @@ -26,7 +26,8 @@ export class TaskOrchestrator {
this.hasher,
this.taskGraph,
this.workspace,
this.options
this.options,
this.projectGraph
);

// region internal state
Expand Down
10 changes: 8 additions & 2 deletions packages/workspace/src/tasks-runner/tasks-schedule.spec.ts
@@ -1,5 +1,5 @@
import { TasksSchedule } from './tasks-schedule';
import { Task, TaskGraph } from '@nrwl/devkit';
import { ProjectGraph, Task, TaskGraph } from '@nrwl/devkit';
import { Workspaces } from '@nrwl/tao/src/shared/workspace';
import { removeTasksFromTaskGraph } from '@nrwl/workspace/src/tasks-runner/utils';

Expand Down Expand Up @@ -82,6 +82,11 @@ describe('TasksSchedule', () => {
},
};

const projectGraph: ProjectGraph = {
nodes: {},
dependencies: {},
};

const hasher = {
hashTaskWithDepsAndContext: () => 'hash',
} as any;
Expand All @@ -96,7 +101,8 @@ describe('TasksSchedule', () => {
endTask: jest.fn(),
scheduleTask: jest.fn(),
},
}
},
projectGraph
);
});

Expand Down
7 changes: 4 additions & 3 deletions packages/workspace/src/tasks-runner/tasks-schedule.ts
@@ -1,4 +1,4 @@
import { Task, TaskGraph } from '@nrwl/devkit';
import { ProjectGraph, Task, TaskGraph } from '@nrwl/devkit';

import { Workspaces } from '@nrwl/tao/src/shared/workspace';

Expand Down Expand Up @@ -31,7 +31,8 @@ export class TasksSchedule {
private readonly hasher: Hasher,
private taskGraph: TaskGraph,
private workspace: Workspaces,
private options: DefaultTasksRunnerOptions
private options: DefaultTasksRunnerOptions,
private readonly projectGraph: ProjectGraph
) {}

public async scheduleNextTasks() {
Expand Down Expand Up @@ -163,7 +164,7 @@ export class TasksSchedule {
private async hashTask(task: Task) {
const customHasher = getCustomHasher(task, this.workspace);
const { value, details } = await (customHasher
? customHasher(task, this.taskGraph, this.hasher)
? customHasher(task, this.taskGraph, this.hasher, this.projectGraph)
: this.hasher.hashTaskWithDepsAndContext(task));
task.hash = value;
task.hashDetails = details;
Expand Down

0 comments on commit 7d38f86

Please sign in to comment.