Skip to content

Commit

Permalink
Merge pull request #14391 from mshima/skip_ci_needle_angular
Browse files Browse the repository at this point in the history
Fixes angular duplicated navbar element generation.
  • Loading branch information
pascalgrimaud authored Mar 20, 2021
2 parents cff779c + 6856bca commit c4e6e19
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
2 changes: 1 addition & 1 deletion generators/bootstrap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const { STRING: TYPE_STRING, LONG: TYPE_LONG } = CommonDBTypes;

module.exports = class extends BaseGenerator {
constructor(args, options) {
super(args, options, { unique: 'namespace' });
super(args, options, { unique: 'namespace', customCommitTask: true });

/*
* When testing a generator with yeoman-test using 'withLocalConfig(localConfig)', it instantiates the
Expand Down
14 changes: 11 additions & 3 deletions generators/client/needle-api/needle-client-angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ module.exports = class extends needleClientBase {
modulePath,
importNeedle
);
importRewriteFileModel.prettierAware = true;
this.addBlockContentToFile(importRewriteFileModel, errorMessage);

const moduleRewriteFileModel = this._generateRewriteFileModelAddModule(appName, angularName, modulePath, moduleNeedle);
Expand Down Expand Up @@ -155,30 +156,34 @@ module.exports = class extends needleClientBase {
) {
const errorMessage = `${chalk.yellow('Reference to ') + routerName} ${chalk.yellow('not added to menu.\n')}`;
const entityMenuPath = `${this.CLIENT_MAIN_SRC_DIR}app/layouts/navbar/navbar.component.html`;
const routerLink = `routerLink="${routerName}"`;
const entityEntry =
// prettier-ignore
this.generator.stripMargin(`|<li>
| <a class="dropdown-item" routerLink="${routerName}" routerLinkActive="active" [routerLinkActiveOptions]="{ exact: true }" (click)="collapseNavbar()">
| <a class="dropdown-item" ${routerLink} routerLinkActive="active" [routerLinkActiveOptions]="{ exact: true }" (click)="collapseNavbar()">
| <fa-icon icon="asterisk" [fixedWidth]="true"></fa-icon>
| <span${enableTranslation ? ` ${jhiPrefix}Translate="global.menu.entities.${entityTranslationKeyMenu}"` : ''}>${entityTranslationValue}</span>
| </a>
| </li>`);
const rewriteFileModel = this.generateFileModel(entityMenuPath, 'jhipster-needle-add-entity-to-menu', entityEntry);
rewriteFileModel.regexp = routerLink;

this.addBlockContentToFile(rewriteFileModel, errorMessage);
}

addElementToMenu(routerName, iconName, enableTranslation, translationKeyMenu = routerName, jhiPrefix = 'jhi') {
const errorMessage = `${chalk.yellow('Reference to ') + routerName} ${chalk.yellow('not added to menu.\n')}`;
const entityMenuPath = `${this.CLIENT_MAIN_SRC_DIR}app/layouts/navbar/navbar.component.html`;
const routerLink = `routerLink="${routerName}"`;
// prettier-ignore
const entityEntry = `<li class="nav-item" routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}">
<a class="nav-link" routerLink="${routerName}" (click)="collapseNavbar()">
<a class="nav-link" ${routerLink} (click)="collapseNavbar()">
<fa-icon icon="${iconName}" [fixedWidth]="true"></fa-icon>
<span${enableTranslation ? ` ${jhiPrefix}Translate="global.menu.${translationKeyMenu}"` : ''}>${_.startCase(routerName)}</span>
</a>
</li>`;
const rewriteFileModel = this.generateFileModel(entityMenuPath, 'jhipster-needle-add-element-to-menu', entityEntry);
rewriteFileModel.regexp = routerLink;

this.addBlockContentToFile(rewriteFileModel, errorMessage);
this.addIcon(iconName);
Expand All @@ -187,14 +192,16 @@ module.exports = class extends needleClientBase {
addElementToAdminMenu(routerName, iconName, enableTranslation, translationKeyMenu = routerName, jhiPrefix = 'jhi') {
const errorMessage = `${chalk.yellow('Reference to ') + routerName} ${chalk.yellow('not added to admin menu.\n')}`;
const navbarAdminPath = `${this.CLIENT_MAIN_SRC_DIR}app/layouts/navbar/navbar.component.html`;
const routerLink = `routerLink="${routerName}"`;
// prettier-ignore
const entityEntry = `<li>
<a class="dropdown-item" routerLink="${routerName}" routerLinkActive="active" (click)="collapseNavbar()">
<a class="dropdown-item" ${routerLink} routerLinkActive="active" (click)="collapseNavbar()">
<fa-icon icon="${iconName}" [fixedWidth]="true"></fa-icon>
<span${enableTranslation ? ` ${jhiPrefix}Translate="global.menu.admin.${translationKeyMenu}"` : ''}>${_.startCase(routerName)}</span>
</a>
</li>`;
const rewriteFileModel = this.generateFileModel(navbarAdminPath, 'jhipster-needle-add-element-to-admin-menu', entityEntry);
rewriteFileModel.regexp = routerLink;

this.addBlockContentToFile(rewriteFileModel, errorMessage);
this.addIcon(iconName);
Expand All @@ -218,6 +225,7 @@ module.exports = class extends needleClientBase {
| },`
);
const rewriteFileModel = this.generateFileModel(filePath, needleName, routingEntry);
rewriteFileModel.prettierAware = true;
this.addBlockContentToFile(rewriteFileModel, errorMessage);
}

Expand Down
8 changes: 4 additions & 4 deletions generators/generator-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,21 +391,21 @@ module.exports = class JHipsterBaseGenerator extends PrivateBase {
addEntityToMenu(
routerName,
enableTranslation,
clientFramework,
clientFramework = this.clientFramework,
entityTranslationKeyMenu = _.camelCase(routerName),
entityTranslationValue = _.startCase(routerName)
) {
if (this.clientFramework === ANGULAR) {
if (clientFramework === ANGULAR) {
this.needleApi.clientAngular.addEntityToMenu(
routerName,
enableTranslation,
entityTranslationKeyMenu,
entityTranslationValue,
this.jhiPrefix
);
} else if (this.clientFramework === REACT) {
} else if (clientFramework === REACT) {
this.needleApi.clientReact.addEntityToMenu(routerName, enableTranslation, entityTranslationKeyMenu, entityTranslationValue);
} else if (this.clientFramework === VUE) {
} else if (clientFramework === VUE) {
this.needleApi.clientVue.addEntityToMenu(routerName, enableTranslation, entityTranslationKeyMenu, entityTranslationValue);
}
}
Expand Down
24 changes: 23 additions & 1 deletion generators/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,37 @@ function normalizeLineEndings(str) {
return isWin32 ? str.replace(/\r\n/g, '\n') : str;
}

/**
* Change spaces sequences and '>' to allow any number of spaces or new line prefix
*
* @param {string} str string
* @returns {string} string where CRLF is replaced with LF in Windows
*/
function convertToPrettierExpressions(str) {
return str.replace(/\s+/g, '([\\s\n]*)').replace(/>+/g, '(\n?[\\s]*)>');
}

/**
* Rewrite using the passed argument object.
*
* @param {object} args arguments object (containing splicable, haystack, needle properties) to be used
* @param {string[]} args.splicable - content to be added.
* @param {boolean} [args.prettierAware] - apply prettier aware expressions before looking for applied needles.
* @param {string|RegExp} [args.regexp] - use another content for looking for applied needles.
* @returns {*} re-written file
*/
function rewrite(args) {
// check if splicable is already in the body text
const re = new RegExp(args.splicable.map(line => `\\s*${escapeRegExp(normalizeLineEndings(line))}`).join('\n'));
let re = args.regexp;
if (re) {
re = re.test ? re : new RegExp(re);
} else {
let content = args.splicable.map(line => `\\s*${escapeRegExp(normalizeLineEndings(line))}`).join('\n');
if (args.prettierAware) {
content = convertToPrettierExpressions(content);
}
re = new RegExp(content);
}

if (re.test(normalizeLineEndings(args.haystack))) {
return args.haystack;
Expand Down

0 comments on commit c4e6e19

Please sign in to comment.