From 2b6523c4964ce04174436e602e722580157fb7d2 Mon Sep 17 00:00:00 2001 From: khalilou88 Date: Fri, 5 Jan 2024 23:09:05 +0100 Subject: [PATCH] build: add function for code reuse --- .../src/generators/application/generator.ts | 55 +++++-------------- .../src/generators/library/generator.ts | 39 +++---------- .../generators/parent-project/generator.ts | 31 ++--------- packages/nx-maven/src/utils/index.ts | 46 ++++++++++++++++ 4 files changed, 74 insertions(+), 97 deletions(-) diff --git a/packages/nx-maven/src/generators/application/generator.ts b/packages/nx-maven/src/generators/application/generator.ts index c29506e54..b9be59323 100644 --- a/packages/nx-maven/src/generators/application/generator.ts +++ b/packages/nx-maven/src/generators/application/generator.ts @@ -13,7 +13,6 @@ import { quarkusVersion, springBootVersion, } from '@jnxplus/common'; -import { readXmlTree } from '@jnxplus/xml'; import { ProjectConfiguration, Tree, @@ -23,17 +22,14 @@ import { joinPathFragments, names, offsetFromRoot, - readProjectConfiguration, } from '@nx/devkit'; import * as path from 'path'; import { addMissedProperties, addProjectToAggregator, - getArtifactId, - getDependencyManagement, - getGroupId, + extractRootPomValues, getMavenRootDirectory, - getVersion, + getParentProjectValues, } from '../../utils'; import { NxMavenAppGeneratorSchema } from './schema'; @@ -53,7 +49,6 @@ interface NormalizedSchema extends NxMavenAppGeneratorSchema { parentProjectName: string; parentProjectVersion: string; relativePath: string; - parentProjectRoot: string; isCustomPort: boolean; springBootVersion: string; quarkusVersion: string; @@ -100,42 +95,21 @@ function normalizeOptions( const packageDirectory = generatePackageDirectory(packageName); - const rootPomXmlContent = readXmlTree( - tree, - path.join(mavenRootDirectory, 'pom.xml'), - ); - - const rootParentProjectName = getArtifactId(rootPomXmlContent); - - const parentProjectRoot = - options.parentProject && options.parentProject !== rootParentProjectName - ? readProjectConfiguration(tree, options.parentProject).root - : mavenRootDirectory - ? mavenRootDirectory - : ''; - - const parentProjectPomPath = path.join(parentProjectRoot, 'pom.xml'); - - const pomXmlContent = readXmlTree(tree, parentProjectPomPath); - const relativePath = joinPathFragments( - path.relative(projectRoot, parentProjectRoot), - 'pom.xml', - ); - - const parentProjectName = getArtifactId(pomXmlContent); - const parentGroupId = getGroupId(parentProjectName, pomXmlContent); - const parentProjectVersion = getVersion(parentProjectName, pomXmlContent); + const [relativePath, parentProjectName, parentGroupId, parentProjectVersion] = + getParentProjectValues( + tree, + mavenRootDirectory, + projectRoot, + options.parentProject, + ); const isCustomPort = isCustomPortFunction({ port: options.port }); - let quarkusVersion = ''; - if (options.framework === 'quarkus') { - quarkusVersion = - rootPomXmlContent?.childNamed('properties')?.childNamed('quarkus.version') - ?.val || 'quarkusVersion'; - } - - const dependencyManagement = getDependencyManagement(rootPomXmlContent); + const [quarkusVersion, dependencyManagement] = extractRootPomValues( + tree, + mavenRootDirectory, + options.framework, + ); return { ...options, @@ -150,7 +124,6 @@ function normalizeOptions( parentProjectName, parentProjectVersion, relativePath, - parentProjectRoot, isCustomPort, springBootVersion, quarkusVersion, diff --git a/packages/nx-maven/src/generators/library/generator.ts b/packages/nx-maven/src/generators/library/generator.ts index 63a66d2c5..7b3e222c1 100644 --- a/packages/nx-maven/src/generators/library/generator.ts +++ b/packages/nx-maven/src/generators/library/generator.ts @@ -11,7 +11,6 @@ import { quarkusVersion, springBootVersion, } from '@jnxplus/common'; -import { readXmlTree } from '@jnxplus/xml'; import { ProjectConfiguration, Tree, @@ -21,17 +20,14 @@ import { joinPathFragments, names, offsetFromRoot, - readProjectConfiguration, } from '@nx/devkit'; import * as path from 'path'; import { addLibraryToProjects, addMissedProperties, addProjectToAggregator, - getArtifactId, - getGroupId, getMavenRootDirectory, - getVersion, + getParentProjectValues, } from '../../utils'; import { NxMavenLibGeneratorSchema } from './schema'; @@ -51,7 +47,6 @@ interface NormalizedSchema extends NxMavenLibGeneratorSchema { parentProjectName: string; parentProjectVersion: string; relativePath: string; - parentProjectRoot: string; springBootVersion: string; quarkusVersion: string; micronautVersion: string; @@ -91,30 +86,13 @@ function normalizeOptions( const parsedProjects = generateParsedProjects({ projects: options.projects }); - const rootPomXmlContent = readXmlTree( - tree, - path.join(mavenRootDirectory, 'pom.xml'), - ); - const rootParentProjectName = getArtifactId(rootPomXmlContent); - - const parentProjectRoot = - options.parentProject && options.parentProject !== rootParentProjectName - ? readProjectConfiguration(tree, options.parentProject).root - : mavenRootDirectory - ? mavenRootDirectory - : ''; - - const parentProjectPomPath = path.join(parentProjectRoot, 'pom.xml'); - - const pomXmlContent = readXmlTree(tree, parentProjectPomPath); - const relativePath = joinPathFragments( - path.relative(projectRoot, parentProjectRoot), - 'pom.xml', - ); - - const parentProjectName = getArtifactId(pomXmlContent); - const parentGroupId = getGroupId(parentProjectName, pomXmlContent); - const parentProjectVersion = getVersion(parentProjectName, pomXmlContent); + const [relativePath, parentProjectName, parentGroupId, parentProjectVersion] = + getParentProjectValues( + tree, + mavenRootDirectory, + projectRoot, + options.parentProject, + ); return { ...options, @@ -129,7 +107,6 @@ function normalizeOptions( parentProjectName, parentProjectVersion, relativePath, - parentProjectRoot, springBootVersion, quarkusVersion, micronautVersion, diff --git a/packages/nx-maven/src/generators/parent-project/generator.ts b/packages/nx-maven/src/generators/parent-project/generator.ts index 41a855d5f..ca44d3655 100644 --- a/packages/nx-maven/src/generators/parent-project/generator.ts +++ b/packages/nx-maven/src/generators/parent-project/generator.ts @@ -18,25 +18,20 @@ import { quarkusVersion, springBootVersion, } from '@jnxplus/common'; -import { readXmlTree } from '@jnxplus/xml'; import { Tree, addProjectConfiguration, formatFiles, generateFiles, - joinPathFragments, names, offsetFromRoot, - readProjectConfiguration, } from '@nx/devkit'; import * as path from 'path'; import { addMissedProperties, addProjectToAggregator, - getArtifactId, - getGroupId, getMavenRootDirectory, - getVersion, + getParentProjectValues, } from '../../utils'; import { NxMavenParentProjectGeneratorSchema } from './schema'; @@ -56,7 +51,6 @@ interface NormalizedSchema extends NxMavenParentProjectGeneratorSchema { parentProjectName: string; parentProjectVersion: string; relativePath: string; - parentProjectRoot: string; springBootVersion: string; quarkusVersion: string; micronautVersion: string; @@ -97,25 +91,13 @@ function normalizeOptions( const parsedTags = generateParsedTags({ tags: options.tags }); - let parentProjectRoot = mavenRootDirectory; - if (options.parentProject) { - parentProjectRoot = readProjectConfiguration( + const [relativePath, parentProjectName, parentGroupId, parentProjectVersion] = + getParentProjectValues( tree, + mavenRootDirectory, + projectRoot, options.parentProject, - ).root; - } - - const parentProjectPomPath = path.join(parentProjectRoot, 'pom.xml'); - - const pomXmlContent = readXmlTree(tree, parentProjectPomPath); - const relativePath = joinPathFragments( - path.relative(projectRoot, parentProjectRoot), - 'pom.xml', - ); - - const parentProjectName = getArtifactId(pomXmlContent); - const parentGroupId = getGroupId(parentProjectName, pomXmlContent); - const parentProjectVersion = getVersion(parentProjectName, pomXmlContent); + ); return { ...options, @@ -127,7 +109,6 @@ function normalizeOptions( parentProjectName, parentProjectVersion, relativePath, - parentProjectRoot, springBootVersion, quarkusVersion, micronautVersion, diff --git a/packages/nx-maven/src/utils/index.ts b/packages/nx-maven/src/utils/index.ts index d629e126f..48f9297c6 100644 --- a/packages/nx-maven/src/utils/index.ts +++ b/packages/nx-maven/src/utils/index.ts @@ -431,3 +431,49 @@ export function getEffectiveVersion( return version; } + +export function getParentProjectValues( + tree: Tree, + mavenRootDirectory: string, + projectRoot: string, + parentProject: string | undefined, +) { + let parentProjectRoot = mavenRootDirectory; + if (parentProject) { + parentProjectRoot = readProjectConfiguration(tree, parentProject).root; + } + + const parentProjectPomPath = path.join(parentProjectRoot, 'pom.xml'); + + 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); + + return [relativePath, parentProjectName, parentGroupId, parentProjectVersion]; +} + +export function extractRootPomValues( + tree: Tree, + mavenRootDirectory: string, + framework: string | undefined, +): [string, 'bom' | 'spring-boot-parent-pom' | 'micronaut-parent-pom'] { + const rootPomXmlContent = readXmlTree( + tree, + path.join(mavenRootDirectory, 'pom.xml'), + ); + + let quarkusVersion = ''; + if (framework === 'quarkus') { + quarkusVersion = + rootPomXmlContent?.childNamed('properties')?.childNamed('quarkus.version') + ?.val || 'quarkusVersion'; + } + + return [quarkusVersion, getDependencyManagement(rootPomXmlContent)]; +}