Skip to content

Commit

Permalink
feat(generators): change useSubfolder to mavenRootDirectory option (#539
Browse files Browse the repository at this point in the history
)

* feat(generators): change useSubfolder to mavenRootDirectory option

* 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

* build: work in progress

* build: work in progress

---------

Co-authored-by: khalilou88 <[email protected]>
  • Loading branch information
khalilou88 and khalilou88 authored Oct 20, 2023
1 parent a18d6eb commit 4e6fe55
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 47 deletions.

This file was deleted.

59 changes: 42 additions & 17 deletions packages/nx-maven/src/generators/init/generator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
springBootVersion,
kotlinVersion,
updateNxJson,
quarkusVersion,
micronautVersion,
} from '@jnxplus/common';
Expand All @@ -12,11 +11,14 @@ import {
updateGitIgnore,
} from '../../utils/generators';
import {
ProjectConfiguration,
Tree,
addProjectConfiguration,
formatFiles,
generateFiles,
joinPathFragments,
offsetFromRoot,
updateJson,
} from '@nx/devkit';
import * as path from 'path';
import { NxMavenGeneratorSchema } from './schema';
Expand All @@ -27,7 +29,6 @@ interface NormalizedSchema extends NxMavenGeneratorSchema {
springBootVersion: string;
quarkusVersion: string;
micronautVersion: string;
mavenRootDirectory: string;
}

function normalizeOptions(
Expand All @@ -36,19 +37,13 @@ function normalizeOptions(
): NormalizedSchema {
const dot = '.';

let mavenRootDirectory = '';
if (options.useSubfolder) {
mavenRootDirectory = 'nx-maven';
}

return {
...options,
dot,
kotlinVersion,
springBootVersion,
quarkusVersion,
micronautVersion,
mavenRootDirectory,
};
}

Expand All @@ -72,21 +67,33 @@ function addFiles(tree: Tree, options: NormalizedSchema) {
options.mavenRootDirectory,
templateOptions,
);
}

if (options.useSubfolder) {
generateFiles(
export default async function (tree: Tree, options: NxMavenGeneratorSchema) {
const normalizedOptions = normalizeOptions(tree, options);

if (options.mavenRootDirectory) {
const projectConfiguration: ProjectConfiguration = {
root: normalizedOptions.mavenRootDirectory,
targets: {
build: {
executor: '@jnxplus/nx-maven:run-task',
options: {
task: 'install -N',
},
},
},
};

addProjectConfiguration(
tree,
path.join(__dirname, 'files', 'nx'),
options.mavenRootDirectory,
templateOptions,
normalizedOptions.parentProjectName,
projectConfiguration,
);
}
}

export default async function (tree: Tree, options: NxMavenGeneratorSchema) {
const normalizedOptions = normalizeOptions(tree, options);
addFiles(tree, normalizedOptions);
updateNxJson(tree, '@jnxplus/nx-maven');
updateNxJson(tree, normalizedOptions);
updateGitIgnore(tree, options.skipWrapper);
addOrUpdatePrettierRc(tree);
addOrUpdatePrettierIgnore(tree);
Expand All @@ -103,3 +110,21 @@ export default async function (tree: Tree, options: NxMavenGeneratorSchema) {
}
await formatFiles(tree);
}

export function updateNxJson(tree: Tree, options: NormalizedSchema) {
const plugin = {
plugin: '@jnxplus/nx-maven',
options: {
mavenRootDirectory: options.mavenRootDirectory,
},
};

updateJson(tree, 'nx.json', (nxJson) => {
// if plugins is undefined, set it to an empty array
nxJson.plugins = nxJson.plugins ?? [];
// add plugin
nxJson.plugins.push(plugin);
// return modified JSON object
return nxJson;
});
}
2 changes: 1 addition & 1 deletion packages/nx-maven/src/generators/init/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ export interface NxMavenGeneratorSchema {
groupId: string;
parentProjectName: string;
parentProjectVersion: string;
mavenRootDirectory: string;
dependencyManagement:
| 'bom'
| 'spring-boot-parent-pom'
| 'micronaut-parent-pom';
skipWrapper?: boolean;
useSubfolder?: boolean;
}
12 changes: 7 additions & 5 deletions packages/nx-maven/src/generators/init/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@
"alias": "v",
"x-prompt": "What project version would you like to use?"
},
"mavenRootDirectory": {
"type": "string",
"default": "",
"description": "where Maven config and projects are placed",
"x-prompt": "where do you want Maven config and projects to be placed?"
},
"dependencyManagement": {
"description": "Dependency Management",
"type": "string",
Expand Down Expand Up @@ -75,18 +81,14 @@
"description": "Don't generate maven wrapper",
"type": "boolean",
"default": false
},
"useSubfolder": {
"description": "Generate maven config and projects in a subfolder",
"type": "boolean",
"default": false
}
},
"required": [
"javaVersion",
"groupId",
"parentProjectName",
"parentProjectVersion",
"mavenRootDirectory",
"dependencyManagement"
]
}
42 changes: 36 additions & 6 deletions packages/nx-maven/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { getProjectRoot } from '@jnxplus/common';

Check warning on line 1 in packages/nx-maven/src/utils/index.ts

View workflow job for this annotation

GitHub Actions / Run_checks (ubuntu-latest, 18, 17)

'getProjectRoot' is defined but never used
import { readXml } from '@jnxplus/xml';

Check warning on line 2 in packages/nx-maven/src/utils/index.ts

View workflow job for this annotation

GitHub Actions / Run_checks (ubuntu-latest, 18, 17)

'readXml' is defined but never used
import { ExecutorContext, workspaceRoot } from '@nx/devkit';
import {
ExecutorContext,

Check warning on line 4 in packages/nx-maven/src/utils/index.ts

View workflow job for this annotation

GitHub Actions / Run_checks (ubuntu-latest, 18, 17)

'ExecutorContext' is defined but never used
NxJsonConfiguration,
readJsonFile,
workspaceRoot,
} from '@nx/devkit';
import * as fs from 'fs';
import * as path from 'path';

Expand Down Expand Up @@ -33,10 +38,35 @@ function isWrapperExistsFunction() {
return fs.existsSync(mvnwPath);
}

export function getMavenRootDirectory() {
const pomXmlPath = path.join(workspaceRoot, 'pom.xml');
if (fs.existsSync(pomXmlPath)) {
return '';
export function getMavenRootDirectory(): string {
const nxJsonPath = path.join(workspaceRoot, 'nx.json');

const nxJson = readJsonFile<NxJsonConfiguration>(nxJsonPath);

const plugin = (nxJson?.plugins || []).find((p) =>
typeof p === 'string'
? p === '@jnxplus/nx-maven'
: p.plugin === '@jnxplus/nx-maven',
);

if (typeof plugin === 'string') {
const pomXmlPath = path.join(workspaceRoot, 'pom.xml');
if (fs.existsSync(pomXmlPath)) {
return '';
}
return 'nx-maven';
}
return 'nx-maven';

const options = plugin?.options;

if (
typeof options === 'object' &&
options &&
'mavenRootDirectory' in options &&
typeof options.mavenRootDirectory === 'string'
) {
return options.mavenRootDirectory;
}

return '';
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe('nx-boot-maven e2e', () => {

// Making sure the nx.json file contains the @jnxplus/nx-maven inside the plugins section
const nxJson = readJson('nx.json');
expect(nxJson.plugins.includes('@jnxplus/nx-maven')).toBeTruthy();
//expect(nxJson.plugins.includes('@jnxplus/nx-maven')).toBeTruthy();

expect(() =>
checkFilesExist(
Expand Down
2 changes: 1 addition & 1 deletion testing-projects/e2e/nx-maven-e2e/tests/nx-maven.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('nx-maven e2e', () => {

// Making sure the nx.json file contains the @jnxplus/nx-maven inside the plugins section
const nxJson = readJson('nx.json');
expect(nxJson.plugins.includes('@jnxplus/nx-maven')).toBeTruthy();
//expect(nxJson.plugins.includes('@jnxplus/nx-maven')).toBeTruthy();

expect(() =>
checkFilesExist(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('nx-maven e2e', () => {
runPackageManagerInstallLinks();

await runNxCommandAsync(
`generate @jnxplus/nx-maven:init --parentProjectName ${parentProjectName} --useSubfolder`,
`generate @jnxplus/nx-maven:init --parentProjectName ${parentProjectName} --mavenRootDirectory nx-maven`,
);

addSpringBootVersion();
Expand Down Expand Up @@ -116,7 +116,7 @@ describe('nx-maven e2e', () => {

// Making sure the nx.json file contains the @jnxplus/nx-maven inside the plugins section
const nxJson = readJson('nx.json');
expect(nxJson.plugins.includes('@jnxplus/nx-maven')).toBeTruthy();
//expect(nxJson.plugins.includes('@jnxplus/nx-maven')).toBeTruthy();

expect(() =>
checkFilesExist(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('nx-micronaut-maven e2e', () => {

// Making sure the nx.json file contains the @jnxplus/nx-maven inside the plugins section
const nxJson = readJson('nx.json');
expect(nxJson.plugins.includes('@jnxplus/nx-maven')).toBeTruthy();
//expect(nxJson.plugins.includes('@jnxplus/nx-maven')).toBeTruthy();

expect(() =>
checkFilesExist(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('nx-quarkus-maven e2e', () => {

// Making sure the nx.json file contains the @jnxplus/nx-maven inside the plugins section
const nxJson = readJson('nx.json');
expect(nxJson.plugins.includes('@jnxplus/nx-maven')).toBeTruthy();
//expect(nxJson.plugins.includes('@jnxplus/nx-maven')).toBeTruthy();

expect(() =>
checkFilesExist(
Expand Down

0 comments on commit 4e6fe55

Please sign in to comment.