Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improvements to generate-sample #26176

Merged
merged 1 commit into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions .blueprint/generate-sample/command.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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],
Expand Down
50 changes: 43 additions & 7 deletions .blueprint/generate-sample/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -11,6 +11,8 @@ export default class extends BaseGenerator {
global;
projectFolder;
projectVersion;
entitiesSample;
sampleYorcFolder;

get [BaseGenerator.INITIALIZING]() {
return this.asInitializingTaskGroup({
Expand All @@ -33,22 +35,29 @@ export default class extends BaseGenerator {
});
}

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

get [BaseGenerator.END]() {
return this.asEndTaskGroup({
async generateJdlSample() {
if (extname(this.sampleName) !== '.jdl') return;

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,
});

Expand All @@ -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 };
}
Expand All @@ -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', {
Expand Down
12 changes: 2 additions & 10 deletions .blueprint/generate-sample/support/generate-sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions .blueprint/generate-sample/support/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './copy-entity-samples.js';
export * from './generate-sample.js';
Loading