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

generate-blueprint: add skipWorkflows and ignoreExistingGenerators options #27382

Merged
merged 2 commits into from
Sep 23, 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
24 changes: 14 additions & 10 deletions generators/base-core/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -858,23 +858,26 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`);
}

const normalizeEjs = file => file.replace('.ejs', '');
const resolveCallback = (val, fallback?) => {
if (val === undefined) {
const resolveCallback = (maybeCallback, fallback?) => {
if (maybeCallback === undefined) {
if (typeof fallback === 'function') {
return resolveCallback(fallback);
}
return fallback;
}
if (typeof val === 'boolean' || typeof val === 'string') {
return val;
if (typeof maybeCallback === 'boolean' || typeof maybeCallback === 'string') {
return maybeCallback;
}
if (typeof val === 'function') {
return val.call(this, templateData) || false;
if (typeof maybeCallback === 'function') {
return (maybeCallback as any).call(this, templateData) || false;
}
throw new Error(`Type not supported ${val}`);
throw new Error(`Type not supported ${maybeCallback}`);
};

const renderTemplate = async ({ sourceFile, destinationFile, options, noEjs, transform, binary }) => {
const renderTemplate = async ({ condition, sourceFile, destinationFile, options, noEjs, transform, binary }) => {
if (condition !== undefined && !resolveCallback(condition, true)) {
return undefined;
}
const extension = extname(sourceFile);
const isBinary = binary || ['.png', '.jpg', '.gif', '.svg', '.ico'].includes(extension);
const appendEjs = noEjs === undefined ? !isBinary && extension !== '.ejs' : !noEjs;
Expand Down Expand Up @@ -1064,7 +1067,7 @@ templates: ${JSON.stringify(existingTemplates, null, 2)}`;
return { sourceFile, destinationFile, noEjs, transform: derivedTransform };
}

const { options, file, renameTo, transform: fileTransform = [], binary } = fileSpec;
const { condition, options, file, renameTo, transform: fileTransform = [], binary } = fileSpec;
let { sourceFile, destinationFile } = fileSpec;

if (typeof fileTransform === 'boolean') {
Expand Down Expand Up @@ -1099,6 +1102,7 @@ templates: ${JSON.stringify(existingTemplates, null, 2)}`;
}

return {
condition,
sourceFile,
destinationFile,
options,
Expand All @@ -1119,7 +1123,7 @@ templates: ${JSON.stringify(existingTemplates, null, 2)}`;
});
}

const files = await Promise.all(parsedTemplates.map(template => renderTemplate(template)));
const files = await Promise.all(parsedTemplates.map(template => renderTemplate(template)).filter(Boolean));
this.log.debug(`Time taken to write files: ${new Date().getMilliseconds() - startTime}ms`);
return files.filter(file => file);
}
Expand Down
11 changes: 7 additions & 4 deletions generators/base/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,19 @@ export type CascatedEditFileCallback<Generator = CoreGenerator> = (
...callbacks: EditFileCallback<Generator>[]
) => CascatedEditFileCallback<Generator>;

type DataCallback<Type, DataType = ApplicationType<Entity>, Generator = CoreGenerator> = Type | ((this: Generator, data: DataType) => Type);

export type WriteFileTemplate<DataType = ApplicationType<Entity>, Generator = CoreGenerator> =
| string
| ((this: Generator, data: DataType, filePath: string) => string)
| {
condition?: DataCallback<boolean, DataType, Generator>;
/** source file */
sourceFile?: string | ((this: Generator, data: DataType) => string);
sourceFile?: DataCallback<string, DataType, Generator>;
/** destination file */
destinationFile?: string | ((this: Generator, destinationFile: DataType) => string);
destinationFile?: DataCallback<string, DataType, Generator>;
/** @deprecated, use sourceFile instead */
file?: string | ((this: Generator, data: DataType) => string);
file?: DataCallback<string, DataType, Generator>;
/** @deprecated, use destinationFile instead */
renameTo?: string | ((this: Generator, data: DataType, filePath: string) => string);
/** transforms (files processing) to be applied */
Expand All @@ -136,7 +139,7 @@ export type WriteFileTemplate<DataType = ApplicationType<Entity>, Generator = Co
binary?: boolean;
/** ejs options. Refer to https://ejs.co/#docs */
options?: Record<string, object>;
override?: boolean | ((this: Generator, data: DataType) => boolean);
override?: DataCallback<boolean, DataType, Generator>;
};

export type WriteFileBlock<DataType = ApplicationType<Entity>, Generator = CoreGenerator> = {
Expand Down
14 changes: 14 additions & 0 deletions generators/generate-blueprint/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ const command: JHipsterCommandDefinition = {
},
scope: 'generator',
},
skipWorkflows: {
description: 'Skip github workflows',
cli: {
type: Boolean,
},
scope: 'generator',
},
ignoreExistingGenerators: {
description: 'Ignore existing generators',
cli: {
type: Boolean,
},
scope: 'generator',
},
githubRepository: {
cli: {
description: 'Github Repository',
Expand Down
47 changes: 24 additions & 23 deletions generators/generate-blueprint/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export const files = asWriteFilesSection<any>({
'.github/workflows/generator.yml',
'.prettierignore.jhi.blueprint',
{ sourceFile: 'eslint.config.js.jhi.blueprint', destinationFile: ctx => `${ctx.eslintConfigFile}.jhi.blueprint` },
'README.md',
{
sourceFile: 'README.md',
override: data => !data.ignoreExistingGenerators,
},
'tsconfig.json',
'vitest.config.ts',
'.blueprint/cli/commands.mjs',
Expand All @@ -39,7 +42,7 @@ export const files = asWriteFilesSection<any>({
],
},
{
condition: ctx => !ctx[LOCAL_BLUEPRINT_OPTION] && ctx.githubWorkflows,
condition: ctx => !ctx[LOCAL_BLUEPRINT_OPTION] && ctx.githubWorkflows && !ctx.skipWorkflows,
templates: [
'.blueprint/github-build-matrix/build-matrix.mjs',
'.blueprint/github-build-matrix/generator.mjs',
Expand All @@ -59,30 +62,28 @@ export const files = asWriteFilesSection<any>({
],
});

export const generatorFiles = {
export const generatorFiles = asWriteFilesSection<any>({
generator: [
{
path: 'generators/generator',
to: ctx => `${ctx.application.blueprintsPath}${ctx.generator}`,
templates: [
{ file: 'generator.mjs.jhi', renameTo: ctx => (ctx.js ? 'generator.js.jhi' : 'generator.mjs.jhi') },
{ file: 'index.mjs', renameTo: ctx => (ctx.js ? 'index.js' : 'index.mjs') },
],
},
{
path: 'generators/generator',
condition: ctx => ctx.priorities.find(priority => priority.name === 'initializing'),
to: ctx => `${ctx.application.blueprintsPath}${ctx.generator}`,
templates: [{ file: 'command.mjs', renameTo: ctx => (ctx.js ? 'command.js' : 'command.mjs') }],
},
{
path: 'generators/generator',
to: ctx => `${ctx.application.blueprintsPath}${ctx.generator}`,
condition: ctx => !ctx.generator.startsWith('entity') && !ctx.application[LOCAL_BLUEPRINT_OPTION],
templates: [
{ sourceFile: 'index.mjs', destinationFile: ctx => (ctx.js ? 'index.js' : 'index.mjs') },
{
sourceFile: 'command.mjs',
destinationFile: ctx => (ctx.js ? 'command.js' : 'command.mjs'),
override: data => !data.ignoreExistingGenerators,
},
{
sourceFile: 'generator.mjs.jhi',
destinationFile: ctx => (ctx.js ? 'generator.js.jhi' : 'generator.mjs.jhi'),
override: data => !data.ignoreExistingGenerators,
},
{
file: 'generator.spec.mjs',
renameTo: ctx => (ctx.js ? 'generator.spec.js' : 'generator.spec.mjs'),
condition: data => !data.generator.startsWith('entity') && !data.application[LOCAL_BLUEPRINT_OPTION],
sourceFile: 'generator.spec.mjs',
destinationFile: data => (data.js ? 'generator.spec.js' : 'generator.spec.mjs'),
override: data => !data.ignoreExistingGenerators,
},
],
},
Expand All @@ -95,10 +96,10 @@ export const generatorFiles = {
transform: false,
templates: [
{
file: 'templates/template-file.ejs',
renameTo: ctx => `templates/template-file-${ctx.generator}.ejs`,
sourceFile: 'templates/template-file.ejs',
destinationFile: ctx => `templates/template-file-${ctx.generator}.ejs`,
},
],
},
],
};
});
15 changes: 13 additions & 2 deletions generators/generate-blueprint/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const defaultPublishedFiles = ['generators', '!**/__*', '!**/*.snap', '!**/*.spe
export default class extends BaseGenerator {
application!: any;
recreatePackageLock!: boolean;
skipWorkflows!: boolean;
ignoreExistingGenerators!: boolean;

async _beforeQueue() {
if (!this.fromBlueprint) {
Expand Down Expand Up @@ -210,16 +212,23 @@ export default class extends BaseGenerator {
'8.7.2': ['vitest.test-setup.ts'],
});
},
async writing() {
async writing({ application }) {
this.application.sampleWritten = this.jhipsterConfig.sampleWritten;
const { skipWorkflows, ignoreExistingGenerators } = this;
await this.writeFiles({
sections: files,
context: this.application,
context: {
...application,
skipWorkflows,
ignoreExistingGenerators,
...this.application,
},
});
this.jhipsterConfig.sampleWritten = true;
},
async writingGenerators() {
if (!this.application[GENERATORS]) return;
const { skipWorkflows, ignoreExistingGenerators } = this;
for (const generator of Object.keys(this.application[GENERATORS])) {
const subGeneratorStorage = this.getSubGeneratorStorage(generator);
const subGeneratorConfig = subGeneratorStorage.getAll();
Expand All @@ -231,6 +240,8 @@ export default class extends BaseGenerator {
const customGenerator = !Object.values(GENERATOR_LIST).includes(generator);
const jhipsterGenerator = customGenerator || subGeneratorConfig.sbs ? 'base-application' : generator;
const subTemplateData = {
skipWorkflows,
ignoreExistingGenerators,
js: this.application.js,
application: this.application,
...defaultSubGeneratorConfig(),
Expand Down
Loading