Skip to content

Commit

Permalink
build: refactor getProjectRootFromTree function (#1337)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
khalilou88 authored Oct 10, 2024
1 parent a7be6ad commit aa7ddbc
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 51 deletions.
1 change: 1 addition & 0 deletions .github/workflows/e2e-plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion packages/nx-maven/src/generators/application/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
addMissedProperties,
addProjectToAggregator,
extractRootPomValues,
getAggregatorProjectRoot,
getMavenRootDirectory,
getParentProjectValues,
getPlugin,
Expand Down Expand Up @@ -71,6 +72,7 @@ interface NormalizedSchema extends NxMavenAppGeneratorSchema {
serveTargetName: string;
testTargetName: string;
integrationTestTargetName: string;
aggregatorProjectRoot: string;
}

function normalizeOptions(
Expand Down Expand Up @@ -133,6 +135,12 @@ function normalizeOptions(
const testTargetName = getTestTargetName(plugin);
const integrationTestTargetName = getIntegrationTestTargetName(plugin);

const aggregatorProjectRoot = getAggregatorProjectRoot(
tree,
options.aggregatorProject,
mavenRootDirectory,
);

return {
...options,
projectName,
Expand All @@ -158,6 +166,7 @@ function normalizeOptions(
serveTargetName,
testTargetName,
integrationTestTargetName,
aggregatorProjectRoot,
};
}

Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 10 additions & 1 deletion packages/nx-maven/src/generators/library/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
addLibraryToProjects,
addMissedProperties,
addProjectToAggregator,
getAggregatorProjectRoot,
getMavenRootDirectory,
getParentProjectValues,
getPlugin,
Expand Down Expand Up @@ -58,6 +59,7 @@ interface NormalizedSchema extends NxMavenLibGeneratorSchema {
mavenRootDirectory: string;
buildTargetName: string;
testTargetName: string;
aggregatorProjectRoot: string;
}

function normalizeOptions(
Expand Down Expand Up @@ -105,6 +107,12 @@ function normalizeOptions(
const buildTargetName = getBuildTargetName(plugin);
const testTargetName = getTestTargetName(plugin);

const aggregatorProjectRoot = getAggregatorProjectRoot(
tree,
options.aggregatorProject,
mavenRootDirectory,
);

return {
...options,
projectName,
Expand All @@ -124,6 +132,7 @@ function normalizeOptions(
mavenRootDirectory,
buildTargetName,
testTargetName,
aggregatorProjectRoot,
};
}

Expand Down Expand Up @@ -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);
Expand Down
15 changes: 12 additions & 3 deletions packages/nx-maven/src/generators/parent-project/generator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
parseTags,
generateProjectDirectory,
generateProjectName,
generateProjectRoot,
generateSimpleProjectName,
getBuildTargetName,
kotlinVersion,
mavenCompilerPluginVersion,
mavenEnforcerPluginVersion,
Expand All @@ -16,9 +16,9 @@ import {
micronautSerializationVersion,
micronautTestResourcesVersion,
micronautVersion,
parseTags,
quarkusVersion,
springBootVersion,
getBuildTargetName,
} from '@jnxplus/common';
import {
Tree,
Expand All @@ -32,6 +32,7 @@ import * as path from 'path';
import {
addMissedProperties,
addProjectToAggregator,
getAggregatorProjectRoot,
getMavenRootDirectory,
getParentProjectValues,
getPlugin,
Expand Down Expand Up @@ -69,6 +70,7 @@ interface NormalizedSchema extends NxMavenParentProjectGeneratorSchema {
mavenSurefirePluginVersion: string;
mavenFailsafePluginVersion: string;
buildTargetName: string;
aggregatorProjectRoot: string;
}

function normalizeOptions(
Expand Down Expand Up @@ -106,6 +108,12 @@ function normalizeOptions(
const plugin = getPlugin();
const buildTargetName = getBuildTargetName(plugin);

const aggregatorProjectRoot = getAggregatorProjectRoot(
tree,
options.aggregatorProject,
mavenRootDirectory,
);

return {
...options,
projectName,
Expand All @@ -131,6 +139,7 @@ function normalizeOptions(
mavenSurefirePluginVersion,
mavenFailsafePluginVersion,
buildTargetName,
aggregatorProjectRoot,
};
}

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
97 changes: 62 additions & 35 deletions packages/nx-maven/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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`,
);
Expand Down

0 comments on commit aa7ddbc

Please sign in to comment.