多module開發時module模板外掛開發
1.需求
製作外掛,根據module名和包名生成對應的module模板
2.程式碼
2.1下載Intellij Idea
2.2建立Plugin專案
image
2.3定義plugin.xml配置檔案
<idea-plugin> <id>com.skateboard.modulegenerator</id> <name>元件化模板module產生器</name> <version>1.0</version> <vendor email="skateboard1991@163.com" url="">YourCompany</vendor> <description><![CDATA[ 用來產生模板module ]]></description> <change-notes><![CDATA[ Add change notes here.<br> <em>most HTML tags may be used</em> ]]> </change-notes> <!-- please see for description --> <idea-version since-build="173.0"/> <!-- please see on how to target different products --> <!-- uncomment to enable plugin in all products <depends>com.intellij.modules.lang</depends> --> <extensions defaultExtensionNs="com.intellij"> <!-- Add your extensions here --> </extensions> <actions> <!-- Add your actions here --> <group id="com.skateboard.modulegenerator" popup="true" text="GenerateModule"> <action id="MultiModuleGenerator" class="com.skateboard.modulegenerator.GeneratorAction" text="ModuleGenerator" description="Use to generate module"> <add-to-group group-id="FileMenu" anchor="first"/> <keyboard-shortcut keymap="$default" first-keystroke="shift ctrl P"/> </action> </group> </actions></idea-plugin>
其中的<name>和<description>就是外掛名稱和功能描述,會在你安裝外掛時顯示。<actions>標籤定義了外掛的一些動作,其中<group>定義了外掛顯示的選單名稱和顯示樣式,<add-to-group>定義了將會把GenerateModule選單欄 新增到File選單下。<actiton>定義了action的程式碼的位置。
2.4對話方塊的編寫
當點選ModuleGenerator選項後,會出現一個對話方塊,讓你輸入module的名稱以及包名,這個對話方塊其實就是用java swing編寫的
ublic class ModuleInfoDialog extends JDialog { private DialogCallback dialogCallback; private final static String OK = "OK"; private final static String CANCEL = "CANCEL"; private final static String TITLE = "建立新module"; private final static String MODULE_NAME_HINT = "請輸入模組名稱:"; private final static String PACKAGE_NAME_HINT = "請輸入包名:"; private final static String ERROR_MESSAGE = "模組名和包名不能為空"; public ModuleInfoDialog(DialogCallback dialogCallback) { setSize(450, 300); setModal(true); setTitle(TITLE); prepareUI(); this.dialogCallback = dialogCallback; } private void prepareUI() { GridBagLayout gridBagLayout = new GridBagLayout(); JPanel contentPannel = new JPanel(); contentPannel.setLayout(gridBagLayout); GridBagConstraints constraints = new GridBagConstraints(); constraints.gridx = 0; constraints.gridy = 0; constraints.gridwidth = 1; constraints.weightx = 0; JLabel moduleHintLabel = new JLabel(); moduleHintLabel.setText(MODULE_NAME_HINT); contentPannel.add(moduleHintLabel, constraints); constraints.gridx = 0; constraints.gridy = 1; constraints.weightx=0; JLabel packageNameHintLabel = new JLabel(); packageNameHintLabel.setText(PACKAGE_NAME_HINT); contentPannel.add(packageNameHintLabel, constraints); constraints.gridx = 1; constraints.gridy = 0; constraints.weightx=1; constraints.fill=GridBagConstraints.HORIZONTAL; JTextField moduleNameImp = new JTextField(); moduleNameImp.setForeground(Color.WHITE); contentPannel.add(moduleNameImp, constraints); constraints.gridx = 1; constraints.gridy = 1; constraints.weightx=1; constraints.fill=GridBagConstraints.HORIZONTAL; JTextField packageNameImp = new JTextField(); packageNameImp.setForeground(Color.WHITE); contentPannel.add(packageNameImp, constraints); JLabel errorLabel = new JLabel(); errorLabel.setForeground(Color.RED); errorLabel.setVisible(false); JButton okBtn = new JButton(); okBtn.setText(OK); okBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (moduleNameImp.getText().isEmpty() || packageNameImp.getText().isEmpty()) { errorLabel.setVisible(true); errorLabel.setText(ERROR_MESSAGE); return; } if (dialogCallback != null) { dialogCallback.onOkClicked(moduleNameImp.getText(), packageNameImp.getText()); } dispose(); } }); JButton cancelBtn = new JButton(); cancelBtn.setText(CANCEL); cancelBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (dialogCallback != null) { dialogCallback.onCancelClicked(); } dispose(); } }); constraints.anchor=WEST; constraints.weightx=0; constraints.gridx = 0; constraints.gridy = 2; contentPannel.add(okBtn, constraints); constraints.fill=GridBagConstraints.NONE; constraints.gridy = 2; constraints.gridx = 1; contentPannel.add(cancelBtn, constraints); constraints.gridy = 3; constraints.gridx = 0; contentPannel.add(errorLabel, constraints); setContentPane(contentPannel); } interface DialogCallback { void onOkClicked(String moduleName, String packageName); void onCancelClicked(); } public static void pop(DialogCallback callback) { ModuleInfoDialog infoDialog = new ModuleInfoDialog(callback); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); infoDialog.setLocation(screenSize.width / 2 - infoDialog.getWidth() / 2, screenSize.height / 2 - infoDialog.getHeight() / 2); infoDialog.setVisible(true); } }
沒什麼特別要說的,就是構建了一個dilaog,其中的靜態方法pop()用來顯示這個dialog
2.5action的編寫
當我們點選ModuleGenerator的這個選項時,會產生一個action,這裡要實現對應的邏輯,就是我們建立模板檔案的地方。首先建立一個類GeneratorAction並繼承自AnAction,同時需要複寫actionPerformed方法,看一下GeneratorAction的actionPerformed方法
ModuleInfoDialog.pop(new ModuleInfoDialog.DialogCallback() { @Override public void onOkClicked(String moduleName, String packageName) { generateModuleFiles(moduleName, packageName, e.getProject().getBasePath()); e.getProject().getBaseDir().refresh(true, true); } @Override public void onCancelClicked() { } });
首先就是產生剛才說的用來輸入module名和包名的dialog,當點選ok按鈕後,就會建立專案模板檔案和同步工程檔案了。其中的generateModuleFils方法就是建立相應的資料夾和檔案的,具體實現看程式碼就好了,沒什麼可說的。
3.問題
gradle檔案中指定的intellij的版本號
intellij { version '2018.1'}
plugin.xml中指定的idea的build version
<idea-version since-build="181.0"/>
作者:滑板上的老砒霜
連結:
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/430/viewspace-2821671/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- React-Native iOS Module開發ReactiOS
- 架設 Go Module Proxy Server 加速團隊開發GoServer
- chrome 外掛開發Chrome
- flutter 外掛開發Flutter
- 開發Rhino外掛
- VscodeIDEA開發外掛VSCodeIdea
- Skywalking 外掛開發
- Webstorm 外掛開發WebORM
- Flutter外掛開發Flutter
- Mybatis外掛開發MyBatis
- Flutter開發之Flutter外掛開發Flutter
- (是時候開發屬於自己的外掛了)資料校驗外掛開發指南
- 本地搭建halo模板和外掛開發簡要步驟
- 元件化開發跨module互動方式—ModuleBus互動元件化
- dlopen開發外掛庫
- Chrome外掛開發教程Chrome
- eslint外掛開發教程EsLint
- Eclipse外掛開發demoEclipse
- Maven外掛開發教程Maven
- Idea日常開發外掛Idea
- VScode股票外掛開發VSCode
- babel 外掛開發案例Babel
- Vue-外掛開發Vue
- babel外掛開發心得Babel
- wordpress外掛開發02-首頁文章自動摘要外掛開發
- wordpress外掛開發03-簡單的all in one seo 外掛開發
- Flutter外掛開發《iOS原生模組開發》FlutteriOS
- centos安裝Python外掛後找不到對應的外掛ModuleNotFoundError: No module named*CentOSPythonError
- Golang之go module開發系列二--使用偽版本和GoCenterGolang
- ZBlog外掛開發檔案結構(外掛)
- apisix~lua外掛開發與外掛註冊API
- 使用pgrx開發postgre外掛
- go~wasm外掛的開發GoASM
- Electron 外掛開發實踐
- blazor wasm開發chrome外掛BlazorASMChrome
- Flutter外掛開發---iOS篇FlutteriOS
- Flutter外掛開發---Android篇FlutterAndroid
- Flutter 原生外掛開發流程Flutter