feat(gradle): add technology (#22528)

This commit is contained in:
Emily Xiong 2024-03-27 11:11:17 -04:00 committed by GitHub
parent 590c7ae905
commit c0b209f151
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,6 +1,7 @@
import { import {
CreateNodes, CreateNodes,
CreateNodesContext, CreateNodesContext,
ProjectConfiguration,
TargetConfiguration, TargetConfiguration,
readJsonFile, readJsonFile,
writeJsonFile, writeJsonFile,
@ -13,7 +14,6 @@ import { projectGraphCacheDirectory } from 'nx/src/utils/cache-directory';
import { getGradleBinaryPath } from '../utils/exec-gradle'; import { getGradleBinaryPath } from '../utils/exec-gradle';
import { getGradleReport } from '../utils/get-gradle-report'; import { getGradleReport } from '../utils/get-gradle-report';
const nonCacheableGradleTaskTypes = new Set(['Application']);
const dependsOnMap = { const dependsOnMap = {
build: ['^build', 'classes'], build: ['^build', 'classes'],
test: ['classes'], test: ['classes'],
@ -37,12 +37,20 @@ const targetsCache = existsSync(cachePath) ? readTargetsCache() : {};
export const calculatedTargets: Record< export const calculatedTargets: Record<
string, string,
{ name: string; targets: Record<string, TargetConfiguration> } {
name: string;
targets: Record<string, TargetConfiguration>;
targetGroups: Record<string, string[]>;
}
> = {}; > = {};
function readTargetsCache(): Record< function readTargetsCache(): Record<
string, string,
{ name: string; targets: Record<string, TargetConfiguration> } {
name: string;
targets: Record<string, TargetConfiguration>;
targetGroups: Record<string, string[]>;
}
> { > {
return readJsonFile(cachePath); return readJsonFile(cachePath);
} }
@ -50,7 +58,11 @@ function readTargetsCache(): Record<
export function writeTargetsToCache( export function writeTargetsToCache(
targets: Record< targets: Record<
string, string,
{ name: string; targets: Record<string, TargetConfiguration> } {
name: string;
targets: Record<string, TargetConfiguration>;
targetGroups: Record<string, string[]>;
}
> >
) { ) {
writeJsonFile(cachePath, targets); writeJsonFile(cachePath, targets);
@ -74,7 +86,12 @@ export const createNodes: CreateNodes<GradlePluginOptions> = [
calculatedTargets[hash] = targetsCache[hash]; calculatedTargets[hash] = targetsCache[hash];
return { return {
projects: { projects: {
[projectRoot]: targetsCache[hash], [projectRoot]: {
...targetsCache[hash],
metadata: {
technologies: ['gradle'],
},
},
}, },
}; };
} }
@ -111,7 +128,7 @@ export const createNodes: CreateNodes<GradlePluginOptions> = [
string string
>; >;
const targets = createGradleTargets( const { targets, targetGroups } = createGradleTargets(
tasks, tasks,
projectRoot, projectRoot,
options, options,
@ -121,15 +138,20 @@ export const createNodes: CreateNodes<GradlePluginOptions> = [
calculatedTargets[hash] = { calculatedTargets[hash] = {
name: projectName, name: projectName,
targets, targets,
targetGroups,
};
const project: Omit<ProjectConfiguration, 'root'> = {
name: projectName,
targets,
metadata: {
technologies: ['gradle'],
},
}; };
return { return {
projects: { projects: {
[projectRoot]: { [projectRoot]: project,
root: projectRoot,
name: projectName,
targets,
},
}, },
}; };
} catch (e) { } catch (e) {
@ -145,10 +167,14 @@ function createGradleTargets(
options: GradlePluginOptions | undefined, options: GradlePluginOptions | undefined,
context: CreateNodesContext, context: CreateNodesContext,
outputDirs: Map<string, string> outputDirs: Map<string, string>
): Record<string, TargetConfiguration> { ): {
targetGroups: Record<string, string[]>;
targets: Record<string, TargetConfiguration>;
} {
const inputsMap = createInputsMap(context); const inputsMap = createInputsMap(context);
const targets: Record<string, TargetConfiguration> = {}; const targets: Record<string, TargetConfiguration> = {};
const targetGroups: Record<string, string[]> = {};
for (const task of tasks) { for (const task of tasks) {
const targetName = options?.[`${task.name}TargetName`] ?? task.name; const targetName = options?.[`${task.name}TargetName`] ?? task.name;
@ -158,13 +184,17 @@ function createGradleTargets(
options: { options: {
cwd: projectRoot, cwd: projectRoot,
}, },
cache: !nonCacheableGradleTaskTypes.has(task.type), cache: !!outputs,
inputs: inputsMap[task.name], inputs: inputsMap[task.name],
outputs: outputs ? [outputs] : undefined, outputs: outputs ? [outputs] : undefined,
dependsOn: dependsOnMap[task.name], dependsOn: dependsOnMap[task.name],
}; };
if (!targetGroups[task.type]) {
targetGroups[task.type] = [];
} }
return targets; targetGroups[task.type].push(task.name);
}
return { targetGroups, targets };
} }
function createInputsMap( function createInputsMap(