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

more adjusts for Kotlin blueprint migration #26477

Merged
merged 4 commits into from
Jun 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
3 changes: 3 additions & 0 deletions generators/app/__snapshots__/generator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ exports[`generator - app with default config should match snapshot 1`] = `
"clientThemeVariant": "",
"communicationSpringWebsocket": false,
"cucumberTests": false,
"customizeTemplatePaths": [],
"cypressTests": false,
"dasherizedBaseName": "jhipster",
"databaseMigration": undefined,
Expand Down Expand Up @@ -869,6 +870,7 @@ exports[`generator - app with gateway should match snapshot 1`] = `
"clientThemeVariant": "",
"communicationSpringWebsocket": false,
"cucumberTests": false,
"customizeTemplatePaths": [],
"cypressTests": false,
"dasherizedBaseName": "jhipster",
"databaseMigration": undefined,
Expand Down Expand Up @@ -1465,6 +1467,7 @@ exports[`generator - app with microservice should match snapshot 1`] = `
"clientThemeVariant": undefined,
"communicationSpringWebsocket": false,
"cucumberTests": false,
"customizeTemplatePaths": [],
"cypressTests": false,
"dasherizedBaseName": "jhipster",
"databaseMigration": undefined,
Expand Down
9 changes: 9 additions & 0 deletions generators/base-application/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ export type BaseApplication = {

skipClient?: boolean;
skipServer?: boolean;

/** Customize templates sourceFile and destinationFile */
customizeTemplatePaths: Array<
(file: {
namespace: string;
sourceFile: string;
destinationFile: string;
}) => undefined | { sourceFile: string; destinationFile: string }
>;
} & I18nApplication;

/* ApplicationType Start */
Expand Down
55 changes: 37 additions & 18 deletions generators/base-core/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export default class CoreGenerator extends YeomanGenerator<JHipsterGeneratorOpti
skipChecks?: boolean;
experimental?: boolean;
debugEnabled?: boolean;
jhipster7Migration?: boolean;
jhipster7Migration?: boolean | 'verbose' | 'silent';
relativeDir = relativeDir;
relative = posixRelative;

Expand Down Expand Up @@ -726,10 +726,16 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`);
assert(paramCount > 0, 'One of sections, blocks or templates is required');
assert(paramCount === 1, 'Only one of sections, blocks or templates must be provided');

const { sections, blocks, templates, rootTemplatesPath, context = this, transform: methodTransform = [] } = options as any;
const { sections, blocks, context = this, templates } = options as any;
const { rootTemplatesPath, customizeTemplatePath = file => file, transform: methodTransform = [] } = options;
const { _: commonSpec = {} } = sections || {};
const { transform: sectionTransform = [] } = commonSpec;
const startTime = new Date().getMilliseconds();
const { customizeTemplatePaths: contextCustomizeTemplatePaths = [] } = context as BaseApplication;

const templateData = this.jhipster7Migration
? createJHipster7Context(this, context, { log: this.jhipster7Migration === 'verbose' ? msg => this.log.info(msg) : () => {} })
: context;

/* Build lookup order first has preference.
* Example
Expand All @@ -748,7 +754,7 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`);
rootTemplatesAbsolutePath = rootTemplatesPath;
} else {
rootTemplatesAbsolutePath = (this as any).jhipsterTemplatesFolders
.map(templateFolder => [].concat(rootTemplatesPath).map(relativePath => join(templateFolder, relativePath)))
.map(templateFolder => ([] as string[]).concat(rootTemplatesPath).map(relativePath => join(templateFolder, relativePath)))
.flat();
}

Expand All @@ -764,7 +770,7 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`);
return val;
}
if (typeof val === 'function') {
return val.call(this, context) || false;
return val.call(this, templateData) || false;
}
throw new Error(`Type not supported ${val}`);
};
Expand All @@ -773,16 +779,33 @@ You can ignore this error by passing '--skip-checks' to jhipster command.`);
const extension = extname(sourceFile);
const isBinary = binary || ['.png', '.jpg', '.gif', '.svg', '.ico'].includes(extension);
const appendEjs = noEjs === undefined ? !isBinary && extension !== '.ejs' : !noEjs;
const ejsFile = appendEjs || extension === '.ejs';
let targetFile;
if (typeof destinationFile === 'function') {
targetFile = resolveCallback(destinationFile);
} else {
targetFile = appendEjs ? normalizeEjs(destinationFile) : destinationFile;
}

const files = customizeTemplatePath({ sourceFile, destinationFile: targetFile });
if (!files) {
return undefined;
}
sourceFile = files.sourceFile;
targetFile = files.destinationFile;

for (const contextCustomizeTemplatePath of contextCustomizeTemplatePaths) {
const files = contextCustomizeTemplatePath({ namespace: this.options.namespace, sourceFile, destinationFile: targetFile });
if (!files) {
return undefined;
}
sourceFile = files.sourceFile;
targetFile = files.destinationFile;
}

let sourceFileFrom;
if (Array.isArray(rootTemplatesAbsolutePath)) {
if (isAbsolute(sourceFile)) {
sourceFileFrom = sourceFile;
} else if (Array.isArray(rootTemplatesAbsolutePath)) {
// Look for existing templates
const existingTemplates = rootTemplatesAbsolutePath
.map(rootPath => this.templatePath(rootPath, sourceFile))
Expand All @@ -807,17 +830,14 @@ templates: ${JSON.stringify(existingTemplates, null, 2)}`;
} else {
sourceFileFrom = this.templatePath(sourceFile);
}
if (appendEjs) {
sourceFileFrom = `${sourceFileFrom}.ejs`;
}

if (!ejsFile) {
if (!appendEjs && extname(sourceFileFrom) !== '.ejs') {
await (this as any).copyTemplateAsync(sourceFileFrom, targetFile);
} else {
let useAsync = true;
if (context.entityClass) {
if (!context.baseName) {
throw new Error('baseName is require at templates context');
throw new Error('baseName is required at templates context');
}
const sourceBasename = basename(sourceFileFrom);
const seed = `${context.entityClass}-${sourceBasename}${context.fakerSeed ?? ''}`;
Expand All @@ -836,18 +856,17 @@ templates: ${JSON.stringify(existingTemplates, null, 2)}`;
cache: false,
};
const copyOptions = { noGlob: true };
const { jhipster7Migration } = this as any;
const data = jhipster7Migration
? createJHipster7Context(this, context, { log: jhipster7Migration === 'verbose' ? msg => this.log.info(msg) : () => {} })
: context;
if (appendEjs) {
sourceFileFrom = `${sourceFileFrom}.ejs`;
}
if (useAsync) {
await (this as any).renderTemplateAsync(sourceFileFrom, targetFile, data, renderOptions, copyOptions);
await (this as any).renderTemplateAsync(sourceFileFrom, targetFile, templateData, renderOptions, copyOptions);
} else {
(this as any).renderTemplate(sourceFileFrom, targetFile, data, renderOptions, copyOptions);
(this as any).renderTemplate(sourceFileFrom, targetFile, templateData, renderOptions, copyOptions);
}
}
if (!isBinary && transform && transform.length) {
(this as any).editFile(targetFile, ...transform);
this.editFile(targetFile, ...transform);
}
return targetFile;
};
Expand Down
8 changes: 7 additions & 1 deletion generators/base/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export type JHipsterGeneratorFeatures = BaseFeatures & {
/**
* Wraps write context and shows removed fields and replacements if exists.
*/
jhipster7Migration?: boolean;
jhipster7Migration?: boolean | 'verbose' | 'silent';
sbsBlueprint?: boolean;
checkBlueprint?: boolean;
/**
Expand Down Expand Up @@ -159,6 +159,12 @@ export type WriteFileOptions<Generator = CoreGenerator, DataType = any> = {
* Single absolute path or relative path(s) between the templates folder and template path.
*/
rootTemplatesPath?: string | string[];

/** @experimental Customize templates sourceFile and destinationFile */
customizeTemplatePath?: (file: {
sourceFile: string;
destinationFile: string;
}) => undefined | { sourceFile: string; destinationFile: string };
} & (
| {
sections: WriteFileSection<Generator, DataType>;
Expand Down
6 changes: 5 additions & 1 deletion generators/base/shared-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ export default class SharedData<ApplicationType extends BaseApplication = BaseAp
control: initialControl,
props: {},
});
this._storage.sharedApplication.nodeDependencies = this._storage.sharedApplication.nodeDependencies ?? {};

defaults(this._storage.sharedApplication, {
nodeDependencies: {},
customizeTemplatePaths: [],
});
}

getSource() {
Expand Down
22 changes: 22 additions & 0 deletions generators/base/support/jhipster7-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,28 @@ const deprecatedProperties = {
replacement: 'entityTableName || relationship.columnName',
get: () => hibernateSnakeCase,
},
isUsingBuiltInUser: {
replacement: 'generateBuiltInUserEntity',
get:
({ data }) =>
() =>
data.generateBuiltInUserEntity,
},
isUsingBuiltInAuthority: {
replacement: 'generateBuiltInAuthorityEntity',
get:
({ data }) =>
() =>
data.generateBuiltInAuthorityEntity,
},
jhipsterConfig: {
replacement: 'none',
get: ({ generator }) => generator.config?.getAll?.(),
},
configOptions: {
replacement: 'none',
get: () => ({}),
},
};

const ejsBuiltInProperties = ['__append', '__line', 'escapeFn', 'include', 'undefined'];
Expand Down
Loading