Skip to content

Commit

Permalink
fix(gradle): cache gradle report
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongemi committed Dec 16, 2024
1 parent ee4de0b commit 4d371f5
Showing 1 changed file with 116 additions and 2 deletions.
118 changes: 116 additions & 2 deletions 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 All @@ -53,7 +159,7 @@ export function getCurrentGradleReport() {
* If `projectReportAll` fails, it runs the `projectReport` task instead.
* It will throw an error if both tasks fail.
* It will accumulate the output of all gradlew files.
* @param workspaceRoot
* @param workspaceRootg
* @param gradlewFiles absolute paths to all gradlew files in the workspace
* @returns Promise<void>
*/
Expand All @@ -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

0 comments on commit 4d371f5

Please sign in to comment.