gen-jhipster
Version:
VHipster - Spring Boot + Angular/React/Vue in one handy generator
93 lines (92 loc) • 4.3 kB
JavaScript
/**
* Copyright 2013-2026 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { SERVER_MAIN_PROTO_DIR, SERVER_MAIN_RES_DIR, SERVER_MAIN_SRC_DIR, SERVER_TEST_RES_DIR, SERVER_TEST_SRC_DIR, } from "../../generator-constants.js";
export const replaceEntityFilePathVariables = (data, filePath) => {
filePath = filePath
?.replace(/_package_/, data.packageFolder)
?.replace(/_entityPackage_/, data.entityJavaPackageFolder)
?.replace(/_mainClass_/, data.mainClass)
?.replace(/_persistClass_/, data.persistClass)
?.replace(/_entityClass_/, data.entityClass)
?.replace(/_entityUnderscoredName_/, data.entityUnderscoredName)
?.replace(/_dtoClass_/, data.dtoClass);
return filePath?.includes('.jhi.') ? filePath : filePath?.replace(/_\w*/, '');
};
/**
* Move the template to `javaPackageSrcDir` (defaults to`src/main/java/${packageFolder}/${filePath}`).
* Removes trailing specifiers.
*/
export const moveToJavaPackageSrcDir = (data, filePath) => `${data.javaPackageSrcDir}${replaceEntityFilePathVariables(data, filePath) ?? ''}`;
/**
* Move the template to `javaPackageTestDir` (defaults to`src/main/java/${packageFolder}/${filePath}`).
* Removes trailing specifiers.
*/
export const moveToJavaPackageTestDir = (data, filePath) => `${data.javaPackageTestDir}${replaceEntityFilePathVariables(data, filePath) ?? ''}`;
export const moveToSrcMainResourcesDir = (data, filePath) => `${data.srcMainResources}${replaceEntityFilePathVariables(data, filePath) ?? ''}`;
/**
* Move the template to src/main/proto/ (for gRPC proto files).
*/
export const moveToSrcMainProtoDir = (data, filePath) => `${SERVER_MAIN_PROTO_DIR}${replaceEntityFilePathVariables(data, filePath) ?? filePath}`;
/**
* Move the template to src/main/proto/util/ (for gRPC util proto files).
*/
export const moveToSrcMainProtoUtilDir = (_data, filePath) => `${SERVER_MAIN_PROTO_DIR}util/${filePath}`;
export function javaWriteFileSection(section) {
return section;
}
export function javaMainPackageTemplatesBlock(blockOrRelativePath = '') {
return javaBlock({
srcPath: `${SERVER_MAIN_SRC_DIR}_package_/`,
destProperty: 'javaPackageSrcDir',
blockOrRelativePath,
});
}
export function javaMainResourceTemplatesBlock(blockOrRelativePath = '') {
return javaBlock({
srcPath: SERVER_MAIN_RES_DIR,
destProperty: 'srcMainResources',
blockOrRelativePath,
});
}
export function javaTestResourceTemplatesBlock(blockOrRelativePath = '') {
return javaBlock({
srcPath: SERVER_TEST_RES_DIR,
destProperty: 'srcTestResources',
blockOrRelativePath,
});
}
export function javaTestPackageTemplatesBlock(blockOrRelativePath = '') {
return javaBlock({
srcPath: `${SERVER_TEST_SRC_DIR}_package_/`,
destProperty: 'javaPackageTestDir',
blockOrRelativePath,
});
}
function javaBlock({ srcPath, destProperty, blockOrRelativePath = '', }) {
const block = typeof blockOrRelativePath === 'string' ? undefined : blockOrRelativePath;
const blockRenameTo = typeof block?.renameTo === 'function' ? block.renameTo : undefined;
const relativePath = typeof blockOrRelativePath === 'string' ? blockOrRelativePath : (blockOrRelativePath.relativePath ?? '');
return {
path: `${srcPath}${relativePath}`,
...block,
renameTo(data, filePath) {
return `${data[destProperty]}${replaceEntityFilePathVariables(data, relativePath) ?? ''}${replaceEntityFilePathVariables(data, blockRenameTo?.call?.(this, data, filePath) ?? filePath) ?? ''}`;
},
};
}