From 4e6fe55c690016e9274c59cf64d750afd1abaa25 Mon Sep 17 00:00:00 2001 From: khalilou88 <32600911+khalilou88@users.noreply.github.com> Date: Fri, 20 Oct 2023 14:36:08 +0200 Subject: [PATCH] feat(generators): change useSubfolder to mavenRootDirectory option (#539) * feat(generators): change useSubfolder to mavenRootDirectory option * build: work in progress * build: work in progress * build: work in progress * build: work in progress * build: work in progress * build: work in progress * build: work in progress * build: work in progress * build: work in progress --------- Co-authored-by: khalilou88 --- .../init/files/nx/project.json__template__ | 12 ---- .../nx-maven/src/generators/init/generator.ts | 59 +++++++++++++------ .../nx-maven/src/generators/init/schema.d.ts | 2 +- .../nx-maven/src/generators/init/schema.json | 12 ++-- packages/nx-maven/src/utils/index.ts | 42 +++++++++++-- .../tests/nx-boot-maven.spec.ts | 2 +- .../e2e/nx-maven-e2e/tests/nx-maven.spec.ts | 2 +- .../tests/nx-maven.spec.ts | 4 +- .../tests/nx-micronaut-maven.spec.ts | 2 +- .../tests/nx-quarkus-maven.spec.ts | 2 +- 10 files changed, 92 insertions(+), 47 deletions(-) delete mode 100644 packages/nx-maven/src/generators/init/files/nx/project.json__template__ diff --git a/packages/nx-maven/src/generators/init/files/nx/project.json__template__ b/packages/nx-maven/src/generators/init/files/nx/project.json__template__ deleted file mode 100644 index d3c9ddc11..000000000 --- a/packages/nx-maven/src/generators/init/files/nx/project.json__template__ +++ /dev/null @@ -1,12 +0,0 @@ -{ - "name": "<%= parentProjectName %>", - "$schema": "../node_modules/nx/schemas/project-schema.json", - "targets": { - "build": { - "executor": "@jnxplus/nx-maven:run-task", - "options": { - "task": "install -N" - } - } - } - } \ No newline at end of file diff --git a/packages/nx-maven/src/generators/init/generator.ts b/packages/nx-maven/src/generators/init/generator.ts index 8b0d4aae5..a302f1f37 100644 --- a/packages/nx-maven/src/generators/init/generator.ts +++ b/packages/nx-maven/src/generators/init/generator.ts @@ -1,7 +1,6 @@ import { springBootVersion, kotlinVersion, - updateNxJson, quarkusVersion, micronautVersion, } from '@jnxplus/common'; @@ -12,11 +11,14 @@ import { updateGitIgnore, } from '../../utils/generators'; import { + ProjectConfiguration, Tree, + addProjectConfiguration, formatFiles, generateFiles, joinPathFragments, offsetFromRoot, + updateJson, } from '@nx/devkit'; import * as path from 'path'; import { NxMavenGeneratorSchema } from './schema'; @@ -27,7 +29,6 @@ interface NormalizedSchema extends NxMavenGeneratorSchema { springBootVersion: string; quarkusVersion: string; micronautVersion: string; - mavenRootDirectory: string; } function normalizeOptions( @@ -36,11 +37,6 @@ function normalizeOptions( ): NormalizedSchema { const dot = '.'; - let mavenRootDirectory = ''; - if (options.useSubfolder) { - mavenRootDirectory = 'nx-maven'; - } - return { ...options, dot, @@ -48,7 +44,6 @@ function normalizeOptions( springBootVersion, quarkusVersion, micronautVersion, - mavenRootDirectory, }; } @@ -72,21 +67,33 @@ function addFiles(tree: Tree, options: NormalizedSchema) { options.mavenRootDirectory, templateOptions, ); +} - if (options.useSubfolder) { - generateFiles( +export default async function (tree: Tree, options: NxMavenGeneratorSchema) { + const normalizedOptions = normalizeOptions(tree, options); + + if (options.mavenRootDirectory) { + const projectConfiguration: ProjectConfiguration = { + root: normalizedOptions.mavenRootDirectory, + targets: { + build: { + executor: '@jnxplus/nx-maven:run-task', + options: { + task: 'install -N', + }, + }, + }, + }; + + addProjectConfiguration( tree, - path.join(__dirname, 'files', 'nx'), - options.mavenRootDirectory, - templateOptions, + normalizedOptions.parentProjectName, + projectConfiguration, ); } -} -export default async function (tree: Tree, options: NxMavenGeneratorSchema) { - const normalizedOptions = normalizeOptions(tree, options); addFiles(tree, normalizedOptions); - updateNxJson(tree, '@jnxplus/nx-maven'); + updateNxJson(tree, normalizedOptions); updateGitIgnore(tree, options.skipWrapper); addOrUpdatePrettierRc(tree); addOrUpdatePrettierIgnore(tree); @@ -103,3 +110,21 @@ export default async function (tree: Tree, options: NxMavenGeneratorSchema) { } await formatFiles(tree); } + +export function updateNxJson(tree: Tree, options: NormalizedSchema) { + const plugin = { + plugin: '@jnxplus/nx-maven', + options: { + mavenRootDirectory: options.mavenRootDirectory, + }, + }; + + updateJson(tree, 'nx.json', (nxJson) => { + // if plugins is undefined, set it to an empty array + nxJson.plugins = nxJson.plugins ?? []; + // add plugin + nxJson.plugins.push(plugin); + // return modified JSON object + return nxJson; + }); +} diff --git a/packages/nx-maven/src/generators/init/schema.d.ts b/packages/nx-maven/src/generators/init/schema.d.ts index 1a215ace9..91a653e22 100644 --- a/packages/nx-maven/src/generators/init/schema.d.ts +++ b/packages/nx-maven/src/generators/init/schema.d.ts @@ -3,10 +3,10 @@ export interface NxMavenGeneratorSchema { groupId: string; parentProjectName: string; parentProjectVersion: string; + mavenRootDirectory: string; dependencyManagement: | 'bom' | 'spring-boot-parent-pom' | 'micronaut-parent-pom'; skipWrapper?: boolean; - useSubfolder?: boolean; } diff --git a/packages/nx-maven/src/generators/init/schema.json b/packages/nx-maven/src/generators/init/schema.json index fa22b365f..f6cbba336 100644 --- a/packages/nx-maven/src/generators/init/schema.json +++ b/packages/nx-maven/src/generators/init/schema.json @@ -48,6 +48,12 @@ "alias": "v", "x-prompt": "What project version would you like to use?" }, + "mavenRootDirectory": { + "type": "string", + "default": "", + "description": "where Maven config and projects are placed", + "x-prompt": "where do you want Maven config and projects to be placed?" + }, "dependencyManagement": { "description": "Dependency Management", "type": "string", @@ -75,11 +81,6 @@ "description": "Don't generate maven wrapper", "type": "boolean", "default": false - }, - "useSubfolder": { - "description": "Generate maven config and projects in a subfolder", - "type": "boolean", - "default": false } }, "required": [ @@ -87,6 +88,7 @@ "groupId", "parentProjectName", "parentProjectVersion", + "mavenRootDirectory", "dependencyManagement" ] } diff --git a/packages/nx-maven/src/utils/index.ts b/packages/nx-maven/src/utils/index.ts index a49f018b6..3463720b7 100644 --- a/packages/nx-maven/src/utils/index.ts +++ b/packages/nx-maven/src/utils/index.ts @@ -1,6 +1,11 @@ import { getProjectRoot } from '@jnxplus/common'; import { readXml } from '@jnxplus/xml'; -import { ExecutorContext, workspaceRoot } from '@nx/devkit'; +import { + ExecutorContext, + NxJsonConfiguration, + readJsonFile, + workspaceRoot, +} from '@nx/devkit'; import * as fs from 'fs'; import * as path from 'path'; @@ -33,10 +38,35 @@ function isWrapperExistsFunction() { return fs.existsSync(mvnwPath); } -export function getMavenRootDirectory() { - const pomXmlPath = path.join(workspaceRoot, 'pom.xml'); - if (fs.existsSync(pomXmlPath)) { - return ''; +export function getMavenRootDirectory(): string { + const nxJsonPath = path.join(workspaceRoot, 'nx.json'); + + const nxJson = readJsonFile(nxJsonPath); + + const plugin = (nxJson?.plugins || []).find((p) => + typeof p === 'string' + ? p === '@jnxplus/nx-maven' + : p.plugin === '@jnxplus/nx-maven', + ); + + if (typeof plugin === 'string') { + const pomXmlPath = path.join(workspaceRoot, 'pom.xml'); + if (fs.existsSync(pomXmlPath)) { + return ''; + } + return 'nx-maven'; } - return 'nx-maven'; + + const options = plugin?.options; + + if ( + typeof options === 'object' && + options && + 'mavenRootDirectory' in options && + typeof options.mavenRootDirectory === 'string' + ) { + return options.mavenRootDirectory; + } + + return ''; } diff --git a/testing-projects/e2e/nx-boot-maven-e2e/tests/nx-boot-maven.spec.ts b/testing-projects/e2e/nx-boot-maven-e2e/tests/nx-boot-maven.spec.ts index 3b7630331..b23c18cda 100644 --- a/testing-projects/e2e/nx-boot-maven-e2e/tests/nx-boot-maven.spec.ts +++ b/testing-projects/e2e/nx-boot-maven-e2e/tests/nx-boot-maven.spec.ts @@ -113,7 +113,7 @@ describe('nx-boot-maven e2e', () => { // Making sure the nx.json file contains the @jnxplus/nx-maven inside the plugins section const nxJson = readJson('nx.json'); - expect(nxJson.plugins.includes('@jnxplus/nx-maven')).toBeTruthy(); + //expect(nxJson.plugins.includes('@jnxplus/nx-maven')).toBeTruthy(); expect(() => checkFilesExist( diff --git a/testing-projects/e2e/nx-maven-e2e/tests/nx-maven.spec.ts b/testing-projects/e2e/nx-maven-e2e/tests/nx-maven.spec.ts index d62fa8787..454659ffb 100644 --- a/testing-projects/e2e/nx-maven-e2e/tests/nx-maven.spec.ts +++ b/testing-projects/e2e/nx-maven-e2e/tests/nx-maven.spec.ts @@ -115,7 +115,7 @@ describe('nx-maven e2e', () => { // Making sure the nx.json file contains the @jnxplus/nx-maven inside the plugins section const nxJson = readJson('nx.json'); - expect(nxJson.plugins.includes('@jnxplus/nx-maven')).toBeTruthy(); + //expect(nxJson.plugins.includes('@jnxplus/nx-maven')).toBeTruthy(); expect(() => checkFilesExist( diff --git a/testing-projects/e2e/nx-maven-subfolder-e2e/tests/nx-maven.spec.ts b/testing-projects/e2e/nx-maven-subfolder-e2e/tests/nx-maven.spec.ts index effbfb18e..86601bb2e 100644 --- a/testing-projects/e2e/nx-maven-subfolder-e2e/tests/nx-maven.spec.ts +++ b/testing-projects/e2e/nx-maven-subfolder-e2e/tests/nx-maven.spec.ts @@ -85,7 +85,7 @@ describe('nx-maven e2e', () => { runPackageManagerInstallLinks(); await runNxCommandAsync( - `generate @jnxplus/nx-maven:init --parentProjectName ${parentProjectName} --useSubfolder`, + `generate @jnxplus/nx-maven:init --parentProjectName ${parentProjectName} --mavenRootDirectory nx-maven`, ); addSpringBootVersion(); @@ -116,7 +116,7 @@ describe('nx-maven e2e', () => { // Making sure the nx.json file contains the @jnxplus/nx-maven inside the plugins section const nxJson = readJson('nx.json'); - expect(nxJson.plugins.includes('@jnxplus/nx-maven')).toBeTruthy(); + //expect(nxJson.plugins.includes('@jnxplus/nx-maven')).toBeTruthy(); expect(() => checkFilesExist( diff --git a/testing-projects/e2e/nx-micronaut-maven-e2e/tests/nx-micronaut-maven.spec.ts b/testing-projects/e2e/nx-micronaut-maven-e2e/tests/nx-micronaut-maven.spec.ts index a655d0b5e..882e855cf 100644 --- a/testing-projects/e2e/nx-micronaut-maven-e2e/tests/nx-micronaut-maven.spec.ts +++ b/testing-projects/e2e/nx-micronaut-maven-e2e/tests/nx-micronaut-maven.spec.ts @@ -115,7 +115,7 @@ describe('nx-micronaut-maven e2e', () => { // Making sure the nx.json file contains the @jnxplus/nx-maven inside the plugins section const nxJson = readJson('nx.json'); - expect(nxJson.plugins.includes('@jnxplus/nx-maven')).toBeTruthy(); + //expect(nxJson.plugins.includes('@jnxplus/nx-maven')).toBeTruthy(); expect(() => checkFilesExist( diff --git a/testing-projects/e2e/nx-quarkus-maven-e2e/tests/nx-quarkus-maven.spec.ts b/testing-projects/e2e/nx-quarkus-maven-e2e/tests/nx-quarkus-maven.spec.ts index 203cde2fc..227f424f6 100644 --- a/testing-projects/e2e/nx-quarkus-maven-e2e/tests/nx-quarkus-maven.spec.ts +++ b/testing-projects/e2e/nx-quarkus-maven-e2e/tests/nx-quarkus-maven.spec.ts @@ -115,7 +115,7 @@ describe('nx-quarkus-maven e2e', () => { // Making sure the nx.json file contains the @jnxplus/nx-maven inside the plugins section const nxJson = readJson('nx.json'); - expect(nxJson.plugins.includes('@jnxplus/nx-maven')).toBeTruthy(); + //expect(nxJson.plugins.includes('@jnxplus/nx-maven')).toBeTruthy(); expect(() => checkFilesExist(