From 40428fda75723934afd1342cfafd594199bd7c3e Mon Sep 17 00:00:00 2001 From: Emily Xiong Date: Thu, 2 May 2024 15:48:03 -0400 Subject: [PATCH] fix(gradle): add namedInputs to nx.json in gradle init --- .../gradle/src/generators/init/init.spec.ts | 159 ++++++++++++------ packages/gradle/src/generators/init/init.ts | 18 ++ packages/gradle/src/plugin/nodes.ts | 4 +- 3 files changed, 130 insertions(+), 51 deletions(-) diff --git a/packages/gradle/src/generators/init/init.spec.ts b/packages/gradle/src/generators/init/init.spec.ts index 38b7131455987..c1107825f4d51 100644 --- a/packages/gradle/src/generators/init/init.spec.ts +++ b/packages/gradle/src/generators/init/init.spec.ts @@ -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", - ] - `); }); }); diff --git a/packages/gradle/src/generators/init/init.ts b/packages/gradle/src/generators/init/init.ts index e4d1c4d8f92d0..1fa573bd5d62b 100644 --- a/packages/gradle/src/generators/init/init.ts +++ b/packages/gradle/src/generators/init/init.ts @@ -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; diff --git a/packages/gradle/src/plugin/nodes.ts b/packages/gradle/src/plugin/nodes.ts index 43807a96fea96..1e330022e5ff9 100644 --- a/packages/gradle/src/plugin/nodes.ts +++ b/packages/gradle/src/plugin/nodes.ts @@ -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'], }; }