Skip to content

Commit

Permalink
Merge pull request #24955 from mshima/nested-generators
Browse files Browse the repository at this point in the history
Add support to nested generators and split Kafka/pulsar generators.
  • Loading branch information
DanielFran authored Jan 25, 2024
2 parents a754166 + 277a18c commit c69efeb
Show file tree
Hide file tree
Showing 27 changed files with 299 additions and 161 deletions.
20 changes: 17 additions & 3 deletions cli/environment-builder.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ const __dirname = dirname(__filename);
const jhipsterDevBlueprintPath = process.env.JHIPSTER_DEV_BLUEPRINT === 'true' ? path.join(__dirname, '../.blueprint') : undefined;
const devBlueprintNamespace = '@jhipster/jhipster-dev';
const localBlueprintNamespace = '@jhipster/jhipster-local';
const defaultLookupOptions = {
lookups: ['generators', 'generators/*/generators'],
customizeNamespace: ns => ns?.replaceAll(':generators:', ':'),
};

function loadYoRc(filePath = '.yo-rc.json') {
if (!existsSync(filePath)) {
Expand Down Expand Up @@ -155,7 +159,12 @@ export default class EnvironmentBuilder {
packagePath = path.join(__dirname, '../..');
lookup = `${sourceRoot}/generators`;
}
(await this.env.lookup({ packagePaths: [packagePath], lookups: [lookup] })).forEach(generator => {
const generators = await this.env.lookup({
...defaultLookupOptions,
packagePaths: [packagePath],
lookups: [lookup, `${lookup}/*/generators`],
});
generators.forEach(generator => {
// Verify jhipster generators namespace.
assert(
generator.namespace.startsWith(`${CLI_NAME}:`),
Expand Down Expand Up @@ -193,7 +202,7 @@ export default class EnvironmentBuilder {
async _lookups(lookups = []) {
lookups = [].concat(lookups);
for (const lookup of lookups) {
await this.env.lookup(lookup);
await this.env.lookup({ ...defaultLookupOptions, ...lookup });
}
return this;
}
Expand Down Expand Up @@ -226,7 +235,12 @@ export default class EnvironmentBuilder {

if (missingBlueprints && missingBlueprints.length > 0) {
// Lookup for blueprints.
await this.env.lookup({ ...options, filterPaths: true, packagePatterns: missingBlueprints });
await this.env.lookup({
...defaultLookupOptions,
...options,
filterPaths: true,
packagePatterns: missingBlueprints,
});
}
return this;
}
Expand Down
2 changes: 1 addition & 1 deletion generators/generate-blueprint/templates/cli/cli.cjs.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const blueprint = packageFolderName.startsWith('jhipster-') ? `generator-${packa
console.log('===================== JHipster <%= baseName %> =====================');
console.log('');
},
lookups: [{ packagePaths: [packagePath], lookups: ['generators'] }],
lookups: [{ packagePaths: [packagePath] }],
}).catch(done);

process.on('unhandledRejection', up => {
Expand Down
128 changes: 13 additions & 115 deletions generators/spring-cloud-stream/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

import BaseApplicationGenerator from '../base-application/index.js';
import { GENERATOR_SPRING_CLOUD_STREAM, GENERATOR_BOOTSTRAP_APPLICATION_SERVER } from '../generator-list.js';
import cleanupFilesTask from './cleanup.js';
import writeFilesTask from './files.js';

export default class KafkaGenerator extends BaseApplicationGenerator {
async beforeQueue() {
Expand All @@ -33,6 +31,19 @@ export default class KafkaGenerator extends BaseApplicationGenerator {
}
}

get composing() {
return this.asComposingTaskGroup({
async compose() {
const { messageBroker } = this.jhipsterConfig;
await this.composeWithJHipster(`jhipster:spring-cloud-stream:${messageBroker}`);
},
});
}

get [BaseApplicationGenerator.COMPOSING]() {
return this.delegateTasksToBlueprint(() => this.composing);
}

get preparing() {
return this.asPreparingTaskGroup({
preparing({ application }) {
Expand All @@ -47,117 +58,4 @@ export default class KafkaGenerator extends BaseApplicationGenerator {
get [BaseApplicationGenerator.PREPARING]() {
return this.delegateTasksToBlueprint(() => this.preparing);
}

get writing() {
return this.asWritingTaskGroup({
cleanupFilesTask,
writeFilesTask,
});
}

get [BaseApplicationGenerator.WRITING]() {
return this.delegateTasksToBlueprint(() => this.writing);
}

get postWriting() {
return this.asPostWritingTaskGroup({
customizeApplicationForKafka({ source, application }) {
if (application.messageBrokerKafka) {
source.addLogbackMainLog?.({ name: 'org.apache.kafka', level: 'INFO' });
source.addLogbackTestLog?.({ name: 'kafka', level: 'WARN' });
source.addLogbackTestLog?.({ name: 'org.I0Itec', level: 'WARN' });
source.addIntegrationTestAnnotation?.({ package: `${application.packageName}.config`, annotation: 'EmbeddedKafka' });

source.addTestSpringFactory?.({
key: 'org.springframework.test.context.ContextCustomizerFactory',
value: `${application.packageName}.config.KafkaTestContainersSpringContextCustomizerFactory`,
});
}
},
applyKafkaGradleConventionPlugin({ source, application }) {
if (application.buildToolGradle && application.messageBrokerKafka) {
if (application.messageBrokerKafka) {
source.addGradlePlugin?.({ id: 'jhipster.kafka-conventions' });
}
}
},
addKafkaMavenDependencies({ application, source }) {
if (application.buildToolMaven && application.messageBrokerKafka) {
source.addMavenDependency?.([
{
groupId: 'org.springframework.cloud',
artifactId: 'spring-cloud-stream',
},
{
groupId: 'org.springframework.cloud',
artifactId: 'spring-cloud-starter-stream-kafka',
},
{
groupId: 'org.springframework.cloud',
artifactId: 'spring-cloud-stream-test-binder',
scope: 'test',
},
{
groupId: 'org.testcontainers',
artifactId: 'junit-jupiter',
scope: 'test',
},
{
groupId: 'org.testcontainers',
artifactId: 'testcontainers',
scope: 'test',
},
{
groupId: 'org.testcontainers',
artifactId: 'kafka',
scope: 'test',
},
]);
}
},
customizeApplicationForPulsar({ source, application }) {
if (application.messageBrokerPulsar) {
source.addLogbackMainLog?.({ name: 'org.apache.pulsar', level: 'INFO' });
source.addIntegrationTestAnnotation?.({ package: `${application.packageName}.config`, annotation: 'EmbeddedPulsar' });

source.addTestSpringFactory?.({
key: 'org.springframework.test.context.ContextCustomizerFactory',
value: `${application.packageName}.config.PulsarTestContainersSpringContextCustomizerFactory`,
});
}
},
applyPulsarGradleConventionPlugin({ source, application }) {
if (application.buildToolGradle && application.messageBrokerPulsar) {
const { javaDependencies } = application;
source.addGradlePlugin?.({ id: 'jhipster.pulsar-conventions' });
source.addGradleDependencyCatalogVersion?.({ name: 'pulsar-spring', version: javaDependencies?.['spring-pulsar'] });
source.addGradleBuildSrcDependencyCatalogVersion?.({ name: 'pulsar-spring', version: javaDependencies?.['spring-pulsar'] });
}
},
addPulsarMavenDependencies({ application, source }) {
if (application.buildToolMaven && application.messageBrokerPulsar) {
const { javaDependencies } = application;
source.addMavenDefinition?.({
properties: [{ property: 'spring-pulsar.version', value: javaDependencies?.['spring-pulsar'] }],
dependencies: [
{ groupId: 'org.springframework.cloud', artifactId: 'spring-cloud-stream' },
{
groupId: 'org.springframework.pulsar',
artifactId: 'spring-pulsar-spring-cloud-stream-binder',
// eslint-disable-next-line no-template-curly-in-string
version: '${spring-pulsar.version}',
},
{ groupId: 'org.testcontainers', artifactId: 'junit-jupiter', scope: 'test' },
{ groupId: 'org.testcontainers', artifactId: 'testcontainers', scope: 'test' },
{ groupId: 'org.testcontainers', artifactId: 'pulsar', scope: 'test' },
],
});
}
},
});
}

get [BaseApplicationGenerator.POST_WRITING]() {
return this.asPostWritingTaskGroup(this.delegateTasksToBlueprint(() => this.postWriting));
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { WriteFileSection } from '../base/api.js';
import { SERVER_MAIN_SRC_DIR, SERVER_TEST_SRC_DIR, GRADLE_BUILD_SRC_MAIN_DIR } from '../generator-constants.js';
import { moveToJavaPackageSrcDir, moveToJavaPackageTestDir } from '../server/support/index.js';
import KafkaGenerator from './generator.js';
import { WriteFileSection } from '../../../base/api.js';
import { SERVER_MAIN_SRC_DIR, SERVER_TEST_SRC_DIR, GRADLE_BUILD_SRC_MAIN_DIR } from '../../../generator-constants.js';
import { moveToJavaPackageSrcDir, moveToJavaPackageTestDir } from '../../../server/support/index.js';

export const kafkaFiles: WriteFileSection<any, any> = {
config: [
Expand Down Expand Up @@ -68,41 +67,3 @@ export const kafkaFiles: WriteFileSection<any, any> = {
},
],
};

export const pulsarFiles: WriteFileSection<any, any> = {
config: [
{
condition: data => data.buildToolGradle,
path: GRADLE_BUILD_SRC_MAIN_DIR,
templates: ['jhipster.pulsar-conventions.gradle'],
},
],
test: [
{
path: `${SERVER_TEST_SRC_DIR}_package_/`,
renameTo: moveToJavaPackageTestDir,
templates: [
'broker/PulsarIT.java',
'config/BrokerConfiguration.java',
'config/EmbeddedPulsar.java',
'config/PulsarTestContainer.java',
'config/PulsarTestContainersSpringContextCustomizerFactory.java',
],
},
],
};

export default async function writeFilesTask(this: KafkaGenerator, { application }) {
if (application.messageBrokerKafka) {
await this.writeFiles({
sections: kafkaFiles,
context: application,
});
}
if (application.messageBrokerPulsar) {
await this.writeFiles({
sections: pulsarFiles,
context: application,
});
}
}
99 changes: 99 additions & 0 deletions generators/spring-cloud-stream/generators/kafka/generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* Copyright 2013-2024 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import BaseApplicationGenerator from '../../../base-application/index.js';
import cleanupKafkaFilesTask from './cleanup.js';
import { kafkaFiles } from './files.js';

export default class KafkaGenerator extends BaseApplicationGenerator {
get writing() {
return this.asWritingTaskGroup({
cleanupKafkaFilesTask,
async writing({ application }) {
await this.writeFiles({
sections: kafkaFiles,
context: application,
});
},
});
}

get [BaseApplicationGenerator.WRITING]() {
return this.delegateTasksToBlueprint(() => this.writing);
}

get postWriting() {
return this.asPostWritingTaskGroup({
customizeApplicationForKafka({ source, application }) {
source.addLogbackMainLog?.({ name: 'org.apache.kafka', level: 'INFO' });
source.addLogbackTestLog?.({ name: 'kafka', level: 'WARN' });
source.addLogbackTestLog?.({ name: 'org.I0Itec', level: 'WARN' });
source.addIntegrationTestAnnotation?.({ package: `${application.packageName}.config`, annotation: 'EmbeddedKafka' });

source.addTestSpringFactory?.({
key: 'org.springframework.test.context.ContextCustomizerFactory',
value: `${application.packageName}.config.KafkaTestContainersSpringContextCustomizerFactory`,
});
},
applyKafkaGradleConventionPlugin({ source, application }) {
if (application.buildToolGradle) {
source.addGradlePlugin?.({ id: 'jhipster.kafka-conventions' });
}
},
addKafkaMavenDependencies({ application, source }) {
if (application.buildToolMaven) {
source.addMavenDependency?.([
{
groupId: 'org.springframework.cloud',
artifactId: 'spring-cloud-stream',
},
{
groupId: 'org.springframework.cloud',
artifactId: 'spring-cloud-starter-stream-kafka',
},
{
groupId: 'org.springframework.cloud',
artifactId: 'spring-cloud-stream-test-binder',
scope: 'test',
},
{
groupId: 'org.testcontainers',
artifactId: 'junit-jupiter',
scope: 'test',
},
{
groupId: 'org.testcontainers',
artifactId: 'testcontainers',
scope: 'test',
},
{
groupId: 'org.testcontainers',
artifactId: 'kafka',
scope: 'test',
},
]);
}
},
});
}

get [BaseApplicationGenerator.POST_WRITING]() {
return this.asPostWritingTaskGroup(this.delegateTasksToBlueprint(() => this.postWriting));
}
}
19 changes: 19 additions & 0 deletions generators/spring-cloud-stream/generators/kafka/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright 2013-2024 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { default } from './generator.js';
Loading

0 comments on commit c69efeb

Please sign in to comment.