Skip to content

Commit

Permalink
feat(core): add task plans to --graph=file.json argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli committed Dec 7, 2023
1 parent be821aa commit dc40a86
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 21 deletions.
25 changes: 20 additions & 5 deletions packages/nx/src/command-line/graph/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { readFileMapCache } from '../../project-graph/nx-deps-cache';
import { ConfigurationSourceMaps } from '../../project-graph/utils/project-configuration-utils';

import { filterUsingGlobPatterns } from '../../hasher/task-hasher';
import { createTaskHasher } from '../../hasher/create-task-hasher';

export interface ProjectGraphClientResponse {
hash: string;
Expand Down Expand Up @@ -343,7 +344,7 @@ export async function generateGraph(
if (args.file === 'stdout') {
console.log(
JSON.stringify(
createJsonOutput(prunedGraph, args.projects, args.targets),
await createJsonOutput(prunedGraph, args.projects, args.targets),
null,
2
)
Expand Down Expand Up @@ -404,7 +405,11 @@ export async function generateGraph(
} else if (ext === '.json') {
ensureDirSync(dirname(fullFilePath));

const json = createJsonOutput(prunedGraph, args.projects, args.targets);
const json = await createJsonOutput(
rawGraph,
args.projects,
args.targets
);
json.affectedProjects = affectedProjects;
json.criticalPath = affectedProjects;

Expand Down Expand Up @@ -1014,6 +1019,7 @@ function expandInputs(

interface GraphJsonResponse {
tasks?: TaskGraph;
taskPlans?: Record<string, string[]>;
graph: ProjectGraph;

/**
Expand All @@ -1027,11 +1033,11 @@ interface GraphJsonResponse {
criticalPath?: string[];
}

function createJsonOutput(
async function createJsonOutput(
graph: ProjectGraph,
projects: string[],
targets?: string[]
): GraphJsonResponse {
): Promise<GraphJsonResponse> {
const response: GraphJsonResponse = {
graph,
};
Expand All @@ -1043,14 +1049,23 @@ function createJsonOutput(
nxJson.targetDefaults
);

response.tasks = createTaskGraph(
const taskGraph = createTaskGraph(
graph,
defaultDependencyConfigs,
projects,
targets,
undefined,
{}
);

const hasher = createTaskHasher(graph, readNxJson());
let tasks = Object.values(taskGraph.tasks);
const hashes = await hasher.hashTasks(tasks, taskGraph);
response.tasks = taskGraph;
response.taskPlans = tasks.reduce((acc, task, index) => {
acc[task.id] = Object.keys(hashes[index].details.nodes).sort();
return acc;
}, {});
}

return response;
Expand Down
29 changes: 29 additions & 0 deletions packages/nx/src/hasher/create-task-hasher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { NxJsonConfiguration } from '../config/nx-json';
import { ProjectGraph } from '../config/project-graph';
import { daemonClient } from '../daemon/client/client';
import { getFileMap } from '../project-graph/build-project-graph';
import {
DaemonBasedTaskHasher,
InProcessTaskHasher,
TaskHasher,
} from './task-hasher';

export function createTaskHasher(
projectGraph: ProjectGraph,
nxJson: NxJsonConfiguration,
runnerOptions?: any
): TaskHasher {
if (daemonClient.enabled()) {
return new DaemonBasedTaskHasher(daemonClient, runnerOptions);
} else {
const { fileMap, allWorkspaceFiles, rustReferences } = getFileMap();
return new InProcessTaskHasher(
fileMap?.projectFileMap,
allWorkspaceFiles,
projectGraph,
nxJson,
rustReferences,
runnerOptions
);
}
}
4 changes: 2 additions & 2 deletions packages/nx/src/hasher/task-hasher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export class InProcessTaskHasher implements TaskHasher {
this.projectGraph,
{
selectivelyHashTsConfig:
this.options.selectivelyHashTsConfig ?? false,
this.options?.selectivelyHashTsConfig ?? false,
}
)
: new NativeTaskHasherImpl(
Expand All @@ -178,7 +178,7 @@ export class InProcessTaskHasher implements TaskHasher {
this.externalRustReferences,
{
selectivelyHashTsConfig:
this.options.selectivelyHashTsConfig ?? false,
this.options?.selectivelyHashTsConfig ?? false,
}
);
}
Expand Down
16 changes: 2 additions & 14 deletions packages/nx/src/tasks-runner/run-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { daemonClient } from '../daemon/client/client';
import { StoreRunInformationLifeCycle } from './life-cycles/store-run-information-life-cycle';
import { getFileMap } from '../project-graph/build-project-graph';
import { performance } from 'perf_hooks';
import { createTaskHasher } from '../hasher/create-task-hasher';

async function getTerminalOutputLifeCycle(
initiatingProject: string,
Expand Down Expand Up @@ -233,20 +234,7 @@ export async function invokeTasksRunner({

const { tasksRunner, runnerOptions } = getRunner(nxArgs, nxJson);

let hasher: TaskHasher;
if (daemonClient.enabled()) {
hasher = new DaemonBasedTaskHasher(daemonClient, runnerOptions);
} else {
const { fileMap, allWorkspaceFiles, rustReferences } = getFileMap();
hasher = new InProcessTaskHasher(
fileMap?.projectFileMap,
allWorkspaceFiles,
projectGraph,
nxJson,
rustReferences,
runnerOptions
);
}
let hasher = createTaskHasher(projectGraph, nxJson, runnerOptions);

// this is used for two reasons: to fetch all remote cache hits AND
// to submit everything that is known in advance to Nx Cloud to run in
Expand Down

0 comments on commit dc40a86

Please sign in to comment.