|
@@ -31,69 +31,74 @@ public class BaseCodeGenerate {
|
|
|
* 代码生成
|
|
* 代码生成
|
|
|
*
|
|
*
|
|
|
* @param createFileConfig 创建文件配置
|
|
* @param createFileConfig 创建文件配置
|
|
|
- * @param projectPath 项目路径
|
|
|
|
|
- * @param config 数据项配置
|
|
|
|
|
|
|
+ * @param generateRootPath 生成文件路径
|
|
|
|
|
+ * @param dataModel 数据模型
|
|
|
* @throws Exception 异常
|
|
* @throws Exception 异常
|
|
|
*/
|
|
*/
|
|
|
- protected void generate(CreateFileConfig createFileConfig, String projectPath, Map<String, Object> config) throws Exception {
|
|
|
|
|
- LOGGER.debug("--------generate----projectPath--------" + projectPath);
|
|
|
|
|
- // 遍历路径生成代码
|
|
|
|
|
- for (int i = 0; i < createFileConfig.getTemplateRootDirs().size(); i++) {
|
|
|
|
|
- generate(projectPath, createFileConfig.getTemplateRootDirs().get(i), config, createFileConfig);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ protected void generate(CreateFileConfig createFileConfig, String generateRootPath, Map<String, Object> dataModel) throws Exception {
|
|
|
|
|
+ LOGGER.debug("--------generate----generateRootPath--------" + generateRootPath);
|
|
|
|
|
+// // 遍历路径生成代码
|
|
|
|
|
+// for (int i = 0; i < createFileConfig.getTemplateRootDirs().size(); i++) {
|
|
|
|
|
+// generate(generateRootPath, createFileConfig.getTemplateRootDirs().get(i), dataModel, createFileConfig);
|
|
|
|
|
+// }
|
|
|
|
|
+ generate(generateRootPath, createFileConfig.getTemplateRootDir(), dataModel, createFileConfig);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 代码生成
|
|
* 代码生成
|
|
|
*
|
|
*
|
|
|
- * @param projectPath 项目路径
|
|
|
|
|
- * @param templateRootDir 模板根目录
|
|
|
|
|
- * @param config 配置
|
|
|
|
|
|
|
+ * @param generateRootPath 生成文件根路径
|
|
|
|
|
+ * @param templateRootPath 模板根路径
|
|
|
|
|
+ * @param dataModel 数据模型
|
|
|
* @param createFileConfig 创建文件配置
|
|
* @param createFileConfig 创建文件配置
|
|
|
* @throws Exception 异常
|
|
* @throws Exception 异常
|
|
|
*/
|
|
*/
|
|
|
- protected void generate(String projectPath, File templateRootDir, Map<String, Object> config, CreateFileConfig createFileConfig) throws Exception {
|
|
|
|
|
- if (templateRootDir == null) {
|
|
|
|
|
- throw new IllegalStateException("'templateRootDir' must be not null");
|
|
|
|
|
|
|
+ protected void generate(String generateRootPath, File templateRootPath, Map<String, Object> dataModel, CreateFileConfig createFileConfig) throws Exception {
|
|
|
|
|
+ if (templateRootPath == null) {
|
|
|
|
|
+ throw new IllegalStateException("'templateRootPath' must be not null");
|
|
|
}
|
|
}
|
|
|
- LOGGER.info(" load template from templateRootDir = '" + templateRootDir.getAbsolutePath() + "',stylePath ='" + createFileConfig.getStylePath() + "', out GenerateRootDir:" + CodeConfigProperties.projectPath);
|
|
|
|
|
|
|
+ LOGGER.info("load template from below config");
|
|
|
|
|
+ LOGGER.info("templateRootPath = '" + templateRootPath.getAbsolutePath() + "'");
|
|
|
|
|
+ LOGGER.info("stylePath = '" + createFileConfig.getStylePath() + "'");
|
|
|
|
|
+ LOGGER.info("out generateRootPath = '" + generateRootPath + "'");
|
|
|
// 获取要生成的代码模板
|
|
// 获取要生成的代码模板
|
|
|
- List<File> fileList = FileHelper.getTemplateFiles(templateRootDir);
|
|
|
|
|
- LOGGER.debug("----srcFiles----size-----------" + fileList.size());
|
|
|
|
|
- LOGGER.debug("----srcFiles----list------------" + fileList);
|
|
|
|
|
- for (File file : fileList) {
|
|
|
|
|
- generate(projectPath, templateRootDir, config, file, createFileConfig);
|
|
|
|
|
|
|
+ List<File> templateFileList = FileHelper.getTemplateFiles(templateRootPath);
|
|
|
|
|
+ LOGGER.debug("----srcFiles----size-----------" + templateFileList.size());
|
|
|
|
|
+ LOGGER.debug("----srcFiles----list------------" + templateFileList);
|
|
|
|
|
+ for (File templateFile : templateFileList) {
|
|
|
|
|
+ generate(generateRootPath, templateRootPath, dataModel, templateFile, createFileConfig);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 代码生成
|
|
* 代码生成
|
|
|
*
|
|
*
|
|
|
- * @param projectPath 项目路径
|
|
|
|
|
- * @param templateRootDir 模板根目录
|
|
|
|
|
- * @param config 配置
|
|
|
|
|
- * @param srcFile 模板文件
|
|
|
|
|
|
|
+ * @param generateRootPath 生成文件根路径
|
|
|
|
|
+ * @param templateRootPath 模板根路径
|
|
|
|
|
+ * @param dataModel 数据模型
|
|
|
|
|
+ * @param templateFile 模板文件
|
|
|
* @param createFileConfig 创建文件配置
|
|
* @param createFileConfig 创建文件配置
|
|
|
* @throws Exception 异常
|
|
* @throws Exception 异常
|
|
|
*/
|
|
*/
|
|
|
- protected void generate(String projectPath, File templateRootDir, Map<String, Object> config, File srcFile, CreateFileConfig createFileConfig) throws Exception {
|
|
|
|
|
- LOGGER.debug("-------templateRootDir--" + templateRootDir.getPath());
|
|
|
|
|
- LOGGER.debug("-------srcFile--" + srcFile.getPath());
|
|
|
|
|
- String templateFile = FileHelper.getTemplateFilePath(templateRootDir, srcFile);
|
|
|
|
|
|
|
+ protected void generate(String generateRootPath, File templateRootPath, Map<String, Object> dataModel, File templateFile, CreateFileConfig createFileConfig) throws Exception {
|
|
|
|
|
+ LOGGER.debug("-------templateRootPath--" + templateRootPath.getPath());
|
|
|
|
|
+ LOGGER.debug("-------templateFile--" + templateFile.getPath());
|
|
|
|
|
+ // 截取模板文件子路径
|
|
|
|
|
+ String templateFilePath = FileHelper.getTemplateFilePath(templateRootPath, templateFile);
|
|
|
try {
|
|
try {
|
|
|
- LOGGER.debug("-------templateFile--" + templateFile);
|
|
|
|
|
|
|
+ LOGGER.debug("-------templateFilePath--" + templateFilePath);
|
|
|
// 模板风格未设置,或者是以模板风格开头
|
|
// 模板风格未设置,或者是以模板风格开头
|
|
|
- if (createFileConfig.getStylePath() == null || "".equals(createFileConfig.getStylePath()) || templateFile.replace(File.separator, ".").startsWith(createFileConfig.getStylePath())) {
|
|
|
|
|
- String outputFilepath = getOutputFilepath(config, templateFile, createFileConfig);
|
|
|
|
|
- LOGGER.debug("-------outputFilepath--" + outputFilepath);
|
|
|
|
|
- if (outputFilepath.startsWith("java")) {
|
|
|
|
|
- String srcFileName = (projectPath + File.separator + CodeConfigProperties.sourceRootPackage.replace(".", File.separator)) + outputFilepath.substring("java".length());
|
|
|
|
|
- LOGGER.debug("-------java----outputFilepath--" + srcFileName);
|
|
|
|
|
- generate(templateFile, srcFileName, config, createFileConfig);
|
|
|
|
|
|
|
+ if (createFileConfig.getStylePath() == null || "".equals(createFileConfig.getStylePath()) || templateFilePath.replace(File.separator, ".").startsWith(createFileConfig.getStylePath())) {
|
|
|
|
|
+ String outputFilepath = getOutputFilepath(dataModel, templateFilePath, createFileConfig);
|
|
|
|
|
+ LOGGER.debug("-------outputFilePath--" + outputFilepath);
|
|
|
|
|
+ if (outputFilepath.startsWith("java")) { // java 文件模板解析
|
|
|
|
|
+ String targetFilePath = generateRootPath + File.separator + CodeConfigProperties.sourceRootPackage.replace(".", File.separator) + outputFilepath.substring("java".length());
|
|
|
|
|
+ LOGGER.debug("-------java----outputFilePath--" + targetFilePath);
|
|
|
|
|
+ generate(createFileConfig.getTemplateRootDir(), templateFilePath, targetFilePath, dataModel);
|
|
|
} else if (outputFilepath.startsWith("webapp")) {
|
|
} else if (outputFilepath.startsWith("webapp")) {
|
|
|
- String webOutputFilepath = (projectPath + File.separator + CodeConfigProperties.webRootPackage.replace(".", File.separator)) + outputFilepath.substring("webapp".length());
|
|
|
|
|
- LOGGER.debug("-------webapp---outputFilepath---" + webOutputFilepath);
|
|
|
|
|
- generate(templateFile, webOutputFilepath, config, createFileConfig);
|
|
|
|
|
|
|
+ String targetFilePath = (generateRootPath + File.separator + CodeConfigProperties.webRootPackage.replace(".", File.separator)) + outputFilepath.substring("webapp".length());
|
|
|
|
|
+ LOGGER.debug("-------webapp---outputFilePath---" + targetFilePath);
|
|
|
|
|
+ generate(createFileConfig.getTemplateRootDir(), templateFilePath, targetFilePath, dataModel);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -104,53 +109,43 @@ public class BaseCodeGenerate {
|
|
|
/**
|
|
/**
|
|
|
* 代码生成
|
|
* 代码生成
|
|
|
*
|
|
*
|
|
|
- * @param templateRootDir 模板根目录
|
|
|
|
|
- * @param srcFileName 模板文件名
|
|
|
|
|
|
|
+ * @param templateRootDir 模板根路径
|
|
|
|
|
+ * @param templateFilePath 模板文件路径
|
|
|
|
|
+ * @param targetFilePath 生成目标文件路径
|
|
|
* @param config 配置
|
|
* @param config 配置
|
|
|
- * @param createFileConfig 创建文件配置
|
|
|
|
|
* @throws Exception 异常
|
|
* @throws Exception 异常
|
|
|
*/
|
|
*/
|
|
|
- protected void generate(String templateRootDir, String srcFileName, Map<String, Object> config, CreateFileConfig createFileConfig) throws Exception {
|
|
|
|
|
|
|
+ protected void generate(File templateRootDir, String templateFilePath, String targetFilePath, Map<String, Object> config) throws Exception {
|
|
|
// 移除后缀(i)
|
|
// 移除后缀(i)
|
|
|
- if (srcFileName.endsWith("i")) {
|
|
|
|
|
- srcFileName = srcFileName.substring(0, srcFileName.length() - 1);
|
|
|
|
|
|
|
+ if (targetFilePath.endsWith("i")) {
|
|
|
|
|
+ targetFilePath = targetFilePath.substring(0, targetFilePath.length() - 1);
|
|
|
}
|
|
}
|
|
|
// 替换 双下划线(__)为点(.)
|
|
// 替换 双下划线(__)为点(.)
|
|
|
- if (srcFileName.contains(UNDERLINE_2)) {
|
|
|
|
|
- srcFileName = srcFileName.replace(UNDERLINE_2, ".");
|
|
|
|
|
|
|
+ if (targetFilePath.contains(UNDERLINE_2)) {
|
|
|
|
|
+ targetFilePath = targetFilePath.replace(UNDERLINE_2, ".");
|
|
|
}
|
|
}
|
|
|
// 统一路径分隔符
|
|
// 统一路径分隔符
|
|
|
- srcFileName = FileHelper.unifyPath(srcFileName);
|
|
|
|
|
|
|
+ targetFilePath = FileHelper.unifyPath(targetFilePath);
|
|
|
|
|
|
|
|
// 设置模板字符编码
|
|
// 设置模板字符编码
|
|
|
- Template template = getTemplate(templateRootDir, createFileConfig);
|
|
|
|
|
|
|
+// Template template = getTemplate(templateRootDir, createFileConfig);
|
|
|
|
|
+// Template template = FreemarkerHelper.getTemplate(templateRootPathList, templateFilePath);
|
|
|
|
|
+ Template template = FreemarkerHelper.getTemplate(templateRootDir, templateFilePath);
|
|
|
template.setOutputEncoding(StandardCharsets.UTF_8.name());
|
|
template.setOutputEncoding(StandardCharsets.UTF_8.name());
|
|
|
- File srcFile = FileHelper.createFile(srcFileName);
|
|
|
|
|
- LOGGER.info("[generate]\t template:" + templateRootDir + " ==> " + srcFileName);
|
|
|
|
|
|
|
+ File targetFile = FileHelper.createFile(targetFilePath);
|
|
|
|
|
+ LOGGER.info("[generate]\t template:" + templateFilePath + " ==> " + targetFilePath);
|
|
|
// 编译模板生成代码
|
|
// 编译模板生成代码
|
|
|
- FreemarkerHelper.process(template, config, srcFile);
|
|
|
|
|
|
|
+ FreemarkerHelper.process(template, config, targetFile);
|
|
|
// 一多一
|
|
// 一多一
|
|
|
- if (!isOne2Many(srcFile)) {
|
|
|
|
|
- genFileList.add("生成成功:" + FileHelper.unifyPath(srcFileName));
|
|
|
|
|
|
|
+ if (!isOne2Many(targetFile)) {
|
|
|
|
|
+ genFileList.add("生成成功:" + FileHelper.unifyPath(targetFilePath));
|
|
|
}
|
|
}
|
|
|
// 一对多
|
|
// 一对多
|
|
|
- if (isOne2Many(srcFile)) {
|
|
|
|
|
- generate(srcFile, "#segment#");
|
|
|
|
|
|
|
+ if (isOne2Many(targetFile)) {
|
|
|
|
|
+ generate(targetFile, "#segment#");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /**
|
|
|
|
|
- * 获取模板
|
|
|
|
|
- *
|
|
|
|
|
- * @param templateRootDir 模板根目录
|
|
|
|
|
- * @param createFileConfig 创建文件配置
|
|
|
|
|
- * @return 模板
|
|
|
|
|
- * @throws IOException 异常
|
|
|
|
|
- */
|
|
|
|
|
- protected Template getTemplate(String templateRootDir, CreateFileConfig createFileConfig) throws IOException {
|
|
|
|
|
- return FreemarkerHelper.getConfiguration(createFileConfig.getTemplateRootDirs(), templateRootDir).getTemplate(templateRootDir);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 判断是否是一对多
|
|
* 判断是否是一对多
|
|
|
*
|
|
*
|
|
@@ -294,19 +289,19 @@ public class BaseCodeGenerate {
|
|
|
/**
|
|
/**
|
|
|
* 获取生成文件路径
|
|
* 获取生成文件路径
|
|
|
*
|
|
*
|
|
|
- * @param config 模型配置
|
|
|
|
|
|
|
+ * @param dataModel 数据模型
|
|
|
* @param templateFile 模板文件路径
|
|
* @param templateFile 模板文件路径
|
|
|
* @param createFileConfig 文件配置
|
|
* @param createFileConfig 文件配置
|
|
|
* @return 生成文件路径
|
|
* @return 生成文件路径
|
|
|
* @throws Exception 异常
|
|
* @throws Exception 异常
|
|
|
*/
|
|
*/
|
|
|
- protected static String getOutputFilepath(Map<String, Object> config, String templateFile, CreateFileConfig createFileConfig) throws Exception {
|
|
|
|
|
|
|
+ protected static String getOutputFilepath(Map<String, Object> dataModel, String templateFile, CreateFileConfig createFileConfig) throws Exception {
|
|
|
String templatePath = templateFile;
|
|
String templatePath = templateFile;
|
|
|
int indexOf = templateFile.indexOf('@');
|
|
int indexOf = templateFile.indexOf('@');
|
|
|
if (indexOf != -1) {
|
|
if (indexOf != -1) {
|
|
|
templatePath = templateFile.substring(0, indexOf);
|
|
templatePath = templateFile.substring(0, indexOf);
|
|
|
String substring = templateFile.substring(indexOf + 1);
|
|
String substring = templateFile.substring(indexOf + 1);
|
|
|
- Object obj = config.get(substring);
|
|
|
|
|
|
|
+ Object obj = dataModel.get(substring);
|
|
|
if (obj == null) {
|
|
if (obj == null) {
|
|
|
System.err.println("[not-generate] WARN: test expression is null by key:[" + substring + "] on template:[" + templateFile + "]");
|
|
System.err.println("[not-generate] WARN: test expression is null by key:[" + substring + "] on template:[" + templateFile + "]");
|
|
|
return null;
|
|
return null;
|
|
@@ -315,7 +310,7 @@ public class BaseCodeGenerate {
|
|
|
return null;
|
|
return null;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- String srcFilePath = FreemarkerHelper.getTemplateString(templatePath, config, FreemarkerHelper.getConfiguration(createFileConfig.getTemplateRootDirs()));
|
|
|
|
|
|
|
+ String srcFilePath = FreemarkerHelper.getTemplateString(templatePath, dataModel, FreemarkerHelper.getConfiguration(createFileConfig.getTemplateRootDir()));
|
|
|
String stylePath = createFileConfig.getStylePath();
|
|
String stylePath = createFileConfig.getStylePath();
|
|
|
if (!(stylePath == null || "".equals(stylePath))) {
|
|
if (!(stylePath == null || "".equals(stylePath))) {
|
|
|
srcFilePath = srcFilePath.substring(stylePath.length() + 1);
|
|
srcFilePath = srcFilePath.substring(stylePath.length() + 1);
|