diff --git a/src/main/java/com/sjhy/plugin/comm/AbstractService.java b/src/main/java/com/sjhy/plugin/comm/AbstractService.java index 9624f14..37e71de 100644 --- a/src/main/java/com/sjhy/plugin/comm/AbstractService.java +++ b/src/main/java/com/sjhy/plugin/comm/AbstractService.java @@ -1,7 +1,7 @@ package com.sjhy.plugin.comm; import com.sjhy.plugin.entity.TypeMapperGroup; -import com.sjhy.plugin.tool.ConfigInfo; +import com.sjhy.plugin.config.Settings; /** * 抽象的服务 @@ -14,7 +14,7 @@ public abstract class AbstractService { /** * 配置信息对象 */ - protected ConfigInfo configInfo = ConfigInfo.getInstance(); + protected Settings settings = Settings.getInstance(); /** * 获取当前的类型映射Mapper @@ -22,7 +22,7 @@ public abstract class AbstractService { * @return 类型映射Mapper */ protected TypeMapperGroup getCurrMapper() { - return configInfo.getTypeMapperGroupMap().get(configInfo.getCurrTypeMapperGroupName()); + return settings.getTypeMapperGroupMap().get(settings.getCurrTypeMapperGroupName()); } /** @@ -31,6 +31,6 @@ public abstract class AbstractService { * @param typeMapper 类型映射Mapper */ protected void setCurrMapper(TypeMapperGroup typeMapper) { - configInfo.getTypeMapperGroupMap().put(configInfo.getCurrTypeMapperGroupName(), typeMapper); + settings.getTypeMapperGroupMap().put(settings.getCurrTypeMapperGroupName(), typeMapper); } } diff --git a/src/main/java/com/sjhy/plugin/config/Settings.java b/src/main/java/com/sjhy/plugin/config/Settings.java index 94ab8de..1ab226f 100644 --- a/src/main/java/com/sjhy/plugin/config/Settings.java +++ b/src/main/java/com/sjhy/plugin/config/Settings.java @@ -1,57 +1,260 @@ package com.sjhy.plugin.config; +import com.intellij.ide.fileTemplates.impl.UrlUtil; import com.intellij.openapi.components.PersistentStateComponent; import com.intellij.openapi.components.ServiceManager; import com.intellij.openapi.components.State; import com.intellij.openapi.components.Storage; +import com.intellij.util.ExceptionUtil; import com.intellij.util.xmlb.XmlSerializerUtil; +import com.intellij.util.xmlb.annotations.Transient; +import com.sjhy.plugin.entity.*; import lombok.Data; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.io.IOException; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + /** - * 全局设置 + * 全局配置信息 * * @author makejava * @version 1.0.0 - * @since 2018/08/09 15:00 + * @since 2018/07/18 09:33 */ @Data @State(name = "EasyCodeSetting", storages = @Storage("easy-code-setting.xml")) public class Settings implements PersistentStateComponent { /** - * 获取实例对象 + * 默认名称 + */ + @Transient + public static final String DEFAULT_NAME = "Default"; + + /** + * 版本号 + */ + private String version; + /** + * 当前类型映射组名 + */ + private String currTypeMapperGroupName; + /** + * 类型映射组 + */ + private Map typeMapperGroupMap; + /** + * 当前模板组名 + */ + private String currTemplateGroupName; + /** + * 模板组 + */ + private Map templateGroupMap; + /** + * 当前配置表组名 + */ + private String currColumnConfigGroupName; + /** + * 配置表组 + */ + private Map columnConfigGroupMap; + /** + * 当前全局配置组名 + */ + private String currGlobalConfigGroupName; + /** + * 全局配置组 + */ + private Map globalConfigGroupMap; + /** + * 默认编码 + */ + private String encode; + /** + * 作者 + */ + private String author; + + /** + * 获取单例实例对象 + * + * @return 实例对象 */ public static Settings getInstance() { return ServiceManager.getService(Settings.class); } /** - * 获取持久化状态信息 - * - * @return 持久化状态信息 + * 默认构造方法 */ + @SuppressWarnings("unused") + public Settings() { + initDefault(); + } + + /** + * 初始化默认设置 + */ + public void initDefault() { + // 版本号 + this.version = "1.1.0"; + // 默认编码 + this.encode = "UTF-8"; + // 作者名称 + this.author = "makejava"; + // 当前各项分组名称 + this.currTemplateGroupName = DEFAULT_NAME; + this.currTypeMapperGroupName = DEFAULT_NAME; + this.currColumnConfigGroupName = DEFAULT_NAME; + this.currGlobalConfigGroupName = DEFAULT_NAME; + //配置默认模板 + if (this.templateGroupMap == null) { + this.templateGroupMap = new LinkedHashMap<>(); + } + this.templateGroupMap.put(DEFAULT_NAME, loadTemplateGroup(DEFAULT_NAME, "entity", "dao", "service", "serviceImpl", "controller")); + this.templateGroupMap.put("MybatisPlus", loadTemplateGroup("MybatisPlus", "entity", "dao", "service", "serviceImpl", "controller")); + + //配置默认类型映射 + if (this.typeMapperGroupMap == null) { + this.typeMapperGroupMap = new LinkedHashMap<>(); + } + TypeMapperGroup typeMapperGroup = new TypeMapperGroup(); + List typeMapperList = new ArrayList<>(); + typeMapperList.add(new TypeMapper("varchar(\\(\\d+\\))?", "java.lang.String")); + typeMapperList.add(new TypeMapper("text", "java.lang.String")); + typeMapperList.add(new TypeMapper("decimal(\\(\\d+\\))?", "java.lang.Double")); + typeMapperList.add(new TypeMapper("integer", "java.lang.Integer")); + typeMapperList.add(new TypeMapper("int(\\(\\d+\\))?", "java.lang.Integer")); + typeMapperList.add(new TypeMapper("int4", "java.lang.Integer")); + typeMapperList.add(new TypeMapper("int8", "java.lang.Long")); + typeMapperList.add(new TypeMapper("bigint(\\(\\d+\\))?", "java.lang.Long")); + typeMapperList.add(new TypeMapper("datetime", "java.util.Date")); + typeMapperList.add(new TypeMapper("timestamp", "java.util.Date")); + typeMapperList.add(new TypeMapper("boolean", "java.lang.Boolean")); + typeMapperGroup.setName(DEFAULT_NAME); + typeMapperGroup.setElementList(typeMapperList); + typeMapperGroupMap.put(DEFAULT_NAME, typeMapperGroup); + + //初始化表配置 + if (this.columnConfigGroupMap == null) { + this.columnConfigGroupMap = new LinkedHashMap<>(); + } + ColumnConfigGroup columnConfigGroup = new ColumnConfigGroup(); + List columnConfigList = new ArrayList<>(); + columnConfigList.add(new ColumnConfig("disable", ColumnConfigType.BOOLEAN)); + columnConfigGroup.setName(DEFAULT_NAME); + columnConfigGroup.setElementList(columnConfigList); + columnConfigGroupMap.put(DEFAULT_NAME, columnConfigGroup); + + //初始化全局配置 + if (this.globalConfigGroupMap == null) { + this.globalConfigGroupMap = new LinkedHashMap<>(); + } + this.globalConfigGroupMap.put(DEFAULT_NAME, loadGlobalConfigGroup(DEFAULT_NAME, "init", "define", "autoImport")); + } + + /** + * 加载模板文件 + * + * @param filePath 模板路径 + * @return 模板文件内容 + */ + private static String loadTemplate(String filePath) { + try { + return UrlUtil.loadText(Settings.class.getResource(filePath)).replace("\r", ""); + } catch (IOException e) { + ExceptionUtil.rethrow(e); + } + return ""; + } + + /** + * 加载模板组 + * + * @param groupName 组名 + * @param templateNames 模板名称 + * @return 模板组 + */ + private static TemplateGroup loadTemplateGroup(String groupName, String... templateNames) { + TemplateGroup templateGroup = new TemplateGroup(); + templateGroup.setName(groupName); + templateGroup.setElementList(new ArrayList<>()); + for (String templateName : templateNames) { + String path = "/template/" + groupName + "/" + templateName + ".vm"; + templateGroup.getElementList().add(new Template(templateName, loadTemplate(path))); + } + return templateGroup; + } + + /** + * 加载全局配置组 + * + * @param groupName 组名 + * @param templateNames 模板名称 + * @return 模板组 + */ + private static GlobalConfigGroup loadGlobalConfigGroup(String groupName, String... templateNames) { + GlobalConfigGroup globalConfigGroup = new GlobalConfigGroup(); + globalConfigGroup.setName(groupName); + globalConfigGroup.setElementList(new ArrayList<>()); + for (String templateName : templateNames) { + String path = "/globalConfig/" + groupName + "/" + templateName + ".vm"; + globalConfigGroup.getElementList().add(new GlobalConfig(templateName, loadTemplate(path))); + } + return globalConfigGroup; + } + @Nullable @Override public Settings getState() { return this; } - /** - * 读取系统中保存的持久化状态信息 - * - * @param state 读取到的持久化状态信息 - */ @Override - public void loadState(@NotNull Settings state) { - XmlSerializerUtil.copyBean(state, this); - } + public void loadState(@NotNull Settings settings) { + // 备份初始配置 + Map templateGroupMap = this.getTemplateGroupMap(); + Map globalConfigGroupMap = this.getGlobalConfigGroupMap(); + String version = this.getVersion(); + // 覆盖初始配置 + XmlSerializerUtil.copyBean(settings, this); - /** - * 未读取到持久化状态信息前进行初始化操作 - */ - @Override - public void noStateLoaded() { + // 已经合并不再重复合并 + if (settings.getVersion() != null && settings.getVersion().equals(version)) { + return; + } + // 模板备份 + TemplateGroup oldTemplateGroup = settings.getTemplateGroupMap().get(DEFAULT_NAME); + String newName = oldTemplateGroup.getName() + "Bak"; + int i = 0; + while (settings.getTemplateGroupMap().containsKey(newName)) { + newName = newName + i++; + } + oldTemplateGroup.setName(newName); + // 保存 + settings.getTemplateGroupMap().put(newName, oldTemplateGroup); + // 覆盖 + settings.getTemplateGroupMap().replace(DEFAULT_NAME, templateGroupMap.get(DEFAULT_NAME)); + + + + // 全局配置备份 + GlobalConfigGroup oldGlobalConfigGroup = settings.getGlobalConfigGroupMap().get(DEFAULT_NAME); + newName = oldGlobalConfigGroup.getName() + "Bak"; + i = 0; + while (settings.getGlobalConfigGroupMap().containsKey(newName)) { + newName = newName + i++; + } + oldGlobalConfigGroup.setName(newName); + // 保存 + settings.getGlobalConfigGroupMap().put(newName, oldGlobalConfigGroup); + // 覆盖 + settings.getGlobalConfigGroupMap().replace(DEFAULT_NAME, globalConfigGroupMap.get(DEFAULT_NAME)); } } diff --git a/src/main/java/com/sjhy/plugin/tool/ConfigInfo.java b/src/main/java/com/sjhy/plugin/tool/ConfigInfo.java deleted file mode 100644 index 10e11f6..0000000 --- a/src/main/java/com/sjhy/plugin/tool/ConfigInfo.java +++ /dev/null @@ -1,231 +0,0 @@ -package com.sjhy.plugin.tool; - -import com.intellij.openapi.components.PersistentStateComponent; -import com.intellij.openapi.components.ServiceManager; -import com.intellij.openapi.components.State; -import com.intellij.openapi.components.Storage; -import com.intellij.util.xmlb.XmlSerializerUtil; -import com.intellij.util.xmlb.annotations.Transient; -import com.sjhy.plugin.entity.*; -import lombok.Data; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -/** - * 全局配置信息 - * - * @author makejava - * @version 1.0.0 - * @since 2018/07/18 09:33 - */ -@Data -@State(name = "EasyCodeSetting", storages = @Storage("easy-code-setting.xml")) -public class ConfigInfo implements PersistentStateComponent { - /** - * 默认名称 - */ - @Transient - public static final String DEFAULT_NAME = "Default"; - - /** - * 版本号 - */ - private String version; - /** - * 当前类型映射组名 - */ - private String currTypeMapperGroupName; - /** - * 类型映射组 - */ - private Map typeMapperGroupMap; - /** - * 当前模板组名 - */ - private String currTemplateGroupName; - /** - * 模板组 - */ - private Map templateGroupMap; - /** - * 当前配置表组名 - */ - private String currColumnConfigGroupName; - /** - * 配置表组 - */ - private Map columnConfigGroupMap; - /** - * 当前全局配置组名 - */ - private String currGlobalConfigGroupName; - /** - * 全局配置组 - */ - private Map globalConfigGroupMap; - /** - * 默认编码 - */ - private String encode; - /** - * 作者 - */ - private String author; - - /** - * 获取单例实例对象 - * - * @return 实例对象 - */ - public static ConfigInfo getInstance() { - return ServiceManager.getService(ConfigInfo.class); - } - - /** - * 默认构造方法 - */ - @SuppressWarnings("unused") - public ConfigInfo() { - initDefault(); - } - - /** - * 初始化默认设置 - */ - public void initDefault() { - // 版本号 - this.version = "1.1.0"; - // 默认编码 - this.encode = "UTF-8"; - // 作者名称 - this.author = "makejava"; - // 当前各项分组名称 - this.currTemplateGroupName = DEFAULT_NAME; - this.currTypeMapperGroupName = DEFAULT_NAME; - this.currColumnConfigGroupName = DEFAULT_NAME; - this.currGlobalConfigGroupName = DEFAULT_NAME; - //配置默认模板 - if (this.templateGroupMap == null) { - this.templateGroupMap = new LinkedHashMap<>(); - } - this.templateGroupMap.put(DEFAULT_NAME, loadTemplateGroup(DEFAULT_NAME, "entity", "dao", "service", "serviceImpl", "controller")); - this.templateGroupMap.put("MybatisPlus", loadTemplateGroup("MybatisPlus", "entity", "dao", "service", "serviceImpl", "controller")); - - //配置默认类型映射 - if (this.typeMapperGroupMap == null) { - this.typeMapperGroupMap = new LinkedHashMap<>(); - } - TypeMapperGroup typeMapperGroup = new TypeMapperGroup(); - List typeMapperList = new ArrayList<>(); - typeMapperList.add(new TypeMapper("varchar(\\(\\d+\\))?", "java.lang.String")); - typeMapperList.add(new TypeMapper("text", "java.lang.String")); - typeMapperList.add(new TypeMapper("decimal(\\(\\d+\\))?", "java.lang.Double")); - typeMapperList.add(new TypeMapper("integer", "java.lang.Integer")); - typeMapperList.add(new TypeMapper("int(\\(\\d+\\))?", "java.lang.Integer")); - typeMapperList.add(new TypeMapper("int4", "java.lang.Integer")); - typeMapperList.add(new TypeMapper("int8", "java.lang.Long")); - typeMapperList.add(new TypeMapper("bigint(\\(\\d+\\))?", "java.lang.Long")); - typeMapperList.add(new TypeMapper("datetime", "java.util.Date")); - typeMapperList.add(new TypeMapper("timestamp", "java.util.Date")); - typeMapperList.add(new TypeMapper("boolean", "java.lang.Boolean")); - typeMapperGroup.setName(DEFAULT_NAME); - typeMapperGroup.setElementList(typeMapperList); - typeMapperGroupMap.put(DEFAULT_NAME, typeMapperGroup); - - //初始化表配置 - if (this.columnConfigGroupMap == null) { - this.columnConfigGroupMap = new LinkedHashMap<>(); - } - ColumnConfigGroup columnConfigGroup = new ColumnConfigGroup(); - List columnConfigList = new ArrayList<>(); - columnConfigList.add(new ColumnConfig("disable", ColumnConfigType.BOOLEAN)); - columnConfigGroup.setName(DEFAULT_NAME); - columnConfigGroup.setElementList(columnConfigList); - columnConfigGroupMap.put(DEFAULT_NAME, columnConfigGroup); - - //初始化全局配置 - if (this.globalConfigGroupMap == null) { - this.globalConfigGroupMap = new LinkedHashMap<>(); - } - this.globalConfigGroupMap.put(DEFAULT_NAME, loadGlobalConfigGroup(DEFAULT_NAME, "init", "define", "autoImport")); - } - - /** - * 加载模板文件 - * - * @param filePath 模板路径 - * @return 模板文件内容 - */ - private static String loadTemplate(String filePath) { - return FileUtils.getInstance().read(ConfigInfo.class.getResourceAsStream(filePath)).replaceAll("\r", ""); - } - - /** - * 加载模板组 - * - * @param groupName 组名 - * @param templateNames 模板名称 - * @return 模板组 - */ - private static TemplateGroup loadTemplateGroup(String groupName, String... templateNames) { - TemplateGroup templateGroup = new TemplateGroup(); - templateGroup.setName(groupName); - templateGroup.setElementList(new ArrayList<>()); - for (String templateName : templateNames) { - String path = "/template/" + groupName + "/" + templateName + ".vm"; - templateGroup.getElementList().add(new Template(templateName, loadTemplate(path))); - } - return templateGroup; - } - - /** - * 加载全局配置组 - * - * @param groupName 组名 - * @param templateNames 模板名称 - * @return 模板组 - */ - private static GlobalConfigGroup loadGlobalConfigGroup(String groupName, String... templateNames) { - GlobalConfigGroup globalConfigGroup = new GlobalConfigGroup(); - globalConfigGroup.setName(groupName); - globalConfigGroup.setElementList(new ArrayList<>()); - for (String templateName : templateNames) { - String path = "/globalConfig/" + groupName + "/" + templateName + ".vm"; - globalConfigGroup.getElementList().add(new GlobalConfig(templateName, loadTemplate(path))); - } - return globalConfigGroup; - } - - @Nullable - @Override - public ConfigInfo getState() { - return this; - } - - @Override - public void loadState(@NotNull ConfigInfo configInfo) { - // 备份初始配置 - Map templateGroupMap = this.getTemplateGroupMap(); - String version = this.getVersion(); - // 覆盖初始配置 - XmlSerializerUtil.copyBean(configInfo, this); - - // 已经合并不再重复合并 - if (configInfo.getVersion() != null && configInfo.getVersion().equals(version)) { - return; - } - - // 合并配置 - templateGroupMap.forEach((name, templateGroup) -> { - if (this.getTemplateGroupMap().containsKey(name)) { - return; - } - this.getTemplateGroupMap().put(name, templateGroup); - }); - } -} diff --git a/src/main/java/com/sjhy/plugin/tool/VelocityUtils.java b/src/main/java/com/sjhy/plugin/tool/VelocityUtils.java index 034b08d..b3ebf25 100644 --- a/src/main/java/com/sjhy/plugin/tool/VelocityUtils.java +++ b/src/main/java/com/sjhy/plugin/tool/VelocityUtils.java @@ -5,6 +5,7 @@ import com.intellij.openapi.ui.MessageDialogBuilder; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.vfs.VirtualFileManager; import com.intellij.util.ExceptionUtil; +import com.sjhy.plugin.config.Settings; import com.sjhy.plugin.constants.MsgValue; import com.sjhy.plugin.entity.Callback; import com.sjhy.plugin.entity.GlobalConfig; @@ -105,12 +106,12 @@ public class VelocityUtils { * @return 全局参数 */ private Map handlerMap() { - ConfigInfo configInfo = ConfigInfo.getInstance(); + Settings settings = Settings.getInstance(); Map map = new HashMap<>(16); // 编码类型 - String encode = configInfo.getEncode(); + String encode = settings.getEncode(); // 坐着名称 - String author = configInfo.getAuthor(); + String author = settings.getAuthor(); // 表信息集合 List tableInfoList = tableInfoUtils.handler(cacheDataUtils.getDbTableList()); // 选中的module @@ -206,14 +207,14 @@ public class VelocityUtils { if (!createPath(cacheDataUtils.getSavePath())) { return; } - ConfigInfo configInfo = ConfigInfo.getInstance(); + Settings settings = Settings.getInstance(); // 获取覆盖的表配置信息 List tableInfoList = coverConfigInfo(); List