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

cleanup generator-private methods #20944

Merged
merged 1 commit into from
Jan 27, 2023
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: 1 addition & 2 deletions generators/app/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import BaseGenerator from '../base-application/index.mjs';
import { checkNode } from './support/index.mjs';
import gitOptions from '../git/options.mjs';
import serverOptions from '../server/options.mjs';
import { cleanupOldFiles, upgradeFiles } from '../cleanup.mjs';
import { cleanupOldFiles } from '../cleanup.mjs';
import prompts from './prompts.mjs';
import { packageJson } from '../../lib/index.mjs';
import statistics from '../statistics.cjs';
Expand Down Expand Up @@ -488,7 +488,6 @@ export default class JHipsterAppGenerator extends BaseGenerator {
return this.asWritingTaskGroup({
cleanup({ application }) {
cleanupOldFiles(this, application);
upgradeFiles(this);
},
});
}
Expand Down
67 changes: 1 addition & 66 deletions generators/base/generator-base-private.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
* limitations under the License.
*/

import { rmSync, statSync } from 'fs';
import _ from 'lodash';
import Generator from 'yeoman-generator';
import shelljs from 'shelljs';

import { Logger } from './support/logging.mjs';

Expand All @@ -29,12 +27,8 @@ import { Logger } from './support/logging.mjs';
*/

/**
* This is the Generator base private class.
* This provides all the private API methods used internally.
* These methods should not be directly utilized using commonJS require,
* as these can have breaking changes without a major version bump
* This class changes/corrects the yeoman-generator typescript definitions.
*
* The method signatures in private API can be changed without a major version change.
* @class
* @extends {Generator<JHipsterGeneratorOptions>}
*/
Expand Down Expand Up @@ -90,63 +84,4 @@ export default class PrivateBase extends Generator {
spawnCommand(command, args, opt) {
return super.spawnCommand(command, args, opt);
}

/* ======================================================================== */
/* private methods use within generator (not exposed to modules) */
/* ======================================================================== */

/**
* Override yeoman generator's usage function to fine tune --help message.
*/
usage() {
return super.usage().replace('yo jhipster:', 'jhipster ');
}

/**
* Remove File
*
* @param file
*/
removeFile(file) {
const destination = this.destinationPath(file);
if (destination && shelljs.test('-f', destination)) {
this.logger.log(`Removing the file - ${destination}`);
rmSync(destination, { force: true });
}
return destination;
}

/**
* Remove Folder
*
* @param folder
*/
removeFolder(folder) {
folder = this.destinationPath(folder);
try {
if (statSync(folder).isDirectory()) {
rmSync(folder, { recursive: true });
}
} catch (error) {
this.logger.log(`Could not remove folder ${folder}`);
}
}

/**
* @private
* Execute a git mv.
*
* @param {string} source
* @param {string} dest
* @returns {boolean} true if success; false otherwise
*/
gitMove(from, to) {
const source = this.destinationPath(from);
const dest = this.destinationPath(to);
if (source && dest && shelljs.test('-f', source)) {
this.logger.info(`Renaming the file - ${source} to ${dest}`);
return !shelljs.exec(`git mv -f ${source} ${dest}`).code;
}
return true;
}
}
40 changes: 40 additions & 0 deletions generators/base/generator.mts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import _ from 'lodash';
import { simpleGit } from 'simple-git';
import type { CopyOptions } from 'mem-fs-editor';
import type { Data as TemplateData, Options as TemplateOptions } from 'ejs';
import { statSync, rmSync } from 'fs';

import SharedData from './shared-data.mjs';
import JHipsterBaseBlueprintGenerator from './generator-base-blueprint.mjs';
Expand Down Expand Up @@ -89,6 +90,13 @@ export default class BaseGenerator extends JHipsterBaseBlueprintGenerator {
this.jhipsterOptions(baseOptions as JHipsterOptions);
}

/**
* Override yeoman generator's usage function to fine tune --help message.
*/
usage(): string {
return super.usage().replace('yo jhipster:', 'jhipster ');
}

/**
* Get arguments for the priority
*/
Expand Down Expand Up @@ -147,6 +155,38 @@ export default class BaseGenerator extends JHipsterBaseBlueprintGenerator {
});
}

/**
* Remove File
* @param file
*/
removeFile(...path: string[]) {
const destinationFile = this.destinationPath(...path);
try {
if (destinationFile && statSync(destinationFile).isFile()) {
this.logger.log(`Removing the file - ${destinationFile}`);
rmSync(destinationFile, { force: true });
}
} catch {
this.logger.log(`Could not remove file ${destinationFile}`);
}
return destinationFile;
}

/**
* Remove Folder
* @param path
*/
removeFolder(...path: string[]) {
const destinationFolder = this.destinationPath(...path);
try {
if (statSync(destinationFolder).isDirectory()) {
rmSync(destinationFolder, { recursive: true });
}
} catch (error) {
this.logger.log(`Could not remove folder ${destinationFolder}`);
}
}

/**
* Utility function to write file.
*
Expand Down
34 changes: 2 additions & 32 deletions generators/cleanup.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
* limitations under the License.
*/

import { SERVER_MAIN_RES_DIR, ANGULAR_DIR, REACT_DIR, VUE_DIR, CLIENT_WEBPACK_DIR, DOCKER_DIR } from './generator-constants.mjs';
import { languageSnakeCase, languageToJavaLanguage } from './languages/support/index.mjs';
import { ANGULAR_DIR, REACT_DIR, VUE_DIR, CLIENT_WEBPACK_DIR, DOCKER_DIR } from './generator-constants.mjs';
import { clientFrameworkTypes, authenticationTypes } from '../jdl/jhipster/index.mjs';

const { ANGULAR, REACT, VUE } = clientFrameworkTypes;
Expand All @@ -34,6 +33,7 @@ const { OAUTH2, SESSION } = authenticationTypes;
*
* @param {any} generator - reference to generator
*/
// eslint-disable-next-line import/prefer-default-export
export function cleanupOldFiles(generator, data) {
if (generator.isJhipsterVersionLessThan('3.2.0')) {
// removeFile and removeFolder methods should be called here for files and folders to cleanup
Expand Down Expand Up @@ -337,33 +337,3 @@ export function cleanupOldFiles(generator, data) {
}
}
}

/**
* Upgrade files.
*
* @param {any} generator - reference to generator
*/
export function upgradeFiles(generator) {
let atLeastOneSuccess = false;
if (generator.isJhipsterVersionLessThan('6.1.0')) {
const languages = generator.config.get('languages');
if (languages) {
const langNameDiffer = function (lang) {
const langProp = languageSnakeCase(lang);
const langJavaProp = languageToJavaLanguage(lang);
return langProp !== langJavaProp ? [langProp, langJavaProp] : undefined;
};
languages
.map(langNameDiffer)
.filter(props => props)
.forEach(props => {
const code = generator.gitMove(
`${SERVER_MAIN_RES_DIR}i18n/messages_${props[0]}.properties`,
`${SERVER_MAIN_RES_DIR}i18n/messages_${props[1]}.properties`
);
atLeastOneSuccess = atLeastOneSuccess || code;
});
}
}
return atLeastOneSuccess;
}
4 changes: 3 additions & 1 deletion generators/docker-compose/files.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ const { OAUTH2 } = authenticationTypes;
export function writeFiles() {
return {
cleanup() {
this.removeFile('realm-config/jhipster-users-0.json');
if (this.isJhipsterVersionLessThan('7.10.0')) {
this.removeFile('realm-config/jhipster-users-0.json');
}
},

writeDockerCompose() {
Expand Down
2 changes: 2 additions & 0 deletions generators/languages/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { updateLanguagesTask as updateLanguagesInReact } from '../react/support/
import { updateLanguagesTask as updateLanguagesInVue } from '../vue/support/index.mjs';
import { updateLanguagesTask as updateLanguagesInJava } from '../server/support/index.mjs';
import { SERVER_MAIN_RES_DIR, SERVER_TEST_RES_DIR } from '../generator-constants.mjs';
import upgradeFilesTask from './upgrade-files-task.mjs';

/**
* This is the base class for a generator that generates entities.
Expand Down Expand Up @@ -268,6 +269,7 @@ export default class LanguagesGenerator extends BaseApplicationGenerator {
// Public API method used by the getter and also by Blueprints
get writing() {
return {
upgradeFilesTask,
async writeClientTranslations({ application }) {
if (!application.enableTranslation || application.skipClient) return;
await Promise.all(
Expand Down
1 change: 1 addition & 0 deletions generators/languages/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
* limitations under the License.
*/
export { default } from './generator.mjs';
export { default as upgradeFilesTask } from './upgrade-files-task.mjs';
52 changes: 52 additions & 0 deletions generators/languages/upgrade-files-task.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import shelljs from 'shelljs';

import BaseGenerator from '../base/index.mjs';
import { SERVER_MAIN_RES_DIR } from '../generator-constants.mjs';
import { languageSnakeCase, languageToJavaLanguage } from './support/index.mjs';

/**
* Execute a git mv.
* A git mv operation is required due to file casing change eg: en_us -> en_US at a case insensitive OS.
*
* @param source
* @param dest
* @returns {boolean} true if success; false otherwise
*/
function gitMove(this: BaseGenerator, from: string, to: string) {
const source = this.destinationPath(from);
const dest = this.destinationPath(to);
if (source && dest && shelljs.test('-f', source)) {
this.logger.info(`Renaming the file - ${source} to ${dest}`);
return !shelljs.exec(`git mv -f ${source} ${dest}`).code;
}
return true;
}

/**
* Upgrade files.
*/
export default function upgradeFilesTask(this: BaseGenerator) {
let atLeastOneSuccess = false;
if (this.isJhipsterVersionLessThan('6.1.0')) {
const languages = this.config.get('languages');
if (languages) {
const langNameDiffer = function (lang) {
const langProp = languageSnakeCase(lang);
const langJavaProp = languageToJavaLanguage(lang);
return langProp !== langJavaProp ? [langProp, langJavaProp] : undefined;
};
languages
.map(langNameDiffer)
.filter(props => props)
.forEach(props => {
const code = gitMove.call(
this,
`${SERVER_MAIN_RES_DIR}i18n/messages_${props[0]}.properties`,
`${SERVER_MAIN_RES_DIR}i18n/messages_${props[1]}.properties`
);
atLeastOneSuccess = atLeastOneSuccess || code;
});
}
}
return atLeastOneSuccess;
}
4 changes: 2 additions & 2 deletions generators/upgrade/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import path from 'path';
import childProcess from 'child_process';

import BaseGenerator from '../base/index.mjs';
import { upgradeFiles } from '../cleanup.mjs';
import { upgradeFilesTask as upgradeLanguagesFilesTask } from '../languages/index.mjs';
import { SERVER_MAIN_RES_DIR } from '../generator-constants.mjs';
import statistics from '../statistics.cjs';
import { parseBluePrints } from '../../utils/blueprint.mjs';
Expand Down Expand Up @@ -162,7 +162,7 @@ export default class UpgradeGenerator extends BaseGenerator {
}

_upgradeFiles() {
if (upgradeFiles(this)) {
if (upgradeLanguagesFilesTask.call(this)) {
const gitCommit = this.gitExec(['commit', '-q', '-m', '"Upgrade preparation."', '--no-verify'], { silent: this.silent });
if (gitCommit.code !== 0) throw new Error(`Unable to prepare upgrade:\n${gitCommit.stderr}`);
this.success('Upgrade preparation');
Expand Down