Skip to content

Commit

Permalink
Merge pull request #26326 from mshima/from-issue
Browse files Browse the repository at this point in the history
improve from-issue
  • Loading branch information
DanielFran authored May 31, 2024
2 parents 136dec3 + 8d48f3e commit aa57047
Show file tree
Hide file tree
Showing 15 changed files with 329 additions and 537 deletions.
18 changes: 16 additions & 2 deletions .blueprint/from-issue/command.mts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,25 @@
*/
import { JHipsterCommandDefinition } from '../../generators/base/api.js';
import { GENERATOR_APP, GENERATOR_WORKSPACES } from '../../generators/generator-list.js';
import { parseIssue } from '../../testing/github.js';

const command: JHipsterCommandDefinition = {
arguments: {
configs: {
issue: {
type: String,
argument: {
type: String,
description: 'GitHub issue to generate',
},
configure(gen: any) {
// Gets the owner, repo and issue_number from a string such as, "jhipster/generator-jhipster#12345"
const parsedIssue = parseIssue(gen.issue);
if (parsedIssue) {
gen.owner = parsedIssue.owner;
gen.repository = parsedIssue.repository;
gen.issue = parsedIssue.issue;
}
},
scope: 'generator',
},
},
options: {
Expand Down
156 changes: 0 additions & 156 deletions .blueprint/from-issue/generator.mjs

This file was deleted.

125 changes: 125 additions & 0 deletions .blueprint/from-issue/generator.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { join } from 'node:path';

import BaseGenerator from '../../generators/base/index.js';
import { getGithubIssue, setGithubTaskOutput, prepareSample, parseIssue } from '../../testing/index.js';
import { promptSamplesFolder } from '../support.mjs';
import { GENERATOR_APP, GENERATOR_JDL, GENERATOR_WORKSPACES } from '../../generators/generator-list.js';
import { GENERATOR_JHIPSTER } from '../../generators/generator-constants.js';
import { extractDataFromInfo, type InfoData } from '../../generators/info/support/index.js';
import EnvironmentBuilder from '../../cli/environment-builder.mjs';
import { mutateData } from '../../generators/base/support/config.js';

const YO_RC_OUTPUT = 'yo-rc';
const ENTITIES_JDL_OUTPUT = 'entities-jdl';
const RESULT_OUTPUT = 'result';
const VALID_OUTPUT = 'valid';
const CONTAINS_SAMPLE = 'contains-sample';

const BLANK = 'blank';
const VALID = 'valid';
const ERROR = 'error';
const SUCCESS = 'successfully generated';

export default class extends BaseGenerator {
issue!: string;
projectFolder!: string;
owner!: string;
codeWorkspace!: boolean;
repository!: string;
data!: InfoData;

get [BaseGenerator.INITIALIZING]() {
return this.asInitializingTaskGroup({
async parseCommand() {
await this.parseCurrentJHipsterCommand();
},
});
}

get [BaseGenerator.PROMPTING]() {
return this.asPromptingTaskGroup({
async promptOptions() {
if (this.codeWorkspace) {
await promptSamplesFolder.call(this);
}
},
});
}

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

get [BaseGenerator.DEFAULT]() {
return this.asDefaultTaskGroup({
async generateSample() {
const issue = await getGithubIssue({ owner: this.owner, repository: this.repository, issue: this.issue });

this.projectFolder = this.destinationPath(
this.projectFolder ?? join(this._globalConfig.get('samplesFolder'), `issues/${this.issue}`),
);

this.data = extractDataFromInfo(issue.body ?? '');
if (this.data.yoRcBlank) {
setGithubTaskOutput(YO_RC_OUTPUT, BLANK);
} else {
setGithubTaskOutput(YO_RC_OUTPUT, this.data.yoRcValid ? VALID : ERROR);
}

setGithubTaskOutput(ENTITIES_JDL_OUTPUT, this.data.jdlEntitiesDefinitions ? VALID : BLANK);
setGithubTaskOutput(CONTAINS_SAMPLE, Boolean(this.data.yoRcContent && this.data.jdlEntitiesDefinitions));
setGithubTaskOutput(VALID_OUTPUT, this.data.yoRcValid);

for (const file of await prepareSample(this.projectFolder, this.data.files)) {
this.writeDestination(file.filename, file.content);
}
},
});
}

get [BaseGenerator.END]() {
return this.asEndTaskGroup({
async generateSample() {
const envOptions = { cwd: this.projectFolder, logCwd: this.destinationPath() };
const generatorOptions = { ...this.options, skipPriorities: ['prompting'], skipInstall: true, experimental: true };
delete generatorOptions.sharedData;

const { workspacesFolders, jdlEntitiesDefinitions, yoRcContent, jdlDefinitions } = this.data;
if (jdlEntitiesDefinitions) {
try {
await EnvironmentBuilder.run(
[`jhipster:${GENERATOR_JDL}`],
{ ...generatorOptions, inline: jdlEntitiesDefinitions, jsonOnly: true },
envOptions,
);
} catch (error) {
setGithubTaskOutput(ENTITIES_JDL_OUTPUT, ERROR);
throw error;
}
}
if (yoRcContent) {
await EnvironmentBuilder.run(
[`jhipster:${workspacesFolders ? GENERATOR_WORKSPACES : GENERATOR_APP}`],
{ ...generatorOptions, ...(workspacesFolders ? { workspacesFolders, generateApplications: true } : {}) },
envOptions,
);
} else if (jdlDefinitions) {
await EnvironmentBuilder.run([`jhipster:${GENERATOR_JDL}`], { ...generatorOptions, inline: jdlDefinitions }, envOptions);
}
setGithubTaskOutput(RESULT_OUTPUT, SUCCESS);

if (this.codeWorkspace) {
await this.composeWithJHipster('@jhipster/jhipster-dev:code-workspace', {
generatorOptions: {
samplePath: this.projectFolder,
} as any,
});
}
},
});
}
}
6 changes: 6 additions & 0 deletions cli/environment-builder.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ export default class EnvironmentBuilder {
return EnvironmentBuilder.create(...args).prepare();
}

static async run(args, generatorOptions = {}, envOptions = {}) {
const envBuilder = await EnvironmentBuilder.createDefaultBuilder(envOptions);
const env = envBuilder.getEnvironment();
await env.run(args, generatorOptions);
}

/**
* Class to manipulate yeoman environment for jhipster needs.
* - Registers jhipster generators.
Expand Down
3 changes: 2 additions & 1 deletion cli/program.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { packageJson } from '../lib/index.js';
import { packageNameToNamespace } from '../generators/base/support/index.js';
import command from '../generators/base/command.js';
import { GENERATOR_APP, GENERATOR_BOOTSTRAP, GENERATOR_JDL } from '../generators/generator-list.js';
import { extractArgumentsFromConfigs } from '../generators/base/internal/command.js';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
Expand Down Expand Up @@ -118,7 +119,7 @@ const addCommandGeneratorOptions = async (command, generatorMeta, { root, bluepr
const addCommandRootGeneratorOptions = async (command, generatorMeta, { usage = true } = {}) => {
const generatorModule = await generatorMeta.importModule();
if (generatorModule.command) {
command.addJHipsterArguments(generatorModule.command.arguments);
command.addJHipsterArguments(generatorModule.command.arguments ?? extractArgumentsFromConfigs(generatorModule.command.configs));
} else {
const generator = await generatorMeta.instantiateHelp();
command.addGeneratorArguments(generator._arguments);
Expand Down
15 changes: 2 additions & 13 deletions generators/base-core/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import { convertConfigToOption } from '../../lib/internal/index.js';
import { getGradleLibsVersionsProperties } from '../gradle/support/dependabot-gradle.js';
import { dockerPlaceholderGenerator } from '../docker/utils.js';
import { getConfigWithDefaults } from '../../jdl/index.js';
import { extractArgumentsFromConfigs } from '../base/internal/command.js';

const {
INITIALIZING,
Expand Down Expand Up @@ -423,19 +424,7 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`);
if (commandDef.arguments) {
this.parseJHipsterArguments(commandDef.arguments);
} else if (commandDef.configs) {
this.parseJHipsterArguments(
Object.fromEntries(
Object.entries(commandDef.configs as Record<string, any>)
.filter(([_name, def]) => def.argument)
.map(([name, def]) => [
name,
{
description: def.description,
...def.argument,
},
]),
) as any,
);
this.parseJHipsterArguments(extractArgumentsFromConfigs(commandDef.configs));
}
if (commandDef.options || commandDef.configs) {
this.parseJHipsterOptions(commandDef.options, commandDef.configs);
Expand Down
16 changes: 16 additions & 0 deletions generators/base/internal/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { JHipsterArguments, JHipsterConfigs } from '../api.js';

export const extractArgumentsFromConfigs = (configs: JHipsterConfigs | undefined): JHipsterArguments => {
if (!configs) return {};
return Object.fromEntries(
Object.entries(configs)
.filter(([_name, def]) => def.argument)
.map(([name, def]) => [
name,
{
description: def.description,
...def.argument,
},
]),
) as JHipsterArguments;
};
1 change: 1 addition & 0 deletions generators/base/internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
* limitations under the License.
*/
export * from './blueprint.js';
export * from './command.js';
Loading

0 comments on commit aa57047

Please sign in to comment.