Skip to content

Commit

Permalink
add queueCommandTasks feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Jun 25, 2024
1 parent b4aaf4e commit 96efdd4
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 15 deletions.
15 changes: 4 additions & 11 deletions .blueprint/generate-sample/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ export default class extends BaseGenerator {
entitiesSample;
sampleYorcFolder;

constructor(args, options, features) {
super(args, options, { queueCommandTasks: true, ...features });
}

get [BaseGenerator.INITIALIZING]() {
return this.asInitializingTaskGroup({
async parseCommand() {
await this.parseCurrentJHipsterCommand();
},
projectVersion() {
this.projectVersion = `${packageJson.version}-git`;
},
Expand All @@ -35,14 +36,6 @@ export default class extends BaseGenerator {
});
}

get [BaseGenerator.CONFIGURING]() {
return this.asConfiguringTaskGroup({
async configureCommand() {
await this.configureCurrentJHipsterCommandConfig();
},
});
}

get [BaseGenerator.END]() {
return this.asEndTaskGroup({
async generateJdlSample() {
Expand Down
96 changes: 95 additions & 1 deletion generators/base-core/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import YeomanGenerator, { type ComposeOptions, type Storage } from 'yeoman-gener
import type Environment from 'yeoman-environment';
import latestVersion from 'latest-version';
import SharedData from '../base/shared-data.js';
import { CUSTOM_PRIORITIES, PRIORITY_NAMES, PRIORITY_PREFIX } from '../base/priorities.js';
import { CUSTOM_PRIORITIES, PRIORITY_NAMES, PRIORITY_PREFIX, QUEUES } from '../base/priorities.js';
import {
createJHipster7Context,
formatDateForChangelog,
Expand Down Expand Up @@ -218,6 +218,12 @@ export default class CoreGenerator extends YeomanGenerator<JHipsterGeneratorOpti
// Add base template folder.
this.jhipsterTemplatesFolders = [this.templatePath()];
this.jhipster7Migration = this.features.jhipster7Migration ?? false;

if (this.features.queueCommandTasks === true) {
this.on('before:queueOwnTasks', () => {
this.queueCurrentJHipsterCommandTasks();
});
}
}

/**
Expand Down Expand Up @@ -316,6 +322,13 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`);
return [{ control }];
}

/**
* Check if the generator should ask for prompts.
*/
shouldAskForPrompts({ control }): boolean {
return !control.existingProject || this.options.askAnswered === true;
}

/**
* Override yeoman-generator method that gets methods to be queued, filtering the result.
*/
Expand All @@ -329,6 +342,87 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`);
return priorities;
}

queueCurrentJHipsterCommandTasks() {
this.queueTask({
queueName: QUEUES.INITIALIZING_QUEUE,
taskName: 'parseCurrentCommand',
cancellable: true,
async method() {
try {
await this.getCurrentJHipsterCommand();
} catch {
return;
}
await this.parseCurrentJHipsterCommand();
},
});

this.queueTask({
queueName: QUEUES.PROMPTING_QUEUE,
taskName: 'promptCurrentCommand',
cancellable: true,
async method() {
try {
const command = await this.getCurrentJHipsterCommand();
if (!command.configs) return;
} catch {
return;
}
const taskArgs = this.getArgsForPriority(PRIORITY_NAMES.INITIALIZING);
const [{ control }] = taskArgs;
if (!control) throw new Error(`Control object not found in ${this.options.namespace}`);
if (!this.shouldAskForPrompts({ control })) return;
await this.promptCurrentJHipsterCommand();
},
});

this.queueTask({
queueName: QUEUES.CONFIGURING_QUEUE,
taskName: 'configureCurrentCommand',
cancellable: true,
async method() {
try {
const command = await this.getCurrentJHipsterCommand();
if (!command.configs) return;
} catch {
return;
}
await this.configureCurrentJHipsterCommandConfig();
},
});

this.queueTask({
queueName: QUEUES.COMPOSING_QUEUE,
taskName: 'composeCurrentCommand',
cancellable: true,
async method() {
try {
await this.getCurrentJHipsterCommand();
} catch {
return;
}
await this.composeCurrentJHipsterCommand();
},
});

this.queueTask({
queueName: QUEUES.LOADING_QUEUE,
taskName: 'loadCurrentCommand',
cancellable: true,
async method() {
try {
const command = await this.getCurrentJHipsterCommand();
if (!command.configs) return;
} catch {
return;
}
const taskArgs = this.getArgsForPriority(PRIORITY_NAMES.LOADING);
const [{ application }] = taskArgs as any;
await this.loadCurrentJHipsterCommandConfig(application ?? this);
},
});
}

/**
* Get the current Command Definition for the generator.
* `generatorCommand` takes precedence.
Expand Down
12 changes: 12 additions & 0 deletions generators/base/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ export type JHipsterGeneratorFeatures = BaseFeatures & {
* Create transforms for commit.
*/
commitTransformFactory?: () => any;

/**
* Queue tasks to handle command definitions.
* - parse options and configurations from cli.
* - prompt configurations.
* - configure configurations.
* - compose with generators defined in command.
* - load configurations.
*
* Defaults to true for built-in generator-jhipster generators and false for blueprints.
*/
queueCommandTasks?: boolean;
};

// eslint-disable-next-line no-use-before-define
Expand Down
7 changes: 7 additions & 0 deletions generators/base/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ export default class JHipsterBaseBlueprintGenerator<
this.log.warn('Error adding current blueprint templates as alternative for JHipster templates.');
this.log.log(error);
}
// TODO change the condition to `this.features.queueCommandTasks === undefined` so builtin generators will default to queue command tasks
} else if (this.options.namespace === 'jhipster:bootstrap') {
this.on('before:queueOwnTasks', () => {
if (!this.delegateToBlueprint) {
this.queueCurrentJHipsterCommandTasks();
}
});
}
}

Expand Down
3 changes: 0 additions & 3 deletions generators/bootstrap/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ export default class BootstrapGenerator extends BaseGenerator {

get initializing() {
return this.asInitializingTaskGroup({
async parseCommand() {
await this.parseCurrentJHipsterCommand();
},
validateBlueprint() {
if (this.jhipsterConfig.blueprints && !this.skipChecks) {
this.jhipsterConfig.blueprints.forEach(blueprint => {
Expand Down

0 comments on commit 96efdd4

Please sign in to comment.