diff --git a/hdc/gn_IntelliJ_plugin/resources/META-INF/plugin.xml b/hdc/gn_IntelliJ_plugin/resources/META-INF/plugin.xml new file mode 100644 index 0000000000000000000000000000000000000000..1fe4affa3b5e714b59984a71463e0136321280b2 --- /dev/null +++ b/hdc/gn_IntelliJ_plugin/resources/META-INF/plugin.xml @@ -0,0 +1,37 @@ + + com.sk.gn + Gn Generate + 1.0.1 + 深圳开鸿数字产业发展有限公司 + + Introduction +

    The current OpenHarmony 3.1Release and later versions use bundle.json to integrate subsystem code. However, some libraries are integrated as cmakelists.txt, which cannot be integrated into OpenHarmony source code. You need to compile the library to a build.gn file, and then use the generated GN script to integrate the library into OpenHarmony.

+

Sources on Gitee

+ ]]> +
+ + + com.intellij.modules.lang + + + + + + + + + + + + + + + + + + + + + +
\ No newline at end of file diff --git a/hdc/gn_IntelliJ_plugin/resources/images/openDisk.png b/hdc/gn_IntelliJ_plugin/resources/images/openDisk.png new file mode 100644 index 0000000000000000000000000000000000000000..5956a0e538a400c7deaf41167ae5ea70348b8584 Binary files /dev/null and b/hdc/gn_IntelliJ_plugin/resources/images/openDisk.png differ diff --git a/hdc/gn_IntelliJ_plugin/resources/images/step_one.png b/hdc/gn_IntelliJ_plugin/resources/images/step_one.png new file mode 100644 index 0000000000000000000000000000000000000000..10638d6cf157dd83e2f0a091c637bad05f5c8e7b Binary files /dev/null and b/hdc/gn_IntelliJ_plugin/resources/images/step_one.png differ diff --git a/hdc/gn_IntelliJ_plugin/resources/images/step_two.png b/hdc/gn_IntelliJ_plugin/resources/images/step_two.png new file mode 100644 index 0000000000000000000000000000000000000000..a298764419264adfe58ece0c7dfcb72abb4804f0 Binary files /dev/null and b/hdc/gn_IntelliJ_plugin/resources/images/step_two.png differ diff --git a/hdc/gn_IntelliJ_plugin/src/com/sk/gn/action/InputScriptAction.java b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/action/InputScriptAction.java new file mode 100644 index 0000000000000000000000000000000000000000..a8f157f5ecf14c102b870ce31ba63e99299f208c --- /dev/null +++ b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/action/InputScriptAction.java @@ -0,0 +1,72 @@ +/* + * 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.gn.action; + +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JFileChooser; +import javax.swing.JTextField; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.File; + +/** + * 选择脚本路径 + * + * @author: zhaoxudong@kaihong.com + * @see: select script path + * @version: v1.0.0 + * @since 2022-09-21 + */ +public class InputScriptAction implements ActionListener { + private final JButton button; + private final JTextField textField; + private final JComboBox comboBox; + private final JTextField transplantTextField; + + public InputScriptAction(JButton button, JTextField textField, JComboBox comboBox, JTextField transplantTextField) { + this.button = button; + this.textField = textField; + this.comboBox = comboBox; + this.transplantTextField = transplantTextField; + } + + @Override + public void actionPerformed(ActionEvent actionEvent) { + if (actionEvent.getSource().equals(button)) { + JFileChooser fcDlg = new JFileChooser(textField.getText()); + fcDlg.setDialogTitle("请选择输入脚本路径..."); + fcDlg.setFileSelectionMode(JFileChooser.FILES_ONLY); + int returnVal = fcDlg.showOpenDialog(null); + if (returnVal == JFileChooser.APPROVE_OPTION) { + String filepath = fcDlg.getSelectedFile().getPath(); + if (filepath.contains(File.separator)) { + textField.setText(filepath.substring(filepath.lastIndexOf("third_party"))); + transplantTextField.setText(filepath.substring(filepath.lastIndexOf("/"))); + } else { + textField.setText(filepath); + transplantTextField.setText(filepath); + } + if (filepath.contains("Makefile")) { + comboBox.setSelectedIndex(0); + } else if (filepath.contains("scons")) { + comboBox.setSelectedIndex(2); + } else { + comboBox.setSelectedIndex(1); + } + } + } + } +} \ No newline at end of file diff --git a/hdc/gn_IntelliJ_plugin/src/com/sk/gn/action/SelectBankAction.java b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/action/SelectBankAction.java new file mode 100644 index 0000000000000000000000000000000000000000..ffa40a7cfcf0d90efcb2f109f496cb2ce227410f --- /dev/null +++ b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/action/SelectBankAction.java @@ -0,0 +1,58 @@ +/* + * 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.gn.action; + +import javax.swing.JButton; +import javax.swing.JFileChooser; +import javax.swing.JTextField; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.File; + +/** + * 选择库移植的路径 + * + * @author: zhaoxudong@kaihong.com + * @see: select transplant path + * @version: v1.0.0 + * @since 2022-09-21 + */ +public class SelectBankAction implements ActionListener { + private final JButton button; + private final JTextField textField; + + public SelectBankAction(JButton button, JTextField textField) { + this.button = button; + this.textField = textField; + } + + @Override + public void actionPerformed(ActionEvent actionEvent) { + if (actionEvent.getSource().equals(button)) { + JFileChooser fcDlg = new JFileChooser(textField.getText()); + fcDlg.setDialogTitle("请选择选择库移植的路径..."); + fcDlg.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + int returnVal = fcDlg.showOpenDialog(null); + if (returnVal == JFileChooser.APPROVE_OPTION) { + String filepath = fcDlg.getSelectedFile().getPath(); + if (filepath.contains(File.separator)) { + textField.setText(filepath.substring(filepath.lastIndexOf(File.separator))); + } else { + textField.setText(filepath); + } + } + } + } +} \ No newline at end of file diff --git a/hdc/gn_IntelliJ_plugin/src/com/sk/gn/action/SelectOriginCodeAction.java b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/action/SelectOriginCodeAction.java new file mode 100644 index 0000000000000000000000000000000000000000..453873e482843e981d4d234a211f6ce220abf9c7 --- /dev/null +++ b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/action/SelectOriginCodeAction.java @@ -0,0 +1,53 @@ +/* + * 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.gn.action; + +import javax.swing.JButton; +import javax.swing.JFileChooser; +import javax.swing.JTextField; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +/** + * 选择源码路径 + * + * @author: zhaoxudong@kaihong.com + * @see: select origin code path + * @version: v1.0.0 + * @since 2022-09-21 + */ +public class SelectOriginCodeAction implements ActionListener { + private final JButton button; + private final JTextField textField; + + public SelectOriginCodeAction(JButton button, JTextField textField) { + this.button = button; + this.textField = textField; + } + + @Override + public void actionPerformed(ActionEvent actionEvent) { + if (actionEvent.getSource().equals(button)) { + JFileChooser fcDlg = new JFileChooser(textField.getText()); + fcDlg.setDialogTitle("请选择源码路径..."); + fcDlg.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + int returnVal = fcDlg.showOpenDialog(null); + if (returnVal == JFileChooser.APPROVE_OPTION) { + String filepath = fcDlg.getSelectedFile().getPath(); + textField.setText(filepath); + } + } + } +} diff --git a/hdc/gn_IntelliJ_plugin/src/com/sk/gn/action/SelectOutDirAction.java b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/action/SelectOutDirAction.java new file mode 100644 index 0000000000000000000000000000000000000000..d7bb23f755b6e75646c745ffb400520c0663e04b --- /dev/null +++ b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/action/SelectOutDirAction.java @@ -0,0 +1,63 @@ +/* + * 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.gn.action; + +import javax.swing.JButton; +import javax.swing.JFileChooser; +import javax.swing.JTextField; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.io.File; + +/** + * 选择输出路径 + * + * @author: zhaoxudong@kaihong.com + * @see: select out path + * @version: v1.0.0 + * @since 2022-09-21 + */ +public class SelectOutDirAction implements ActionListener { + private final JButton button; + private final JTextField textField; + + public SelectOutDirAction(JButton button, JTextField textField) { + this.button = button; + this.textField = textField; + } + + @Override + public void actionPerformed(ActionEvent actionEvent) { + if (actionEvent.getSource().equals(button)) { + JFileChooser fcDlg = new JFileChooser(textField.getText()); + fcDlg.setDialogTitle("请选择输出路径..."); + fcDlg.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); + int returnVal = fcDlg.showOpenDialog(null); + if (returnVal == JFileChooser.APPROVE_OPTION) { + String filepath = fcDlg.getSelectedFile().getPath(); + if (filepath.contains(File.separator)) { + String path = filepath.substring(0, filepath.lastIndexOf(File.separator)); + if (path.contains(File.separator)) { + textField.setText(filepath.substring(path.lastIndexOf(File.separator) + 1)); + } else { + textField.setText(filepath.substring(filepath.lastIndexOf(File.separator) + 1)); + } + } else { + textField.setText(filepath); + } + } + } + } +} diff --git a/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/ConfirmDialog.java b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/ConfirmDialog.java new file mode 100644 index 0000000000000000000000000000000000000000..a880debc78579acf9aa700a05006f04f87bbe9e6 --- /dev/null +++ b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/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.gn.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 ConfirmDialogPane confirmDiagPane; + + /** + * 构造函数 + * @param message 弹出框信息内容 + */ + public ConfirmDialog(String message) { + super(true); + confirmDiagPane = new ConfirmDialogPane(message); + setOKButtonText("Yes"); + setCancelButtonText("No"); + setUndecorated(true); + setResizable(false); + init(); + } + + @Override + @Nullable + protected JComponent createCenterPanel() { + return confirmDiagPane.getContentPanel(); + } +} diff --git a/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/ConfirmDialogPane.form b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/ConfirmDialogPane.form new file mode 100644 index 0000000000000000000000000000000000000000..1f894207c24cbe1105b20aea248e6d7523d46d1e --- /dev/null +++ b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/ConfirmDialogPane.form @@ -0,0 +1,75 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/ConfirmDialogPane.java b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/ConfirmDialogPane.java new file mode 100644 index 0000000000000000000000000000000000000000..f083c554abd11715f8ebd9ba6ab5deb8b0ce7150 --- /dev/null +++ b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/ConfirmDialogPane.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.gn.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 ConfirmDialogPane extends JDialog { + private JPanel contentPane; + private JLabel msgLabel; + private JLabel iconLabel; + + /** + * 构造函数 + * + * @param sErrorMessage 错误信息 + */ + public ConfirmDialogPane(String sErrorMessage) { + msgLabel.setText(sErrorMessage); + iconLabel.setIcon(new ImageIcon("")); + } + + + JPanel getContentPanel() { + return contentPane; + } +} diff --git a/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/ErrorDialog.form b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/ErrorDialog.form new file mode 100644 index 0000000000000000000000000000000000000000..af9042f6fb805e4a9f6662c1f7e6902fa637f7d4 --- /dev/null +++ b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/ErrorDialog.form @@ -0,0 +1,86 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/ErrorDialog.java b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/ErrorDialog.java new file mode 100644 index 0000000000000000000000000000000000000000..589dfbb0a3a30de31c50c3625f1544462d9660d2 --- /dev/null +++ b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/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.gn.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(); + } + + 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/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenDialogPane.form b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenDialogPane.form new file mode 100644 index 0000000000000000000000000000000000000000..0404e5e16440964dd860401ff719e289d818fe65 --- /dev/null +++ b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenDialogPane.form @@ -0,0 +1,313 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenDialogPane.java b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenDialogPane.java new file mode 100644 index 0000000000000000000000000000000000000000..9cbbccbcb476af30f4c6ef34d857e3b8b95badc1 --- /dev/null +++ b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenDialogPane.java @@ -0,0 +1,475 @@ +/* + * 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.gn.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.gn.action.InputScriptAction; +import com.sk.gn.action.SelectBankAction; +import com.sk.gn.action.SelectOriginCodeAction; +import com.sk.gn.action.SelectOutDirAction; +import com.sk.gn.utils.FileUtil; +import com.sk.gn.utils.GenNotification; +import org.apache.http.util.TextUtils; +import org.jetbrains.annotations.Nullable; + +import javax.swing.JButton; +import javax.swing.JComboBox; +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.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import static com.sk.gn.utils.FileUtil.writeTmpFile; + +/** + * GenerateDialogPane生成工具主界面 + * + * @author: zhaoxudong@kaihong.com + * @see: select generate dialog + * @version: v1.0.0 + * @since 2022-02-21 + */ +public class GenDialogPane extends JDialog { + private static final Logger LOG = Logger.getInstance(GenDialogPane.class); + private static final String SYS_NAME = System.getProperties().getProperty("os.name").toUpperCase(); + + private JPanel contentPane; + private JTextField opOutPathTextField; + private JTextField opOriginTextField; + private JTextField transplantTextField; + private JButton selectOPOutButton; + private JButton selectOPOriginButton; + private JButton selectTransplantButton; + private JButton selectScriptButton; + private JTextField subsystemTextField; + private JTextField test_partTextField; + private JTextField compileTextField; + private JTextField inputScriptTextField; + private JComboBox comboBox; + private boolean generateSuccess = true; + private String sErrorMessage = ""; + + /** + * 输出路径 -o + */ + private String outputCodeDir; + + /** + * 源码路径 -p + */ + private String originCodeDir; + + /** + * 输入脚本路径 -f + */ + private String inputScriptDir; + + /** + * 输入脚本类型 -t + */ + private String scriptType; + + /** + * 移植库路径/GN生成路径 -m + */ + private String transplantDir; + + /** + * 子系统名称 -s + */ + private String subsystemName; + + /** + * 组件名称 -m + */ + private String componentName; + + /** + * 编译选项 -a + */ + private String compileOptions; + + private final Project project; + + /** + * 构造函数 + * + * @param project projectId + * @param filePath 文件路径 + */ + public GenDialogPane(Project project, String filePath) { + this.project = project; + // call onCancel() on ESCAPE + contentPane.registerKeyboardAction(actionEvent -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), + JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + + selectOPOutButton.addActionListener(new SelectOutDirAction(selectOPOutButton, opOutPathTextField)); + selectOPOriginButton.addActionListener(new SelectOriginCodeAction(selectOPOriginButton, opOriginTextField)); + selectTransplantButton.addActionListener(new SelectBankAction(selectTransplantButton, transplantTextField)); + selectScriptButton.addActionListener(new InputScriptAction(selectScriptButton, inputScriptTextField, + comboBox, transplantTextField)); + inputScriptTextField.setText(filePath); + } + + @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() { + getEditText(); + ValidationInfo validationInfo = null; + String warnMsg; + if (TextUtils.isEmpty(outputCodeDir)) { + warnMsg = "输出路径不能为空"; + warningMessage(warnMsg); + validationInfo = new ValidationInfo(warnMsg); + return validationInfo; + } + if (TextUtils.isEmpty(originCodeDir)) { + warnMsg = "源码路径不能为空"; + warningMessage(warnMsg); + validationInfo = new ValidationInfo(warnMsg); + return validationInfo; + } + if (TextUtils.isEmpty(transplantDir)) { + warnMsg = "库移植的路径不能为空"; + warningMessage(warnMsg); + validationInfo = new ValidationInfo(warnMsg); + return validationInfo; + } + if (TextUtils.isEmpty(inputScriptDir)) { + warnMsg = "输入脚本路径不能为空"; + warningMessage(warnMsg); + validationInfo = new ValidationInfo(warnMsg); + return validationInfo; + } + if (TextUtils.isEmpty(subsystemName)) { + warnMsg = "子系统名称不能为空"; + warningMessage(warnMsg); + validationInfo = new ValidationInfo(warnMsg); + return validationInfo; + } + if (TextUtils.isEmpty(componentName)) { + warnMsg = "组件名称不能为空"; + warningMessage(warnMsg); + validationInfo = new ValidationInfo(warnMsg); + return validationInfo; + } + File file = new File(transplantTextField.getText() + "/build_tmp"); + if (file.exists()) { + ConfirmDialog confirmDialog = new ConfirmDialog("是否替换已存在的生成结果?"); + if (!confirmDialog.showAndGet()) { + validationInfo = new ValidationInfo(String.format("不替换现有生成结果:%s", file)); + return validationInfo; + } + } + return validationInfo; + } + + private void getEditText() { + // 输出路径 + outputCodeDir = opOutPathTextField.getText().trim(); + // 源码路径 + originCodeDir = opOriginTextField.getText().trim(); + // 库移植的路径 + transplantDir = transplantTextField.getText().trim(); + // 输入脚本路径 + inputScriptDir = inputScriptTextField.getText().trim(); + // 输入脚本类型 + scriptType = comboBox.getSelectedItem().toString().trim(); + // 子系统名称 + subsystemName = subsystemTextField.getText().trim(); + // 组件名称 + componentName = test_partTextField.getText().trim(); + // 编译选项 + compileOptions = compileTextField.getText().trim(); + } + + private void onCancel() { + dispose(); + } + + private void warningMessage(String title) { + String notifyContent = "带*号均为必填项"; + GenNotification.notifyMessage(this.project, notifyContent, title, NotificationType.WARNING); + } + + /** + * 执行主程序入口 + * + * @return 执行状态 + */ + public boolean runFun() { + createCopyResMakeFile(); + createCopyResToolChainFile(); + GenNotification.notifyMessage(this.project, "", "正在生成", NotificationType.INFORMATION); + String command; + command = genCommand(); + try { + if (!TextUtils.isEmpty(command) && callExtProcess(command)) { + GenNotification.notifyMessage(project, opOriginTextField.getText(), "提示", + NotificationType.INFORMATION, true); + return true; + } + } catch (IOException | InterruptedException ex) { + GenNotification.notifyMessage(project, opOriginTextField.getText(), "Command exec error", + NotificationType.ERROR); + LOG.error(ex); + } + return false; + } + + /** + * 生成命令行指令 + * + * @return 返回命令行执行内容 + */ + private String genCommand() { + String tmpDirFile = System.getProperty("java.io.tmpdir"); + String execFn; + if (SYS_NAME.contains("WIN")) { + execFn = "cmds/win/gn-gen-win.exe"; + tmpDirFile += "/gn-gen-win.exe"; + } else if (SYS_NAME.contains("LINUX")) { + execFn = "cmds/linux/gn-gen-linux"; + tmpDirFile += "/gn-gen-linux"; + } else { + execFn = "cmds/mac/gn-gen-macos"; + tmpDirFile += "/gn-gen-macos"; + } + File file = new File(tmpDirFile); + writeTmpFile(tmpDirFile, execFn, project); + if (SYS_NAME.contains("LINUX") || SYS_NAME.contains("MAC OS")) { + try { + executable(tmpDirFile); + } catch (IOException | InterruptedException e) { + GenNotification.notifyMessage(this.project, e.getMessage(), "Can not Find File:" + execFn, + NotificationType.ERROR); + LOG.error(e); + } + } + String command = file.toString(); + command += " -o " + outputCodeDir + " -p " + originCodeDir + " -f " + inputScriptDir + " -t " + scriptType + + " -s " + subsystemName + " -m " + componentName + " -d " + transplantDir; + if (!TextUtils.isEmpty(compileTextField.getText().trim())) { + command += " -a " + "\"" + compileOptions + "\""; + } + return command; + } + + private void createCopyResMakeFile() { + String makeFilePath = "cmds/res/linux/bin/make"; + if (SYS_NAME.contains("WIN")) { + makeFilePath = "cmds/res/win64/bin/gnumake.exe"; + } + String tmpDirFile = System.getProperty("java.io.tmpdir") + "/res/linux/bin/"; + File file = new File(tmpDirFile); + if (file.mkdirs()) { + LOG.info("create dir success"); + } + String tmp = SYS_NAME.contains("WIN") ? file.getPath() + "/gnumake.exe" : file.getPath() + "/make"; + writeTmpFile(tmp, makeFilePath, project); + try { + executable(tmp); + } catch (IOException | InterruptedException e) { + GenNotification.notifyMessage(this.project, e.getMessage(), "Can not Find File:" + makeFilePath, + NotificationType.ERROR); + LOG.error(e); + } + } + + private void createCopyResToolChainFile() { + String toolchainFileDir = "cmds/res/linux/ohos.toolchain.cmake"; + if (SYS_NAME.contains("WIN")) { + toolchainFileDir = "cmds/res/win64/ohos.toolchain.cmake"; + } + String tmpDirFile = System.getProperty("java.io.tmpdir") + "/res/linux/"; + File file = new File(tmpDirFile); + if (file.mkdirs()) { + LOG.info("create dir success"); + } + String tmp = file.getPath() + "/ohos.toolchain.cmake"; + writeTmpFile(tmp, toolchainFileDir, project); + try { + executable(tmp); + } catch (IOException | InterruptedException e) { + GenNotification.notifyMessage(this.project, e.getMessage(), "Can not Find File:" + toolchainFileDir, + 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 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/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenGenerateDialog.java b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenGenerateDialog.java new file mode 100644 index 0000000000000000000000000000000000000000..0429418b43b8a50339614ade10c241f232091f67 --- /dev/null +++ b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenGenerateDialog.java @@ -0,0 +1,151 @@ +/* + * 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.gn.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-09-27 + */ +public class GenGenerateDialog extends DialogWrapper { + private static final Logger LOG = Logger.getInstance(GenGenerateDialog.class); + private static final String TITLE = "Gn Generate Frame"; + private static final String URL = "https://gitee.com/openharmony/napi_generator/tree/master/hdc/gn-gen"; + + private final GenDialogPane genDiag; + + /** + * 构造函数 + * + * @param project projectId + * @param filePath 文件路径 + */ + public GenGenerateDialog(Project project, String filePath) { + super(true); + this.setResizable(false); + setTitle(TITLE); + setModal(true); + genDiag = new GenDialogPane(project, filePath); + 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/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenResultDialog.form b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenResultDialog.form new file mode 100644 index 0000000000000000000000000000000000000000..e699fec7484412b6ce692537415de424f3d3ad17 --- /dev/null +++ b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenResultDialog.form @@ -0,0 +1,78 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenResultDialog.java b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenResultDialog.java new file mode 100644 index 0000000000000000000000000000000000000000..42f8eecb01853b25772a25ed238698e6d1f8a93b --- /dev/null +++ b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/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.gn.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: zhaoxudong@kaihong.com + * @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/hdc/gn_IntelliJ_plugin/src/com/sk/gn/ng/GnDTS.java b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/ng/GnDTS.java new file mode 100644 index 0000000000000000000000000000000000000000..ebd170b247acd673f87531f0ccec05ace83941e9 --- /dev/null +++ b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/ng/GnDTS.java @@ -0,0 +1,64 @@ +/* + * 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.gn.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.gn.dialog.GenGenerateDialog; +import com.sk.gn.utils.FileUtil; +import com.sk.gn.utils.GenNotification; + +/** + * 项目文件入口 + * + * @author: zhaoxudong@kaihong.com + * @see: tool conversion plug-in + * @version: v1.0.0 + * @since 2022-09-21 + */ +public class GnDTS extends AnAction { + + @Override + public void actionPerformed(AnActionEvent anActionEvent) { + Project project = anActionEvent.getProject(); + 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(); + GenGenerateDialog wrapper = new GenGenerateDialog(project, filePath); + 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/gn_IntelliJ_plugin/src/com/sk/gn/ng/GnMenuDTS.java b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/ng/GnMenuDTS.java new file mode 100644 index 0000000000000000000000000000000000000000..9e609d8f7dbe80e8dc9b3bacadd2664269ce5830 --- /dev/null +++ b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/ng/GnMenuDTS.java @@ -0,0 +1,44 @@ +/* + * 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.gn.ng; + +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.project.Project; +import com.sk.gn.dialog.GenGenerateDialog; +import org.jetbrains.annotations.NotNull; + +/** + * 工具菜单入口 + * + * @author: zhaoxudong@kaihong.com + * @see: tool conversion plug-in + * @version: v1.0.0 + * @since 2022-09-27 + */ +public class GnMenuDTS extends AnAction { + + @Override + public void actionPerformed(@NotNull AnActionEvent anActionEvent) { + Project project = anActionEvent.getProject(); + + if (project == null) { + return; + } + GenGenerateDialog wrapper = new GenGenerateDialog(project, ""); + wrapper.showAndGet(); + } + +} diff --git a/hdc/gn_IntelliJ_plugin/src/com/sk/gn/utils/FileUtil.java b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/utils/FileUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..8f771c19c1f6cc40df8331dc748218c08226683a --- /dev/null +++ b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/utils/FileUtil.java @@ -0,0 +1,174 @@ +/* + * 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.gn.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.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; +import java.util.regex.Pattern; + +/** + * 文本文件工具 + * + * @author: zhaoxudong@kaihong.com + * @see: file utils + * @version: v1.0.0 + * @since 2022-09-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(); + } + + /** + * 获取换行符 + * + * @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]+).(am|txt)"; + return Pattern.matches(pattern, fileName); + } + + /** + * check project SDK + * + * @param project project + * @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; + } + + /** + * 拷贝可执行文件到临时文件夹 + * + * @param path 目标文件路径 + * @param oldPath 原文件路径 + * @param project project + * @throws IOException exception + */ + public static void writeTmpFile(String path, String oldPath, Project project) { + File file = new File(path); + try (InputStream inputStream = FileUtil.class.getClassLoader().getResourceAsStream(oldPath)) { + if (inputStream == null) { + throw new IOException("exec File InputStream is Null"); + } + byte[] bs = inputStream.readAllBytes(); + file.setWritable(true); + boolean isNewFile = file.createNewFile(); + if (!isNewFile) { + LOG.info("writeTmpFile createNewFile error"); + } + FileOutputStream fw = new FileOutputStream(file); + fw.write(bs, 0, bs.length); + fw.close(); + } catch (IOException e) { + GenNotification.notifyMessage(project, e.getMessage(), "Can not Find File:" + oldPath, + NotificationType.ERROR); + LOG.error(e); + } + } +} diff --git a/hdc/gn_IntelliJ_plugin/src/com/sk/gn/utils/GenNotification.java b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/utils/GenNotification.java new file mode 100644 index 0000000000000000000000000000000000000000..5281519de0fb19ddb55d442fe861f4ff5ec9390d --- /dev/null +++ b/hdc/gn_IntelliJ_plugin/src/com/sk/gn/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.gn.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-09-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())); + } + } + } +}