Skip to content

Commit

Permalink
build: add function for code reuse (#737)
Browse files Browse the repository at this point in the history
* build: add function for code reuse

* build: work in progress

* build: work in progress

---------

Co-authored-by: khalilou88 <[email protected]>
  • Loading branch information
khalilou88 and khalilou88 authored Jan 6, 2024
1 parent fe682ed commit a8e7492
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 98 deletions.
55 changes: 14 additions & 41 deletions packages/nx-maven/src/generators/application/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
quarkusVersion,
springBootVersion,
} from '@jnxplus/common';
import { readXmlTree } from '@jnxplus/xml';
import {
ProjectConfiguration,
Tree,
Expand All @@ -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';

Expand All @@ -53,7 +49,6 @@ interface NormalizedSchema extends NxMavenAppGeneratorSchema {
parentProjectName: string;
parentProjectVersion: string;
relativePath: string;
parentProjectRoot: string;
isCustomPort: boolean;
springBootVersion: string;
quarkusVersion: string;
Expand Down Expand Up @@ -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,
Expand All @@ -150,7 +124,6 @@ function normalizeOptions(
parentProjectName,
parentProjectVersion,
relativePath,
parentProjectRoot,
isCustomPort,
springBootVersion,
quarkusVersion,
Expand Down
39 changes: 8 additions & 31 deletions packages/nx-maven/src/generators/library/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
quarkusVersion,
springBootVersion,
} from '@jnxplus/common';
import { readXmlTree } from '@jnxplus/xml';
import {
ProjectConfiguration,
Tree,
Expand All @@ -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';

Expand All @@ -51,7 +47,6 @@ interface NormalizedSchema extends NxMavenLibGeneratorSchema {
parentProjectName: string;
parentProjectVersion: string;
relativePath: string;
parentProjectRoot: string;
springBootVersion: string;
quarkusVersion: string;
micronautVersion: string;
Expand Down Expand Up @@ -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,
Expand All @@ -129,7 +107,6 @@ function normalizeOptions(
parentProjectName,
parentProjectVersion,
relativePath,
parentProjectRoot,
springBootVersion,
quarkusVersion,
micronautVersion,
Expand Down
31 changes: 6 additions & 25 deletions packages/nx-maven/src/generators/parent-project/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -56,7 +51,6 @@ interface NormalizedSchema extends NxMavenParentProjectGeneratorSchema {
parentProjectName: string;
parentProjectVersion: string;
relativePath: string;
parentProjectRoot: string;
springBootVersion: string;
quarkusVersion: string;
micronautVersion: string;
Expand Down Expand Up @@ -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,
Expand All @@ -127,7 +109,6 @@ function normalizeOptions(
parentProjectName,
parentProjectVersion,
relativePath,
parentProjectRoot,
springBootVersion,
quarkusVersion,
micronautVersion,
Expand Down
66 changes: 65 additions & 1 deletion packages/nx-maven/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ function isParentPomExits(
return parentPom === artifactIdXml?.val;
}

export function getDependencyManagement(
function getDependencyManagement(
xmldoc: XmlDocument,
): 'bom' | 'spring-boot-parent-pom' | 'micronaut-parent-pom' {
if (isParentPomExits(xmldoc, 'spring-boot-starter-parent')) {
Expand Down Expand Up @@ -431,3 +431,67 @@ export function getEffectiveVersion(

return version;
}

export function getParentProjectValues(
tree: Tree,
mavenRootDirectory: string,
projectRoot: string,
parentProject: string | undefined,
) {
let parentProjectRoot = mavenRootDirectory;
if (parentProject) {
try {
parentProjectRoot = readProjectConfiguration(tree, parentProject).root;
} catch (err) {
const mavenRootDirAbsolutePath = path.join(
workspaceRoot,
mavenRootDirectory,
);

const projectBasedir = execSync(
`${getExecutable()} help:evaluate -Dexpression=project.basedir -q -DforceStdout -pl :${parentProject}`,
{
cwd: mavenRootDirAbsolutePath,
windowsHide: true,
},
)
.toString()
.trim();
parentProjectRoot = path.relative(workspaceRoot, projectBasedir);
}
}

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)];
}

0 comments on commit a8e7492

Please sign in to comment.