diff --git a/generators/generate-blueprint/__snapshots__/generator.spec.ts.snap b/generators/generate-blueprint/__snapshots__/generator.spec.ts.snap index 7a5521ffb139..06fa2e502cc0 100644 --- a/generators/generate-blueprint/__snapshots__/generator.spec.ts.snap +++ b/generators/generate-blueprint/__snapshots__/generator.spec.ts.snap @@ -11,6 +11,9 @@ exports[`generator - generate-blueprint with all option should match snapshot 1` ".blueprint/generate-sample/generator.mjs": { "stateCleared": "modified", }, + ".blueprint/generate-sample/get-samples.mjs": { + "stateCleared": "modified", + }, ".blueprint/generate-sample/index.mjs": { "stateCleared": "modified", }, @@ -877,6 +880,9 @@ exports[`generator - generate-blueprint with default config should write files a ".blueprint/generate-sample/generator.mjs": { "stateCleared": "modified", }, + ".blueprint/generate-sample/get-samples.mjs": { + "stateCleared": "modified", + }, ".blueprint/generate-sample/index.mjs": { "stateCleared": "modified", }, diff --git a/generators/generate-blueprint/files.ts b/generators/generate-blueprint/files.ts index a80f107d63fa..f29bbb24980b 100644 --- a/generators/generate-blueprint/files.ts +++ b/generators/generate-blueprint/files.ts @@ -33,6 +33,7 @@ export const files = asWriteFilesSection({ '.blueprint/cli/commands.mjs', '.blueprint/generate-sample/command.mjs', '.blueprint/generate-sample/generator.mjs', + '.blueprint/generate-sample/get-samples.mjs', '.blueprint/generate-sample/index.mjs', // Always write cli for devBlueprint usage 'cli/cli.cjs', @@ -43,6 +44,7 @@ export const files = asWriteFilesSection({ condition: ctx => !ctx[LOCAL_BLUEPRINT_OPTION] && ctx.githubWorkflows, templates: [ '.blueprint/github-build-matrix/build-matrix.mjs', + '.blueprint/github-build-matrix/command.mjs', '.blueprint/github-build-matrix/generator.mjs', '.blueprint/github-build-matrix/index.mjs', ], diff --git a/generators/generate-blueprint/templates/.blueprint/generate-sample/command.mjs.ejs b/generators/generate-blueprint/templates/.blueprint/generate-sample/command.mjs.ejs index f1c986d5fa23..848eda382565 100644 --- a/generators/generate-blueprint/templates/.blueprint/generate-sample/command.mjs.ejs +++ b/generators/generate-blueprint/templates/.blueprint/generate-sample/command.mjs.ejs @@ -18,6 +18,7 @@ */ import { readdir } from 'node:fs/promises'; import { GENERATOR_APP } from 'generator-jhipster/generators'; +import { getSamples } from './get-samples.mjs'; /** * @type {import('generator-jhipster').JHipsterCommandDefinition} @@ -34,7 +35,7 @@ const command = { when: !gen.all, type: 'list', message: 'which sample do you want to generate?', - choices: async () => readdir(gen.templatePath('samples')), + choices: async () => getSamples(gen.templatePath(gen.samplesFolder)), }), scope: 'generator', }, @@ -45,6 +46,14 @@ const command = { }, scope: 'generator', }, + samplesFolder: { + description: 'Path to the samples folder', + cli: { + type: String, + }, + default: 'samples', + scope: 'generator', + }, }, options: {}, import: [GENERATOR_APP], diff --git a/generators/generate-blueprint/templates/.blueprint/generate-sample/generator.mjs.ejs b/generators/generate-blueprint/templates/.blueprint/generate-sample/generator.mjs.ejs index 891a6b0af79f..a043d272818d 100644 --- a/generators/generate-blueprint/templates/.blueprint/generate-sample/generator.mjs.ejs +++ b/generators/generate-blueprint/templates/.blueprint/generate-sample/generator.mjs.ejs @@ -5,36 +5,20 @@ import BaseGenerator from 'generator-jhipster/generators/base'; export default class extends BaseGenerator { sampleName; all; + samplesFolder; constructor(args, opts, features) { - super(args, opts, { ...features, jhipsterBootstrap: false }); - } - - get [BaseGenerator.INITIALIZING]() { - return this.asInitializingTaskGroup({ - async initializeOptions() { - if (this.sampleName && !this.sampleName.endsWith('.jdl')) { - this.sampleName += '.jdl'; - } - }, - }); - } - - get [BaseGenerator.PROMPTING]() { - return this.asPromptingTaskGroup({ - async askForSample() { - await this.promptCurrentJHipsterCommand(); - }, - }); + super(args, opts, { ...features, queueCommandTasks: true, jhipsterBootstrap: false }); } get [BaseGenerator.WRITING]() { return this.asWritingTaskGroup({ async copySample() { + const samplesFolder = `${this.samplesFolder ?? 'samples'}/`; if (this.all) { - this.copyTemplate('samples/*.jdl', ''); + this.copyTemplate(`${samplesFolder}*.jdl`, ''); } else { - this.copyTemplate(`samples/${this.sampleName}`, this.sampleName, { noGlob: true }); + this.copyTemplate(`${samplesFolder}${this.sampleName}`, this.sampleName, { noGlob: true }); } }, }); diff --git a/generators/generate-blueprint/templates/.blueprint/generate-sample/get-samples.mjs.ejs b/generators/generate-blueprint/templates/.blueprint/generate-sample/get-samples.mjs.ejs new file mode 100644 index 000000000000..007d1ae01f59 --- /dev/null +++ b/generators/generate-blueprint/templates/.blueprint/generate-sample/get-samples.mjs.ejs @@ -0,0 +1,29 @@ +<%# + 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 { readdir, stat } from 'node:fs/promises'; +import { extname } from 'path'; + +export const getSamples = async samplesFolder => { + const filenames = await readdir(samplesFolder); + const entries = await Promise.all(filenames.map(async filename => [filename, await stat(`${samplesFolder}/${filename}`)])); + return entries + .filter(([filename, statResult]) => extname(filename) === '.jdl' || statResult.isDirectory()) + .map(([filename]) => filename) + .filter(filename => !filename.includes('disabled')); +}; diff --git a/generators/generate-blueprint/templates/.blueprint/github-build-matrix/build-matrix.mjs.ejs b/generators/generate-blueprint/templates/.blueprint/github-build-matrix/build-matrix.mjs.ejs index d60e02c3f995..41e45bbe1088 100644 --- a/generators/generate-blueprint/templates/.blueprint/github-build-matrix/build-matrix.mjs.ejs +++ b/generators/generate-blueprint/templates/.blueprint/github-build-matrix/build-matrix.mjs.ejs @@ -9,14 +9,17 @@ const defaultMatrix = { 'default-environment': ['prod'], }; -export const buildMatrix = async samplesFolder => { - const samples = await readdir(samplesFolder); +export const buildMatrix = ({ samples, samplesFolder }) => { return { include: Object.values( fromMatrix({ ...defaultMatrix, - 'sample-name': samples.filter(sample => !sample.includes('disabled')), + 'sample-name': samples, }), - ), + ).map(sample => ({ + ...sample, + 'job-name': sample['sample-name'], + 'extra-args': `--samples-folder ${samplesFolder}`, + })), }; }; diff --git a/generators/generate-blueprint/templates/.blueprint/github-build-matrix/command.mjs.ejs b/generators/generate-blueprint/templates/.blueprint/github-build-matrix/command.mjs.ejs new file mode 100644 index 000000000000..33aabf03f279 --- /dev/null +++ b/generators/generate-blueprint/templates/.blueprint/github-build-matrix/command.mjs.ejs @@ -0,0 +1,38 @@ +<%# + 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 { readdir } from 'node:fs/promises'; + +/** + * @type {import('generator-jhipster').JHipsterCommandDefinition} + */ +const command = { + configs: { + samplesFolder: { + description: 'Samples folder', + cli: { + type: String, + }, + default: 'samples', + scope: 'generator', + }, + }, + options: {}, +}; + +export default command; diff --git a/generators/generate-blueprint/templates/.blueprint/github-build-matrix/generator.mjs.ejs b/generators/generate-blueprint/templates/.blueprint/github-build-matrix/generator.mjs.ejs index 485f9579574a..7156e85aa79a 100644 --- a/generators/generate-blueprint/templates/.blueprint/github-build-matrix/generator.mjs.ejs +++ b/generators/generate-blueprint/templates/.blueprint/github-build-matrix/generator.mjs.ejs @@ -1,19 +1,27 @@ import { existsSync, appendFileSync } from 'node:fs'; import os from 'node:os'; import BaseGenerator from 'generator-jhipster/generators/base'; -import { setGithubTaskOutput } from 'generator-jhipster/testing'; import { buildMatrix } from './build-matrix.mjs'; +import { getSamples } from '../generate-sample/get-samples.mjs'; export default class extends BaseGenerator { + samplesFolder; + constructor(args, opts, features) { - super(args, opts, { ...features, jhipsterBootstrap: false }); + super(args, opts, { ...features, queueCommandTasks: true, jhipsterBootstrap: false }); } get [BaseGenerator.WRITING]() { return this.asWritingTaskGroup({ async buildMatrix() { - const matrix = await buildMatrix(this.templatePath('../../generate-sample/templates/samples')); - setGithubTaskOutput('matrix', matrix); + const samples = await getSamples(this.templatePath(`../../generate-sample/templates/${this.samplesFolder}`)); + const matrix = buildMatrix({ samples, samplesFolder: this.samplesFolder }); + const matrixoutput = `matrix<