Skip to content

Commit

Permalink
fix: use help:evaluate to get project version (#704)
Browse files Browse the repository at this point in the history
* fix: use help:evaluate to get project version

* 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 <[email protected]>
  • Loading branch information
khalilou88 and khalilou88 authored Dec 22, 2023
1 parent 58b86e7 commit d6fdd93
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 137 deletions.
138 changes: 98 additions & 40 deletions packages/nx-maven/src/graph/create-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,27 @@ import {
import { execSync } from 'child_process';
import { existsSync } from 'fs';
import * as cache from 'memory-cache';
import { dirname, join } from 'path';
import * as path from 'path';
import { XmlDocument } from 'xmldoc';
import { getExecutable, getMavenRootDirectory } from '../utils';

export const createNodes: CreateNodes = [
'**/pom.xml',
(pomXmlFilePath: string) => {
let projectName;
const projectRoot = dirname(pomXmlFilePath);
const projectRoot = path.dirname(pomXmlFilePath);
let targets: {
[targetName: string]: TargetConfiguration;
} = {};

const projectJsonPath = join(workspaceRoot, projectRoot, 'project.json');
const projectPath = path.join(workspaceRoot, projectRoot);
const projectJsonPath = path.join(projectPath, 'project.json');

const mavenRootDirectory = getMavenRootDirectory();
const mavenRootDirAbsolutePath = path.join(
workspaceRoot,
mavenRootDirectory,
);

if (existsSync(projectJsonPath)) {
const projectJson = readJsonFile(projectJsonPath);
Expand All @@ -36,8 +43,15 @@ export const createNodes: CreateNodes = [
const pomXmlContent = readXml(pomXmlFilePath);
const artifactId = getArtifactId(pomXmlContent);
const groupId = getGroupId(artifactId, pomXmlContent);
const projectVersion = getVersion(artifactId, pomXmlContent);
const localRepositoryLocation = getLocalRepositoryLocation();
const projectVersion = getVersion(
artifactId,
pomXmlContent,
mavenRootDirAbsolutePath,
projectPath,
);
const localRepositoryLocation = getLocalRepositoryLocation(
mavenRootDirAbsolutePath,
);

const outputDirLocalRepo = getOutputDirLocalRepo(
localRepositoryLocation,
Expand All @@ -56,8 +70,15 @@ export const createNodes: CreateNodes = [
const pomXmlContent = readXml(pomXmlFilePath);
const artifactId = getArtifactId(pomXmlContent);
const groupId = getGroupId(artifactId, pomXmlContent);
const projectVersion = getVersion(artifactId, pomXmlContent);
const localRepositoryLocation = getLocalRepositoryLocation();
const projectVersion = getVersion(
artifactId,
pomXmlContent,
mavenRootDirAbsolutePath,
projectPath,
);
const localRepositoryLocation = getLocalRepositoryLocation(
mavenRootDirAbsolutePath,
);

const outputDirLocalRepo = getOutputDirLocalRepo(
localRepositoryLocation,
Expand Down Expand Up @@ -117,25 +138,6 @@ function getParentGroupId(
return groupIdXml?.val;
}

function getParentVersion(
artifactId: string,
pomXmlContent: XmlDocument,
): string {
const parentXml = pomXmlContent.childNamed('parent');

if (parentXml === undefined) {
throw new Error(`Parent tag not found for project ${artifactId}`);
}

const versionXml = parentXml.childNamed('version');

if (versionXml === undefined) {
throw new Error(`ParentVersion not found for project ${artifactId}`);
}

return versionXml?.val;
}

function getGroupId(artifactId: string, pomXmlContent: XmlDocument) {
const groupIdXml = pomXmlContent.childNamed('groupId');
if (groupIdXml === undefined) {
Expand All @@ -152,21 +154,13 @@ function getArtifactId(pomXmlContent: XmlDocument) {
return artifactIdXml.val;
}

function getVersion(artifactId: string, pomXmlContent: XmlDocument) {
const versionXml = pomXmlContent.childNamed('version');
if (versionXml === undefined) {
return getParentVersion(artifactId, pomXmlContent);
}
return versionXml.val;
}

function getOutputDirLocalRepo(
localRepositoryLocation: string,
groupId: string,
artifactId: string,
projectVersion: string,
) {
return join(
return path.join(
localRepositoryLocation,
`${groupId.replace(
new RegExp(/\./, 'g'),
Expand All @@ -183,7 +177,7 @@ function getTask(projectRoot: string) {
return 'install';
}

function getLocalRepositoryLocation() {
function getLocalRepositoryLocation(mavenRootDirAbsolutePath: string) {
const key = 'localRepositoryLocation';
const cachedData = cache.get(key);
if (cachedData) {
Expand All @@ -193,7 +187,11 @@ function getLocalRepositoryLocation() {
const regexp = /<localRepository>(.+?)<\/localRepository>/g;
const command = `${getExecutable()} help:effective-settings`;

const data = runCommandAndExtractRegExp(command, regexp);
const data = runCommandAndExtractRegExp(
command,
mavenRootDirAbsolutePath,
regexp,
);

// Store data in cache for future use
cache.put(key, data, 60000); // Cache for 60 seconds
Expand All @@ -211,14 +209,74 @@ function isPomPackaging(pomXmlContent: XmlDocument): boolean {
return packagingXml.val === 'pom';
}

function runCommandAndExtractRegExp(command: string, regexp: RegExp) {
const mavenRootDirectory = getMavenRootDirectory();
function runCommandAndExtractRegExp(
command: string,
mavenRootDirAbsolutePath: string,
regexp: RegExp,
) {
const objStr = execSync(command, {
cwd: join(workspaceRoot, mavenRootDirectory),
cwd: mavenRootDirAbsolutePath,
}).toString();

const matches = (objStr.match(regexp) || []).map((e) =>
e.replace(regexp, '$1'),
);
return matches[0];
}

function getVersion(
artifactId: string,
pomXmlContent: XmlDocument,
mavenRootDirAbsolutePath: string,
projectPath: string,
) {
let version;
const versionXml = pomXmlContent.childNamed('version');
if (versionXml === undefined) {
version = getParentVersion(artifactId, pomXmlContent);
} else {
version = versionXml.val;
}

if (version.indexOf('${') >= 0) {
version = getEffectiveVersion(mavenRootDirAbsolutePath, projectPath);
}

return version;
}

function getParentVersion(
artifactId: string,
pomXmlContent: XmlDocument,
): string {
const parentXml = pomXmlContent.childNamed('parent');

if (parentXml === undefined) {
throw new Error(`Parent tag not found for project ${artifactId}`);
}

const versionXml = parentXml.childNamed('version');

if (versionXml === undefined) {
throw new Error(`ParentVersion not found for project ${artifactId}`);
}

return versionXml?.val;
}

function getEffectiveVersion(
mavenRootDirAbsolutePath: string,
projectPath: string,
): string {
const pomRelativePath = path.relative(mavenRootDirAbsolutePath, projectPath);
const version = execSync(
`${getExecutable()} -f ${pomRelativePath} help:evaluate -Dexpression=project.version -q -DforceStdout`,
{
cwd: mavenRootDirAbsolutePath,
},
)
.toString()
.trim();

return version;
}
18 changes: 9 additions & 9 deletions testing-projects/jnxplus-e2e/tests/nx-maven/all-bom.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('nx-maven all bom e2e', () => {
if (isCI) {
removeTmpFromGitignore();
}
}, 120000);
}, 240000);

afterAll(async () => {
if (isCI) {
Expand All @@ -52,7 +52,7 @@ describe('nx-maven all bom e2e', () => {

it('should set NX_VERBOSE_LOGGING to true', async () => {
expect(process.env['NX_VERBOSE_LOGGING']).toBe('true');
}, 120000);
}, 240000);

it('should init the workspace with @jnxplus/nx-maven capabilities', async () => {
// Making sure the package.json file contains the @jnxplus/nx-maven dependency
Expand All @@ -72,7 +72,7 @@ describe('nx-maven all bom e2e', () => {
'pom.xml',
),
).not.toThrow();
}, 120000);
}, 240000);

it('1 none app', async () => {
const appName = uniq('maven-app-');
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('nx-maven all bom e2e', () => {

// const lintResult = await runNxCommandAsync(`lint ${appName}`);
// expect(lintResult.stdout).toContain('Executor ran for Lint');
}, 120000);
}, 240000);

it('should test an app with none option', async () => {
const appName = uniq('maven-app-');
Expand All @@ -123,7 +123,7 @@ describe('nx-maven all bom e2e', () => {

const testResult = await runNxCommandAsync(`test ${appName}`);
expect(testResult.stdout).toContain('Executor ran for Test');
}, 120000);
}, 240000);

it('should serve an app with none option', async () => {
const appName = uniq('maven-app-');
Expand All @@ -134,7 +134,7 @@ describe('nx-maven all bom e2e', () => {
const serveResult = await runNxCommandAsync(`serve ${appName}`);
expect(serveResult.stdout).toContain('Executor ran for Serve');
expect(serveResult.stdout).toContain('Hello World!');
}, 120000);
}, 240000);

it('2 none app kt', async () => {
const appName = uniq('maven-app-');
Expand Down Expand Up @@ -172,7 +172,7 @@ describe('nx-maven all bom e2e', () => {
const serveResult = await runNxCommandAsync(`serve ${appName}`);
expect(serveResult.stdout).toContain('Executor ran for Serve');
expect(serveResult.stdout).toContain('Hello World!');
}, 120000);
}, 240000);

it('1 none lib', async () => {
const libName = uniq('maven-lib-');
Expand Down Expand Up @@ -206,7 +206,7 @@ describe('nx-maven all bom e2e', () => {

// const lintResult = await runNxCommandAsync(`lint ${libName}`);
// expect(lintResult.stdout).toContain('Executor ran for Lint');
}, 120000);
}, 240000);

it('2 none lib kt', async () => {
const libName = uniq('maven-lib-');
Expand Down Expand Up @@ -238,5 +238,5 @@ describe('nx-maven all bom e2e', () => {

// const lintResult = await runNxCommandAsync(`lint ${libName}`);
// expect(lintResult.stdout).toContain('Executor ran for Lint');
}, 120000);
}, 240000);
});
Loading

0 comments on commit d6fdd93

Please sign in to comment.