Skip to content

Commit

Permalink
fix(gradle): get gradlew path with projectRoot joins workspaceRoot (#…
Browse files Browse the repository at this point in the history
…22988)

(cherry picked from commit cec57c4)
  • Loading branch information
xiongemi authored and FrozenPandaz committed Apr 25, 2024
1 parent dcc14e3 commit 3369237
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 2 deletions.
152 changes: 152 additions & 0 deletions packages/gradle/src/plugin/nodes.spec.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>([
['proj/gradle.build', 'proj'],
]),
buildFileToDepsMap: new Map<string, string>(),
gradleFileToOutputDirsMap: new Map<string, Map<string, string>>([
['proj/gradle.build', new Map([['build', 'build']])],
]),
gradleProjectToTasksTypeMap: new Map<string, Map<string, string>>([
['proj', new Map([['test', 'Test']])],
]),
gradleProjectToProjectName: new Map<string, string>([['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<string, string>([
['nested/nested/proj/gradle.build', 'proj'],
]),
buildFileToDepsMap: new Map<string, string>(),
gradleFileToOutputDirsMap: new Map<string, Map<string, string>>([
['nested/nested/proj/gradle.build', new Map([['build', 'build']])],
]),
gradleProjectToTasksTypeMap: new Map<string, Map<string, string>>([
['proj', new Map([['test', 'Test']])],
]),
gradleProjectToProjectName: new Map<string, string>([['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,
},
},
}
`);
});
});
6 changes: 5 additions & 1 deletion packages/gradle/src/plugin/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion packages/gradle/src/utils/get-gradle-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>;
buildFileToDepsMap: Map<string, string>;
gradleFileToOutputDirsMap: Map<string, Map<string, string>>;
Expand Down

0 comments on commit 3369237

Please sign in to comment.