Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'],
};
}