From 5e7d5569bf5bc5825511eaeebfcae332a23135de Mon Sep 17 00:00:00 2001 From: lijiahangmax Date: Tue, 26 Sep 2023 02:06:38 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81=E7=94=9F?= =?UTF-8?q?=E6=88=90=E6=A8=A1=E6=9D=BF.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ops/launch/generator/CodeGenerator.java | 26 ++---- .../VelocityTemplateEngine.java | 39 ++++---- .../generator/template/CacheTemplate.java | 70 ++++++++++++++ .../generator/template/EnumsTemplate.java | 16 ++++ .../generator/template/ServerTemplate.java | 93 +++++++++++++++++++ .../GenTable.java => template/Table.java} | 84 ++++++++++++----- .../launch/generator/template/Template.java | 81 ++++++++++++++++ .../generator/template/VueTemplate.java | 66 +++++++++++++ 8 files changed, 416 insertions(+), 59 deletions(-) rename orion-ops-launch/src/main/java/com/orion/ops/launch/generator/{core => engine}/VelocityTemplateEngine.java (93%) create mode 100644 orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/CacheTemplate.java create mode 100644 orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/EnumsTemplate.java create mode 100644 orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/ServerTemplate.java rename orion-ops-launch/src/main/java/com/orion/ops/launch/generator/{core/GenTable.java => template/Table.java} (52%) create mode 100644 orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/Template.java create mode 100644 orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/VueTemplate.java diff --git a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/CodeGenerator.java b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/CodeGenerator.java index 0495ee3f..1b35237d 100644 --- a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/CodeGenerator.java +++ b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/CodeGenerator.java @@ -15,8 +15,8 @@ import com.orion.lang.utils.ansi.style.color.AnsiForeground; import com.orion.lang.utils.ext.yml.YmlExt; import com.orion.ops.framework.mybatis.core.domain.BaseDO; import com.orion.ops.framework.mybatis.core.mapper.IMapper; -import com.orion.ops.launch.generator.core.GenTable; -import com.orion.ops.launch.generator.core.VelocityTemplateEngine; +import com.orion.ops.launch.generator.template.Table; +import com.orion.ops.launch.generator.engine.VelocityTemplateEngine; import org.apache.ibatis.annotations.Mapper; import java.io.File; @@ -40,20 +40,14 @@ public class CodeGenerator { // 作者 String author = Const.ORION_AUTHOR; // 模块 - String module = "asset"; + String module = "infra"; // 生成的表 - GenTable[] tables = { + Table[] tables = { // new GenTable("system_user", "用户", "user") // .vue("user", "user") // .enums(UserStatusEnum.class), - new GenTable("host_key", "主机秘钥", "host") - .vue("asset", "host-key") - .useDrawerForm() - .ignoreTest(), - new GenTable("host_identity", "主机身份", "host") - .vue("asset", "host-identity") - .useDrawerForm() - .ignoreTest(), + new Table("preference", "用户偏好", "preference") + .vue("user", "preference") }; // jdbc 配置 - 使用配置文件 File yamlFile = new File("orion-ops-launch/src/main/resources/application-dev.yaml"); @@ -76,7 +70,7 @@ public class CodeGenerator { String url, String username, String password, - GenTable[] tables, + Table[] tables, String module) { // 创建引擎 VelocityTemplateEngine engine = getEngine(tables); @@ -125,7 +119,7 @@ public class CodeGenerator { * @param tables 表 * @return 渲染引擎 */ - private static VelocityTemplateEngine getEngine(GenTable[] tables) { + private static VelocityTemplateEngine getEngine(Table[] tables) { return new VelocityTemplateEngine(tables); } @@ -184,9 +178,9 @@ public class CodeGenerator { * @param tables 生成的表名 * @return 策略配置 */ - private static StrategyConfig getStrategyConfig(GenTable[] tables) { + private static StrategyConfig getStrategyConfig(Table[] tables) { String[] tableNames = Arrays.stream(tables) - .map(GenTable::getTableName) + .map(Table::getTableName) .toArray(String[]::new); // 策略配置 return new StrategyConfig.Builder() diff --git a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/core/VelocityTemplateEngine.java b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/engine/VelocityTemplateEngine.java similarity index 93% rename from orion-ops-launch/src/main/java/com/orion/ops/launch/generator/core/VelocityTemplateEngine.java rename to orion-ops-launch/src/main/java/com/orion/ops/launch/generator/engine/VelocityTemplateEngine.java index 452e7560..7b519a94 100644 --- a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/core/VelocityTemplateEngine.java +++ b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/engine/VelocityTemplateEngine.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.orion.ops.launch.generator.core; +package com.orion.ops.launch.generator.engine; import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.generator.config.ConstVal; @@ -31,6 +31,7 @@ import com.orion.lang.utils.reflect.BeanMap; import com.orion.lang.utils.reflect.Fields; import com.orion.ops.framework.common.constant.Const; import com.orion.ops.framework.common.constant.OrionOpsProConst; +import com.orion.ops.launch.generator.template.Table; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; @@ -56,13 +57,13 @@ import java.util.stream.Collectors; */ public class VelocityTemplateEngine extends AbstractTemplateEngine { - private final Map tables; + private final Map tables; private VelocityEngine velocityEngine; - public VelocityTemplateEngine(GenTable[] tables) { + public VelocityTemplateEngine(Table[] tables) { this.tables = Arrays.stream(tables) - .collect(Collectors.toMap(GenTable::getTableName, Function.identity())); + .collect(Collectors.toMap(Table::getTableName, Function.identity())); } { @@ -136,30 +137,30 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine { @NotNull TableInfo tableInfo) { // 生成文件副本 List files = originCustomerFile.stream().map(s -> - new CustomFile.Builder() - .enableFileOverride() - .templatePath(s.getTemplatePath()) - .filePath(s.getFilePath()) - .fileName(s.getFileName()) - .packageName(s.getPackageName()) - .build()) + new CustomFile.Builder() + .enableFileOverride() + .templatePath(s.getTemplatePath()) + .filePath(s.getFilePath()) + .fileName(s.getFileName()) + .packageName(s.getPackageName()) + .build()) .collect(Collectors.toList()); // 获取 table - GenTable table = tables.get(tableInfo.getName()); + Table table = tables.get(tableInfo.getName()); // 不生成对外 api 文件 - if (!table.isGenProviderApi()) { + if (!table.isEnableProviderApi()) { files.removeIf(file -> this.isServerProviderFile(file.getTemplatePath())); // 不生成对外 api 单元测试文件 - if (table.isGenUnitTest()) { + if (table.isEnableUnitTest()) { files.removeIf(file -> this.isServerProviderTestFile(file.getTemplatePath())); } } // 不生成单元测试文件 - if (!table.isGenUnitTest()) { + if (!table.isEnableUnitTest()) { files.removeIf(file -> this.isServerUnitTestFile(file.getTemplatePath())); } // 不生成 vue 文件 - if (!table.isGenVue()) { + if (!table.isEnableVue()) { files.removeIf(file -> this.isVueFile(file.getTemplatePath())); } return files; @@ -298,7 +299,7 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine { */ private void generatorVueFile(@NotNull List customFiles, @NotNull TableInfo tableInfo, @NotNull Map objectMap) { // 不生成 vue 文件 - if (!tables.get(tableInfo.getName()).isGenVue()) { + if (!tables.get(tableInfo.getName()).isEnableVue()) { return; } // 过滤文件 @@ -308,7 +309,7 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine { // 元数据 String outPath = getConfigBuilder().getGlobalConfig().getOutputDir(); - GenTable table = tables.get(tableInfo.getName()); + Table table = tables.get(tableInfo.getName()); BeanMap beanMap = BeanMap.create(table, "enums"); // 模块名称实体 beanMap.put("moduleEntity", VariableStyles.SPINE.toBigHump(table.getModule())); @@ -394,7 +395,7 @@ public class VelocityTemplateEngine extends AbstractTemplateEngine { * @param table table * @return enums */ - private Object getEnumMap(GenTable table) { + private Object getEnumMap(Table table) { List>> enums = table.getEnums(); Map> enumMap = new LinkedHashMap<>(); for (Class> e : enums) { diff --git a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/CacheTemplate.java b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/CacheTemplate.java new file mode 100644 index 00000000..629b268e --- /dev/null +++ b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/CacheTemplate.java @@ -0,0 +1,70 @@ +package com.orion.ops.launch.generator.template; + +import java.util.concurrent.TimeUnit; + +/** + * 后端代码缓存模板 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/9/26 1:14 + */ +public class CacheTemplate extends ServerTemplate { + + public CacheTemplate(Table table) { + super(table); + table.enableCache = true; + table.cacheExpireTime = 1; + table.cacheExpireUnit = TimeUnit.HOURS; + } + + /** + * 设置缓存 key + * + * @param key key + * @return this + */ + public CacheTemplate key(String key) { + table.cacheKey = key; + return this; + } + + /** + * 设置缓存过期时间 + * + * @param time time + * @return this + */ + public CacheTemplate expireTime(Integer time) { + table.cacheExpired = true; + table.cacheExpireTime = time; + return this; + } + + /** + * 设置缓存过期时间单位 + * + * @param unit unit + * @return this + */ + public CacheTemplate expireUnit(TimeUnit unit) { + table.cacheExpired = true; + table.cacheExpireUnit = unit; + return this; + } + + /** + * 设置缓存过期时间 + * + * @param time time + * @param unit unit + * @return this + */ + public CacheTemplate expire(Integer time, TimeUnit unit) { + table.cacheExpired = true; + table.cacheExpireTime = time; + table.cacheExpireUnit = unit; + return this; + } + +} diff --git a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/EnumsTemplate.java b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/EnumsTemplate.java new file mode 100644 index 00000000..298070b8 --- /dev/null +++ b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/EnumsTemplate.java @@ -0,0 +1,16 @@ +package com.orion.ops.launch.generator.template; + +/** + * 前端代码枚举模板 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/9/26 1:14 + */ +public class EnumsTemplate extends VueTemplate { + + public EnumsTemplate(Table table) { + super(table); + } + +} diff --git a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/ServerTemplate.java b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/ServerTemplate.java new file mode 100644 index 00000000..ec1a4214 --- /dev/null +++ b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/ServerTemplate.java @@ -0,0 +1,93 @@ +package com.orion.ops.launch.generator.template; + +/** + * 后端代码模板 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/9/26 1:14 + */ +public class ServerTemplate extends Template { + + public ServerTemplate(Table table) { + super(table); + table.enableUnitTest = true; + } + + public ServerTemplate(Table table, String tableName) { + super(table); + table.tableName = tableName; + table.enableUnitTest = true; + } + + public ServerTemplate(Table table, String tableName, String comment, String bizPackage) { + super(table); + table.tableName = tableName; + table.comment = comment; + table.bizPackage = bizPackage; + table.enableUnitTest = true; + } + + /** + * 设置表名称 + * + * @param tableName tableName + * @return this + */ + public ServerTemplate tableName(String tableName) { + table.tableName = tableName; + return this; + } + + /** + * 设置业务注释 + * + * @param comment comment + * @return this + */ + public ServerTemplate comment(String comment) { + table.comment = comment; + return this; + } + + /** + * 设置业务实体包名 + * + * @param bizPackage bizPackage + * @return this + */ + public ServerTemplate bizPackage(String bizPackage) { + table.bizPackage = bizPackage; + return this; + } + + /** + * 是否生成对外 api + * + * @return this + */ + public ServerTemplate enableProviderApi() { + table.enableProviderApi = true; + return this; + } + + /** + * 不生成单元测试 + * + * @return this + */ + public ServerTemplate disableUnitTest() { + table.enableUnitTest = false; + return this; + } + + /** + * 设置 cache + * + * @return cache + */ + public CacheTemplate cache() { + return new CacheTemplate(table); + } + +} diff --git a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/core/GenTable.java b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/Table.java similarity index 52% rename from orion-ops-launch/src/main/java/com/orion/ops/launch/generator/core/GenTable.java rename to orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/Table.java index 50a332d0..1a62b9b8 100644 --- a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/core/GenTable.java +++ b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/Table.java @@ -1,10 +1,12 @@ -package com.orion.ops.launch.generator.core; +package com.orion.ops.launch.generator.template; import com.orion.lang.utils.collect.Lists; import lombok.Data; +import lombok.Getter; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.TimeUnit; /** * @author Jiahang Li @@ -12,70 +14,105 @@ import java.util.List; * @since 2023/7/17 10:44 */ @Data -public class GenTable { +@Getter +public class Table { // -------------------- 后端 -------------------- /** * 表名称 */ - private String tableName; + protected String tableName; /** * 业务注释 */ - private String comment; + protected String comment; /** * 业务实体包名 *

* request dto 包 */ - private String bizPackage; + protected String bizPackage; /** * 是否生成对外 api */ - private boolean genProviderApi; + protected boolean enableProviderApi; /** * 是否生成单元测试 */ - private boolean genUnitTest; + protected boolean enableUnitTest; + + /** + * 是否可缓存 + */ + protected boolean enableCache; + + /** + * 缓存的 key + */ + protected String cacheKey; + + /** + * 缓存是否会过期 + */ + protected boolean cacheExpired; + + /** + * 缓存过期时间 + */ + protected Integer cacheExpireTime; + + /** + * 缓存过期时间单位 + */ + protected TimeUnit cacheExpireUnit; // -------------------- 前端 -------------------- /** * 是否生成 vue 代码 */ - private boolean genVue; + protected boolean enableVue; /** * 模块 用于文件名称生成 */ - private String module; + protected String module; /** * 功能 用于文件名称生成 */ - private String feature; + protected String feature; /** * 使用抽屉表单 */ - private boolean drawerForm; + protected boolean enableDrawerForm; + + /** + * 列表可多选 + */ + protected boolean enableRowSelection; /** * 生成的枚举文件 + * field name [k,v,k,v,k,v] label value color other */ - private List>> enums; + protected List>> enums; - public GenTable(String tableName, String comment, String bizPackage) { + public Table() { + } + + public Table(String tableName, String comment, String bizPackage) { this.tableName = tableName; this.comment = comment; this.bizPackage = bizPackage; - this.genProviderApi = true; - this.genUnitTest = true; + this.enableProviderApi = true; + this.enableUnitTest = true; this.enums = new ArrayList<>(); } @@ -84,8 +121,8 @@ public class GenTable { * * @return this */ - public GenTable ignoreApi() { - this.genProviderApi = false; + public Table ignoreApi() { + this.enableProviderApi = false; return this; } @@ -94,8 +131,8 @@ public class GenTable { * * @return this */ - public GenTable ignoreTest() { - this.genUnitTest = false; + public Table ignoreTest() { + this.enableUnitTest = false; return this; } @@ -106,8 +143,8 @@ public class GenTable { * @param feature feature * @return this */ - public GenTable vue(String module, String feature) { - this.genVue = true; + public Table vue(String module, String feature) { + this.enableVue = true; this.module = module; this.feature = feature; return this; @@ -118,8 +155,7 @@ public class GenTable { * * @return this */ - public GenTable useDrawerForm() { - this.drawerForm = true; + public Table useDrawerForm() { return this; } @@ -130,7 +166,7 @@ public class GenTable { * @return enums */ @SafeVarargs - public final GenTable enums(Class>... enums) { + public final Table enums(Class>... enums) { this.enums.addAll(Lists.of(enums)); return this; } diff --git a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/Template.java b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/Template.java new file mode 100644 index 00000000..40af0418 --- /dev/null +++ b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/Template.java @@ -0,0 +1,81 @@ +package com.orion.ops.launch.generator.template; + +import lombok.Data; + +/** + * 代码生成模板 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/9/26 0:51 + */ +@Data +public class Template { + + protected final Table table; + + protected Template(Table table) { + this.table = table; + } + + /** + * 创建模板 + * + * @param tableName 表名称 + * @return Template + */ + public static ServerTemplate create(String tableName) { + return new ServerTemplate(new Table(), tableName); + } + + /** + * 创建模板 + * + * @param tableName 表名称 + * @param comment 业务注释 + * @param bizPackage 业务包名 + * @return Template + */ + public static ServerTemplate create(String tableName, String comment, String bizPackage) { + return new ServerTemplate(new Table(), tableName, comment, bizPackage); + } + + /** + * 设置 server + * + * @return ServerTemplate + */ + public ServerTemplate server() { + return new ServerTemplate(table); + } + + /** + * 设置 vue + * + * @return vue + */ + public VueTemplate vue() { + return new VueTemplate(table); + } + + /** + * 设置 vue + * + * @param module 模块 + * @param feature 功能 + * @return vue + */ + public VueTemplate vue(String module, String feature) { + return new VueTemplate(table, module, feature); + } + + /** + * 构建 + * + * @return table + */ + public Table build() { + return table; + } + +} diff --git a/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/VueTemplate.java b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/VueTemplate.java new file mode 100644 index 00000000..51a08c64 --- /dev/null +++ b/orion-ops-launch/src/main/java/com/orion/ops/launch/generator/template/VueTemplate.java @@ -0,0 +1,66 @@ +package com.orion.ops.launch.generator.template; + +/** + * 前端代码模板 + * + * @author Jiahang Li + * @version 1.0.0 + * @since 2023/9/26 1:17 + */ +public class VueTemplate extends Template { + + public VueTemplate(Table table) { + super(table); + table.enableVue = true; + } + + public VueTemplate(Table table, String module, String feature) { + super(table); + table.enableVue = true; + table.module = module; + table.feature = feature; + } + + /** + * 设置模块 用于文件名称生成 + * + * @param module module + * @return this + */ + public VueTemplate module(String module) { + table.module = module; + return this; + } + + /** + * 设置功能 用于文件名称生成 + * + * @param feature feature + * @return this + */ + public VueTemplate feature(String feature) { + table.feature = feature; + return this; + } + + /** + * 使用抽屉表单 + * + * @return this + */ + public VueTemplate enableDrawerForm() { + table.enableDrawerForm = true; + return this; + } + + /** + * 列表可多选 + * + * @return this + */ + public VueTemplate enableRowSelection() { + table.enableRowSelection = true; + return this; + } + +}