优化代码,封装文件操作工具类。加入储存路径的回调支持

This commit is contained in:
makejava 2018-01-13 14:12:28 +08:00
parent 54c72df3d9
commit cf45f70231
6 changed files with 160 additions and 45 deletions

View File

@ -2,6 +2,15 @@ package com.sjhy.plugin.entity;
public class Callback {
private String fileName;
private String savePath;
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String getFileName() {
return fileName;

View File

@ -2,16 +2,15 @@ package com.sjhy.plugin.service.impl;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.util.xmlb.XmlSerializerUtil;
import com.sjhy.plugin.entity.Template;
import com.sjhy.plugin.entity.TemplateGroup;
import com.sjhy.plugin.entity.TypeMapper;
import com.sjhy.plugin.entity.TypeMapperGroup;
import com.sjhy.plugin.service.ConfigService;
import com.sjhy.plugin.tool.FileUtils;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
@ -32,8 +31,8 @@ public class ConfigServiceImpl implements ConfigService {
//作者
private String author;
//是否初始化
private Boolean init = false;
private FileUtils fileUtils = FileUtils.getInstance();
public ConfigServiceImpl() {
initDefault();
@ -110,12 +109,7 @@ public class ConfigServiceImpl implements ConfigService {
}
private String loadTemplate(String name) {
try {
byte[] temp = FileUtil.loadBytes(getClass().getResourceAsStream("/template/"+name+".vm"));
return new String(temp, "UTF-8").replaceAll("\r", "");
} catch (IOException e) {
throw new RuntimeException(e);
}
return fileUtils.read(getClass().getResourceAsStream("/template/"+name+".vm")).replaceAll("\r", "");
}
//GET SET
@ -178,12 +172,4 @@ public class ConfigServiceImpl implements ConfigService {
public void setTemplateGroupMap(Map<String, TemplateGroup> templateGroupMap) {
this.templateGroupMap = templateGroupMap;
}
public Boolean getInit() {
return init;
}
public void setInit(Boolean init) {
this.init = init;
}
}

View File

@ -24,6 +24,15 @@ public class CacheDataUtils {
private String savePath;
private List<Template> selectTemplate;
private String packageName;
private Module selectModule;
public Module getSelectModule() {
return selectModule;
}
public void setSelectModule(Module selectModule) {
this.selectModule = selectModule;
}
public String getPackageName() {
return packageName;

View File

@ -0,0 +1,58 @@
package com.sjhy.plugin.tool;
import com.intellij.openapi.util.io.FileUtil;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
public class FileUtils {
//单例模式
private static class Instance {
private static final FileUtils ME = new FileUtils();
}
public static FileUtils getInstance() {
return Instance.ME;
}
private FileUtils(){}
public String read(File file) {
StringBuilder builder = new StringBuilder();
try {
builder.append(FileUtil.loadFileText(file));
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}
public String read(InputStream in) {
try {
byte[] temp = FileUtil.loadBytes(in);
return new String(temp, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in!=null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
public void write(File file, String content) {
write(file, content, false);
}
public void write(File file, String content, boolean append) {
try {
FileUtil.writeToFile(file, content, append);
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -1,6 +1,6 @@
package com.sjhy.plugin.tool;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.sjhy.plugin.entity.Callback;
import com.sjhy.plugin.entity.TableInfo;
@ -14,6 +14,7 @@ import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
public class VelocityUtils {
//单例模式
@ -31,6 +32,7 @@ public class VelocityUtils {
private CacheDataUtils cacheDataUtils = CacheDataUtils.getInstance();
private TableInfoUtils tableInfoUtils = TableInfoUtils.getInstance();
private NameUtils nameUtils = NameUtils.getInstance();
private FileUtils fileUtils = FileUtils.getInstance();
private String generate(String template, Map<String,Object> map, String encode) {
VelocityContext velocityContext = new VelocityContext();
@ -45,34 +47,83 @@ public class VelocityUtils {
return stringWriter.toString();
}
public void handler() {
String savePath = cacheDataUtils.getSavePath();
//设置全局参数
private Map<String, Object> handlerMap() {
ConfigService configService = ConfigService.getInstance();
Map<String, Object> map = new HashMap<>();
String encode = configService.getEncode();
String author = configService.getAuthor();
List<TableInfo> tableInfoList = tableInfoUtils.handler(cacheDataUtils.getDbTableList());
Module selectModule = cacheDataUtils.getSelectModule();
map.put("encode", encode);
//所有表数据
map.put("tableInfoList", tableInfoList);
//作者
map.put("author", author);
//工具类
map.put("tool", nameUtils);
//设置的包名
map.put("packageName", cacheDataUtils.getPackageName());
if (selectModule!=null){
//module路径
//noinspection ConstantConditions
map.put("modulePath", selectModule.getModuleFile().getParent().getPath());
}
return map;
}
//创建目录
private boolean createPath(String savePath) {
File path = new File(savePath);
if (!new File(savePath).exists()) {
int result = JOptionPane.showConfirmDialog(null, "Save Path Is Not Exists, Confirm Create?", "Title Info", JOptionPane.OK_CANCEL_OPTION);
if(result==0){
if(!path.mkdirs()){
return;
return false;
}
}else{
return;
return false;
}
}
if (path.isFile()){
JOptionPane.showMessageDialog(null, "Error,Save Path Is File!");
return false;
}
return true;
}
//创建或覆盖文件
private boolean coverFile(File file) {
if (file.exists()) {
if (file.isDirectory()){
JOptionPane.showMessageDialog(null, "Error,Save File Is Path!");
return false;
}
int result = JOptionPane.showConfirmDialog(null, "File "+file.getName()+" Exists, Confirm Continue?", "Title Info", JOptionPane.OK_CANCEL_OPTION);
return result == 0;
}else{
try {
if(!file.createNewFile()){
return false;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
public void handler() {
AtomicReference<String> savePath = new AtomicReference<>(cacheDataUtils.getSavePath());
if (!createPath(savePath.get())){
return;
}
List<TableInfo> tableInfoList = tableInfoUtils.handler(cacheDataUtils.getDbTableList());
List<Template> templateList = cacheDataUtils.getSelectTemplate();
ConfigService configService = ConfigService.getInstance();
String encode = configService.getEncode();
String author = configService.getAuthor();
Map<String, Object> map = new HashMap<>();
map.put("tableInfoList", tableInfoList);
map.put("author", author);
map.put("tool", nameUtils);
map.put("packageName", cacheDataUtils.getPackageName());
Map<String, Object> map = handlerMap();
tableInfoList.forEach(tableInfo -> {
Callback callback = new Callback();
@ -81,29 +132,30 @@ public class VelocityUtils {
map.put("callback", callback);
templateList.forEach(template -> {
String content = generate(template.getCode(), map, encode).trim();
//保存的文件名
String fileName = callback.getFileName();
//保存路径
String callbackSavePath = callback.getSavePath();
//是否使用保存路径
if (callbackSavePath!=null && callbackSavePath.trim().length()>0){
if (!createPath(callbackSavePath)){
return;
}
savePath.set(callbackSavePath);
}
if (fileName==null) {
fileName = tableInfo.getName()+"Default.java";
}
File file = new File(path, fileName);
try {
if (file.exists()) {
int result = JOptionPane.showConfirmDialog(null, "File "+fileName+" Exists, Confirm Continue?", "Title Info", JOptionPane.OK_CANCEL_OPTION);
if (result!=0){
return;
}
}else{
if(!file.createNewFile()){
return;
}
}
FileUtil.writeToFile(file, content, false);
} catch (IOException e) {
e.printStackTrace();
File file = new File(new File(savePath.get()), fileName);
//覆盖或创建文件
if (!coverFile(file)){
return;
}
fileUtils.write(file, content);
});
});
JOptionPane.showMessageDialog(null, "Code Generate Success!");
//刷新整个项目
VirtualFileManager.getInstance().syncRefresh();
}

View File

@ -92,6 +92,7 @@ public class SelectSavePath extends JDialog {
cacheDataUtils.setSavePath(savePath);
cacheDataUtils.setSelectTemplate(selectTemplateList);
cacheDataUtils.setPackageName(packageField.getText());
cacheDataUtils.setSelectModule(getSelectModule());
VelocityUtils.getInstance().handler();
dispose();
}