From f2c82d92aa5e11da1e94fd9616ee078f619dbc71 Mon Sep 17 00:00:00 2001 From: lengege Date: Thu, 16 Jun 2022 11:01:49 +0800 Subject: [PATCH] Decoupling IntelliJ plugin Signed-off-by: lengege --- OAT.xml | 3 +- napi_IntelliJ_plugin/generator/README_zh.md | 42 ++ .../generator/resources/META-INF/plugin.xml | 59 +++ .../cmds/linux/INTELLIJ_README_LINUX_zh.md | 4 + .../cmds/mac/INTELLIJ_README_MAC_zh.md | 4 + .../cmds/win/INTELLIJ_README_WIN_zh.md | 4 + .../generator/resources/images/openDisk.png | Bin 0 -> 176 bytes .../generator/resources/images/text.png | Bin 0 -> 122 bytes .../src/com/sk/action/BrowseAction.java | 132 ++++++ .../src/com/sk/action/GenAction.java | 53 +++ .../src/com/sk/action/ScriptAction.java | 53 +++ .../src/com/sk/dialog/ConfirmDiagPane.form | 61 +++ .../src/com/sk/dialog/ConfirmDiagPane.java | 50 ++ .../src/com/sk/dialog/ConfirmDialog.java | 53 +++ .../src/com/sk/dialog/ErrorDialog.form | 72 +++ .../src/com/sk/dialog/ErrorDialog.java | 97 ++++ .../src/com/sk/dialog/GenResultDialog.form | 64 +++ .../src/com/sk/dialog/GenResultDialog.java | 82 ++++ .../src/com/sk/dialog/GenerateDialog.java | 152 ++++++ .../src/com/sk/dialog/GenerateDialogPane.form | 160 +++++++ .../src/com/sk/dialog/GenerateDialogPane.java | 447 ++++++++++++++++++ .../generator/src/com/sk/ng/GenDTS.java | 73 +++ .../generator/src/com/sk/ng/GenMenuDTS.java | 51 ++ .../generator/src/com/sk/utils/FileUtil.java | 187 ++++++++ .../src/com/sk/utils/GenNotification.java | 125 +++++ 25 files changed, 2027 insertions(+), 1 deletion(-) create mode 100644 napi_IntelliJ_plugin/generator/README_zh.md create mode 100644 napi_IntelliJ_plugin/generator/resources/META-INF/plugin.xml create mode 100644 napi_IntelliJ_plugin/generator/resources/cmds/linux/INTELLIJ_README_LINUX_zh.md create mode 100644 napi_IntelliJ_plugin/generator/resources/cmds/mac/INTELLIJ_README_MAC_zh.md create mode 100644 napi_IntelliJ_plugin/generator/resources/cmds/win/INTELLIJ_README_WIN_zh.md create mode 100644 napi_IntelliJ_plugin/generator/resources/images/openDisk.png create mode 100644 napi_IntelliJ_plugin/generator/resources/images/text.png create mode 100644 napi_IntelliJ_plugin/generator/src/com/sk/action/BrowseAction.java create mode 100644 napi_IntelliJ_plugin/generator/src/com/sk/action/GenAction.java create mode 100644 napi_IntelliJ_plugin/generator/src/com/sk/action/ScriptAction.java create mode 100644 napi_IntelliJ_plugin/generator/src/com/sk/dialog/ConfirmDiagPane.form create mode 100644 napi_IntelliJ_plugin/generator/src/com/sk/dialog/ConfirmDiagPane.java create mode 100644 napi_IntelliJ_plugin/generator/src/com/sk/dialog/ConfirmDialog.java create mode 100644 napi_IntelliJ_plugin/generator/src/com/sk/dialog/ErrorDialog.form create mode 100644 napi_IntelliJ_plugin/generator/src/com/sk/dialog/ErrorDialog.java create mode 100644 napi_IntelliJ_plugin/generator/src/com/sk/dialog/GenResultDialog.form create mode 100644 napi_IntelliJ_plugin/generator/src/com/sk/dialog/GenResultDialog.java create mode 100644 napi_IntelliJ_plugin/generator/src/com/sk/dialog/GenerateDialog.java create mode 100644 napi_IntelliJ_plugin/generator/src/com/sk/dialog/GenerateDialogPane.form create mode 100644 napi_IntelliJ_plugin/generator/src/com/sk/dialog/GenerateDialogPane.java create mode 100644 napi_IntelliJ_plugin/generator/src/com/sk/ng/GenDTS.java create mode 100644 napi_IntelliJ_plugin/generator/src/com/sk/ng/GenMenuDTS.java create mode 100644 napi_IntelliJ_plugin/generator/src/com/sk/utils/FileUtil.java create mode 100644 napi_IntelliJ_plugin/generator/src/com/sk/utils/GenNotification.java diff --git a/OAT.xml b/OAT.xml index df2e5ab5..e190b51e 100644 --- a/OAT.xml +++ b/OAT.xml @@ -64,9 +64,10 @@ Note:If the text contains special characters, please escape them according to th + - + diff --git a/napi_IntelliJ_plugin/generator/README_zh.md b/napi_IntelliJ_plugin/generator/README_zh.md new file mode 100644 index 00000000..a3202496 --- /dev/null +++ b/napi_IntelliJ_plugin/generator/README_zh.md @@ -0,0 +1,42 @@ +# IDEA插件开发环境配置 + +基础环境要求: +JDK 11 ,IDEA Community 2021.3.3 + +1.下载IDEA Community 与 JDK11 配置好环境 +点击 https://www.jetbrains.com/idea/download/ 下载Community版本,并完成安装。 + + +2.打开IDEA Community 应用程序。 +依次点击项目File>Open 选择napi_generator/src/generator项目文件夹。 +![](../../figures/IntelliJ_env_config_open_proj.png) + +3.项目配置 +项目打开完成,点击File>Project Structure +![](../../figures/IntelliJ_env_proj_structure.png) + + +4.配置Modules. +Project Settings > Modules 新建Modules.点击上方“-”删除原有的Modules,“+”选择 New Modules。 +![](../../figures/IntelliJ_env_Proj_Module.png) + + +5.配置Module SDK. +在New Modules对话框中,选择IntelliJ Platform Plugin。若为首次环境配置,请在Module SDK 下拉框中点击 Add IntelliJ Platform Plugin SDK 选择IDEA Community安装目录,点击OK,在Select Internal Java Platform 选择 JAVA SDK 11(213版本只支持 11) +![](../../figures/IntelliJ_env_Proj_Module_New.png) + +6.配置Root Content. +在上图界面点击Next,选择Content root:为napi_generator/src/generator文件夹,module name会自动变为generator,若出现提示已存在是否覆盖的提示,请点“是”完成配置。 +![](../../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) \ No newline at end of file diff --git a/napi_IntelliJ_plugin/generator/resources/META-INF/plugin.xml b/napi_IntelliJ_plugin/generator/resources/META-INF/plugin.xml new file mode 100644 index 00000000..031258e0 --- /dev/null +++ b/napi_IntelliJ_plugin/generator/resources/META-INF/plugin.xml @@ -0,0 +1,59 @@ + + com.sk.ng + Napi Generator + 1.0 + YourCompany + + Introduction +

    One-click generation of NAPI framework code, business code framework, GN file, etc. according to the ts (typescript) interface file in the user-specified path.

+

    When developing the interface between JS applications and NAPI, the developers of the underlying framework do not need to pay attention to the upper-level application conversion logic such as Nodejs syntax, data type conversion between C++ and JS, and only focus on the underlying business logic. Professional people do professional things. Thus, the development efficiency can be greatly improved

+ +

Sources on Gitee

+ + + + + + + + + ]]> +
+ + Features + ]]> + + + + + + + com.intellij.modules.platform + + + + + + + + + + + + + + + + + + + + + + + + +
\ No newline at end of file diff --git a/napi_IntelliJ_plugin/generator/resources/cmds/linux/INTELLIJ_README_LINUX_zh.md b/napi_IntelliJ_plugin/generator/resources/cmds/linux/INTELLIJ_README_LINUX_zh.md new file mode 100644 index 00000000..fbc3760d --- /dev/null +++ b/napi_IntelliJ_plugin/generator/resources/cmds/linux/INTELLIJ_README_LINUX_zh.md @@ -0,0 +1,4 @@ +# 目录说明 + 此目录用于存放napi_generator-linux可执行文件 + +[可执行文件下载](http://ftpkaihongdigi.i234.me:5000/fsdownload/PPVcNMgVv/2022-06-13) \ No newline at end of file diff --git a/napi_IntelliJ_plugin/generator/resources/cmds/mac/INTELLIJ_README_MAC_zh.md b/napi_IntelliJ_plugin/generator/resources/cmds/mac/INTELLIJ_README_MAC_zh.md new file mode 100644 index 00000000..387afad4 --- /dev/null +++ b/napi_IntelliJ_plugin/generator/resources/cmds/mac/INTELLIJ_README_MAC_zh.md @@ -0,0 +1,4 @@ +# 目录说明 + 此目录用于存放napi_generator-macos可执行文件 + +[可执行文件下载](http://ftpkaihongdigi.i234.me:5000/fsdownload/PPVcNMgVv/2022-06-13) \ No newline at end of file diff --git a/napi_IntelliJ_plugin/generator/resources/cmds/win/INTELLIJ_README_WIN_zh.md b/napi_IntelliJ_plugin/generator/resources/cmds/win/INTELLIJ_README_WIN_zh.md new file mode 100644 index 00000000..6985f40f --- /dev/null +++ b/napi_IntelliJ_plugin/generator/resources/cmds/win/INTELLIJ_README_WIN_zh.md @@ -0,0 +1,4 @@ +# 目录说明 + 此目录用于存放napi_generator-win.exe可执行文件 + +[可执行文件下载](http://ftpkaihongdigi.i234.me:5000/fsdownload/PPVcNMgVv/2022-06-13) \ No newline at end of file diff --git a/napi_IntelliJ_plugin/generator/resources/images/openDisk.png b/napi_IntelliJ_plugin/generator/resources/images/openDisk.png new file mode 100644 index 0000000000000000000000000000000000000000..5e66baa62e361484a32069b4277e269bcdce722f GIT binary patch literal 176 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`<(@8%Ar-fh6C~soEUZpqW_$NO ze8HK9b)KA4BpnzoA8Yt^Pc!QYd(!zMTiWKyO>CRzS?aQzMa=I)KzP=RnKBNO!V?Tb z@0)6_Te#l8L+AEEy%#ey!tdKEc4eGZ=-#!=OM!>Ms#Rq<^WHA04u|IxQmilRZdR9P aWO&}dG-q+zo@+oSFnGH9xvX +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/napi_IntelliJ_plugin/generator/src/com/sk/dialog/ConfirmDiagPane.java b/napi_IntelliJ_plugin/generator/src/com/sk/dialog/ConfirmDiagPane.java new file mode 100644 index 00000000..62cc25fe --- /dev/null +++ b/napi_IntelliJ_plugin/generator/src/com/sk/dialog/ConfirmDiagPane.java @@ -0,0 +1,50 @@ +/* + * 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.dialog; + +import javax.swing.JPanel; +import javax.swing.JLabel; +import javax.swing.JDialog; +import javax.swing.ImageIcon; + +/** + * ConfirmDiagPane自定义确认对话框 + * 解决ShowConfirmDiag 在Deveco里面会出现界面错位问题。 + * + * @author: xudong + * @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/napi_IntelliJ_plugin/generator/src/com/sk/dialog/ConfirmDialog.java b/napi_IntelliJ_plugin/generator/src/com/sk/dialog/ConfirmDialog.java new file mode 100644 index 00000000..7bcf2479 --- /dev/null +++ b/napi_IntelliJ_plugin/generator/src/com/sk/dialog/ConfirmDialog.java @@ -0,0 +1,53 @@ +/* + * 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.dialog; + +import com.intellij.openapi.ui.DialogWrapper; +import org.jetbrains.annotations.Nullable; +import javax.swing.JComponent; + +/** + * 自定义确认对话框Wrapper + * + * @author: liulongc digitalchina.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/napi_IntelliJ_plugin/generator/src/com/sk/dialog/ErrorDialog.form b/napi_IntelliJ_plugin/generator/src/com/sk/dialog/ErrorDialog.form new file mode 100644 index 00000000..ae897a57 --- /dev/null +++ b/napi_IntelliJ_plugin/generator/src/com/sk/dialog/ErrorDialog.form @@ -0,0 +1,72 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/napi_IntelliJ_plugin/generator/src/com/sk/dialog/ErrorDialog.java b/napi_IntelliJ_plugin/generator/src/com/sk/dialog/ErrorDialog.java new file mode 100644 index 00000000..263cc10f --- /dev/null +++ b/napi_IntelliJ_plugin/generator/src/com/sk/dialog/ErrorDialog.java @@ -0,0 +1,97 @@ +/* + * 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.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: xudong + * @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(); + } + + 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/napi_IntelliJ_plugin/generator/src/com/sk/dialog/GenResultDialog.form b/napi_IntelliJ_plugin/generator/src/com/sk/dialog/GenResultDialog.form new file mode 100644 index 00000000..b6e02973 --- /dev/null +++ b/napi_IntelliJ_plugin/generator/src/com/sk/dialog/GenResultDialog.form @@ -0,0 +1,64 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/napi_IntelliJ_plugin/generator/src/com/sk/dialog/GenResultDialog.java b/napi_IntelliJ_plugin/generator/src/com/sk/dialog/GenResultDialog.java new file mode 100644 index 00000000..bb0b1617 --- /dev/null +++ b/napi_IntelliJ_plugin/generator/src/com/sk/dialog/GenResultDialog.java @@ -0,0 +1,82 @@ +/* + * 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.dialog; + +import com.intellij.openapi.diagnostic.Logger; + +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JList; +import javax.swing.JPanel; +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +/** + * GenResultDialog结果生成框 + * + * @author: xudong + * @see: generate success dialog + * @version: v1.0.0 + * @since 2022-02-21 + */ +public class GenResultDialog extends JDialog { + private static final Logger LOG = Logger.getInstance(GenResultDialog.class); + + private JPanel contentPane; + private JButton buttonOK; + private JList resultList; + private String path; + + public GenResultDialog(String directoryPath) { + path = directoryPath; + } + + /** + * 初始化 + */ + public void initResultDialog() { + setContentPane(contentPane); + setModal(true); + getRootPane().setDefaultButton(buttonOK); + setTitle("执行成功"); + buttonOK.addActionListener(actionEvent -> onOK()); + List fileList = getDirFileName(path); + resultList.setListData(fileList.toArray(new String[fileList.size()])); + } + + private void onOK() { + dispose(); + } + + private List getDirFileName(String path) { + List files = new ArrayList<>(); + File file = new File(path); + if (!file.exists()) { + LOG.info("getDirFileName f not exist"); + return files; + } + File[] fileArray = file.listFiles(); + for (int i = 0; i < fileArray.length; i++) { + File fs = fileArray[i]; + if (!fs.isDirectory()) { + files.add(fs.getPath()); + } else { + LOG.info("getDirFileName this file is dir"); + } + } + return files; + } +} diff --git a/napi_IntelliJ_plugin/generator/src/com/sk/dialog/GenerateDialog.java b/napi_IntelliJ_plugin/generator/src/com/sk/dialog/GenerateDialog.java new file mode 100644 index 00000000..0a3cfdd5 --- /dev/null +++ b/napi_IntelliJ_plugin/generator/src/com/sk/dialog/GenerateDialog.java @@ -0,0 +1,152 @@ +/* + * 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.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.JComponent; +import javax.swing.Action; +import java.awt.Desktop; +import java.awt.event.ActionEvent; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; + +/** + * 主界面对话框Wrapper + * + * @author: liulongc digitalchina.com + * @see: tool conversion plug-in + * @version: v1.0.0 + * @since 2022-05-27 + */ +public class GenerateDialog extends DialogWrapper { + private static final Logger LOG = Logger.getInstance(GenerateDialog.class); + private static final String TITLE = "Generate Napi Frame"; + private static final String URL = "https://gitee.com/openharmony/napi_generator"; + + private final GenerateDialogPane genDiag; + + /** + * 构造函数 + * + * @param project projectid + * @param destPath 目录文件 + * @param directoryPath 文件夹目录 + * @param fileName 文件名 + */ + public GenerateDialog(Project project, String destPath, String directoryPath, String fileName) { + super(true); + this.setResizable(false); + setTitle(TITLE); + setModal(true); + genDiag = new GenerateDialogPane(project, destPath, directoryPath, fileName); + 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.runFun()) { + 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/napi_IntelliJ_plugin/generator/src/com/sk/dialog/GenerateDialogPane.form b/napi_IntelliJ_plugin/generator/src/com/sk/dialog/GenerateDialogPane.form new file mode 100644 index 00000000..f846b03b --- /dev/null +++ b/napi_IntelliJ_plugin/generator/src/com/sk/dialog/GenerateDialogPane.form @@ -0,0 +1,160 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/napi_IntelliJ_plugin/generator/src/com/sk/dialog/GenerateDialogPane.java b/napi_IntelliJ_plugin/generator/src/com/sk/dialog/GenerateDialogPane.java new file mode 100644 index 00000000..08ec063d --- /dev/null +++ b/napi_IntelliJ_plugin/generator/src/com/sk/dialog/GenerateDialogPane.java @@ -0,0 +1,447 @@ +/* + * 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.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.action.BrowseAction; +import com.sk.action.GenAction; +import com.sk.action.ScriptAction; +import com.sk.utils.FileUtil; +import com.sk.utils.GenNotification; +import org.apache.http.util.TextUtils; +import org.jetbrains.annotations.Nullable; + +import javax.swing.JComponent; +import javax.swing.JDialog; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.JLabel; +import javax.swing.JButton; +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; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * GenerateDialogPane生成工具主界面 + * + * @author: xudong + * @see: select generate dialog + * @version: v1.0.0 + * @since 2022-02-21 + */ +public class GenerateDialogPane extends JDialog { + private static final Logger LOG = Logger.getInstance(GenerateDialogPane.class); + private static final String COMMAND_STATEMENT = "add_library(napitest SHARED x_napi_tool.cpp napitest.cpp " + + "napitest_middle.cpp)" + FileUtil.getNewline() + "target_link_libraries(napitest libace_napi.z.so)"; + private static final String REGEX = "napitest"; + private static final Pattern LF_PATTERN = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); + + private JPanel contentPane; + private JTextField interPath; + private JTextField genPath; + private JTextField scriptPath; + private JButton selectInter; + private JButton selectGenPath; + private JButton selectScriptPath; + private JLabel interText; + private JLabel genText; + private JLabel scriptText; + private boolean generateSuccess = true; + private String sErrorMessage = ""; + private String interFileOrDir; + private String genOutDir; + private String scriptOutDir; + private final Project project; + + + /** + * 构造函数 + * + * @param project projectid + * @param interFilePath 接口文件路径 + * @param genDir 生成框架文件路径 + * @param scriptDir 脚本目录 + */ + public GenerateDialogPane(Project project, String interFilePath, String genDir, String scriptDir) { + this.project = project; + this.interFileOrDir = interFilePath; + this.genOutDir = genDir; + this.scriptOutDir = scriptDir; + + interText.setText("接口文件:"); + genText.setText("生成框架路径:"); + scriptText.setText("编译脚本路径:"); + + interPath.setText(interFileOrDir); + genPath.setText(genOutDir); + scriptPath.setText(genOutDir); + + // call onCancel() on ESCAPE + contentPane.registerKeyboardAction(actionEvent -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), + JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + + BrowseAction browseAction = new BrowseAction(project, selectInter, interPath, genPath, scriptPath); + selectInter.addActionListener(browseAction); + selectGenPath.addActionListener(new GenAction(selectGenPath, genPath)); + selectScriptPath.addActionListener(new ScriptAction(selectScriptPath, scriptPath)); + } + + @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 fileInter = interPath.getText(); + String scriptDir = scriptPath.getText(); + String filegypDir = genPath.getText(); + boolean isEmptyFile = TextUtils.isEmpty(fileInter) + || TextUtils.isEmpty(scriptDir) + || TextUtils.isEmpty(filegypDir); + + ValidationInfo validationInfo = null; + if (isEmptyFile) { + String warnMsg = "接口文件、框架、编译脚本路径不能为空"; + warningMessage(warnMsg); + validationInfo = new ValidationInfo(warnMsg); + return validationInfo; + } + + File file = new File(filegypDir + "/binding.gyp"); + 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 = "请选择接口文件或文件夹,生成框架路径,编译脚本路径"; + GenNotification.notifyMessage(this.project, notiContent, title, NotificationType.WARNING); + } + + /** + * 执行主程序入口 + * + * @return 执行状态 + */ + public boolean runFun() { + GenNotification.notifyMessage(this.project, "", "正在生成", NotificationType.INFORMATION); + interFileOrDir = interPath.getText(); + genOutDir = genPath.getText(); + scriptOutDir = scriptPath.getText(); + + String command; + command = genCommand(); + + try { + if (!TextUtils.isEmpty(command) && callExtProcess(command)) { + GenNotification.notifyMessage(project, genPath.getText(), "提示", NotificationType.INFORMATION, true); + return true; + } + } catch (IOException | InterruptedException ex) { + GenNotification.notifyMessage(project, genPath.getText(), "Command exec error", NotificationType.ERROR); + LOG.error(ex); + } + return false; + } + + /** + * 生成命令行指令 + * + * @return 返回命令行执行内容 + */ + private String genCommand() { + String sysName = System.getProperties().getProperty("os.name").toUpperCase(); + String tmpDirFile = System.getProperty("java.io.tmpdir"); + String execFn; + if (sysName.contains("WIN")) { + execFn = "cmds/win/napi_generator-win.exe"; + tmpDirFile += "napi_generator-win.exe"; + } else if (sysName.contains("LINUX")) { + execFn = "cmds/linux/napi_generator-linux"; + tmpDirFile += "napi_generator-linux"; + } else { + execFn = "cmds/mac/napi_generator-macos"; + tmpDirFile += "napi_generator-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 inArgs = genInArgs(); + command += inArgs + " -o " + genOutDir; + return command; + } + + /** + * 生成 -f -d 输入参数。 + * + * @return 生成后的值-f -d的值 + */ + private String genInArgs() { + + 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.append(interStr).append(" "); + } else { + tsParam.append(interStr).append(","); + } + } + if (!TextUtils.isEmpty(tsParam.toString().replaceAll("-f", "")) + && !TextUtils.isBlank(tsParam.toString().replaceAll("-f", ""))) { + inputCommand += tsParam.substring(0, tsParam.length() - 1); + } + if (!TextUtils.isEmpty(dirParam.toString().replace("-d", "")) + && !TextUtils.isBlank(dirParam.toString().replace("-d", ""))) { + inputCommand += dirParam.substring(0, dirParam.length() - 1); + } + } + return inputCommand; + } + + 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) { + writeCommand(); + } else { + GenNotification.notifyMessage(project, sErrorMessage, "提示", NotificationType.ERROR); + return false; + } + errConsumer.join(); + outputConsumer.join(); + return true; + } + + /** + * 写makeFile.txt文件 + */ + private void writeCommand() { + FileUtil fileUtil = new FileUtil(); + String filePath = fileUtil.makeFile(genOutDir + "/makeFile.txt"); + if (TextUtils.isEmpty(filePath)) { + LOG.info("makeFile is fail"); + return; + } + Matcher matcher = LF_PATTERN.matcher(COMMAND_STATEMENT); + String statement = matcher.replaceAll(scriptOutDir); + try { + if (!fileUtil.findStringInFile(filePath, statement)) { + fileUtil.writeErrorToTxt(filePath, statement); + } + } catch (IOException ioException) { + LOG.error("writeCommand io error" + ioException); + } + } + + /** + * 赋值可执行文件权限。 + * + * @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/napi_IntelliJ_plugin/generator/src/com/sk/ng/GenDTS.java b/napi_IntelliJ_plugin/generator/src/com/sk/ng/GenDTS.java new file mode 100644 index 00000000..020bc614 --- /dev/null +++ b/napi_IntelliJ_plugin/generator/src/com/sk/ng/GenDTS.java @@ -0,0 +1,73 @@ +/* + * 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.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.dialog.GenerateDialog; +import com.sk.utils.FileUtil; +import com.sk.utils.GenNotification; + +/** + * 项目文件入口 + * + * @author: xudong + * @see: tool conversion plug-in + * @version: v1.0.0 + * @since 2022-02-21 + */ +public class GenDTS extends AnAction { + + @Override + public void actionPerformed(AnActionEvent anActionEvent) { + Project project = anActionEvent.getProject(); + // 获取需要处理的.d.ts文件绝对路径 + 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 baseFile = project.getBasePath(); + + if (FileUtil.checkProjectSDK(project, baseFile)) { + return; + } + + String destPath = file.getPath(); + String directoryPath = file.getParent().getPath(); + String fileName = file.getName(); + GenerateDialog wrapper = new GenerateDialog(project, destPath, directoryPath, fileName); + 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/napi_IntelliJ_plugin/generator/src/com/sk/ng/GenMenuDTS.java b/napi_IntelliJ_plugin/generator/src/com/sk/ng/GenMenuDTS.java new file mode 100644 index 00000000..f69f0049 --- /dev/null +++ b/napi_IntelliJ_plugin/generator/src/com/sk/ng/GenMenuDTS.java @@ -0,0 +1,51 @@ +/* + * 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.ng; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.project.Project; +import com.sk.dialog.GenerateDialog; +import com.sk.utils.FileUtil; +import org.jetbrains.annotations.NotNull; + +/** + * 工具菜单入口 + * + * @author: liulongc digitalchina.com + * @see: tool conversion plug-in + * @version: v1.0.0 + * @since 2022-05-27 + */ +public class GenMenuDTS extends AnAction { + + @Override + public void actionPerformed(@NotNull AnActionEvent anActionEvent) { + Project project = anActionEvent.getProject(); + + if (project == null) { + return; + } + String baseFile = project.getBasePath(); + + if (FileUtil.checkProjectSDK(project, baseFile)) { + return; + } + + GenerateDialog wrapper = new GenerateDialog(project, "", "", ""); + wrapper.showAndGet(); + } + +} diff --git a/napi_IntelliJ_plugin/generator/src/com/sk/utils/FileUtil.java b/napi_IntelliJ_plugin/generator/src/com/sk/utils/FileUtil.java new file mode 100644 index 00000000..7500b0ea --- /dev/null +++ b/napi_IntelliJ_plugin/generator/src/com/sk/utils/FileUtil.java @@ -0,0 +1,187 @@ +/* + * 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.utils; + +import com.intellij.notification.NotificationType; +import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.project.Project; +import org.apache.commons.lang3.StringUtils; +import org.apache.http.util.TextUtils; + +import java.io.File; +import java.io.FileWriter; +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; + +/** + * 文本文件工具 + * + * @author: xudong + * @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; + + /** + * 将错误信息输入到txt中 + * + * @param path 路径 + * @param content 内容 + */ + public void writeErrorToTxt(String path, String content) { + File file = new File(path); + try (FileWriter fw = new FileWriter(file, true)) { + fw.write(content + FileUtil.getNewline()); + } catch (IOException ioException) { + LOG.error("writeErrorToTxt io error" + ioException); + } + } + + /** + * 创建文件 + * + * @param path 文件路径 + * @return 文件路径 + */ + public String makeFile(String path) { + File file = new File(path); + if (!file.exists()) { + try { + boolean isCreateFile = file.createNewFile(); + if (isCreateFile) { + LOG.info(String.format("makeFile %s success", path)); + } + } catch (IOException ioException) { + LOG.error(String.format("makeFile %s error:%s", path, ioException)); + return ""; + } + } + return file.getPath(); + } + + /** + * 判断文件是否包含指定字符串 + * + * @param path 文件路径 + * @param content 指定内容 + * @return 是否包含指定字符串 + * @throws IOException 异常信息 + */ + 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), StandardCharsets.UTF_8); + BufferedReader bufferedReader = new BufferedReader(read)) { + return isContainString(bufferedReader, command); + } catch (FileNotFoundException foundException) { + LOG.error("file not found" + foundException); + return false; + } + } + + private boolean isContainString(BufferedReader bufferedReader, String[] command) { + String line = null; + while (true) { + try { + if ((line = bufferedReader.readLine()) == null) { + return false; + } + } catch (IOException ioException) { + LOG.error("findStringInFile IOException" + ioException); + } + line += line; + if (line.contains(command[0])) { + return true; + } + } + } + + /** + * 获取换行符 + * + * @return 换行符 + */ + public static String getNewline() { + return System.getProperty("line.separator"); + } + + /** + * 正则匹配所选文件名是否符合规范 + * + * @param fileName 文件名 + * @return boolean 是否匹配 + */ + public static boolean patternFileName(String fileName) { + String pattern = "@ohos.([.a-z_A-Z0-9]+).d.ts"; + 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/napi_IntelliJ_plugin/generator/src/com/sk/utils/GenNotification.java b/napi_IntelliJ_plugin/generator/src/com/sk/utils/GenNotification.java new file mode 100644 index 00000000..68dc4374 --- /dev/null +++ b/napi_IntelliJ_plugin/generator/src/com/sk/utils/GenNotification.java @@ -0,0 +1,125 @@ +/* + * 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.utils; + +import com.intellij.ide.actions.OpenFileAction; +import com.intellij.notification.NotificationType; +import com.intellij.notification.Notification; +import com.intellij.notification.NotificationGroup; +import com.intellij.notification.NotificationAction; +import com.intellij.notification.Notifications; +import com.intellij.notification.NotificationDisplayType; +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: liulongc digitalchina.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) { + + NotificationGroup notificationGroup = new NotificationGroup("Generate.Result.Group", + NotificationDisplayType.STICKY_BALLOON); + Notification notification = notificationGroup.createNotification(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