From aa7ddbc2b0bf25d97975c59aee110c83a391cbfc Mon Sep 17 00:00:00 2001 From: Khalil LAGRIDA <32600911+khalilou88@users.noreply.github.com> Date: Thu, 10 Oct 2024 22:34:53 +0200 Subject: [PATCH] build: refactor getProjectRootFromTree function (#1337) * build: refactor getProjectRootFromTree function * build: work in progress * build: work in progress * build: work in progress * build: work in progress * build: work in progress * build: work in progress --- .github/workflows/e2e-plugin.yml | 1 + .../src/generators/application/generator.ts | 11 ++- .../src/generators/library/generator.ts | 11 ++- .../generators/parent-project/generator.ts | 15 ++- .../src/generators/parent-project/schema.d.ts | 2 +- packages/nx-maven/src/utils/index.ts | 97 ++++++++++++------- .../nx-maven/spring-boot-parent-pom.spec.ts | 20 ++-- 7 files changed, 106 insertions(+), 51 deletions(-) diff --git a/.github/workflows/e2e-plugin.yml b/.github/workflows/e2e-plugin.yml index 4979fcf08..a3560bce7 100644 --- a/.github/workflows/e2e-plugin.yml +++ b/.github/workflows/e2e-plugin.yml @@ -31,6 +31,7 @@ on: - nx-maven spring-boot bom e2e - nx-maven spring-boot-parent-pom e2e - should use specified options and hyphen in groupId to create an application + - should --aggregator-project option works and generate java nested sub-projects os: type: choice description: Os diff --git a/packages/nx-maven/src/generators/application/generator.ts b/packages/nx-maven/src/generators/application/generator.ts index e26a0e4c9..e2ac98c06 100644 --- a/packages/nx-maven/src/generators/application/generator.ts +++ b/packages/nx-maven/src/generators/application/generator.ts @@ -37,6 +37,7 @@ import { addMissedProperties, addProjectToAggregator, extractRootPomValues, + getAggregatorProjectRoot, getMavenRootDirectory, getParentProjectValues, getPlugin, @@ -71,6 +72,7 @@ interface NormalizedSchema extends NxMavenAppGeneratorSchema { serveTargetName: string; testTargetName: string; integrationTestTargetName: string; + aggregatorProjectRoot: string; } function normalizeOptions( @@ -133,6 +135,12 @@ function normalizeOptions( const testTargetName = getTestTargetName(plugin); const integrationTestTargetName = getIntegrationTestTargetName(plugin); + const aggregatorProjectRoot = getAggregatorProjectRoot( + tree, + options.aggregatorProject, + mavenRootDirectory, + ); + return { ...options, projectName, @@ -158,6 +166,7 @@ function normalizeOptions( serveTargetName, testTargetName, integrationTestTargetName, + aggregatorProjectRoot, }; } @@ -486,7 +495,7 @@ async function applicationGenerator( addFiles(tree, normalizedOptions); addProjectToAggregator(tree, { projectRoot: normalizedOptions.projectRoot, - aggregatorProject: normalizedOptions.aggregatorProject, + aggregatorProjectRoot: normalizedOptions.aggregatorProjectRoot, mavenRootDirectory: normalizedOptions.mavenRootDirectory, }); if (!options.skipFormat) { diff --git a/packages/nx-maven/src/generators/library/generator.ts b/packages/nx-maven/src/generators/library/generator.ts index c0e9ec123..11b8b7f10 100644 --- a/packages/nx-maven/src/generators/library/generator.ts +++ b/packages/nx-maven/src/generators/library/generator.ts @@ -30,6 +30,7 @@ import { addLibraryToProjects, addMissedProperties, addProjectToAggregator, + getAggregatorProjectRoot, getMavenRootDirectory, getParentProjectValues, getPlugin, @@ -58,6 +59,7 @@ interface NormalizedSchema extends NxMavenLibGeneratorSchema { mavenRootDirectory: string; buildTargetName: string; testTargetName: string; + aggregatorProjectRoot: string; } function normalizeOptions( @@ -105,6 +107,12 @@ function normalizeOptions( const buildTargetName = getBuildTargetName(plugin); const testTargetName = getTestTargetName(plugin); + const aggregatorProjectRoot = getAggregatorProjectRoot( + tree, + options.aggregatorProject, + mavenRootDirectory, + ); + return { ...options, projectName, @@ -124,6 +132,7 @@ function normalizeOptions( mavenRootDirectory, buildTargetName, testTargetName, + aggregatorProjectRoot, }; } @@ -336,7 +345,7 @@ async function libraryGenerator( addFiles(tree, normalizedOptions); addProjectToAggregator(tree, { projectRoot: normalizedOptions.projectRoot, - aggregatorProject: normalizedOptions.aggregatorProject, + aggregatorProjectRoot: normalizedOptions.aggregatorProjectRoot, mavenRootDirectory: normalizedOptions.mavenRootDirectory, }); addLibraryToProjects(tree, normalizedOptions); diff --git a/packages/nx-maven/src/generators/parent-project/generator.ts b/packages/nx-maven/src/generators/parent-project/generator.ts index 3009af95c..bce82b659 100644 --- a/packages/nx-maven/src/generators/parent-project/generator.ts +++ b/packages/nx-maven/src/generators/parent-project/generator.ts @@ -1,9 +1,9 @@ import { - parseTags, generateProjectDirectory, generateProjectName, generateProjectRoot, generateSimpleProjectName, + getBuildTargetName, kotlinVersion, mavenCompilerPluginVersion, mavenEnforcerPluginVersion, @@ -16,9 +16,9 @@ import { micronautSerializationVersion, micronautTestResourcesVersion, micronautVersion, + parseTags, quarkusVersion, springBootVersion, - getBuildTargetName, } from '@jnxplus/common'; import { Tree, @@ -32,6 +32,7 @@ import * as path from 'path'; import { addMissedProperties, addProjectToAggregator, + getAggregatorProjectRoot, getMavenRootDirectory, getParentProjectValues, getPlugin, @@ -69,6 +70,7 @@ interface NormalizedSchema extends NxMavenParentProjectGeneratorSchema { mavenSurefirePluginVersion: string; mavenFailsafePluginVersion: string; buildTargetName: string; + aggregatorProjectRoot: string; } function normalizeOptions( @@ -106,6 +108,12 @@ function normalizeOptions( const plugin = getPlugin(); const buildTargetName = getBuildTargetName(plugin); + const aggregatorProjectRoot = getAggregatorProjectRoot( + tree, + options.aggregatorProject, + mavenRootDirectory, + ); + return { ...options, projectName, @@ -131,6 +139,7 @@ function normalizeOptions( mavenSurefirePluginVersion, mavenFailsafePluginVersion, buildTargetName, + aggregatorProjectRoot, }; } @@ -188,7 +197,7 @@ async function parentProjectGenerator( addFiles(tree, normalizedOptions); addProjectToAggregator(tree, { projectRoot: normalizedOptions.projectRoot, - aggregatorProject: normalizedOptions.aggregatorProject, + aggregatorProjectRoot: normalizedOptions.aggregatorProjectRoot, mavenRootDirectory: normalizedOptions.mavenRootDirectory, }); if (!options.skipFormat) { diff --git a/packages/nx-maven/src/generators/parent-project/schema.d.ts b/packages/nx-maven/src/generators/parent-project/schema.d.ts index 3dd102b13..9c500a7ff 100644 --- a/packages/nx-maven/src/generators/parent-project/schema.d.ts +++ b/packages/nx-maven/src/generators/parent-project/schema.d.ts @@ -8,7 +8,7 @@ export interface NxMavenParentProjectGeneratorSchema { projectType: ProjectType; groupId: string; projectVersion?: string; - parentProject?: string; + parentProject: string; aggregatorProject?: string; framework?: FrameworkType; language: 'java' | 'kotlin' | 'java-kotlin'; diff --git a/packages/nx-maven/src/utils/index.ts b/packages/nx-maven/src/utils/index.ts index 5ff7dbb94..ec30110c6 100644 --- a/packages/nx-maven/src/utils/index.ts +++ b/packages/nx-maven/src/utils/index.ts @@ -114,29 +114,31 @@ export function getPlugin(): PluginConfiguration | undefined { function getProjectRootFromTree( tree: Tree, mavenRootDirectory: string, - projectName: string | undefined, -) { - let projectRoot = mavenRootDirectory; + projectName: string, +): string { + let projectRoot = ''; - if (projectName) { - try { - projectRoot = readProjectConfiguration(tree, projectName).root; - } catch (err) { - const mavenRootDirAbsolutePath = path.join( - workspaceRoot, - mavenRootDirectory, - ); + try { + projectRoot = readProjectConfiguration(tree, projectName).root; + return projectRoot; + } catch (err) { + logger.warn(err); - const projectBasedir = getExpressionValue( - 'project.basedir', - mavenRootDirAbsolutePath, - projectName, - ); - projectRoot = path.relative(workspaceRoot, projectBasedir); - } - } + const mavenRootDirAbsolutePath = path.join( + workspaceRoot, + mavenRootDirectory, + ); - return projectRoot; + const projectBasedir = getExpressionValue( + 'project.basedir', + mavenRootDirAbsolutePath, + projectName, + ); + + projectRoot = path.relative(workspaceRoot, projectBasedir); + + return projectRoot; + } } export function getExpressionValue( @@ -165,21 +167,19 @@ export function addProjectToAggregator( tree: Tree, options: { projectRoot: string; - aggregatorProject: string | undefined; + aggregatorProjectRoot: string; mavenRootDirectory: string; }, ) { - const aggregatorProjectRoot = getProjectRootFromTree( - tree, - options.mavenRootDirectory, - options.aggregatorProject, + const parentProjectPomPath = path.join( + options.aggregatorProjectRoot, + 'pom.xml', ); - const parentProjectPomPath = path.join(aggregatorProjectRoot, 'pom.xml'); const xmlDoc = readXmlTree(tree, parentProjectPomPath); const aggregatorProjectAbsolutPath = path.join( workspaceRoot, - aggregatorProjectRoot, + options.aggregatorProjectRoot, ); const projectAbsolutePath = path.join(workspaceRoot, options.projectRoot); @@ -220,6 +220,10 @@ export function addLibraryToProjects( mavenRootDirectory: string; }, ) { + logger.info( + `Adding lib ${options.projectName} to projects: ${JSON.stringify(options.parsedProjects)}`, + ); + for (const projectName of options.parsedProjects) { const projectRoot = getProjectRootFromTree( tree, @@ -505,22 +509,33 @@ export function getParentProjectValues( tree: Tree, mavenRootDirectory: string, projectRoot: string, - parentProject: string | undefined, + parentProject: string, ) { - const parentProjectRoot = getProjectRootFromTree( - tree, - mavenRootDirectory, - parentProject, - ); + let pomXmlContent; + let parentProjectRoot; + if (!parentProject) { + parentProjectRoot = mavenRootDirectory; + pomXmlContent = readXmlTree( + tree, + joinPathFragments(parentProjectRoot, 'pom.xml'), + ); + } else { + parentProjectRoot = getProjectRootFromTree( + tree, + mavenRootDirectory, + parentProject, + ); - const parentProjectPomPath = path.join(parentProjectRoot, 'pom.xml'); + const parentProjectPomPath = path.join(parentProjectRoot, 'pom.xml'); + + pomXmlContent = readXmlTree(tree, parentProjectPomPath); + } const relativePath = joinPathFragments( path.relative(projectRoot, parentProjectRoot), 'pom.xml', ); - const pomXmlContent = readXmlTree(tree, parentProjectPomPath); const parentProjectName = getArtifactId(pomXmlContent); const parentGroupId = getGroupId(parentProjectName, pomXmlContent); const parentProjectVersion = getVersion(parentProjectName, pomXmlContent); @@ -572,3 +587,15 @@ export function getSkipAggregatorProjectLinkingOption( return false; } + +export function getAggregatorProjectRoot( + tree: Tree, + aggregatorProject: string | undefined, + mavenRootDirectory: string, +) { + if (!aggregatorProject) { + return mavenRootDirectory; + } + + return getProjectRootFromTree(tree, mavenRootDirectory, aggregatorProject); +} diff --git a/testing-projects/jnxplus-e2e/tests/nx-maven/spring-boot-parent-pom.spec.ts b/testing-projects/jnxplus-e2e/tests/nx-maven/spring-boot-parent-pom.spec.ts index b09cae950..d6bf2cb99 100644 --- a/testing-projects/jnxplus-e2e/tests/nx-maven/spring-boot-parent-pom.spec.ts +++ b/testing-projects/jnxplus-e2e/tests/nx-maven/spring-boot-parent-pom.spec.ts @@ -1837,15 +1837,23 @@ describe('nx-maven spring-boot-parent-pom e2e', () => { await killProcessAndPorts(process.pid, port); }, 240000); - it('should generate java nested sub-projects', async () => { + it('should --aggregator-project option works and generate java nested sub-projects', async () => { const appsParentProject = uniq('apps-parent-project-'); await runNxCommandAsync( `generate @jnxplus/nx-maven:parent-project ${appsParentProject} --framework none`, ); + const localTmpDir = path.dirname(tmpProjPath()); + const projectJson1 = path.join( + localTmpDir, + 'proj', + appsParentProject, + 'project.json', + ); + fse.removeSync(projectJson1); const appName = uniq('boot-maven-app-'); await runNxCommandAsync( - `generate @jnxplus/nx-maven:application ${appName} --framework spring-boot --simpleName --parent-project ${appsParentProject} --directory ${appsParentProject} --simplePackageName false`, + `generate @jnxplus/nx-maven:application ${appName} --framework spring-boot --simpleName --aggregator-project ${appsParentProject} --parent-project ${appsParentProject} --directory ${appsParentProject} --simplePackageName false`, ); const buildResult = await runNxCommandAsync(`build ${appName}`); expect(buildResult.stdout).toContain('Executor ran for Build'); @@ -1876,14 +1884,6 @@ describe('nx-maven spring-boot-parent-pom e2e', () => { expect(thirdBuildResult.stdout).toContain('Executor ran for Build'); //graph - const localTmpDir = path.dirname(tmpProjPath()); - const projectJson1 = path.join( - localTmpDir, - 'proj', - appsParentProject, - 'project.json', - ); - fse.removeSync(projectJson1); const depGraphResult = await runNxCommandAsync( `dep-graph --file=dep-graph.json`, );