admin
2022-03-30 fa9d749e04cdd09b351f30df6fa5b08a2c762924
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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();
    }
}