fix(gradle): add namedInputs to nx.json in gradle init (#23152)

<!-- 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 #
This commit is contained in:
Emily Xiong 2024-05-02 16:15:51 -04:00 committed by GitHub
parent bacdc799b4
commit 11f30f638f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 132 additions and 53 deletions

View File

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

View File

@ -30,6 +30,7 @@ export async function initGenerator(tree: Tree, options: InitGeneratorSchema) {
}
addPlugin(tree);
updateNxJsonConfiguration(tree);
addProjectReportToBuildGradle(tree);
if (!options.skipFormat) {
@ -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;

View File

@ -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'],
};
}