Skip to content

Commit 11f30f6

Browse files
authored
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 #
1 parent bacdc79 commit 11f30f6

File tree

3 files changed

+130
-51
lines changed

3 files changed

+130
-51
lines changed

packages/gradle/src/generators/init/init.spec.ts

Lines changed: 109 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -11,63 +11,122 @@ describe('@nx/gradle:init', () => {
1111
tree.write('settings.gradle', '');
1212
});
1313

14-
it('should add the plugin', async () => {
15-
await initGenerator(tree, {
16-
skipFormat: true,
17-
skipPackageJson: false,
14+
describe('plugin', () => {
15+
it('should add the plugin', async () => {
16+
await initGenerator(tree, {
17+
skipFormat: true,
18+
skipPackageJson: false,
19+
});
20+
const nxJson = readNxJson(tree);
21+
expect(nxJson.plugins).toMatchInlineSnapshot(`
22+
[
23+
{
24+
"options": {
25+
"buildTargetName": "build",
26+
"classesTargetName": "classes",
27+
"testTargetName": "test",
28+
},
29+
"plugin": "@nx/gradle",
30+
},
31+
]
32+
`);
1833
});
19-
const nxJson = readNxJson(tree);
20-
expect(nxJson.plugins).toMatchInlineSnapshot(`
21-
[
22-
{
23-
"options": {
24-
"buildTargetName": "build",
25-
"classesTargetName": "classes",
26-
"testTargetName": "test",
27-
},
28-
"plugin": "@nx/gradle",
29-
},
30-
]
31-
`);
32-
});
3334

34-
it('should not overwrite existing plugins', async () => {
35-
updateNxJson(tree, {
36-
plugins: ['foo'],
35+
it('should not overwrite existing plugins', async () => {
36+
updateNxJson(tree, {
37+
plugins: ['foo'],
38+
});
39+
await initGenerator(tree, {
40+
skipFormat: true,
41+
skipPackageJson: false,
42+
});
43+
const nxJson = readNxJson(tree);
44+
expect(nxJson.plugins).toMatchInlineSnapshot(`
45+
[
46+
"foo",
47+
{
48+
"options": {
49+
"buildTargetName": "build",
50+
"classesTargetName": "classes",
51+
"testTargetName": "test",
52+
},
53+
"plugin": "@nx/gradle",
54+
},
55+
]
56+
`);
3757
});
38-
await initGenerator(tree, {
39-
skipFormat: true,
40-
skipPackageJson: false,
58+
59+
it('should not add plugin if already in array', async () => {
60+
updateNxJson(tree, {
61+
plugins: ['@nx/gradle'],
62+
});
63+
await initGenerator(tree, {
64+
skipFormat: true,
65+
skipPackageJson: false,
66+
});
67+
const nxJson = readNxJson(tree);
68+
expect(nxJson.plugins).toMatchInlineSnapshot(`
69+
[
70+
"@nx/gradle",
71+
]
72+
`);
4173
});
42-
const nxJson = readNxJson(tree);
43-
expect(nxJson.plugins).toMatchInlineSnapshot(`
44-
[
45-
"foo",
46-
{
47-
"options": {
48-
"buildTargetName": "build",
49-
"classesTargetName": "classes",
50-
"testTargetName": "test",
51-
},
52-
"plugin": "@nx/gradle",
53-
},
54-
]
55-
`);
5674
});
5775

58-
it('should not add plugin if already in array', async () => {
59-
updateNxJson(tree, {
60-
plugins: ['@nx/gradle'],
76+
describe('namedInputs', () => {
77+
it('should add the namedInputs', async () => {
78+
await initGenerator(tree, {
79+
skipFormat: true,
80+
skipPackageJson: false,
81+
});
82+
const nxJson = readNxJson(tree);
83+
expect(nxJson.namedInputs).toMatchInlineSnapshot(`
84+
{
85+
"default": [
86+
"{projectRoot}/**/*",
87+
],
88+
"production": [
89+
"default",
90+
"!{projectRoot}/test/**/*",
91+
],
92+
}
93+
`);
6194
});
62-
await initGenerator(tree, {
63-
skipFormat: true,
64-
skipPackageJson: false,
95+
96+
it('should not overwrite existing namedInputs', async () => {
97+
updateNxJson(tree, {
98+
namedInputs: {
99+
default: ['foo'],
100+
production: [
101+
'bar',
102+
'!{projectRoot}/cypress/**/*',
103+
'!{projectRoot}/**/*.cy.[jt]s?(x)',
104+
'!{projectRoot}/cypress.config.[jt]s',
105+
'!{projectRoot}/test/**/*',
106+
],
107+
},
108+
});
109+
await initGenerator(tree, {
110+
skipFormat: true,
111+
skipPackageJson: false,
112+
});
113+
const nxJson = readNxJson(tree);
114+
expect(nxJson.namedInputs).toMatchInlineSnapshot(`
115+
{
116+
"default": [
117+
"foo",
118+
"{projectRoot}/**/*",
119+
],
120+
"production": [
121+
"bar",
122+
"!{projectRoot}/cypress/**/*",
123+
"!{projectRoot}/**/*.cy.[jt]s?(x)",
124+
"!{projectRoot}/cypress.config.[jt]s",
125+
"!{projectRoot}/test/**/*",
126+
"default",
127+
],
128+
}
129+
`);
65130
});
66-
const nxJson = readNxJson(tree);
67-
expect(nxJson.plugins).toMatchInlineSnapshot(`
68-
[
69-
"@nx/gradle",
70-
]
71-
`);
72131
});
73132
});

packages/gradle/src/generators/init/init.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export async function initGenerator(tree: Tree, options: InitGeneratorSchema) {
3030
}
3131

3232
addPlugin(tree);
33+
updateNxJsonConfiguration(tree);
3334
addProjectReportToBuildGradle(tree);
3435

3536
if (!options.skipFormat) {
@@ -93,4 +94,21 @@ allprojects {
9394
}
9495
}
9596

97+
function updateNxJsonConfiguration(tree: Tree) {
98+
const nxJson = readNxJson(tree);
99+
100+
if (!nxJson.namedInputs) {
101+
nxJson.namedInputs = {};
102+
}
103+
const defaultFilesSet = nxJson.namedInputs.default ?? [];
104+
nxJson.namedInputs.default = Array.from(
105+
new Set([...defaultFilesSet, '{projectRoot}/**/*'])
106+
);
107+
const productionFileSet = nxJson.namedInputs.production ?? [];
108+
nxJson.namedInputs.production = Array.from(
109+
new Set([...productionFileSet, 'default', '!{projectRoot}/test/**/*'])
110+
);
111+
updateNxJson(tree, nxJson);
112+
}
113+
96114
export default initGenerator;

packages/gradle/src/plugin/nodes.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,8 @@ function createInputsMap(
200200
? ['production', '^production']
201201
: ['default', '^default'],
202202
test: ['default', namedInputs?.production ? '^production' : '^default'],
203-
classes: ['default', '^default'],
203+
classes: namedInputs?.production
204+
? ['production', '^production']
205+
: ['default', '^default'],
204206
};
205207
}

0 commit comments

Comments
 (0)