From 9c16601fade15689af52dec03102c4bd2e0a83a4 Mon Sep 17 00:00:00 2001 From: Emily Xiong Date: Wed, 24 Apr 2024 17:33:08 -0400 Subject: [PATCH] fix(gradle): join projectRoot with workspaceRoot --- packages/gradle/src/plugin/nodes.spec.ts | 152 ++++++++++++++++++ packages/gradle/src/plugin/nodes.ts | 6 +- .../gradle/src/utils/get-gradle-report.ts | 2 +- 3 files changed, 158 insertions(+), 2 deletions(-) create mode 100644 packages/gradle/src/plugin/nodes.spec.ts diff --git a/packages/gradle/src/plugin/nodes.spec.ts b/packages/gradle/src/plugin/nodes.spec.ts new file mode 100644 index 0000000000000..e56ce0b858a38 --- /dev/null +++ b/packages/gradle/src/plugin/nodes.spec.ts @@ -0,0 +1,152 @@ +import { CreateNodesContext } from '@nx/devkit'; + +import { TempFs } from 'nx/src/internal-testing-utils/temp-fs'; +import type { GradleReport } from '../utils/get-gradle-report'; + +let gradleReport: GradleReport; +jest.mock('../utils/get-gradle-report.ts', () => { + return { + getGradleReport: jest.fn().mockImplementation(() => gradleReport), + }; +}); + +import { createNodes } from './nodes'; + +describe('@nx/gradle/plugin', () => { + let createNodesFunction = createNodes[1]; + let context: CreateNodesContext; + let tempFs: TempFs; + let cwd: string; + + beforeEach(async () => { + tempFs = new TempFs('test'); + gradleReport = { + gradleFileToGradleProjectMap: new Map([ + ['proj/gradle.build', 'proj'], + ]), + buildFileToDepsMap: new Map(), + gradleFileToOutputDirsMap: new Map>([ + ['proj/gradle.build', new Map([['build', 'build']])], + ]), + gradleProjectToTasksTypeMap: new Map>([ + ['proj', new Map([['test', 'Test']])], + ]), + gradleProjectToProjectName: new Map([['proj', 'proj']]), + }; + cwd = process.cwd(); + process.chdir(tempFs.tempDir); + context = { + nxJsonConfiguration: { + namedInputs: { + default: ['{projectRoot}/**/*'], + production: ['!{projectRoot}/**/*.spec.ts'], + }, + }, + workspaceRoot: tempFs.tempDir, + configFiles: [], + }; + + await tempFs.createFiles({ + 'proj/gradle.build': ``, + gradlew: '', + }); + }); + + afterEach(() => { + jest.resetModules(); + process.chdir(cwd); + }); + + it('should create nodes based on gradle', async () => { + const nodes = await createNodesFunction( + 'proj/gradle.build', + { + buildTargetName: 'build', + }, + context + ); + + expect(nodes.projects.proj).toMatchInlineSnapshot(` + { + "metadata": { + "technologies": [ + "gradle", + ], + }, + "name": "proj", + "targets": { + "test": { + "cache": false, + "command": "../gradlew test", + "dependsOn": [ + "classes", + ], + "inputs": [ + "default", + "^production", + ], + "options": { + "cwd": "proj", + }, + "outputs": undefined, + }, + }, + } + `); + }); + + it('should create nodes based on gradle for nested project root', async () => { + gradleReport = { + gradleFileToGradleProjectMap: new Map([ + ['nested/nested/proj/gradle.build', 'proj'], + ]), + buildFileToDepsMap: new Map(), + gradleFileToOutputDirsMap: new Map>([ + ['nested/nested/proj/gradle.build', new Map([['build', 'build']])], + ]), + gradleProjectToTasksTypeMap: new Map>([ + ['proj', new Map([['test', 'Test']])], + ]), + gradleProjectToProjectName: new Map([['proj', 'proj']]), + }; + await tempFs.createFiles({ + 'nested/nested/proj/gradle.build': ``, + }); + + const nodes = await createNodesFunction( + 'nested/nested/proj/gradle.build', + { + buildTargetName: 'build', + }, + context + ); + + expect(nodes.projects['nested/nested/proj']).toMatchInlineSnapshot(` + { + "metadata": { + "technologies": [ + "gradle", + ], + }, + "name": "proj", + "targets": { + "test": { + "cache": false, + "command": "../../../gradlew test", + "dependsOn": [ + "classes", + ], + "inputs": [ + "default", + "^production", + ], + "options": { + "cwd": "nested/nested/proj", + }, + "outputs": undefined, + }, + }, + } + `); + }); +}); diff --git a/packages/gradle/src/plugin/nodes.ts b/packages/gradle/src/plugin/nodes.ts index 2e31792ba0c93..4da844fc0e5be 100644 --- a/packages/gradle/src/plugin/nodes.ts +++ b/packages/gradle/src/plugin/nodes.ts @@ -4,6 +4,7 @@ import { ProjectConfiguration, TargetConfiguration, readJsonFile, + workspaceRoot, writeJsonFile, } from '@nx/devkit'; import { calculateHashForCreateNodes } from '@nx/devkit/src/utils/calculate-hash-for-create-nodes'; @@ -180,7 +181,10 @@ function createGradleTargets( const targetName = options?.[`${task.name}TargetName`] ?? task.name; const outputs = outputDirs.get(task.name); - const path = relative(projectRoot, getGradleBinaryPath()); + const path = relative( + join(context.workspaceRoot, projectRoot), + getGradleBinaryPath() + ); targets[targetName] = { command: `${path} ${task.name}`, options: { diff --git a/packages/gradle/src/utils/get-gradle-report.ts b/packages/gradle/src/utils/get-gradle-report.ts index 90562eef48f1d..9f57ef2a765fc 100644 --- a/packages/gradle/src/utils/get-gradle-report.ts +++ b/packages/gradle/src/utils/get-gradle-report.ts @@ -11,7 +11,7 @@ export const fileSeparator = process.platform.startsWith('win') const newLineSeparator = process.platform.startsWith('win') ? '\r\n' : '\n'; -interface GradleReport { +export interface GradleReport { gradleFileToGradleProjectMap: Map; buildFileToDepsMap: Map; gradleFileToOutputDirsMap: Map>;