From 41d8cacb59050715c1d2135d294d04ecc37074cd Mon Sep 17 00:00:00 2001 From: Marcelo Shima Date: Sat, 18 May 2024 15:52:34 -0300 Subject: [PATCH] add options to generate-sample --- .blueprint/generate-sample/command.mts | 49 +++++++++++++++--- .blueprint/generate-sample/generator.mjs | 50 ++++++++++++++++--- .../support/generate-sample.js | 12 +---- .blueprint/generate-sample/support/index.ts | 2 + 4 files changed, 90 insertions(+), 23 deletions(-) create mode 100644 .blueprint/generate-sample/support/index.ts diff --git a/.blueprint/generate-sample/command.mts b/.blueprint/generate-sample/command.mts index cc46cb27b1ac..78ee2b653c9b 100644 --- a/.blueprint/generate-sample/command.mts +++ b/.blueprint/generate-sample/command.mts @@ -16,6 +16,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import { join } from 'node:path'; +import process from 'node:process'; +import { defaultSamplesFolder } from '../constants.js'; import { JHipsterCommandDefinition } from '../../generators/base/api.js'; import { GENERATOR_APP, GENERATOR_WORKSPACES } from '../../generators/generator-list.js'; @@ -25,17 +28,51 @@ const command: JHipsterCommandDefinition = { type: String, }, }, - options: { + configs: { + entitiesSample: { + cli: { + type: String, + description: 'Entities sample to copy', + }, + configure: (gen: any) => { + if (['mongodb', 'couchbase'].includes(gen.entitiesSample)) { + gen.entitiesSample = 'document'; + } + }, + choices: ['sql', 'sqllight', 'micro', 'sqlfull', 'mongodb', 'document', 'cassandra', 'couchbase'], + scope: 'generator', + }, global: { - type: Boolean, - description: 'Generate in global samples folder', + cli: { + type: Boolean, + description: 'Generate in global samples folder', + }, + configure: (gen: any) => { + if (gen.global && !gen.projectFolder) { + gen.projectFolder = join(gen._globalConfig.get('samplesFolder') ?? defaultSamplesFolder, gen.sampleName); + } + }, scope: 'generator', }, projectFolder: { - type: String, - description: 'Folder to generate the sample', + cli: { + type: String, + description: 'Folder to generate the sample', + env: 'JHI_FOLDER_APP', + }, + configure: (gen: any) => { + if (!gen.projectFolder) { + gen.projectFolder = process.cwd(); + } + }, + scope: 'generator', + }, + }, + options: { + sampleYorcFolder: { + type: Boolean, + description: 'Treat sample arg as .yo-rc.json folder path', scope: 'generator', - env: 'JHI_FOLDER_APP', }, }, import: [GENERATOR_APP, GENERATOR_WORKSPACES], diff --git a/.blueprint/generate-sample/generator.mjs b/.blueprint/generate-sample/generator.mjs index 6ffab1769dc6..b7a154de50ce 100644 --- a/.blueprint/generate-sample/generator.mjs +++ b/.blueprint/generate-sample/generator.mjs @@ -2,7 +2,7 @@ import { extname } from 'path'; import { transform } from '@yeoman/transform'; import BaseGenerator from '../../generators/base/index.js'; import { packageJson } from '../../lib/index.js'; -import { generateSample } from './support/generate-sample.js'; +import { generateSample, entitiesByType } from './support/index.js'; import { promptSamplesFolder } from '../support.mjs'; import { GENERATOR_APP, GENERATOR_JDL } from '../../generators/generator-list.js'; @@ -11,6 +11,8 @@ export default class extends BaseGenerator { global; projectFolder; projectVersion; + entitiesSample; + sampleYorcFolder; get [BaseGenerator.INITIALIZING]() { return this.asInitializingTaskGroup({ @@ -33,6 +35,14 @@ export default class extends BaseGenerator { }); } + get [BaseGenerator.CONFIGURING]() { + return this.asConfiguringTaskGroup({ + async configureCommand() { + await this.configureCurrentJHipsterCommandConfig(); + }, + }); + } + get [BaseGenerator.END]() { return this.asEndTaskGroup({ async generateJdlSample() { @@ -40,15 +50,14 @@ export default class extends BaseGenerator { await this.composeWithJHipster(GENERATOR_JDL, { generatorArgs: [this.templatePath('samples', this.sampleName)], - generatorOptions: { projectVersion: this.projectVersion }, + generatorOptions: { projectVersion: this.projectVersion, destinationPath: this.projectFolder }, }); }, async generateSample() { - if (extname(this.sampleName) === '.jdl') return; + if (extname(this.sampleName) === '.jdl' || this.sampleYorcFolder) return; const sample = await generateSample(this.sampleName, { - destSamplesFolder: this._globalConfig.get('samplesFolder'), - destProjectFolder: this.projectFolder ?? this.global ? undefined : process.cwd(), + destProjectFolder: this.projectFolder, fork: false, }); @@ -58,7 +67,11 @@ export default class extends BaseGenerator { transform(() => {}), ); - let generatorOptions = { projectVersion: this.projectVersion, ...sample.sample.generatorOptions }; + let generatorOptions = { + projectVersion: this.projectVersion, + destinationPath: this.projectFolder, + ...sample.sample.generatorOptions, + }; if (sample.sample.workspaces && sample.sample.workspaces !== 'false') { generatorOptions = { ...generatorOptions, workspaces: true, monorepository: true }; } @@ -71,12 +84,35 @@ export default class extends BaseGenerator { if (sample.jdlFiles) { await this.composeWithJHipster(GENERATOR_JDL, { generatorArgs: sample.jdlFiles, - generatorOptions: { jsonOnly: true }, + generatorOptions: { jsonOnly: true, destinationPath: this.projectFolder }, }); } await this.composeWithJHipster(GENERATOR_APP, { generatorOptions }); } }, + async generateYoRcSample() { + if (!this.sampleYorcFolder) return; + this.copyTemplate(`../../../test-integration/${this.sampleName}/.yo-rc.json`, '.yo-rc.json'); + const entitiesFiles = entitiesByType[this.entitiesSample]; + if (entitiesFiles) { + this.jhipsterConfig.entities = entitiesFiles; + /* + this.copyTemplate( + entitiesFiles.map(entity => `.jhipster/${entity}.json`), + this.projectFolder, + { noGlob: true, fromBasePath: this.templatePath('../../../test-integration/samples/') }, + ); + */ + entitiesFiles.forEach(entity => + this.copyTemplate( + `../../../test-integration/samples/.jhipster/${entity}.json`, + `${this.projectFolder}/.jhipster/${entity}.json`, + { noGlob: true }, + ), + ); + } + await this.composeWithJHipster(GENERATOR_APP, { generatorOptions: { destinationPath: this.projectFolder } }); + }, async updateVscodeWorkspace() { if (this.global) { await this.composeWithJHipster('@jhipster/jhipster-dev:code-workspace', { diff --git a/.blueprint/generate-sample/support/generate-sample.js b/.blueprint/generate-sample/support/generate-sample.js index f6cbd4b5be22..3bf4f2b3db7a 100644 --- a/.blueprint/generate-sample/support/generate-sample.js +++ b/.blueprint/generate-sample/support/generate-sample.js @@ -7,22 +7,14 @@ import { execa } from 'execa'; import getSamples, { DAILY_PREFIX, isDaily } from './get-workflow-samples.js'; import copyEntitySamples from './copy-entity-samples.js'; import copyJdlEntitySamples from './copy-jdl-entity-samples.js'; -import { - dailyBuildsFolder, - defaultSamplesFolder, - jdlEntitiesSamplesFolder, - jdlSamplesFolder, - jhipsterBin, - samplesFolder, -} from '../../constants.js'; +import { dailyBuildsFolder, jdlEntitiesSamplesFolder, jdlSamplesFolder, jhipsterBin, samplesFolder } from '../../constants.js'; const commonCliOptions = ['--skip-jhipster-dependencies', '--skip-checks', '--skip-install', '--no-insight']; export const generateSample = async ( sampleName = process.argv.length > 2 ? process.argv[2] : process.env.JHI_APP, { - destSamplesFolder = defaultSamplesFolder, - destProjectFolder = process.env.JHI_FOLDER_APP ? resolve(process.env.JHI_FOLDER_APP) : join(destSamplesFolder, sampleName), + destProjectFolder, environment: passedEnvironment, war: passedWar, entity: passedEntity, diff --git a/.blueprint/generate-sample/support/index.ts b/.blueprint/generate-sample/support/index.ts new file mode 100644 index 000000000000..8fe6bfb0aafb --- /dev/null +++ b/.blueprint/generate-sample/support/index.ts @@ -0,0 +1,2 @@ +export * from './copy-entity-samples.js'; +export * from './generate-sample.js';