Skip to content

Commit

Permalink
fix(gradle): add namedInputs to nx.json in gradle init (#23152)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
xiongemi authored May 2, 2024
1 parent bacdc79 commit 11f30f6
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 51 deletions.
159 changes: 109 additions & 50 deletions packages/gradle/src/generators/init/init.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,122 @@ describe('@nx/gradle:init', () => {
tree.write('settings.gradle', '');
});

it('should add the plugin', async () => {
await initGenerator(tree, {
skipFormat: true,
skipPackageJson: false,
describe('plugin', () => {
it('should add the plugin', async () => {
await initGenerator(tree, {
skipFormat: true,
skipPackageJson: false,
});
const nxJson = readNxJson(tree);
expect(nxJson.plugins).toMatchInlineSnapshot(`
[
{
"options": {
"buildTargetName": "build",
"classesTargetName": "classes",
"testTargetName": "test",
},
"plugin": "@nx/gradle",
},
]
`);
});
const nxJson = readNxJson(tree);
expect(nxJson.plugins).toMatchInlineSnapshot(`
[
{
"options": {
"buildTargetName": "build",
"classesTargetName": "classes",
"testTargetName": "test",
},
"plugin": "@nx/gradle",
},
]
`);
});

it('should not overwrite existing plugins', async () => {
updateNxJson(tree, {
plugins: ['foo'],
it('should not overwrite existing plugins', async () => {
updateNxJson(tree, {
plugins: ['foo'],
});
await initGenerator(tree, {
skipFormat: true,
skipPackageJson: false,
});
const nxJson = readNxJson(tree);
expect(nxJson.plugins).toMatchInlineSnapshot(`
[
"foo",
{
"options": {
"buildTargetName": "build",
"classesTargetName": "classes",
"testTargetName": "test",
},
"plugin": "@nx/gradle",
},
]
`);
});
await initGenerator(tree, {
skipFormat: true,
skipPackageJson: false,

it('should not add plugin if already in array', async () => {
updateNxJson(tree, {
plugins: ['@nx/gradle'],
});
await initGenerator(tree, {
skipFormat: true,
skipPackageJson: false,
});
const nxJson = readNxJson(tree);
expect(nxJson.plugins).toMatchInlineSnapshot(`
[
"@nx/gradle",
]
`);
});
const nxJson = readNxJson(tree);
expect(nxJson.plugins).toMatchInlineSnapshot(`
[
"foo",
{
"options": {
"buildTargetName": "build",
"classesTargetName": "classes",
"testTargetName": "test",
},
"plugin": "@nx/gradle",
},
]
`);
});

it('should not add plugin if already in array', async () => {
updateNxJson(tree, {
plugins: ['@nx/gradle'],
describe('namedInputs', () => {
it('should add the namedInputs', async () => {
await initGenerator(tree, {
skipFormat: true,
skipPackageJson: false,
});
const nxJson = readNxJson(tree);
expect(nxJson.namedInputs).toMatchInlineSnapshot(`
{
"default": [
"{projectRoot}/**/*",
],
"production": [
"default",
"!{projectRoot}/test/**/*",
],
}
`);
});
await initGenerator(tree, {
skipFormat: true,
skipPackageJson: false,

it('should not overwrite existing namedInputs', async () => {
updateNxJson(tree, {
namedInputs: {
default: ['foo'],
production: [
'bar',
'!{projectRoot}/cypress/**/*',
'!{projectRoot}/**/*.cy.[jt]s?(x)',
'!{projectRoot}/cypress.config.[jt]s',
'!{projectRoot}/test/**/*',
],
},
});
await initGenerator(tree, {
skipFormat: true,
skipPackageJson: false,
});
const nxJson = readNxJson(tree);
expect(nxJson.namedInputs).toMatchInlineSnapshot(`
{
"default": [
"foo",
"{projectRoot}/**/*",
],
"production": [
"bar",
"!{projectRoot}/cypress/**/*",
"!{projectRoot}/**/*.cy.[jt]s?(x)",
"!{projectRoot}/cypress.config.[jt]s",
"!{projectRoot}/test/**/*",
"default",
],
}
`);
});
const nxJson = readNxJson(tree);
expect(nxJson.plugins).toMatchInlineSnapshot(`
[
"@nx/gradle",
]
`);
});
});
18 changes: 18 additions & 0 deletions packages/gradle/src/generators/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export async function initGenerator(tree: Tree, options: InitGeneratorSchema) {
}

addPlugin(tree);
updateNxJsonConfiguration(tree);
addProjectReportToBuildGradle(tree);

if (!options.skipFormat) {
Expand Down Expand Up @@ -93,4 +94,21 @@ allprojects {
}
}

function updateNxJsonConfiguration(tree: Tree) {
const nxJson = readNxJson(tree);

if (!nxJson.namedInputs) {
nxJson.namedInputs = {};
}
const defaultFilesSet = nxJson.namedInputs.default ?? [];
nxJson.namedInputs.default = Array.from(
new Set([...defaultFilesSet, '{projectRoot}/**/*'])
);
const productionFileSet = nxJson.namedInputs.production ?? [];
nxJson.namedInputs.production = Array.from(
new Set([...productionFileSet, 'default', '!{projectRoot}/test/**/*'])
);
updateNxJson(tree, nxJson);
}

export default initGenerator;
4 changes: 3 additions & 1 deletion packages/gradle/src/plugin/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ function createInputsMap(
? ['production', '^production']
: ['default', '^default'],
test: ['default', namedInputs?.production ? '^production' : '^default'],
classes: ['default', '^default'],
classes: namedInputs?.production
? ['production', '^production']
: ['default', '^default'],
};
}

0 comments on commit 11f30f6

Please sign in to comment.