Introducing new features. 支持生成代码预览 https://gitee.com/log4j/pig/issues/I1WQ3C

This commit is contained in:
一朵梨花压海棠 2020-10-09 14:59:28 +08:00
parent 1586480d74
commit 88aa9d8e78
4 changed files with 99 additions and 12 deletions

View File

@ -54,6 +54,16 @@ public class GeneratorController {
return R.ok(generatorService.getPage(page, tableName, dsName));
}
/**
* 预览代码
* @param genConfig 数据表配置
* @return
*/
@GetMapping("/preview")
public R previewCode(GenConfig genConfig) {
return R.ok(generatorService.previewCode(genConfig));
}
/**
* 生成代码
*/

View File

@ -45,4 +45,11 @@ public interface GeneratorService {
*/
IPage<List<Map<String, Object>>> getPage(Page page, String tableName, String name);
/**
* 预览代码
* @param genConfig 查询条件
* @return
*/
Map<String, String> previewCode(GenConfig genConfig);
}

View File

@ -18,8 +18,10 @@ package com.pig4cloud.pig.codegen.service.impl;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@ -63,6 +65,36 @@ public class GeneratorServiceImpl implements GeneratorService {
return generatorMapper.queryList(page, tableName);
}
/**
* 预览代码
* @param genConfig 查询条件
* @return
*/
@Override
public Map<String, String> previewCode(GenConfig genConfig) {
// 根据tableName 查询最新的表单配置
List<GenFormConf> formConfList = genFormConfMapper.selectList(Wrappers.<GenFormConf>lambdaQuery()
.eq(GenFormConf::getTableName, genConfig.getTableName()).orderByDesc(GenFormConf::getCreateTime));
DynamicDataSourceContextHolder.push(genConfig.getDsName());
String tableNames = genConfig.getTableName();
for (String tableName : StrUtil.split(tableNames, StrUtil.DASHED)) {
// 查询表信息
Map<String, String> table = generatorMapper.queryTable(tableName, genConfig.getDsName());
// 查询列信息
List<Map<String, String>> columns = generatorMapper.queryColumns(tableName, genConfig.getDsName());
// 生成代码
if (CollUtil.isNotEmpty(formConfList)) {
return CodeGenUtils.generatorCode(genConfig, table, columns, null, formConfList.get(0));
}
else {
return CodeGenUtils.generatorCode(genConfig, table, columns, null, null);
}
}
return MapUtil.empty();
}
/**
* 生成代码
* @param genConfig 生成配置

View File

@ -39,6 +39,7 @@ import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.*;
@ -102,8 +103,8 @@ public class CodeGenUtils {
* 生成代码
*/
@SneakyThrows
public void generatorCode(GenConfig genConfig, Map<String, String> table, List<Map<String, String>> columns,
ZipOutputStream zip, GenFormConf formConf) {
public Map<String, String> generatorCode(GenConfig genConfig, Map<String, String> table,
List<Map<String, String>> columns, ZipOutputStream zip, GenFormConf formConf) {
// 配置信息
Configuration config = getConfig();
boolean hasBigDecimal = false;
@ -211,18 +212,48 @@ public class CodeGenUtils {
map.put("package", config.getString("package"));
map.put("mainPath", config.getString("mainPath"));
}
// 渲染数据
return renderData(genConfig, zip, formConf, tableEntity, map);
}
/**
* 渲染数据
* @param genConfig 配置信息
* @param zip 为空直接返回Map
* @param formConf 表单信息
* @param tableEntity 表基本信息
* @param map 模板参数
* @return map key-filename value-contents
* @throws IOException
*/
private Map<String, String> renderData(GenConfig genConfig, ZipOutputStream zip, GenFormConf formConf,
TableEntity tableEntity, Map<String, Object> map) throws IOException {
// 设置velocity资源加载器
Properties prop = new Properties();
prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
Velocity.init(prop);
VelocityContext context = new VelocityContext(map);
// 获取模板列表
List<String> templates = getTemplates();
Map<String, String> resultMap = new HashMap<>(8);
for (String template : templates) {
// 如果是crud
if (template.contains(AVUE_CRUD_JS_VM) && formConf != null) {
zip.putNextEntry(
new ZipEntry(Objects.requireNonNull(getFileName(template, tableEntity.getCaseClassName(),
map.get("package").toString(), map.get("moduleName").toString()))));
IoUtil.write(zip, StandardCharsets.UTF_8, false, CRUD_PREFIX + formConf.getFormInfo());
zip.closeEntry();
String fileName = getFileName(template, tableEntity.getCaseClassName(), map.get("package").toString(),
map.get("moduleName").toString());
String contents = CRUD_PREFIX + formConf.getFormInfo();
if (zip != null) {
zip.putNextEntry(new ZipEntry(Objects.requireNonNull(fileName)));
IoUtil.write(zip, StandardCharsets.UTF_8, false, contents);
zip.closeEntry();
}
resultMap.put(template, contents);
continue;
}
@ -232,12 +263,19 @@ public class CodeGenUtils {
tpl.merge(context, sw);
// 添加到zip
zip.putNextEntry(new ZipEntry(Objects.requireNonNull(getFileName(template, tableEntity.getCaseClassName(),
map.get("package").toString(), map.get("moduleName").toString()))));
IoUtil.write(zip, StandardCharsets.UTF_8, false, sw.toString());
IoUtil.close(sw);
zip.closeEntry();
String fileName = getFileName(template, tableEntity.getCaseClassName(), map.get("package").toString(),
map.get("moduleName").toString());
if (zip != null) {
zip.putNextEntry(new ZipEntry(Objects.requireNonNull(fileName)));
IoUtil.write(zip, StandardCharsets.UTF_8, false, sw.toString());
IoUtil.close(sw);
zip.closeEntry();
}
resultMap.put(template, sw.toString());
}
return resultMap;
}
/**