Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gradle): cache gradle report #29381

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 115 additions & 1 deletion packages/gradle/src/utils/get-gradle-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { join, relative } from 'node:path';
import {
AggregateCreateNodesError,
normalizePath,
readJsonFile,
workspaceRoot,
writeJsonFile,
} from '@nx/devkit';

import { hashWithWorkspaceContext } from 'nx/src/utils/workspace-context';
Expand All @@ -15,6 +17,7 @@ import {
fileSeparator,
newLineSeparator,
} from './get-project-report-lines';
import { workspaceDataDirectory } from 'nx/src/utils/cache-directory';

export interface GradleReport {
gradleFileToGradleProjectMap: Map<string, string>;
Expand All @@ -27,8 +30,111 @@ export interface GradleReport {
gradleProjectToChildProjects: Map<string, string[]>;
}

export interface GradleReportJSON {
hash: string;
gradleFileToGradleProjectMap: Record<string, string>;
buildFileToDepsMap: Record<string, string>;
gradleFileToOutputDirsMap: Record<string, Record<string, string>>;
gradleProjectToTasksTypeMap: Record<string, Record<string, string>>;
gradleProjectToTasksMap: Record<string, Array<String>>;
gradleProjectToProjectName: Record<string, string>;
gradleProjectNameToProjectRootMap: Record<string, string>;
gradleProjectToChildProjects: Record<string, string[]>;
}

function readGradleReportCache(
cachePath: string,
hash: string
): GradleReport | undefined {
const gradleReportJson: Partial<GradleReportJSON> = existsSync(cachePath)
? readJsonFile(cachePath)
: undefined;
if (!gradleReportJson || gradleReportJson.hash !== hash) {
return;
}
let results: GradleReport = {
gradleFileToGradleProjectMap: new Map(
Object.entries(gradleReportJson['gradleFileToGradleProjectMap'])
),
buildFileToDepsMap: new Map(
Object.entries(gradleReportJson['buildFileToDepsMap'])
),
gradleFileToOutputDirsMap: new Map(
Object.entries(gradleReportJson['gradleFileToOutputDirsMap']).map(
([key, value]) => [key, new Map(Object.entries(value))]
)
),
gradleProjectToTasksTypeMap: new Map(
Object.entries(gradleReportJson['gradleProjectToTasksTypeMap']).map(
([key, value]) => [key, new Map(Object.entries(value))]
)
),
gradleProjectToTasksMap: new Map(
Object.entries(gradleReportJson['gradleProjectToTasksMap']).map(
([key, value]) => [key, new Set(value)]
)
),
gradleProjectToProjectName: new Map(
Object.entries(gradleReportJson['gradleProjectToProjectName'])
),
gradleProjectNameToProjectRootMap: new Map(
Object.entries(gradleReportJson['gradleProjectNameToProjectRootMap'])
),
gradleProjectToChildProjects: new Map(
Object.entries(gradleReportJson['gradleProjectToChildProjects'])
),
};
return results;
}

export function writeGradleReportToCache(
cachePath: string,
results: GradleReport
) {
let gradleReportJson: GradleReportJSON = {
hash: gradleCurrentConfigHash,
gradleFileToGradleProjectMap: Object.fromEntries(
results.gradleFileToGradleProjectMap
),
buildFileToDepsMap: Object.fromEntries(results.buildFileToDepsMap),
gradleFileToOutputDirsMap: Object.fromEntries(
Array.from(results.gradleFileToOutputDirsMap).map(([key, value]) => [
key,
Object.fromEntries(value),
])
),
gradleProjectToTasksTypeMap: Object.fromEntries(
Array.from(results.gradleProjectToTasksTypeMap).map(([key, value]) => [
key,
Object.fromEntries(value),
])
),
gradleProjectToTasksMap: Object.fromEntries(
Array.from(results.gradleProjectToTasksMap).map(([key, value]) => [
key,
Array.from(value),
])
),
gradleProjectToProjectName: Object.fromEntries(
results.gradleProjectToProjectName
),
gradleProjectNameToProjectRootMap: Object.fromEntries(
results.gradleProjectNameToProjectRootMap
),
gradleProjectToChildProjects: Object.fromEntries(
results.gradleProjectToChildProjects
),
};

writeJsonFile(cachePath, gradleReportJson);
}

let gradleReportCache: GradleReport;
let gradleCurrentConfigHash: string;
let gradleReportCachePath: string = join(
workspaceDataDirectory,
'gradle-report.hash'
);

export function getCurrentGradleReport() {
if (!gradleReportCache) {
Expand Down Expand Up @@ -64,7 +170,14 @@ export async function populateGradleReport(
const gradleConfigHash = await hashWithWorkspaceContext(workspaceRoot, [
gradleConfigAndTestGlob,
]);
if (gradleReportCache && gradleConfigHash === gradleCurrentConfigHash) {
gradleReportCache ??= readGradleReportCache(
gradleReportCachePath,
gradleConfigHash
);
if (
gradleReportCache &&
(!gradleCurrentConfigHash || gradleConfigHash === gradleCurrentConfigHash)
) {
return;
}

Expand Down Expand Up @@ -92,6 +205,7 @@ export async function populateGradleReport(
);
gradleCurrentConfigHash = gradleConfigHash;
gradleReportCache = processProjectReports(projectReportLines);
writeGradleReportToCache(gradleReportCachePath, gradleReportCache);
}

export function processProjectReports(
Expand Down
Loading