From bbefc33574d308cd2f99742f1879fb4075e767c3 Mon Sep 17 00:00:00 2001 From: zhaojunxia Date: Wed, 19 Oct 2022 10:36:19 +0800 Subject: [PATCH] feat: add service intellj plugin Signed-off-by: zhaojunxia --- .../resources/META-INF/plugin.xml | 32 ++ .../resources/images/openDisk.png | Bin 0 -> 318 bytes .../resources/images/text.png | Bin 0 -> 277 bytes .../com/sk/service/action/BrowseAction.java | 117 ++++++ .../service/action/OutPathSelectAction.java | 53 +++ .../sk/service/dialog/ConfirmDiagPane.form | 75 ++++ .../sk/service/dialog/ConfirmDiagPane.java | 49 +++ .../com/sk/service/dialog/ConfirmDialog.java | 52 +++ .../com/sk/service/dialog/ErrorDialog.form | 86 ++++ .../com/sk/service/dialog/ErrorDialog.java | 101 +++++ .../service/dialog/ServiceGenerateDialog.java | 148 +++++++ .../dialog/ServiceGenerateDialogPane.form | 119 ++++++ .../dialog/ServiceGenerateDialogPane.java | 392 ++++++++++++++++++ .../src/com/sk/service/ng/GenMenuService.java | 43 ++ .../src/com/sk/service/ng/GenService.java | 66 +++ .../src/com/sk/service/utils/FileUtil.java | 104 +++++ .../com/sk/service/utils/GenNotification.java | 116 ++++++ 17 files changed, 1553 insertions(+) create mode 100644 hdc/service_IntelliJ_plugin/resources/META-INF/plugin.xml create mode 100644 hdc/service_IntelliJ_plugin/resources/images/openDisk.png create mode 100644 hdc/service_IntelliJ_plugin/resources/images/text.png create mode 100644 hdc/service_IntelliJ_plugin/src/com/sk/service/action/BrowseAction.java create mode 100644 hdc/service_IntelliJ_plugin/src/com/sk/service/action/OutPathSelectAction.java create mode 100644 hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ConfirmDiagPane.form create mode 100644 hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ConfirmDiagPane.java create mode 100644 hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ConfirmDialog.java create mode 100644 hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ErrorDialog.form create mode 100644 hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ErrorDialog.java create mode 100644 hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialog.java create mode 100644 hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialogPane.form create mode 100644 hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialogPane.java create mode 100644 hdc/service_IntelliJ_plugin/src/com/sk/service/ng/GenMenuService.java create mode 100644 hdc/service_IntelliJ_plugin/src/com/sk/service/ng/GenService.java create mode 100644 hdc/service_IntelliJ_plugin/src/com/sk/service/utils/FileUtil.java create mode 100644 hdc/service_IntelliJ_plugin/src/com/sk/service/utils/GenNotification.java diff --git a/hdc/service_IntelliJ_plugin/resources/META-INF/plugin.xml b/hdc/service_IntelliJ_plugin/resources/META-INF/plugin.xml new file mode 100644 index 00000000..7a9c3a8e --- /dev/null +++ b/hdc/service_IntelliJ_plugin/resources/META-INF/plugin.xml @@ -0,0 +1,32 @@ + + com.sk.service.ng + Service Generator + 1.0.0 + 深圳开鸿数字产业发展有限公司 + + + + + com.intellij.modules.lang + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/hdc/service_IntelliJ_plugin/resources/images/openDisk.png b/hdc/service_IntelliJ_plugin/resources/images/openDisk.png new file mode 100644 index 0000000000000000000000000000000000000000..5956a0e538a400c7deaf41167ae5ea70348b8584 GIT binary patch literal 318 zcmV-E0m1%>P)Px#_(?=TR5(wi(;-L%K@`UE|6{vRxMVzRM&rBzgXO?tuxPWYNkI_HY11Yqu_#ys zi{W73vdL&Qip^+WMqGTbh~Dks)cL+2Z{Usa&m(nR51OXA={hN;NsMva#}+dSz$38Q zDO`!jUI^iS7UICIst!DCW?R4^@HP!7iekfo3sqh0hQBA9*8iGr&4Cl(VAi-l?$3Z- z2aYn!9zS;AR8^OA5HouM_8mA^)ul|}8Q5uol+tC4ahL;?W%(i^+btj>*CB+39H6SI z*ObzxKjBVQ=W_rvdk5AXcu>{3OyC0;IWPfMasZ(1{Q8;tLhUN|4gZz<1l}xWu~ehG QI{*Lx07*qoM6N<$f-2F9MgRZ+ literal 0 HcmV?d00001 diff --git a/hdc/service_IntelliJ_plugin/resources/images/text.png b/hdc/service_IntelliJ_plugin/resources/images/text.png new file mode 100644 index 0000000000000000000000000000000000000000..f42534169aa41dd830a7cb09792fe1428c2f34f7 GIT binary patch literal 277 zcmV+w0qXvVP)Px#&q+i$ZIe_x?(#Git;`)en(g=-@Y bfj#>Iy*G* + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ConfirmDiagPane.java b/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ConfirmDiagPane.java new file mode 100644 index 00000000..e8c30005 --- /dev/null +++ b/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ConfirmDiagPane.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sk.service.dialog; + +import javax.swing.JPanel; +import javax.swing.JLabel; +import javax.swing.JDialog; +import javax.swing.ImageIcon; + +/** + * ConfirmDiagPane自定义确认对话框 + * 解决ShowConfirmDiag 在Deveco里面会出现界面错位问题。 + * + * @author: zhaoxudong@kaihong.com + * @see: generator error dialog + * @version: v1.0.0 + * @since 2022-02-21 + */ +public class ConfirmDiagPane extends JDialog { + private JPanel contentPane; + private JLabel msgLabel; + private JLabel iconLabel; + + /** + * 构造函数 + * + * @param sErrorMessage 错误信息 + */ + public ConfirmDiagPane(String sErrorMessage) { + msgLabel.setText(sErrorMessage); + iconLabel.setIcon(new ImageIcon("")); + } + + JPanel getContentPanel() { + return contentPane; + } +} diff --git a/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ConfirmDialog.java b/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ConfirmDialog.java new file mode 100644 index 00000000..2e28ee24 --- /dev/null +++ b/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ConfirmDialog.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2022 Guangzhou Digitalchina Information Technology Co., Ltd. + * All rights reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sk.service.dialog; + +import com.intellij.openapi.ui.DialogWrapper; +import org.jetbrains.annotations.Nullable; +import javax.swing.JComponent; + +/** + * 自定义确认对话框Wrapper + * + * @author: zhaoxudong@kaihong.com + * @see: tool conversion plug-in + * @version: v1.0.0 + * @since 2022-02-21 + */ +public class ConfirmDialog extends DialogWrapper { + private final ConfirmDiagPane confirmDiagPane; + + /** + * 构造函数 + * @param message 弹出框信息内容 + */ + public ConfirmDialog(String message) { + super(true); + confirmDiagPane = new ConfirmDiagPane(message); + setOKButtonText("Yes"); + setCancelButtonText("No"); + setUndecorated(true); + setResizable(false); + init(); + } + + @Override + @Nullable + protected JComponent createCenterPanel() { + return confirmDiagPane.getContentPanel(); + } +} diff --git a/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ErrorDialog.form b/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ErrorDialog.form new file mode 100644 index 00000000..0225c79a --- /dev/null +++ b/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ErrorDialog.form @@ -0,0 +1,86 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ErrorDialog.java b/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ErrorDialog.java new file mode 100644 index 00000000..129bcacc --- /dev/null +++ b/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ErrorDialog.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sk.service.dialog; + +import com.intellij.openapi.diagnostic.Logger; + +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JDialog; +import javax.swing.JPanel; +import javax.swing.JTextArea; +import javax.swing.KeyStroke; +import java.awt.event.KeyEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.io.IOException; + +/** + * ErrorDialog错误对话框 + * + * @author: zhaoxudong@kaihong.com + * @see: generator error dialog + * @version: v1.0.0 + * @since 2022-02-21 + */ +public class ErrorDialog extends JDialog { + private static final Logger LOG = Logger.getInstance(ErrorDialog.class); + private static final String URL = + "rundll32 url.dll,FileProtocolHandler" + " https://gitee" + ".com/openharmony" + "-sig/napi_generator"; + + private JPanel contentPane; + private JButton buttonOK; + private JButton buttonHelp; + private JTextArea textAreaError; + private String errorMessage; + + public ErrorDialog(String sErrorMessage) { + errorMessage = sErrorMessage; + } + + /** + * 初始化 + */ + public void initDialog() { + setContentPane(contentPane); + setModal(true); + getRootPane().setDefaultButton(buttonOK); + setTitle("执行失败"); + textAreaError.setText(errorMessage); + buttonOK.addActionListener(actionEvent -> onOK()); + + buttonHelp.addActionListener(actionEvent -> onCancel()); + + // call onCancel() when cross is clicked + setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); + addWindowListener(new WindowAdapter() { + /** + * close dialog + * @param windowEvent WindowEvent + */ + @Override + public void windowClosing(WindowEvent windowEvent) { + onCancel(); + } + }); + + // call onCancel() on ESCAPE + contentPane.registerKeyboardAction(actionEvent -> onCancel(), + KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), + JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + } + + private void onOK() { + dispose(); + } + + JPanel getContentPanel() { + return contentPane; + } + + private void onCancel() { + try { + Runtime.getRuntime().exec(URL); + } catch (IOException ioException) { + LOG.error("exec command help error" + ioException); + } + dispose(); + } +} \ No newline at end of file diff --git a/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialog.java b/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialog.java new file mode 100644 index 00000000..ec22b302 --- /dev/null +++ b/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialog.java @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2022 Guangzhou Digitalchina Information Technology Co., Ltd. + * All rights reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sk.service.dialog; + +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.DialogWrapper; +import com.intellij.openapi.ui.ValidationInfo; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import javax.swing.Action; +import javax.swing.JComponent; +import java.awt.Desktop; +import java.awt.event.ActionEvent; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; + +/** + * 主界面对话框Wrapper + * + * @author: zhaoxudong@kaihong.com + * @see: tool conversion plug-in + * @version: v1.0.0 + * @since 2022-05-27 + */ +public class ServiceGenerateDialog extends DialogWrapper { + private static final Logger LOG = Logger.getInstance(ServiceGenerateDialog.class); + private static final String TITLE = "Generate Service Frame"; + private static final String URL = "https://gitee.com/openharmony/napi_generator/tree/master/hdc/service-gen"; + + private final ServiceGenerateDialogPane genDiag; + + /** + * 构造函数 + * + * @param project projectId + * @param filePath 目录文件 + * @param dirPath 文件夹目录 + */ + public ServiceGenerateDialog(Project project, String filePath, String dirPath) { + super(true); + this.setResizable(false); + setTitle(TITLE); + setModal(true); + genDiag = new ServiceGenerateDialogPane(project, filePath, dirPath); + init(); + } + + /** + * 创建视图 + * + * @return 组件内容 + */ + @Nullable + @Override + protected JComponent createCenterPanel() { + return genDiag.getContentPanel(); + } + + /** + * 校验数据 + * + * @return 检测文本框架是否有目录。 + */ + @Nullable + @Override + protected ValidationInfo doValidate() { + return genDiag.validationInfo(); + } + + /** + * ok/cancel按钮 + * + * @return Action[] buttos list + */ + @NotNull + @Override + protected Action[] createActions() { + DialogWrapperExitAction exitAction = new DialogWrapperExitAction("Cancel", CANCEL_EXIT_CODE); + CustomOKAction okAction = new CustomOKAction(); + // 设置默认的焦点按钮 + okAction.putValue(DialogWrapper.DEFAULT_ACTION, true); + return new Action[]{exitAction, okAction}; + } + + @NotNull + @Override + protected Action[] createLeftSideActions() { + CustomHelpAction helpAction = new CustomHelpAction(); + return new Action[]{helpAction}; + } + + /** + * 自定义 ok Action + */ + protected class CustomOKAction extends DialogWrapperAction { + + protected CustomOKAction() { + super("OK"); + } + + @Override + protected void doAction(ActionEvent actionEvent) { + ValidationInfo validationInfo = doValidate(); + if (validationInfo != null) { + LOG.info(validationInfo.message); + } else { + if (genDiag.runServiceFun()) { + close(CANCEL_EXIT_CODE); + } + } + } + } + + /** + * 自定义 help Action + */ + protected class CustomHelpAction extends DialogWrapperAction { + + protected CustomHelpAction() { + super("Help"); + } + + @Override + protected void doAction(ActionEvent actionEvent) { + try { + Desktop.getDesktop().browse(new URI(URL)); + } catch (URISyntaxException | IOException e) { + LOG.error("Open help error:" + e); + } + } + } +} diff --git a/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialogPane.form b/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialogPane.form new file mode 100644 index 00000000..987dedf1 --- /dev/null +++ b/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialogPane.form @@ -0,0 +1,119 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialogPane.java b/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialogPane.java new file mode 100644 index 00000000..e4b7bd98 --- /dev/null +++ b/hdc/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialogPane.java @@ -0,0 +1,392 @@ +/* + * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sk.service.dialog; + +import com.intellij.notification.NotificationType; +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.ui.ValidationInfo; +import com.sk.service.action.BrowseAction; +import com.sk.service.action.OutPathSelectAction; +import com.sk.service.utils.FileUtil; +import com.sk.service.utils.GenNotification; +import org.apache.http.util.TextUtils; +import org.jetbrains.annotations.Nullable; + +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JDialog; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.KeyStroke; +import java.awt.event.KeyEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.awt.event.WindowListener; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +/** + * 生成工具主界面 + * + * @author: zhaoxudong@kaihong.com + * @see: select generate dialog + * @version: v1.0.0 + * @since 2022-02-21 + */ +public class ServiceGenerateDialogPane extends JDialog { + private static final Logger LOG = Logger.getInstance(ServiceGenerateDialogPane.class); + + private final Project project; + + private JPanel contentPane; + private JTextField textFieldH; + private JTextField textFieldOutPath; + private JButton buttonOutPath; + private JButton buttonSelectH; + private boolean generateSuccess = true; + private String sErrorMessage = ""; + private String dirPath; + + /** + * 构造函数 + * + * @param project projectId + * @param filePath .h文件 + * @param dirPath 生成框架文件路径 + */ + public ServiceGenerateDialogPane(Project project, String filePath, String dirPath) { + this.project = project; + this.dirPath = dirPath; + textFieldH.setText(filePath); + textFieldOutPath.setText(dirPath); + contentPane.registerKeyboardAction(actionEvent -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), + JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + buttonOutPath.addActionListener(new OutPathSelectAction(buttonOutPath, textFieldOutPath)); + buttonSelectH.addActionListener(new BrowseAction(project, buttonSelectH, textFieldH, textFieldOutPath)); + } + + @Override + public synchronized void addWindowListener(WindowListener windowListener) { + super.addWindowListener(windowListener); + new WindowAdapter() { + /** + * close dialog + * + * @param windowEvent WindowEvent + */ + @Override + public void windowClosing(WindowEvent windowEvent) { + onCancel(); + } + }; + } + + /** + * 验证文本选择框是否空。是否替换已存在的内容 + * + * @return ValidationInfo 返回不符要求的信息。 + */ + @Nullable + public ValidationInfo validationInfo() { + String fileH = textFieldH.getText(); + String outDir = textFieldOutPath.getText(); + boolean isEmptyFile = TextUtils.isEmpty(fileH) || TextUtils.isEmpty(outDir); + ValidationInfo validationInfo = null; + if (isEmptyFile) { + String warnMsg = ".h文件、输出路径不能为空"; + warningMessage(warnMsg); + validationInfo = new ValidationInfo(warnMsg); + return validationInfo; + } + + File file = new File(textFieldOutPath.getText() + "/examservice"); + if (file.exists()) { + ConfirmDialog confirmDialog = new ConfirmDialog("是否替换已存在的结果?"); + if (!confirmDialog.showAndGet()) { + validationInfo = new ValidationInfo(String.format("不替换现有结果:%s", file)); + return validationInfo; + } + } + return validationInfo; + } + + private void onCancel() { + dispose(); + } + + private void warningMessage(String title) { + String notiContent = "请选择.h文件,输出结果路径"; + GenNotification.notifyMessage(this.project, notiContent, title, NotificationType.WARNING); + } + + /** + * 执行主程序入口 + * + * @return 执行状态 + */ + public boolean runServiceFun() { + GenNotification.notifyMessage(this.project, "", "正在生成", NotificationType.INFORMATION); + String command; + command = genCommandService(); + copyServiceHeader(); + try { + if (!TextUtils.isEmpty(command) && callExtProcess(command)) { + GenNotification.notifyMessage(project, "执行成功", "提示", NotificationType.INFORMATION); + return true; + } + } catch (IOException | InterruptedException ex) { + GenNotification.notifyMessage(project, textFieldH.getText(), "Command exec error", NotificationType.ERROR); + LOG.error(ex); + } + return false; + } + + /** + * 生成命令行指令 + * + * @return 返回命令行执行内容 + */ + private String genCommandService() { + String sysName = System.getProperties().getProperty("os.name").toUpperCase(); + String tmpDirFile = System.getProperty("java.io.tmpdir"); + String execFn; + if (sysName.contains("WIN")) { + execFn = "cmds/win/service-gen-win.exe"; + tmpDirFile += "service-gen-win.exe"; + } else if (sysName.contains("LINUX")) { + execFn = "cmds/linux/service-gen-linux"; + tmpDirFile += "service-gen-linux"; + } else { + execFn = "cmds/mac/service-gen-macos"; + tmpDirFile += "service-gen-macos"; + } + File file = new File(tmpDirFile); + if (!file.exists()) { + try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(execFn)) { + if (inputStream == null) { + throw new IOException("exec File InputStream is Null"); + } + byte[] bs = inputStream.readAllBytes(); + writeTmpFile(tmpDirFile, bs); + 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 hFile = textFieldH.getText(); + String outPath = textFieldOutPath.getText(); + command += " -f " + hFile + " -o " + outPath + " -s 9001"; + return command; + } + + /** + * 生成命令行指令 + * + * @return 返回命令行执行内容 + */ + private void copyServiceHeader() { + String sysName = System.getProperties().getProperty("os.name").toUpperCase(); + String tmpDirFile = System.getProperty("java.io.tmpdir"); + String execFn; + if (sysName.contains("WIN")) { + execFn = "cmds/win/header_parser.exe"; + tmpDirFile += "header_parser.exe"; + } else if (sysName.contains("LINUX")) { + execFn = "cmds/linux/service-gen-linux"; + tmpDirFile += "service-gen-linux"; + } else { + execFn = "cmds/mac/service-gen-macos"; + tmpDirFile += "service-gen-macos"; + } + try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(execFn)) { + if (inputStream == null) { + throw new IOException("exec File InputStream is Null"); + } + byte[] bs = inputStream.readAllBytes(); + writeTmpFile(tmpDirFile, bs); + 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); + } + } + + private boolean callExtProcess(String command) throws IOException, InterruptedException { + + if (TextUtils.isEmpty(command)) { + GenNotification.notifyMessage(this.project, "执行命令文件为空", "空命令行提示", NotificationType.ERROR); + return false; + } + Process process = Runtime.getRuntime().exec(command); + genResultLog(process); + StreamConsumer errConsumer = new StreamConsumer(process.getErrorStream()); + StreamConsumer outputConsumer = new StreamConsumer(process.getInputStream()); + errConsumer.start(); + outputConsumer.start(); + if (generateSuccess) { + GenNotification.notifyMessage(project, "执行成功", "提示", NotificationType.INFORMATION); + } else { + GenNotification.notifyMessage(project, sErrorMessage, "提示", NotificationType.ERROR); + return false; + } + errConsumer.join(); + outputConsumer.join(); + return true; + } + + /** + * 赋值可执行文件权限。 + * + * @param execFn 可执行命令 + * @throws IOException 打开文件异常 + * @throws InterruptedException 中断异常 + */ + private void executable(String execFn) throws IOException, InterruptedException { + callExtProcess("chmod a+x " + execFn); + } + + /** + * 拷贝可执行文件到临时文件夹 + * + * @param path 目标文件路径 + * @param bs 字节内容 + * @throws IOException exception + */ + private void writeTmpFile(String path, byte[] bs) throws IOException { + File file = new File(path); + if (!file.exists()) { + boolean isNewFile = file.createNewFile(); + if (!isNewFile) { + LOG.info("writeTmpFile createNewFile error"); + } + } + FileOutputStream fw = new FileOutputStream(file); + fw.write(bs, 0, bs.length); + fw.close(); + } + + /** + * 获取生成成功结果文件。 + * + * @param process 进程ID + */ + private void genResultLog(Process process) { + BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); + BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); + String sErr; + String sOut; + sErr = getErrorResult(stdError); + if (TextUtils.isEmpty(sErr)) { + sOut = genInputLog(stdInput); + if (!generateIsSuccess(sOut)) { + sErrorMessage = sOut; + } + return; + } + generateSuccess = false; + sErrorMessage = sErr; + } + + /** + * 获取生成失败结果文件。 + * + * @param stdError error buff + * @return ErrorResult + */ + private String getErrorResult(BufferedReader stdError) { + StringBuilder sErr = new StringBuilder(); + while (true) { + String sTmp; + try { + if ((sTmp = stdError.readLine()) == null) { + break; + } + sErr.append(sTmp).append(FileUtil.getNewline()); + } catch (IOException ioException) { + LOG.error(" genResultLog stdInput error" + ioException); + } + } + return sErr.toString(); + } + + private boolean generateIsSuccess(String sOut) { + generateSuccess = sOut.contains("success") || TextUtils.isEmpty(sOut); + return generateSuccess; + } + + /** + * 获取生成文本内容。 + * + * @param stdInput input buff + * @return 返回当前输入框内容 + */ + private String genInputLog(BufferedReader stdInput) { + StringBuilder sOut = new StringBuilder(); + while (true) { + String sTmp; + try { + if ((sTmp = stdInput.readLine()) == null) { + break; + } + sOut.append(sTmp).append(FileUtil.getNewline()); + } catch (IOException ioException) { + LOG.error(" genResultLog stdInput error" + ioException); + } + } + return sOut.toString(); + } + + static class StreamConsumer extends Thread { + InputStream is; + + StreamConsumer(InputStream is) { + super.setName("StreamConsumer"); + this.is = is; + } + + @Override + public void run() { + try { + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + String line; + while ((line = br.readLine()) != null) { + LOG.error("StreamConsumer" + line); + } + } catch (IOException ioException) { + LOG.error("StreamConsumer io error" + ioException); + } + } + } + + JPanel getContentPanel() { + return contentPane; + } +} diff --git a/hdc/service_IntelliJ_plugin/src/com/sk/service/ng/GenMenuService.java b/hdc/service_IntelliJ_plugin/src/com/sk/service/ng/GenMenuService.java new file mode 100644 index 00000000..9d1fa0e5 --- /dev/null +++ b/hdc/service_IntelliJ_plugin/src/com/sk/service/ng/GenMenuService.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2022 Guangzhou Digitalchina Information Technology Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sk.service.ng; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.project.Project; +import com.sk.service.dialog.ServiceGenerateDialog; +import org.jetbrains.annotations.NotNull; + +/** + * 工具菜单入口 + * + * @author: zhaoxudong@kaihong.com + * @see: tool conversion plug-in + * @version: v1.0.0 + * @since 2022-05-27 + */ +public class GenMenuService extends AnAction { + + @Override + public void actionPerformed(@NotNull AnActionEvent anActionEvent) { + Project project = anActionEvent.getProject(); + + if (project == null) { + return; + } + ServiceGenerateDialog wrapper = new ServiceGenerateDialog(project, "", ""); + wrapper.showAndGet(); + } +} diff --git a/hdc/service_IntelliJ_plugin/src/com/sk/service/ng/GenService.java b/hdc/service_IntelliJ_plugin/src/com/sk/service/ng/GenService.java new file mode 100644 index 00000000..b5cce8a1 --- /dev/null +++ b/hdc/service_IntelliJ_plugin/src/com/sk/service/ng/GenService.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sk.service.ng; + +import com.intellij.notification.NotificationType; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.PlatformDataKeys; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VirtualFile; +import com.sk.service.dialog.ServiceGenerateDialog; +import com.sk.service.utils.FileUtil; +import com.sk.service.utils.GenNotification; + +/** + * 项目文件入口 + * + * @author: zhaoxudong@kaihong.com + * @see: tool conversion plug-in + * @version: v1.0.0 + * @since 2022-02-21 + */ +public class GenService extends AnAction { + + @Override + public void actionPerformed(AnActionEvent anActionEvent) { + Project project = anActionEvent.getProject(); + // 获取需要处理的.h文件绝对路径 + VirtualFile file = anActionEvent.getData(PlatformDataKeys.VIRTUAL_FILE); + if (file == null) { + GenNotification.notifyMessage(project, "", "file is not exist", NotificationType.ERROR); + return; + } + if (project == null) { + return; + } + String filePath = file.getPath(); + String dirPath = file.getParent().getPath(); + ServiceGenerateDialog wrapper = new ServiceGenerateDialog(project, filePath, dirPath); + wrapper.showAndGet(); + } + + + @Override + public void update(AnActionEvent event) { + // 根据所选文件名,判断是否显示生成菜单项 + VirtualFile file = event.getData(PlatformDataKeys.VIRTUAL_FILE); + if (file == null) { + event.getPresentation().setEnabledAndVisible(false); + } else { + event.getPresentation().setEnabledAndVisible(FileUtil.patternFileName(file.getName())); + } + } +} diff --git a/hdc/service_IntelliJ_plugin/src/com/sk/service/utils/FileUtil.java b/hdc/service_IntelliJ_plugin/src/com/sk/service/utils/FileUtil.java new file mode 100644 index 00000000..0ae6ce4c --- /dev/null +++ b/hdc/service_IntelliJ_plugin/src/com/sk/service/utils/FileUtil.java @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sk.service.utils; + +import com.intellij.notification.NotificationType; +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.project.Project; +import org.apache.http.util.TextUtils; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Properties; +import java.util.regex.Pattern; + +/** + * 文本文件工具 + * + * @author: zhaoxudong@kaihong.com + * @see: file utils + * @version: v1.0.0 + * @since 2022-02-21 + */ +public class FileUtil { + private static final Logger LOG = Logger.getInstance(FileUtil.class); + + private static final int COMPILE_SDK_VERSION = 5; + + /** + * 获取换行符 + * + * @return 换行符 + */ + public static String getNewline() { + return System.getProperty("line.separator"); + } + + /** + * 正则匹配所选文件名是否符合规范 + * + * @param fileName 文件名 + * @return boolean 是否匹配 + */ + public static boolean patternFileName(String fileName) { + String pattern = "([.a-z_A-Z0-9]+).h"; + return Pattern.matches(pattern, fileName); + } + + /** + * check project SDK + * + * @param project projectid + * @param baseFile project root file + * @return boolean + */ + public static boolean checkProjectSDK(Project project, String baseFile) { + + String gradlePath = ""; + 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(); + } + } + } + + Properties properties = new Properties(); + if (TextUtils.isBlank(gradlePath)) { + GenNotification.notifyMessage(project, "项目结构中没有grandle配置文件。", "当前项目结构不支持", + NotificationType.WARNING); + 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 true; + } + String ohosSDK = properties.getProperty("compileSdkVersion"); + + if (ohosSDK != null && Integer.parseInt(ohosSDK) < COMPILE_SDK_VERSION) { + GenNotification.notifyMessage(project, "SKD版本过低,NAPI仅支持5.0及以上版本", "提示", + NotificationType.WARNING); + return true; + } + return false; + } +} diff --git a/hdc/service_IntelliJ_plugin/src/com/sk/service/utils/GenNotification.java b/hdc/service_IntelliJ_plugin/src/com/sk/service/utils/GenNotification.java new file mode 100644 index 00000000..3f15e4d4 --- /dev/null +++ b/hdc/service_IntelliJ_plugin/src/com/sk/service/utils/GenNotification.java @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2022 Guangzhou Digitalchina Information Technology Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.sk.service.utils; + +import com.intellij.ide.actions.OpenFileAction; +import com.intellij.notification.Notification; +import com.intellij.notification.NotificationAction; +import com.intellij.notification.NotificationType; +import com.intellij.notification.Notifications; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.project.Project; +import org.jetbrains.annotations.NotNull; + +import java.io.File; + +/** + * 通知框 + * + * @author: zhaoxudong@kaihong.com + * @see: tool conversion plug-in + * @version: v1.0.0 + * @since 2022-05-27 + */ +public class GenNotification { + + private static final Logger LOG = Logger.getInstance(FileUtil.class); + + private GenNotification() { + } + + /** + * 无action 通知 + * + * @param project projectid + * @param content 提示内容 + * @param title 提示栏内容 + * @param type 提示类型 Error,Waring,info + */ + public static void notifyMessage(@javax.annotation.Nullable Project project, String content, String title, + NotificationType type) { + notifyMessage(project, content, title, type, false); + } + + /** + * 消息通知 + * + * @param project projectid + * @param content 提示内容 + * @param title 提示栏内容 + * @param type 提示类型 Error,Waring,info + * @param addAct 是否添加action + */ + public static void notifyMessage(@javax.annotation.Nullable Project project, String content, String title, + NotificationType type, boolean addAct) { + + Notification notification = new Notification("Generate.Result.Group", "Notes Message", content, type); + notification.setTitle(title); + notification.setContent(content); + + if (NotificationType.ERROR.equals(type)) { + LOG.error(content); + } else if (NotificationType.WARNING.equals(type)) { + LOG.warn(content); + } else { + LOG.info(content); + } + + if (addAct) { + notification.setContent(null); + addAction(project, content, notification); + } + Notifications.Bus.notify(notification, project); + + } + + private static void addAction(Project project, String dirPath, Notification notification) { + File genResultPath = new File(dirPath); + if (!genResultPath.exists()) { + LOG.info(String.format("%s not exist", genResultPath.getPath())); + } + LOG.info("generated file list log:"); + + File[] fa = genResultPath.listFiles(); + for (int i = 0; i < fa.length; i++) { + File fs = fa[i]; + String fileName = fs.getName(); + boolean dissFile = !fileName.endsWith(".log") || !fileName.endsWith(".txt") || !fileName.endsWith(".ts"); + if (!fs.isDirectory() && dissFile) { + String filePath = fs.getPath(); + NotificationAction action = new NotificationAction(filePath) { + @Override + public void actionPerformed(@NotNull AnActionEvent anActionEvent, + @NotNull Notification notification) { + OpenFileAction.openFile(filePath, project); + } + }; + notification.addAction(action); + } else { + LOG.info(String.format("%s is Directory", fs.getPath())); + } + } + } +} -- Gitee