feat: modify mutiple file entry and fix some problem

fix #I5AB9R #I5AAND

Signed-off-by: lengege <liulongc@digitalchina.com>
This commit is contained in:
lengege 2022-06-08 19:01:56 +08:00
parent 72c1b2a61b
commit 37ed36ed9f
9 changed files with 143 additions and 31 deletions

2
.gitignore vendored
View File

@ -24,3 +24,5 @@
/src/generator/resources/cmds/mac/napi_generator-macos /src/generator/resources/cmds/mac/napi_generator-macos
/src/generator/resources/cmds/win/napi_generator-win.exe /src/generator/resources/cmds/win/napi_generator-win.exe
/src/generator/.idea/ /src/generator/.idea/
/src/package-lock.json
/src/package.json

Binary file not shown.

After

Width:  |  Height:  |  Size: 274 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 325 KiB

View File

@ -1,7 +1,7 @@
# IDEA插件开发环境配置 # IDEA插件开发环境配置
基础环境要求: 基础环境要求:
JDK 11 IDEA Community 213及以上 JDK 11 IDEA Community 2021.3.3
1.下载IDEA Community 与 JDK11 配置好环境 1.下载IDEA Community 与 JDK11 配置好环境
点击 https://www.jetbrains.com/idea/download/ 下载Community版本并完成安装。 点击 https://www.jetbrains.com/idea/download/ 下载Community版本并完成安装。
@ -15,14 +15,28 @@ JDK 11 IDEA Community 213及以上
项目打开完成点击File>Project Structure 项目打开完成点击File>Project Structure
![](../../figures/IntelliJ_env_proj_structure.png) ![](../../figures/IntelliJ_env_proj_structure.png)
4.配置Modules. 4.配置Modules.
Project Settings > Modules 新建Modules,再new Modules界面选择IntelliJ Platform Plugin。点击上方“-”删除原有的Modules点击上方侧“+”选择 New Modules。 Project Settings > Modules 新建Modules.点击上方“-”删除原有的Modules“+”选择 New Modules。
![](../../figures/IntelliJ_env_Proj_Module.png) ![](../../figures/IntelliJ_env_Proj_Module.png)
5.配置Module SDK. 5.配置Module SDK.
在New Modules对话框中选择IntelliJ Platform Plugin。若为首次环境配置请在Module SDK 下拉框中点击 Add IntelliJ Platform Plugin SDK 选择IDEA Community安装目录点击OK,在Select Internal Java Platform 选择 JAVA SDK 11213版本只支持 11) 在New Modules对话框中选择IntelliJ Platform Plugin。若为首次环境配置请在Module SDK 下拉框中点击 Add IntelliJ Platform Plugin SDK 选择IDEA Community安装目录点击OK,在Select Internal Java Platform 选择 JAVA SDK 11213版本只支持 11)
![](../../figures/IntelliJ_env_Proj_Module_New.png) ![](../../figures/IntelliJ_env_Proj_Module_New.png)
6.配置Root Content. 6.配置Root Content.
在上图界面点击Next选择Content root:为napi_generator/src/generator文件夹module name会自动变为generator,若出现提示已存在是否覆盖的提示,请点“是”完成配置。 在上图界面点击Next选择Content root:为napi_generator/src/generator文件夹module name会自动变为generator,若出现提示已存在是否覆盖的提示,请点“是”完成配置。
![](../../figures/IntelliJ_env_module_root.png) ![](../../figures/IntelliJ_env_module_root.png)
7.配置完成Modules后若在SDKs中无相应JDK和Plugin SDK,请点击+号分别添加 Add Java JDK和Add Intellij PlantForm Plugin SDK,Java JDK为java11的安装目录Plugin SDK为 IDEA Community 2021.3.3的安装目录。
![](../../figures/IntelliJ_env_config_SDKs.png)
8.若完成步骤7配置点击OK完成配置。Rebuild项目若IDEA依然不能点击右上角的运行。请重新配置一次Modules。
9.项目运行成功后会另起一个IDEA应用程序。插件运行在IDEA中只需要新建一个Grandle Project,添加相应的TS文件到项目文件夹里面就可以右击文件选择Generate napi Frame出现插件主界面进行相应操作。
10.在Deveco stdio中安装插件。
请在IDEA Community中依次点击Build>Prepare Plugin Module " " for development"生成jar包(jar一般生成在generator目录下)。打开DevEco Studio 工具点击File>settings>plugin。点击右方齿轮选择install plugin from disk选择jar包点击确定完成。重新IDE完成安装
![](../../figures/IntelliJ_env_deveco_install.png)

View File

@ -18,12 +18,15 @@ import com.intellij.notification.NotificationType;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import com.sk.utils.FileUtil; import com.sk.utils.FileUtil;
import com.sk.utils.GenNotification; import com.sk.utils.GenNotification;
import org.apache.http.util.TextUtils;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JTextField; import javax.swing.JTextField;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.File;
import java.util.prefs.Preferences; import java.util.prefs.Preferences;
/** /**
@ -49,42 +52,86 @@ public class BrowseAction implements ActionListener {
this.interField = interField; this.interField = interField;
this.genField = geField; this.genField = geField;
this.scriptField = scriptField; this.scriptField = scriptField;
} }
@Override @Override
public void actionPerformed(ActionEvent actionEvent) { public void actionPerformed(ActionEvent actionEvent) {
if (actionEvent.getSource().equals(button)) { if (actionEvent.getSource().equals(button)) {
Preferences preferences = Preferences.userRoot(); Preferences preferences = Preferences.userRoot();
JFileChooser fcDlg = new JFileChooser(); JFileChooser fcDlg = new JFileChooser();
// 获取上次打开文件的路径
String pathRecord = preferences.get("interPathRecord", ""); String pathRecord = preferences.get("interPathRecord", "");
if (!pathRecord.equals("")) { if (!pathRecord.equals("")) {
fcDlg = new JFileChooser(pathRecord); fcDlg = new JFileChooser(pathRecord);
} }
fcDlg.setDialogTitle("请选择接口文件..."); fcDlg.setDialogTitle("请选择接口文件...");
fcDlg.setFileSelectionMode(JFileChooser.FILES_ONLY); fcDlg.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文件(*.ts)", "ts"); FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文件(*.ts)", "ts");
fcDlg.setMultiSelectionEnabled(true);
fcDlg.setFileFilter(filter); fcDlg.setFileFilter(filter);
int returnVal = fcDlg.showOpenDialog(null); int returnVal = fcDlg.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) { if (returnVal == JFileChooser.APPROVE_OPTION) {
String filepath = fcDlg.getSelectedFile().getPath(); String upPath = fcDlg.getSelectedFile().getParent();
String filename = fcDlg.getSelectedFile().getName(); File[] files = fcDlg.getSelectedFiles();
if (!FileUtil.patternFileName(filename)) { String interFile = setSelectFile(files);
GenNotification.notifyMessage(project,
"当前文件名不符合转换规则!", if (TextUtils.isBlank(interFile)) {
"提示",
NotificationType.WARNING);
return; return;
} }
String upPath = fcDlg.getSelectedFile().getParent();
preferences.put("interPathRecord", filepath); // 设置默认打开路径
interField.setText(filepath);
preferences.put("interPathRecord", upPath);
interField.setText(interFile.substring(0, interFile.length() - 1));
genField.setText(upPath); genField.setText(upPath);
scriptField.setText(upPath); scriptField.setText(upPath);
} }
} }
} }
private String setSelectFile(File[] files) {
String interFile = "";
boolean existFile = false;
boolean existDir = false;
for (File file : files) {
if (file.isDirectory()) {
if (!existDir) {
existDir = true;
interFile += file.getPath() + ",";
} else {
GenNotification.notifyMessage(project,
"目前只支持单个文件夹转换",
"选择不符合要求",
NotificationType.WARNING);
interField.setText("");
return "";
}
} else {
if (!FileUtil.patternFileName(file.getName())) {
GenNotification.notifyMessage(project,
file.getPath(),
file.getName() + "文件名不符合",
NotificationType.WARNING);
return "";
}
existFile = true;
interFile += file.getPath() + ",";
}
}
if (existDir && existFile) {
GenNotification.notifyMessage(project,
"不能同时转换文件和文件夹",
"选择不符合要求",
NotificationType.WARNING);
interField.setText("");
return "";
}
return interFile;
}
} }

View File

@ -84,7 +84,6 @@ public class GenerateDialog extends DialogWrapper {
@Nullable @Nullable
@Override @Override
protected ValidationInfo doValidate() { protected ValidationInfo doValidate() {
return genDiag.validationInfo(); return genDiag.validationInfo();
} }
@ -128,6 +127,7 @@ public class GenerateDialog extends DialogWrapper {
if (validationInfo != null) { if (validationInfo != null) {
LOG.info(validationInfo.message); LOG.info(validationInfo.message);
} else { } else {
if (genDiag.runFun()) { if (genDiag.runFun()) {
close(CANCEL_EXIT_CODE); close(CANCEL_EXIT_CODE);
} }

View File

@ -26,6 +26,7 @@
<component id="b2411" class="javax.swing.JTextField" binding="interPath"> <component id="b2411" class="javax.swing.JTextField" binding="interPath">
<constraints/> <constraints/>
<properties> <properties>
<editable value="false"/>
<margin top="2" left="6" bottom="2" right="6"/> <margin top="2" left="6" bottom="2" right="6"/>
<maximumSize width="500" height="30"/> <maximumSize width="500" height="30"/>
<minimumSize width="300" height="30"/> <minimumSize width="300" height="30"/>
@ -56,6 +57,7 @@
<component id="c4c1b" class="javax.swing.JTextField" binding="genPath"> <component id="c4c1b" class="javax.swing.JTextField" binding="genPath">
<constraints/> <constraints/>
<properties> <properties>
<editable value="false"/>
<maximumSize width="500" height="30"/> <maximumSize width="500" height="30"/>
<minimumSize width="300" height="30"/> <minimumSize width="300" height="30"/>
<preferredSize width="400" height="30"/> <preferredSize width="400" height="30"/>
@ -88,6 +90,7 @@
<component id="7194f" class="javax.swing.JTextField" binding="scriptPath"> <component id="7194f" class="javax.swing.JTextField" binding="scriptPath">
<constraints/> <constraints/>
<properties> <properties>
<editable value="false"/>
<maximumSize width="500" height="30"/> <maximumSize width="500" height="30"/>
<minimumSize width="300" height="30"/> <minimumSize width="300" height="30"/>
<preferredSize width="400" height="30"/> <preferredSize width="400" height="30"/>

View File

@ -24,6 +24,8 @@ import com.sk.action.ScriptAction;
import com.sk.utils.FileUtil; import com.sk.utils.FileUtil;
import com.sk.utils.GenNotification; import com.sk.utils.GenNotification;
import org.apache.http.util.TextUtils; import org.apache.http.util.TextUtils;
import org.jetbrains.annotations.Nullable;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.JDialog; import javax.swing.JDialog;
import javax.swing.JPanel; import javax.swing.JPanel;
@ -76,9 +78,11 @@ public class GenerateDialogPane extends JDialog {
private String fileName; private String fileName;
private Project project; private Project project;
/** /**
* 构造函数 * 构造函数
* @param project projectid *
* @param project projectid
* @param interFilePath 接口文件路径 * @param interFilePath 接口文件路径
* @param genDir 生成框架文件路径 * @param genDir 生成框架文件路径
* @param scriptDir 脚本目录 * @param scriptDir 脚本目录
@ -101,7 +105,8 @@ public class GenerateDialogPane extends JDialog {
contentPane.registerKeyboardAction(actionEvent -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), contentPane.registerKeyboardAction(actionEvent -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
selectInter.addActionListener(new BrowseAction(project, selectInter, interPath, genPath, scriptPath)); BrowseAction browseAction = new BrowseAction(project, selectInter, interPath, genPath, scriptPath);
selectInter.addActionListener(browseAction);
selectGenPath.addActionListener(new GenAction(selectGenPath, genPath)); selectGenPath.addActionListener(new GenAction(selectGenPath, genPath));
selectScriptPath.addActionListener(new ScriptAction(selectScriptPath, scriptPath)); selectScriptPath.addActionListener(new ScriptAction(selectScriptPath, scriptPath));
} }
@ -127,6 +132,7 @@ public class GenerateDialogPane extends JDialog {
* *
* @return ValidationInfo 返回不符要求的信息 * @return ValidationInfo 返回不符要求的信息
*/ */
@Nullable
public ValidationInfo validationInfo() { public ValidationInfo validationInfo() {
String fileInter = interPath.getText(); String fileInter = interPath.getText();
String scriptDir = scriptPath.getText(); String scriptDir = scriptPath.getText();
@ -135,7 +141,7 @@ public class GenerateDialogPane extends JDialog {
|| TextUtils.isEmpty(scriptDir) || TextUtils.isEmpty(scriptDir)
|| TextUtils.isEmpty(filegypDir); || TextUtils.isEmpty(filegypDir);
ValidationInfo validationInfo = new ValidationInfo(null); ValidationInfo validationInfo = null;
if (isEmptyFile) { if (isEmptyFile) {
String warnMsg = "接口文件、框架、编译脚本路径不能为空"; String warnMsg = "接口文件、框架、编译脚本路径不能为空";
warningMessage("Please input interface,gen and gyp file directory", warnMsg); warningMessage("Please input interface,gen and gyp file directory", warnMsg);
@ -199,8 +205,6 @@ public class GenerateDialogPane extends JDialog {
String sysName = System.getProperties().getProperty("os.name").toUpperCase(); String sysName = System.getProperties().getProperty("os.name").toUpperCase();
String tmpDirFile = System.getProperty("java.io.tmpdir"); String tmpDirFile = System.getProperty("java.io.tmpdir");
String execFn; String execFn;
if (sysName.indexOf("WIN") >= 0) { if (sysName.indexOf("WIN") >= 0) {
execFn = "cmds/win/napi_generator-win.exe"; execFn = "cmds/win/napi_generator-win.exe";
tmpDirFile += "napi_generator-win.exe"; tmpDirFile += "napi_generator-win.exe";
@ -211,13 +215,11 @@ public class GenerateDialogPane extends JDialog {
execFn = "cmds/mac/napi_generator-macos"; execFn = "cmds/mac/napi_generator-macos";
tmpDirFile += "napi_generator-macos"; tmpDirFile += "napi_generator-macos";
} }
;
File file = new File(tmpDirFile); File file = new File(tmpDirFile);
if (!file.exists()) { if (!file.exists()) {
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(execFn)) { try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(execFn)) {
if (inputStream == null) { if (inputStream == null) {
return ""; throw new IOException("exec File InputStream is Null");
} }
byte[] bs = inputStream.readAllBytes(); byte[] bs = inputStream.readAllBytes();
writeTmpFile(tmpDirFile, bs); writeTmpFile(tmpDirFile, bs);
@ -231,7 +233,44 @@ public class GenerateDialogPane extends JDialog {
return ""; return "";
} }
} }
return file + " " + "-f" + " " + destPath + " " + "-o" + " " + parentPath;
String command = file.toString();
String inArgs = genInArgs(destPath);
command += inArgs + " -o " + parentPath;
return command;
}
/**
* 生成 -f -d 输入参数
*
* @param destPath 源文件路径
* @return 生成后的值-f -d的值
*/
private String genInArgs(String destPath) {
String[] interArr = destPath.split(",");
String tsParam = " -f ";
String dirParam = " -d ";
String inputCommand = "";
if (interArr.length > 0) {
for (String interStr : interArr) {
File interFile = new File(interStr);
if (interFile.isDirectory()) {
dirParam += interStr + " ";
} else {
tsParam += interStr + ",";
}
}
if (!TextUtils.isEmpty(tsParam.replaceAll("-f", ""))
&& !TextUtils.isBlank(tsParam.replaceAll("-f", ""))) {
inputCommand += tsParam.substring(0, tsParam.length() - 1);
}
if (!TextUtils.isEmpty(dirParam.replace("-d", ""))
&& !TextUtils.isBlank(dirParam.replace("-d", ""))) {
inputCommand += dirParam.substring(0, dirParam.length() - 1);
}
}
return inputCommand;
} }
private boolean callExtProcess(String command) throws IOException, InterruptedException { private boolean callExtProcess(String command) throws IOException, InterruptedException {
@ -263,7 +302,7 @@ public class GenerateDialogPane extends JDialog {
/** /**
* 写makeFile.txt文件 * 写makeFile.txt文件
* *
* @throws IOException 文件异常 * @throws IOException 文件异常
*/ */
private void writeCommand() { private void writeCommand() {
FileUtil fileUtil = new FileUtil(); FileUtil fileUtil = new FileUtil();
@ -287,7 +326,7 @@ public class GenerateDialogPane extends JDialog {
* 赋值可执行文件权限 * 赋值可执行文件权限
* *
* @param execFn 可执行命令 * @param execFn 可执行命令
* @throws IOException 打开文件异常 * @throws IOException 打开文件异常
* @throws InterruptedException 中断异常 * @throws InterruptedException 中断异常
*/ */
private void executable(String execFn) throws IOException, InterruptedException { private void executable(String execFn) throws IOException, InterruptedException {

View File

@ -18,6 +18,7 @@ import com.intellij.notification.NotificationType;
import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.http.util.TextUtils;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
@ -93,7 +94,7 @@ public class FileUtil {
File file = new File(path); File file = new File(path);
String[] command = content.split(StringUtils.LF); String[] command = content.split(StringUtils.LF);
try (InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8"); try (InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8");
BufferedReader bufferedReader = new BufferedReader(read)) { BufferedReader bufferedReader = new BufferedReader(read)) {
return isContainString(bufferedReader, command); return isContainString(bufferedReader, command);
} }
} }
@ -144,7 +145,7 @@ public class FileUtil {
/** /**
* check project SDK * check project SDK
* *
* @param project projectid * @param project projectid
* @param baseFile project root file * @param baseFile project root file
* @return boolean * @return boolean
*/ */
@ -155,13 +156,19 @@ public class FileUtil {
if (baseDir.isDirectory()) { if (baseDir.isDirectory()) {
File[] childFile = baseDir.listFiles(); File[] childFile = baseDir.listFiles();
for (File file : childFile) { for (File file : childFile) {
if (file.getName().equals("build.gradle")) { if (file.getName().equals("build.gradle") || file.getName().equals("build-profile.json5")) {
gradlePath = file.getPath(); gradlePath = file.getPath();
} }
} }
} }
Properties properties = new Properties(); Properties properties = new Properties();
if (TextUtils.isBlank(gradlePath)) {
GenNotification.notifyMessage(project, "项目结构中没有grandle配置文件。",
"当前项目结构不支持",
NotificationType.WARNING);
return false;
}
try { try {
properties.load(new FileInputStream(gradlePath)); properties.load(new FileInputStream(gradlePath));
} catch (IOException e) { } catch (IOException e) {