diff --git a/src/generator/src/com/sk/action/BrowseAction.java b/src/generator/src/com/sk/action/BrowseAction.java index 8554c3fe47819b8f8499b3b7d65707baef439eef..b8fca5f242d5246001aaca3c57676f10e2a662ae 100644 --- a/src/generator/src/com/sk/action/BrowseAction.java +++ b/src/generator/src/com/sk/action/BrowseAction.java @@ -38,11 +38,11 @@ import java.util.prefs.Preferences; * @since 2022-02-21 */ public class BrowseAction implements ActionListener { - private JButton button; - private JTextField interField; - private JTextField genField; - private JTextField scriptField; - private Project project; + private final JButton button; + private final JTextField interField; + private final JTextField genField; + private final JTextField scriptField; + private final Project project; public BrowseAction(Project project, JButton button, JTextField interField, @@ -52,10 +52,8 @@ public class BrowseAction implements ActionListener { this.interField = interField; this.genField = geField; this.scriptField = scriptField; - } - @Override public void actionPerformed(ActionEvent actionEvent) { if (actionEvent.getSource().equals(button)) { @@ -68,7 +66,6 @@ public class BrowseAction implements ActionListener { if (!pathRecord.equals("")) { fcDlg = new JFileChooser(pathRecord); } - fcDlg.setDialogTitle("请选择接口文件..."); fcDlg.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); FileNameExtensionFilter filter = new FileNameExtensionFilter("文本文件(*.ts)", "ts"); @@ -78,9 +75,7 @@ public class BrowseAction implements ActionListener { if (returnVal == JFileChooser.APPROVE_OPTION) { String upPath = fcDlg.getSelectedFile().getParent(); File[] files = fcDlg.getSelectedFiles(); - String interFile = setSelectFile(files); - if (TextUtils.isBlank(interFile)) { return; } @@ -96,14 +91,14 @@ public class BrowseAction implements ActionListener { } private String setSelectFile(File[] files) { - String interFile = ""; + StringBuilder interFile = new StringBuilder(); boolean existFile = false; boolean existDir = false; for (File file : files) { if (file.isDirectory()) { if (!existDir) { existDir = true; - interFile += file.getPath() + ","; + interFile.append(file.getPath()).append(","); } else { GenNotification.notifyMessage(project, "目前只支持单个文件夹转换", @@ -121,7 +116,7 @@ public class BrowseAction implements ActionListener { return ""; } existFile = true; - interFile += file.getPath() + ","; + interFile.append(file.getPath()).append(","); } } if (existDir && existFile) { @@ -132,6 +127,6 @@ public class BrowseAction implements ActionListener { interField.setText(""); return ""; } - return interFile; + return interFile.toString(); } } diff --git a/src/generator/src/com/sk/action/GenAction.java b/src/generator/src/com/sk/action/GenAction.java index 89ff34a793861f85a6dfaaa803b185e7117649e5..bd6122f30d3d9fc78ef965894c2ebf84738d2538 100644 --- a/src/generator/src/com/sk/action/GenAction.java +++ b/src/generator/src/com/sk/action/GenAction.java @@ -29,8 +29,8 @@ import java.awt.event.ActionListener; * @since 2022-02-21 */ public class GenAction implements ActionListener { - private JButton button; - private JTextField textField; + private final JButton button; + private final JTextField textField; public GenAction(JButton button, JTextField textField) { this.button = button; diff --git a/src/generator/src/com/sk/action/ScriptAction.java b/src/generator/src/com/sk/action/ScriptAction.java index fbba7bf1da2f044941f83599f5414c2fb7b511f2..c2a172fb93003d7c2870666c4268f980a1372926 100644 --- a/src/generator/src/com/sk/action/ScriptAction.java +++ b/src/generator/src/com/sk/action/ScriptAction.java @@ -29,8 +29,8 @@ import java.awt.event.ActionListener; * @since 2022-02-21 */ public class ScriptAction implements ActionListener { - private JButton button; - private JTextField textField; + private final JButton button; + private final JTextField textField; public ScriptAction(JButton button, JTextField textField) { this.button = button; diff --git a/src/generator/src/com/sk/dialog/ConfirmDialog.java b/src/generator/src/com/sk/dialog/ConfirmDialog.java index f610471dde21523cc32f063db8e9fcef43713d20..7bcf2479d57018d721bd90978f09892362c3ddf4 100644 --- a/src/generator/src/com/sk/dialog/ConfirmDialog.java +++ b/src/generator/src/com/sk/dialog/ConfirmDialog.java @@ -28,7 +28,7 @@ import javax.swing.JComponent; * @since 2022-02-21 */ public class ConfirmDialog extends DialogWrapper { - private ConfirmDiagPane confirmDiagPane; + private final ConfirmDiagPane confirmDiagPane; /** * 构造函数 diff --git a/src/generator/src/com/sk/dialog/GenerateDialog.java b/src/generator/src/com/sk/dialog/GenerateDialog.java index 54053098c8bc4f3b82f2bb602338f8a97cb69fef..0a3cfdd54cde729cae22023eccdd62f3a53ca7ed 100644 --- a/src/generator/src/com/sk/dialog/GenerateDialog.java +++ b/src/generator/src/com/sk/dialog/GenerateDialog.java @@ -42,10 +42,7 @@ public class GenerateDialog extends DialogWrapper { private static final String TITLE = "Generate Napi Frame"; private static final String URL = "https://gitee.com/openharmony/napi_generator"; - private GenerateDialogPane genDiag; - private DialogWrapperExitAction exitAction; - private CustomOKAction okAction; - private CustomHelpAction helpAction; + private final GenerateDialogPane genDiag; /** * 构造函数 @@ -95,8 +92,8 @@ public class GenerateDialog extends DialogWrapper { @NotNull @Override protected Action[] createActions() { - exitAction = new DialogWrapperExitAction("Cancel", CANCEL_EXIT_CODE); - okAction = new CustomOKAction(); + DialogWrapperExitAction exitAction = new DialogWrapperExitAction("Cancel", CANCEL_EXIT_CODE); + CustomOKAction okAction = new CustomOKAction(); // 设置默认的焦点按钮 okAction.putValue(DialogWrapper.DEFAULT_ACTION, true); @@ -106,7 +103,7 @@ public class GenerateDialog extends DialogWrapper { @NotNull @Override protected Action[] createLeftSideActions() { - helpAction = new CustomHelpAction(); + CustomHelpAction helpAction = new CustomHelpAction(); return new Action[]{helpAction}; } @@ -127,7 +124,6 @@ public class GenerateDialog extends DialogWrapper { if (validationInfo != null) { LOG.info(validationInfo.message); } else { - if (genDiag.runFun()) { close(CANCEL_EXIT_CODE); } diff --git a/src/generator/src/com/sk/dialog/GenerateDialogPane.java b/src/generator/src/com/sk/dialog/GenerateDialogPane.java index f1642948a75a89da2160f19ae004100dc8095b7b..08ec063df0e78372872760dc83566286702430a4 100644 --- a/src/generator/src/com/sk/dialog/GenerateDialogPane.java +++ b/src/generator/src/com/sk/dialog/GenerateDialogPane.java @@ -68,15 +68,15 @@ public class GenerateDialogPane extends JDialog { private JButton selectInter; private JButton selectGenPath; private JButton selectScriptPath; + private JLabel interText; private JLabel genText; private JLabel scriptText; - private JLabel interText; private boolean generateSuccess = true; private String sErrorMessage = ""; - private String destPath; - private String directoryPath; - private String fileName; - private Project project; + private String interFileOrDir; + private String genOutDir; + private String scriptOutDir; + private final Project project; /** @@ -89,17 +89,17 @@ public class GenerateDialogPane extends JDialog { */ public GenerateDialogPane(Project project, String interFilePath, String genDir, String scriptDir) { this.project = project; - this.destPath = interFilePath; - this.directoryPath = genDir; - this.fileName = scriptDir; + this.interFileOrDir = interFilePath; + this.genOutDir = genDir; + this.scriptOutDir = scriptDir; interText.setText("接口文件:"); genText.setText("生成框架路径:"); scriptText.setText("编译脚本路径:"); - interPath.setText(destPath); - genPath.setText(directoryPath); - scriptPath.setText(directoryPath); + interPath.setText(interFileOrDir); + genPath.setText(genOutDir); + scriptPath.setText(genOutDir); // call onCancel() on ESCAPE contentPane.registerKeyboardAction(actionEvent -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), @@ -144,7 +144,7 @@ public class GenerateDialogPane extends JDialog { ValidationInfo validationInfo = null; if (isEmptyFile) { String warnMsg = "接口文件、框架、编译脚本路径不能为空"; - warningMessage("Please input interface,gen and gyp file directory", warnMsg); + warningMessage(warnMsg); validationInfo = new ValidationInfo(warnMsg); return validationInfo; } @@ -164,31 +164,32 @@ public class GenerateDialogPane extends JDialog { dispose(); } - private void warningMessage(String message, String title) { - GenNotification.notifyMessage(this.project, message, title, NotificationType.WARNING); + private void warningMessage(String title) { + String notiContent = "请选择接口文件或文件夹,生成框架路径,编译脚本路径"; + GenNotification.notifyMessage(this.project, notiContent, title, NotificationType.WARNING); } /** * 执行主程序入口 * * @return 执行状态 - * @throws IOException 文件打开 */ public boolean runFun() { GenNotification.notifyMessage(this.project, "", "正在生成", NotificationType.INFORMATION); - destPath = interPath.getText(); - String parentPath = genPath.getText(); + interFileOrDir = interPath.getText(); + genOutDir = genPath.getText(); + scriptOutDir = scriptPath.getText(); String command; - command = genCommand(destPath, parentPath); + command = genCommand(); try { if (!TextUtils.isEmpty(command) && callExtProcess(command)) { - GenNotification.notifyMessage(project, directoryPath, "提示", NotificationType.INFORMATION, true); + GenNotification.notifyMessage(project, genPath.getText(), "提示", NotificationType.INFORMATION, true); return true; } } catch (IOException | InterruptedException ex) { - GenNotification.notifyMessage(project, directoryPath, "Command exec error", NotificationType.ERROR); + GenNotification.notifyMessage(project, genPath.getText(), "Command exec error", NotificationType.ERROR); LOG.error(ex); } return false; @@ -197,18 +198,16 @@ public class GenerateDialogPane extends JDialog { /** * 生成命令行指令 * - * @param destPath 目标文件 - * @param parentPath 父目录文件 * @return 返回命令行执行内容 */ - private String genCommand(String destPath, String parentPath) { + private String genCommand() { String sysName = System.getProperties().getProperty("os.name").toUpperCase(); String tmpDirFile = System.getProperty("java.io.tmpdir"); String execFn; - if (sysName.indexOf("WIN") >= 0) { + if (sysName.contains("WIN")) { execFn = "cmds/win/napi_generator-win.exe"; tmpDirFile += "napi_generator-win.exe"; - } else if (sysName.indexOf("LINUX") >= 0) { + } else if (sysName.contains("LINUX")) { execFn = "cmds/linux/napi_generator-linux"; tmpDirFile += "napi_generator-linux"; } else { @@ -223,50 +222,49 @@ public class GenerateDialogPane extends JDialog { } byte[] bs = inputStream.readAllBytes(); writeTmpFile(tmpDirFile, bs); - if (sysName.indexOf("LINUX") >= 0 || sysName.indexOf("MAC OS") >= 0) { + if (sysName.contains("LINUX") || sysName.contains("MAC OS")) { executable(tmpDirFile); } } catch (IOException | InterruptedException e) { GenNotification.notifyMessage(this.project, e.getMessage(), "Can not Find File:" + execFn, NotificationType.ERROR); LOG.error(e); + return ""; } } - String command = file.toString(); - String inArgs = genInArgs(destPath); - command += inArgs + " -o " + parentPath; + String inArgs = genInArgs(); + command += inArgs + " -o " + genOutDir; return command; } /** * 生成 -f -d 输入参数。 * - * @param destPath 源文件路径。 * @return 生成后的值-f -d的值 */ - private String genInArgs(String destPath) { + private String genInArgs() { - String[] interArr = destPath.split(","); - String tsParam = " -f "; - String dirParam = " -d "; + String[] interArr = interFileOrDir.split(","); + StringBuilder tsParam = new StringBuilder(" -f "); + StringBuilder dirParam = new StringBuilder(" -d "); String inputCommand = ""; if (interArr.length > 0) { for (String interStr : interArr) { File interFile = new File(interStr); if (interFile.isDirectory()) { - dirParam += interStr + " "; + dirParam.append(interStr).append(" "); } else { - tsParam += interStr + ","; + tsParam.append(interStr).append(","); } } - if (!TextUtils.isEmpty(tsParam.replaceAll("-f", "")) - && !TextUtils.isBlank(tsParam.replaceAll("-f", ""))) { + if (!TextUtils.isEmpty(tsParam.toString().replaceAll("-f", "")) + && !TextUtils.isBlank(tsParam.toString().replaceAll("-f", ""))) { inputCommand += tsParam.substring(0, tsParam.length() - 1); } - if (!TextUtils.isEmpty(dirParam.replace("-d", "")) - && !TextUtils.isBlank(dirParam.replace("-d", ""))) { + if (!TextUtils.isEmpty(dirParam.toString().replace("-d", "")) + && !TextUtils.isBlank(dirParam.toString().replace("-d", ""))) { inputCommand += dirParam.substring(0, dirParam.length() - 1); } } @@ -281,10 +279,8 @@ public class GenerateDialogPane extends JDialog { } Process process = Runtime.getRuntime().exec(command); genResultLog(process); - StreamConsumer errConsumer = new StreamConsumer(process.getErrorStream()); StreamConsumer outputConsumer = new StreamConsumer(process.getInputStream()); - errConsumer.start(); outputConsumer.start(); @@ -301,18 +297,16 @@ public class GenerateDialogPane extends JDialog { /** * 写makeFile.txt文件 - * - * @throws IOException 文件异常 */ private void writeCommand() { FileUtil fileUtil = new FileUtil(); - String filePath = fileUtil.makeFile(directoryPath + "/makeFile.txt"); + String filePath = fileUtil.makeFile(genOutDir + "/makeFile.txt"); if (TextUtils.isEmpty(filePath)) { - LOG.error("makeFile is error"); + LOG.info("makeFile is fail"); return; } Matcher matcher = LF_PATTERN.matcher(COMMAND_STATEMENT); - String statement = matcher.replaceAll(fileName); + String statement = matcher.replaceAll(scriptOutDir); try { if (!fileUtil.findStringInFile(filePath, statement)) { fileUtil.writeErrorToTxt(filePath, statement); @@ -326,7 +320,7 @@ public class GenerateDialogPane extends JDialog { * 赋值可执行文件权限。 * * @param execFn 可执行命令 - * @throws IOException 打开文件异常 + * @throws IOException 打开文件异常 * @throws InterruptedException 中断异常 */ private void executable(String execFn) throws IOException, InterruptedException { @@ -382,27 +376,23 @@ public class GenerateDialogPane extends JDialog { * @return ErrorResult */ private String getErrorResult(BufferedReader stdError) { - String sErr = ""; + StringBuilder sErr = new StringBuilder(); while (true) { String sTmp; try { if ((sTmp = stdError.readLine()) == null) { break; } - sErr += sTmp + FileUtil.getNewline(); + sErr.append(sTmp).append(FileUtil.getNewline()); } catch (IOException ioException) { LOG.error(" genResultLog stdInput error" + ioException); } } - return sErr; + return sErr.toString(); } private boolean generateIsSuccess(String sOut) { - if (sOut.indexOf("success") >= 0 || TextUtils.isEmpty(sOut)) { - generateSuccess = true; - } else { - generateSuccess = false; - } + generateSuccess = sOut.contains("success") || TextUtils.isEmpty(sOut); return generateSuccess; } @@ -413,22 +403,22 @@ public class GenerateDialogPane extends JDialog { * @return 返回当前输入框内容 */ private String genInputLog(BufferedReader stdInput) { - String sOut = ""; + StringBuilder sOut = new StringBuilder(); while (true) { String sTmp; try { if ((sTmp = stdInput.readLine()) == null) { break; } - sOut += sTmp + FileUtil.getNewline(); + sOut.append(sTmp).append(FileUtil.getNewline()); } catch (IOException ioException) { LOG.error(" genResultLog stdInput error" + ioException); } } - return sOut; + return sOut.toString(); } - class StreamConsumer extends Thread { + static class StreamConsumer extends Thread { InputStream is; StreamConsumer(InputStream is) { diff --git a/src/generator/src/com/sk/ng/GenDTS.java b/src/generator/src/com/sk/ng/GenDTS.java index 10dc40216295f52b596e9bd249aa5b49da28df05..020bc614d318c25b005e9922fe35d72d6b3a0a15 100644 --- a/src/generator/src/com/sk/ng/GenDTS.java +++ b/src/generator/src/com/sk/ng/GenDTS.java @@ -43,10 +43,12 @@ public class GenDTS extends AnAction { GenNotification.notifyMessage(project, "", "file is not exist", NotificationType.ERROR); return; } - + if (project == null) { + return; + } String baseFile = project.getBasePath(); - if (!FileUtil.checkProjectSDK(project, baseFile)) { + if (FileUtil.checkProjectSDK(project, baseFile)) { return; } @@ -54,9 +56,7 @@ public class GenDTS extends AnAction { String directoryPath = file.getParent().getPath(); String fileName = file.getName(); GenerateDialog wrapper = new GenerateDialog(project, destPath, directoryPath, fileName); - if (wrapper.showAndGet()) { - return; - } + wrapper.showAndGet(); } @@ -67,11 +67,7 @@ public class GenDTS extends AnAction { if (file == null) { event.getPresentation().setEnabledAndVisible(false); } else { - if (FileUtil.patternFileName(file.getName())) { - event.getPresentation().setEnabledAndVisible(true); - } else { - event.getPresentation().setEnabledAndVisible(false); - } + event.getPresentation().setEnabledAndVisible(FileUtil.patternFileName(file.getName())); } } } diff --git a/src/generator/src/com/sk/ng/GenMenuDTS.java b/src/generator/src/com/sk/ng/GenMenuDTS.java index 58b9ed34e7f748df4c11e8635d6ffacc18b9326f..f69f004996d4a6364a71a7d14a07d9f43f3f3286 100644 --- a/src/generator/src/com/sk/ng/GenMenuDTS.java +++ b/src/generator/src/com/sk/ng/GenMenuDTS.java @@ -35,16 +35,17 @@ public class GenMenuDTS extends AnAction { public void actionPerformed(@NotNull AnActionEvent anActionEvent) { Project project = anActionEvent.getProject(); + if (project == null) { + return; + } String baseFile = project.getBasePath(); - if (!FileUtil.checkProjectSDK(project, baseFile)) { + if (FileUtil.checkProjectSDK(project, baseFile)) { return; } GenerateDialog wrapper = new GenerateDialog(project, "", "", ""); - if (wrapper.showAndGet()) { - return; - } + wrapper.showAndGet(); } } diff --git a/src/generator/src/com/sk/utils/FileUtil.java b/src/generator/src/com/sk/utils/FileUtil.java index f4f69acc10191109e4ab08145dce2a05fe53817c..184aad3f80f5f57c4d06a568adafb8720a72770c 100644 --- a/src/generator/src/com/sk/utils/FileUtil.java +++ b/src/generator/src/com/sk/utils/FileUtil.java @@ -20,12 +20,14 @@ import com.intellij.openapi.project.Project; import org.apache.commons.lang3.StringUtils; import org.apache.http.util.TextUtils; -import java.io.BufferedReader; import java.io.File; -import java.io.FileInputStream; import java.io.FileWriter; -import java.io.IOException; import java.io.InputStreamReader; +import java.io.FileInputStream; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.FileNotFoundException; +import java.nio.charset.StandardCharsets; import java.util.Properties; import java.util.regex.Pattern; @@ -47,7 +49,6 @@ public class FileUtil { * * @param path 路径 * @param content 内容 - * @throws IOException 异常 */ public void writeErrorToTxt(String path, String content) { File file = new File(path); @@ -70,12 +71,10 @@ public class FileUtil { try { boolean isCreateFile = file.createNewFile(); if (isCreateFile) { - LOG.info("makeFile result success"); - } else { - LOG.info("makeFile result error"); + LOG.info(String.format("makeFile %s success", path)); } } catch (IOException ioException) { - LOG.error("makeFile io error" + ioException); + LOG.error(String.format("makeFile %s error:%s", path, ioException)); return ""; } } @@ -93,9 +92,13 @@ public class FileUtil { public boolean findStringInFile(String path, String content) throws IOException { File file = new File(path); String[] command = content.split(StringUtils.LF); - try (InputStreamReader read = new InputStreamReader(new FileInputStream(file), "UTF-8"); + + try (InputStreamReader read = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8); BufferedReader bufferedReader = new BufferedReader(read)) { return isContainString(bufferedReader, command); + } catch (FileNotFoundException foundException) { + LOG.error("file not found" + foundException); + return false; } } @@ -103,17 +106,11 @@ public class FileUtil { String line = null; while (true) { try { - if (!((line = bufferedReader.readLine()) != null)) { + if ((line = bufferedReader.readLine()) == null) { return false; } } catch (IOException ioException) { LOG.error("findStringInFile IOException" + ioException); - } finally { - try { - bufferedReader.close(); - } catch (IOException ioException) { - LOG.error("findStringInFile io error" + ioException); - } } line += line; if (line.contains(command[0])) { @@ -155,6 +152,7 @@ public class FileUtil { File baseDir = new File(baseFile); if (baseDir.isDirectory()) { File[] childFile = baseDir.listFiles(); + assert childFile != null; for (File file : childFile) { if (file.getName().equals("build.gradle") || file.getName().equals("build-profile.json5")) { gradlePath = file.getPath(); @@ -167,14 +165,14 @@ public class FileUtil { GenNotification.notifyMessage(project, "项目结构中没有grandle配置文件。", "当前项目结构不支持", NotificationType.WARNING); - return false; + return true; } try { properties.load(new FileInputStream(gradlePath)); } catch (IOException e) { GenNotification.notifyMessage(project, e.getMessage(), "提示", NotificationType.ERROR); LOG.error(String.format("Can not load file :%s . %s", gradlePath, e)); - return false; + return true; } String ohosSDK = properties.getProperty("compileSdkVersion"); @@ -182,8 +180,8 @@ public class FileUtil { GenNotification.notifyMessage(project, "SKD版本过低,NAPI仅支持5.0及以上版本", "提示", NotificationType.WARNING); - return false; + return true; } - return true; + return false; } }