@nx/gradle
Version:
143 lines (142 loc) • 6.72 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.writeProjectGraphReportToCache = writeProjectGraphReportToCache;
exports.getCurrentProjectGraphReport = getCurrentProjectGraphReport;
exports.getCurrentBuildFiles = getCurrentBuildFiles;
exports.populateProjectGraph = populateProjectGraph;
exports.processNxProjectGraph = processNxProjectGraph;
const node_fs_1 = require("node:fs");
const node_path_1 = require("node:path");
const devkit_1 = require("@nx/devkit");
const workspace_context_1 = require("nx/src/utils/workspace-context");
const split_config_files_1 = require("../../utils/split-config-files");
const cache_directory_1 = require("nx/src/utils/cache-directory");
const get_project_graph_lines_1 = require("./get-project-graph-lines");
const gradle_plugin_options_1 = require("./gradle-plugin-options");
const devkit_internals_1 = require("nx/src/devkit-internals");
function readProjectGraphReportCache(cachePath, hash) {
const projectGraphReportCache = (0, node_fs_1.existsSync)(cachePath)
? (0, devkit_1.readJsonFile)(cachePath)
: undefined;
if (!projectGraphReportCache || projectGraphReportCache.hash !== hash) {
return;
}
return projectGraphReportCache;
}
function writeProjectGraphReportToCache(cachePath, results, hash) {
let projectGraphReportJson = {
hash,
...results,
};
try {
(0, devkit_1.writeJsonFile)(cachePath, projectGraphReportJson);
}
catch (e) {
devkit_1.logger.warn(`Failed to write Gradle project graph report cache to ${cachePath}: ${e instanceof Error ? e.message : 'unknown error'}`);
}
}
let projectGraphReportCache;
let projectGraphReportCachePath = (0, node_path_1.join)(cache_directory_1.workspaceDataDirectory, 'gradle-nodes.hash');
function getCurrentProjectGraphReport() {
if (!projectGraphReportCache) {
throw new devkit_1.AggregateCreateNodesError([
[
null,
new Error(`Expected cached gradle report. Please open an issue at https://github.com/nrwl/nx/issues/new/choose`),
],
], []);
}
return projectGraphReportCache;
}
function getCurrentBuildFiles() {
const report = getCurrentProjectGraphReport();
return report.buildFiles || [];
}
/**
* This function populates the gradle report cache.
* For each gradlew file, it runs the `nxProjectGraph` task and processes the output.
* It will throw an error if both tasks fail.
* It will accumulate the output of all gradlew files.
* @param workspaceRoot
* @param gradlewFiles absolute paths to all gradlew files in the workspace
* @param options user specified gradle plugin options
* @returns Promise<void>
*/
async function populateProjectGraph(workspaceRoot, gradlewFiles, options) {
const normalizedOptions = (0, gradle_plugin_options_1.normalizeOptions)(options);
const gradleConfigHash = (0, devkit_1.hashArray)([
await (0, workspace_context_1.hashWithWorkspaceContext)(workspaceRoot, [split_config_files_1.gradleConfigAndTestGlob]),
(0, devkit_internals_1.hashObject)(normalizedOptions),
process.env.CI,
]);
const cached = readProjectGraphReportCache(projectGraphReportCachePath, gradleConfigHash);
if (cached) {
projectGraphReportCache = cached;
return;
}
const gradleProjectGraphReportStart = performance.mark('gradleProjectGraphReport:start');
let projectGraphLines;
try {
projectGraphLines = await gradlewFiles.reduce(async (projectGraphLines, gradlewFile) => {
const getNxProjectGraphLinesStart = performance.mark(`${gradlewFile}GetNxProjectGraphLines:start`);
const allLines = await projectGraphLines;
const currentLines = await (0, get_project_graph_lines_1.getNxProjectGraphLines)(gradlewFile, gradleConfigHash, normalizedOptions);
const getNxProjectGraphLinesEnd = performance.mark(`${gradlewFile}GetNxProjectGraphLines:end`);
performance.measure(`${gradlewFile}GetNxProjectGraphLines`, getNxProjectGraphLinesStart.name, getNxProjectGraphLinesEnd.name);
return [...allLines, ...currentLines];
}, Promise.resolve([]));
}
catch (e) {
if (e instanceof Error &&
e.message === 'Gradle project graph generation was cancelled') {
// Cancelled by a newer populateProjectGraph call — silently return
return;
}
throw e;
}
const gradleProjectGraphReportEnd = performance.mark('gradleProjectGraphReport:end');
performance.measure('gradleProjectGraphReport', gradleProjectGraphReportStart.name, gradleProjectGraphReportEnd.name);
projectGraphReportCache = processNxProjectGraph(projectGraphLines);
writeProjectGraphReportToCache(projectGraphReportCachePath, projectGraphReportCache, gradleConfigHash);
}
function processNxProjectGraph(projectGraphLines) {
let index = 0;
let projectGraphReportForAllProjects = {
nodes: {},
dependencies: [],
externalNodes: {},
};
const allBuildFiles = new Set();
while (index < projectGraphLines.length) {
const line = projectGraphLines[index].trim();
if (line.startsWith('> Task ') && line.endsWith(':nxProjectGraph')) {
index++; // Skip the task line before searching for the JSON file path
while (index < projectGraphLines.length &&
!projectGraphLines[index].trim().endsWith('.json')) {
index++;
}
const file = projectGraphLines[index];
const projectGraphReportJson = (0, devkit_1.readJsonFile)(file);
projectGraphReportForAllProjects.nodes = {
...projectGraphReportForAllProjects.nodes,
...projectGraphReportJson.nodes,
};
if (projectGraphReportJson.dependencies) {
projectGraphReportForAllProjects.dependencies.push(...projectGraphReportJson.dependencies);
}
if (Object.keys(projectGraphReportJson.externalNodes ?? {}).length > 0) {
projectGraphReportForAllProjects.externalNodes = {
...projectGraphReportForAllProjects.externalNodes,
...projectGraphReportJson.externalNodes,
};
}
if (projectGraphReportJson.buildFiles) {
projectGraphReportJson.buildFiles.forEach((buildFile) => allBuildFiles.add(buildFile));
}
}
index++;
}
// Convert Set to array for the final result
projectGraphReportForAllProjects.buildFiles = Array.from(allBuildFiles);
return projectGraphReportForAllProjects;
}