Skip to content

Commit

Permalink
feat(generate): 新增配置: 几个路径的默认值
Browse files Browse the repository at this point in the history
  • Loading branch information
gudqs7 committed Aug 4, 2022
1 parent ae7dc11 commit b4a5784
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ public enum PluginSettingEnum {
* 给URL设置一个前缀, 设置后, IP 失效; 此前缀不包含最后的 /
*/
DEFAULT_URL_PREFIX("default.url", PluginSettingTypeEnum.STRING),
/**
* 给桌面路径设置一个默认值, 主要用于防止信息泄漏或测试时避免变量
*/
DEFAULT_DESKTOP_PATH("default.path.desktop", PluginSettingTypeEnum.STRING),
/**
* 给下载路径设置一个默认值, 主要用于防止信息泄漏或测试时避免变量
*/
DEFAULT_DOWNLOAD_PATH("default.path.download", PluginSettingTypeEnum.STRING),
/**
* 给项目路径设置一个默认值, 主要用于防止信息泄漏或测试时避免变量
*/
DEFAULT_PROJECT_PATH("default.path.project", PluginSettingTypeEnum.STRING),
/**
* 若设置为 true, 则生成数据时不生成随机数据(主要用于作者测试)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,6 @@ private StructureAndCommentInfo resolveByPsiType(StructureAndCommentInfo parent,
if (parentPsiClass == null) {
ExceptionUtil.handleSyntaxError(parentPsiClassReferenceType.getCanonicalText());
}
String qualifiedName = parentPsiClass.getQualifiedName();
String canonicalText = psiFieldType.getCanonicalText();
if (Objects.equals(qualifiedName, canonicalText)) {
// 自己依赖自己
return null;
}
}
return resolveByPsiType0(parent, FieldType.BASE.getType(), fieldName, psiFieldType, commentInfo, "%s", fieldPrefix, level);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public class PluginSettingHelper {
private static final String CONFIG_FILE_PATH = "docer-config.properties";
private static final Map<String, String> CONFIG = new ConcurrentHashMap<>(16);

private static VirtualFile configFile;
/**
* 防止不同项目复用同一个缓存, 需按项目路径来分隔
*/
private static final Map<String, VirtualFile> configFileMap = new ConcurrentHashMap<>(32);

/**
* 将配置保存到缓存
Expand Down Expand Up @@ -205,14 +208,16 @@ public static void initConfig(Project project, VirtualFile currentVirtualFile) {
*/
@SneakyThrows
public static Map<String, String> getConfigFromFile(Project project, VirtualFile currentVirtualFile) {
String projectBasePath = project.getBasePath();
VirtualFile configFile = configFileMap.get(projectBasePath);
if (configFile != null && configFile.exists()) {
return toMap();
return toMap(projectBasePath);
}
String defaultConfigPath = project.getBasePath() + File.separator + CONFIG_FILE_PATH;
String defaultConfigPath = projectBasePath + File.separator + CONFIG_FILE_PATH;
VirtualFile virtualFileByDefault = LocalFileSystem.getInstance().findFileByPath(defaultConfigPath);
if (virtualFileByDefault != null) {
configFile = virtualFileByDefault;
return toMap();
configFileMap.put(projectBasePath, virtualFileByDefault);
return toMap(projectBasePath);
}
PsiFile[] filesByName = FilenameIndex.getFilesByName(project, CONFIG_FILE_PATH, GlobalSearchScope.projectScope(project));
if (filesByName.length > 0) {
Expand All @@ -225,20 +230,21 @@ public static Map<String, String> getConfigFromFile(Project project, VirtualFile
String projectBasePath1 = getProjectBasePath(path);
String projectBasePath2 = getProjectBasePath(configFilePath);
if (projectBasePath1.equals(projectBasePath2)) {
configFile = virtualFile;
return toMap();
configFileMap.put(projectBasePath, virtualFile);
return toMap(projectBasePath);
}
if (back == null) {
back = virtualFile;
}
}
configFile = back;
return toMap();
configFileMap.put(projectBasePath, back);
return toMap(projectBasePath);
}
return null;
}

private static Map<String, String> toMap() throws IOException {
private static Map<String, String> toMap(String projectBasePath) throws IOException {
VirtualFile configFile = configFileMap.get(projectBasePath);
Properties properties = new Properties();
properties.load(configFile.getInputStream());
Map<String, String> map = new HashMap<>(8);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/cn/gudqs7/plugins/savior/reader/Java2BulkReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import cn.gudqs7.plugins.common.consts.CommonConst;
import cn.gudqs7.plugins.common.consts.MapKeyConstant;
import cn.gudqs7.plugins.common.enums.FieldType;
import cn.gudqs7.plugins.common.enums.PluginSettingEnum;
import cn.gudqs7.plugins.common.pojo.ReadOnlyMap;
import cn.gudqs7.plugins.common.pojo.resolver.CommentInfo;
import cn.gudqs7.plugins.common.pojo.resolver.StructureAndCommentInfo;
import cn.gudqs7.plugins.common.util.PluginSettingHelper;
import cn.gudqs7.plugins.savior.pojo.PostmanKvInfo;
import cn.gudqs7.plugins.savior.theme.Theme;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -135,6 +137,14 @@ private PostmanKvInfo kvFile(String key, String example, String desc, boolean di
}
String desktopPath = home + "/Desktop";
String downloadPath = home + "/Downloads";

// get var from config
if (PluginSettingHelper.configExists()) {
desktopPath = PluginSettingHelper.getConfigItem(PluginSettingEnum.DEFAULT_DESKTOP_PATH, desktopPath);
downloadPath = PluginSettingHelper.getConfigItem(PluginSettingEnum.DEFAULT_DOWNLOAD_PATH, downloadPath);
projectPath = PluginSettingHelper.getConfigItem(PluginSettingEnum.DEFAULT_PROJECT_PATH, projectPath);
}

example = example.replaceAll("\\$\\{desktop}", desktopPath);
example = example.replaceAll("\\$\\{download}", downloadPath);
example = example.replaceAll("\\$\\{project}", projectPath);
Expand Down

0 comments on commit b4a5784

Please sign in to comment.