package com.yeshi.plugins;
|
|
import com.intellij.openapi.actionSystem.*;
|
import com.intellij.openapi.project.Project;
|
import com.intellij.openapi.ui.Messages;
|
import com.intellij.openapi.vfs.VirtualFile;
|
import org.yeshi.utils.generater.GeneraterManagerV2;
|
import org.yeshi.utils.generater.vo.xmlconfig.GenertorConfig;
|
import org.yeshi.utils.generater.vo.xmlconfig.XmlConfigParseUtil;
|
|
import java.io.File;
|
import java.io.InputStream;
|
|
/**
|
* @author hxh
|
* @title: TextBoxes
|
* @description: Test
|
* @date 2022/3/29 17:10
|
*/
|
public class GenerateModuleCode extends AnAction {
|
|
public GenerateModuleCode() {
|
super("自动化代码生成插件");
|
}
|
|
@Override
|
public void actionPerformed(AnActionEvent e) {
|
Project project = e.getData(PlatformDataKeys.PROJECT);
|
VirtualFile file = DataKeys.VIRTUAL_FILE.getData(e.getDataContext());
|
if (!file.getPath().contains("/resources/")) {
|
Messages.showMessageDialog(project, "请将文件放在resources或其子目录下", "注意", Messages.getInformationIcon());
|
return;
|
}
|
File f = new File(file.getPath());
|
|
while (!f.getName().equalsIgnoreCase("resources")) {
|
f = f.getParentFile();
|
}
|
|
f = new File(f.getParentFile().getAbsolutePath(), "java");
|
|
if (!f.exists()) {
|
Messages.showErrorDialog(project, String.format("文件夹%s不存在", f.getPath()), "错误提示");
|
return;
|
}
|
|
try {
|
InputStream inputStream = file.getInputStream();
|
GenertorConfig config = XmlConfigParseUtil.parse(inputStream);
|
GeneraterManagerV2.getInstance().init(f.getAbsolutePath(), config);
|
GeneraterManagerV2.getInstance().createWholeFunction();
|
Messages.showMessageDialog(project, "代码生成成功", "温馨提示", Messages.getInformationIcon());
|
} catch (Exception e1) {
|
e1.printStackTrace();
|
Messages.showErrorDialog(project, e1.getMessage(), "错误提示");
|
return;
|
}
|
}
|
|
@Override
|
public void update(AnActionEvent e) {
|
super.update(e);
|
String extension = getFileExtension(e.getDataContext());
|
System.out.println(extension);
|
this.getTemplatePresentation().setEnabled(extension != null && "xml".equals(extension));
|
}
|
|
private static String getFileExtension(DataContext dataContext) {
|
VirtualFile file = DataKeys.VIRTUAL_FILE.getData(dataContext);
|
return file == null ? null : file.getExtension();
|
}
|
}
|