From 0527f0a3edff6925c731d59870478ea6a7bf5818 Mon Sep 17 00:00:00 2001 From: HawickMason <1914991129@qq.com> Date: Thu, 15 Aug 2024 16:58:13 +0800 Subject: [PATCH] feat(codegen): support feature and test and table generation based on template or generator - add param parse (#866) * feat(codegen): support feature and test and table generation based on template or generator * feat(codegen): support feature and test and table generation based on template or generator - add param parse * feat(codegen): support feature and test and table generation based on template or generator - add param parse - 1 * feat(codegen): support feature and test and table generation based on template or generator - add param parse - 2 * feat(codegen): support feature and test and table generation based on template or generator - add param parse - 3] --------- Co-authored-by: wangyingjie3 Co-authored-by: wodiwudi <40229449+wodiwudi@users.noreply.github.com> --- .../run/mone/ai/codegen/FeatureGenerator.java | 134 +++++++++++++++--- .../mone/ai/codegen/bo/FeatureGenerateBo.java | 8 +- .../ai/codegen/bo/FeatureGenerateType.java | 42 ++++++ 3 files changed, 165 insertions(+), 19 deletions(-) create mode 100644 jcommon/codegen/src/main/java/run/mone/ai/codegen/bo/FeatureGenerateType.java diff --git a/jcommon/codegen/src/main/java/run/mone/ai/codegen/FeatureGenerator.java b/jcommon/codegen/src/main/java/run/mone/ai/codegen/FeatureGenerator.java index 2b1468021..5ddc8b3df 100644 --- a/jcommon/codegen/src/main/java/run/mone/ai/codegen/FeatureGenerator.java +++ b/jcommon/codegen/src/main/java/run/mone/ai/codegen/FeatureGenerator.java @@ -1,12 +1,18 @@ package run.mone.ai.codegen; import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; import com.mybatisflex.codegen.Generator; import com.mybatisflex.codegen.config.GlobalConfig; import com.zaxxer.hikari.HikariDataSource; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.jdbc.core.JdbcTemplate; +import run.mone.ai.codegen.bo.FeatureGenerateType; +import run.mone.ai.codegen.bo.FeatureGenerateBo; +import run.mone.ai.codegen.util.TemplateUtils; + +import java.lang.reflect.Type; import run.mone.ai.codegen.bo.FeatureGeneratType; import run.mone.ai.codegen.bo.FeatureGenerateBo; import run.mone.ai.codegen.util.TemplateUtils; @@ -23,27 +29,113 @@ @Slf4j public class FeatureGenerator { - private static final Gson gson = new Gson(); - public static void main(String[] args) { - FeatureGenerateBo featureGenerateBo = null; - //方便ai调用的时候,设置表名 + Map map; if (args.length > 0) { - featureGenerateBo = parseArgsAndExtractData(args); - generateWithTemplate(featureGenerateBo); + map = parseArgsAndExtractData(args); + String type = getGenType(map); + if (type == null) { + return; + } + FeatureGenerateType generateType = FeatureGenerateType.getGenerateTypeByCode(Integer.parseInt(type)); + FeatureGenerateBo featureGenerateBo = mapToGenBo(map); + switch (generateType) { + case CODE_WITH_GENERATOR: + case TABLE: + generateWithGenerator(featureGenerateBo); + break; + case CODE_WITH_TEMPLATE: + generateByTemplate(featureGenerateBo); + break; + default: + log.info("did not match any gen type, will do nothing..."); + } } } - public static void generateWithTemplate(FeatureGenerateBo featureGenerateBo) { + private static String getGenType(Map map) { + String type = map.get("type"); + if (StringUtils.isBlank(type)) { + log.error("generate type is not specified!"); + return null; + } + if (!StringUtils.isNumeric(type)) { + log.error("generate type is not valid, must be number! See @FeatureGenerateType.code"); + return null; + } + return type; + } + + private static FeatureGenerateBo mapToGenBo(Map map) { + FeatureGenerateBo.FeatureGenerateBoBuilder builder = FeatureGenerateBo.builder(); + if (map.containsKey("tableName")) { + builder.tableName(map.get("tableName")); + } + if (map.containsKey("testName")) { + builder.sql(map.get("testName")); + } + if (map.containsKey("sql")) { + builder.sql(map.get("sql")); + } + if (map.containsKey("jdbcUrl")) { + builder.jdbcUrl(map.get("jdbcUrl")); + } + if (map.containsKey("userName")) { + builder.userName(map.get("userName")); + } + if (map.containsKey("password")) { + builder.password(map.get("password")); + } + if (map.containsKey("basePackage")) { + builder.basePackage(map.get("basePackage")); + } + if (map.containsKey("className")) { + builder.className(map.get("className")); + } + if (map.containsKey("auth")) { + builder.auth(map.get("auth")); + } + if (map.containsKey("basePath")) { + builder.basePath(map.get("basePath")); + } + if (map.containsKey("serverModulePath")) { + builder.serverModulePath(map.get("serverModulePath")); + } + if (map.containsKey("serviceModulePath")) { + builder.serviceModulePath(map.get("serviceModulePath")); + } + if (map.containsKey("apiModulePath")) { + builder.apiModulePath(map.get("apiModulePath")); + } + if (map.containsKey("createPojo")) { + builder.createPojo(Boolean.parseBoolean(map.get("createPojo"))); + } + if (map.containsKey("createVo")) { + builder.createVo(Boolean.parseBoolean(map.get("createVo"))); + } + if (map.containsKey("createTransfer")) { + builder.createTransfer(Boolean.parseBoolean(map.get("createTransfer"))); + } + if (map.containsKey("createTest")) { + builder.createTest(Boolean.parseBoolean(map.get("createTest"))); + } + if (map.containsKey("createController")) { + builder.createController(Boolean.parseBoolean(map.get("createController"))); + } + return builder.build(); + } + + public static void generateWithGenerator(FeatureGenerateBo featureGenerateBo) { // 类型检查 if (Objects.isNull(featureGenerateBo.getType())) { log.warn("Empty generation type, will do noting!"); return; } - FeatureGeneratType featureGenType = featureGenerateBo.getType(); - if (FeatureGeneratType.CODE_WITH_TEMPLATE != featureGenType - && FeatureGeneratType.TABLE != featureGenType) { + + FeatureGenerateType featureGenType = featureGenerateBo.getType(); + if (FeatureGenerateType.CODE_WITH_TEMPLATE != featureGenType + && FeatureGenerateType.TABLE != featureGenType) { log.warn("generate type:{} is not match with current call", featureGenType); return; } @@ -56,7 +148,8 @@ public static void generateWithTemplate(FeatureGenerateBo featureGenerateBo) { dataSource.setPassword(featureGenerateBo.getPassword()); //创建mapper相关代码 - if (FeatureGeneratType.CODE_WITH_TEMPLATE == featureGenType) { + + if (FeatureGenerateType.CODE_WITH_TEMPLATE == featureGenType) { GlobalConfig globalConfig = createGlobalConfigUseStyle(featureGenerateBo); Generator generator = new Generator(dataSource, globalConfig); generator.generate(); @@ -64,7 +157,8 @@ public static void generateWithTemplate(FeatureGenerateBo featureGenerateBo) { } //创建table - if (FeatureGeneratType.TABLE == featureGenType) { + + if (FeatureGenerateType.TABLE == featureGenType) { JdbcTemplate jt = new JdbcTemplate(dataSource); jt.update(featureGenerateBo.getSql()); } @@ -138,7 +232,7 @@ public static void generateByTemplate(FeatureGenerateBo featureGenerateBo) { } if (featureGenerateBo.isCreateTest()) { - String cn = className + "ServiceTest.java"; + String cn = featureGenerateBo.getTestName(); String test = TemplateUtils.renderTemplateFromFile("tlp/test.java", data); TemplateUtils.writeStringToFile(test, basePath + "/" + featureGenerateBo.getServerModulePath() + "/src/test/java/run/mone/test/service/" + cn); } @@ -161,14 +255,20 @@ public static String replaceDotWithSlash(String input) { return input.replace(".", "/"); } - private static FeatureGenerateBo parseArgsAndExtractData(String[] args) { + private static Map parseArgsAndExtractData(String[] args) { + if (StringUtils.isBlank(args[0])) { + log.warn("no valid input params, will do nothing!"); + } String jsonStr = args[0]; + Type typeOfT = new TypeToken>() { + }.getType(); + jsonStr = new String(Base64.getDecoder().decode(jsonStr)); log.info("jsonStr:{}", jsonStr); - FeatureGenerateBo bo = gson.fromJson(jsonStr, FeatureGenerateBo.class); - log.info("map:{}", bo); - return bo; + Map map = new Gson().fromJson(jsonStr, typeOfT); + log.info("map:{}", map); + return map; } } diff --git a/jcommon/codegen/src/main/java/run/mone/ai/codegen/bo/FeatureGenerateBo.java b/jcommon/codegen/src/main/java/run/mone/ai/codegen/bo/FeatureGenerateBo.java index 248c6c3fb..b1f413fae 100644 --- a/jcommon/codegen/src/main/java/run/mone/ai/codegen/bo/FeatureGenerateBo.java +++ b/jcommon/codegen/src/main/java/run/mone/ai/codegen/bo/FeatureGenerateBo.java @@ -4,8 +4,10 @@ import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; + import lombok.ToString; + /** * @author goodjava@qq.com, HawickMason@xiaomi.com * @date 7/12/24 14:10 @@ -14,11 +16,10 @@ @AllArgsConstructor @Data @Builder -@ToString public class FeatureGenerateBo { @Builder.Default - private FeatureGeneratType type = FeatureGeneratType.CODE_WITH_GENERATOR; + private FeatureGenerateType type = FeatureGenerateType.CODE_WITH_GENERATOR; @Builder.Default private String tableName = ""; @@ -42,6 +43,8 @@ public class FeatureGenerateBo { @Builder.Default private String className = "Dummy"; + private String testName = "T"; + @Builder.Default private String auth = ""; @@ -75,6 +78,7 @@ public class FeatureGenerateBo { @Builder.Default private boolean createController = false; + /** * 目前使用module所在的绝对路径 */ diff --git a/jcommon/codegen/src/main/java/run/mone/ai/codegen/bo/FeatureGenerateType.java b/jcommon/codegen/src/main/java/run/mone/ai/codegen/bo/FeatureGenerateType.java new file mode 100644 index 000000000..136328130 --- /dev/null +++ b/jcommon/codegen/src/main/java/run/mone/ai/codegen/bo/FeatureGenerateType.java @@ -0,0 +1,42 @@ +package run.mone.ai.codegen.bo; + +import java.util.Arrays; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** + * @author goodjava@qq.com, HawickMason@xiaomi.com + * @date 7/12/24 14:15 + */ +public enum FeatureGenerateType { + + CODE_WITH_GENERATOR(1, "使用mybatis-flex-generator生成"), + + CODE_WITH_TEMPLATE(2, "使用预制模板生成"), + + TABLE(3, "创建表"); + + private final int code; + + private final String desc; + + private static final Map valMap = Arrays.stream(values()).collect(Collectors.toMap(FeatureGenerateType::getCode, Function.identity())); + + FeatureGenerateType(int code, String desc) { + this.code = code; + this.desc = desc; + } + + public int getCode() { + return code; + } + + public String getDesc() { + return desc; + } + + public static FeatureGenerateType getGenerateTypeByCode(int code) { + return valMap.getOrDefault(code, CODE_WITH_TEMPLATE); + } +}