mirror of https://gitee.com/makejava/EasyCode.git
完成子组件代码生成功能
This commit is contained in:
parent
28096072d9
commit
b1501d64f9
|
@ -38,6 +38,7 @@ public class EasyCodeGlobalVariableProvider extends VtlGlobalVariableProvider {
|
|||
result.add(new VtlLightVariable("time", file, "com.sjhy.plugin.tool.TimeUtils"));
|
||||
result.add(new VtlLightVariable("tableInfo", file, "com.sjhy.plugin.entity.TableInfo"));
|
||||
result.add(new VtlLightVariable("tableInfoList", file, "java.util.List<com.sjhy.plugin.entity.TableInfo>"));
|
||||
result.add(new VtlLightVariable("generateService", file, "com.sjhy.plugin.tool.ExtraCodeGenerateUtils"));
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -102,22 +102,41 @@ public class CodeGenerateServiceImpl implements CodeGenerateService {
|
|||
* @param title 是否显示提示
|
||||
*/
|
||||
private void generate(Collection<Template> templates, Collection<TableInfo> tableInfoList, boolean title) {
|
||||
generate(templates, tableInfoList, title, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成代码,并自动保存到对应位置
|
||||
*
|
||||
* @param templates 模板
|
||||
* @param tableInfoList 表信息对象
|
||||
* @param title 是否显示提示
|
||||
* @param otherParam 其他参数
|
||||
*/
|
||||
public void generate(Collection<Template> templates, Collection<TableInfo> tableInfoList, boolean title, Map<String, Object> otherParam) {
|
||||
if (CollectionUtil.isEmpty(templates) || CollectionUtil.isEmpty(tableInfoList)) {
|
||||
return;
|
||||
}
|
||||
// 处理模板,注入全局变量(克隆一份,防止篡改)
|
||||
templates = CloneUtils.cloneByJson(templates, new TypeReference<ArrayList<Template>>() {});
|
||||
templates = CloneUtils.cloneByJson(templates, new TypeReference<ArrayList<Template>>() {
|
||||
});
|
||||
TemplateUtils.addGlobalConfig(templates);
|
||||
// 生成代码
|
||||
for (TableInfo tableInfo : tableInfoList) {
|
||||
// 构建参数
|
||||
Map<String, Object> param = getDefaultParam();
|
||||
// 其他参数
|
||||
if (otherParam != null) {
|
||||
param.putAll(otherParam);
|
||||
}
|
||||
// 所有表信息对象
|
||||
param.put("tableInfoList", tableInfoList);
|
||||
// 表信息对象
|
||||
param.put("tableInfo", tableInfo);
|
||||
// 设置模型路径与导包列表
|
||||
setModulePathAndImportList(param, tableInfo);
|
||||
// 设置额外代码生成服务
|
||||
param.put("generateService", new ExtraCodeGenerateUtils(this, tableInfo, title));
|
||||
|
||||
for (Template template : templates) {
|
||||
Callback callback = new Callback();
|
||||
|
|
|
@ -0,0 +1,65 @@
|
|||
package com.sjhy.plugin.tool;
|
||||
|
||||
import com.sjhy.plugin.config.Settings;
|
||||
import com.sjhy.plugin.entity.TableInfo;
|
||||
import com.sjhy.plugin.entity.Template;
|
||||
import com.sjhy.plugin.service.impl.CodeGenerateServiceImpl;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 额外的代码生成工具
|
||||
*
|
||||
* @author makejava
|
||||
* @version 1.0.0
|
||||
* @since 2018/11/01 10:11
|
||||
*/
|
||||
public class ExtraCodeGenerateUtils {
|
||||
/**
|
||||
* 代码生成服务
|
||||
*/
|
||||
private CodeGenerateServiceImpl codeGenerateService;
|
||||
/**
|
||||
* 表信息对象
|
||||
*/
|
||||
private TableInfo tableInfo;
|
||||
/**
|
||||
* 文件覆盖提示
|
||||
*/
|
||||
private Boolean title;
|
||||
|
||||
/**
|
||||
* 设置实例对象
|
||||
*/
|
||||
private Settings settings = Settings.getInstance();
|
||||
|
||||
|
||||
public ExtraCodeGenerateUtils(CodeGenerateServiceImpl codeGenerateService, TableInfo tableInfo, Boolean title) {
|
||||
this.codeGenerateService = codeGenerateService;
|
||||
this.tableInfo = tableInfo;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成代码
|
||||
*
|
||||
* @param templateName 模板名称
|
||||
* @param param 附加参数
|
||||
*/
|
||||
public void run(String templateName, Map<String, Object> param) {
|
||||
// 获取到模板
|
||||
Template currTemplate = null;
|
||||
for (Template template : settings.getTemplateGroupMap().get(settings.getCurrTemplateGroupName()).getElementList()) {
|
||||
if (Objects.equals(template.getName(), templateName)) {
|
||||
currTemplate = template;
|
||||
}
|
||||
}
|
||||
if (currTemplate == null) {
|
||||
return;
|
||||
}
|
||||
// 生成代码
|
||||
codeGenerateService.generate(Collections.singletonList(currTemplate), Collections.singletonList(this.tableInfo), this.title, param);
|
||||
}
|
||||
}
|
|
@ -8,3 +8,4 @@
|
|||
#* @vtlvariable name="time" type="com.sjhy.plugin.tool.TimeUtils" *#
|
||||
#* @vtlvariable name="tableInfo" type="com.sjhy.plugin.entity.TableInfo" *#
|
||||
#* @vtlvariable name="tableInfoList" type="java.util.List<com.sjhy.plugin.entity.TableInfo>" *#
|
||||
#* @vtlvariable name="generateService" type="com.sjhy.plugin.tool.ExtraCodeGenerateUtils" *#
|
||||
|
|
|
@ -59,6 +59,8 @@
|
|||
toJson(Object, Boolean) 将对象转json对象,Boolean:是否格式化json,不填时为不格式化。
|
||||
$time
|
||||
currTime(String format) 获取当前时间,指定时间格式(默认:yyyy-MM-dd HH:mm:ss)
|
||||
$generateService
|
||||
run(String, Map<String,Object>) 代码生成服务,参数1:模板名称,参数2:附加参数。
|
||||
</pre>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue