|
1 | 1 | package com.xwintop.xJavaFxTool.newui.creator; |
2 | 2 |
|
| 3 | +import static com.xwintop.xJavaFxTool.utils.ResourceBundleUtils.toNativeAscii; |
| 4 | + |
| 5 | +import com.xwintop.xJavaFxTool.utils.ResourceUtils; |
| 6 | +import com.xwintop.xcore.javafx.dialog.FxAlerts; |
| 7 | +import java.io.File; |
| 8 | +import java.io.IOException; |
| 9 | +import java.nio.charset.Charset; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.nio.file.Paths; |
| 14 | +import org.apache.commons.io.FileUtils; |
| 15 | + |
3 | 16 | public class CreatePluginProjectService { |
4 | 17 |
|
5 | 18 | private static final CreatePluginProjectService INSTANCE = new CreatePluginProjectService(); |
6 | 19 |
|
| 20 | + public static final Charset CHARSET = StandardCharsets.UTF_8; |
| 21 | + |
7 | 22 | public static CreatePluginProjectService getInstance() { |
8 | 23 | return INSTANCE; |
9 | 24 | } |
10 | 25 |
|
11 | 26 | public void createProject(PluginProjectInfo pluginProjectInfo) { |
| 27 | + try { |
| 28 | + FileUtils.forceMkdir(new File(pluginProjectInfo.getLocation())); |
| 29 | + savePom(pluginProjectInfo); |
| 30 | + saveConfiguration(pluginProjectInfo); |
| 31 | + saveResourceBundle(pluginProjectInfo); |
| 32 | + saveFxml(pluginProjectInfo); |
| 33 | + saveControllerClass(pluginProjectInfo); |
| 34 | + saveMainClass(pluginProjectInfo); |
| 35 | + } catch (Exception e) { |
| 36 | + FxAlerts.error("创建失败", e); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + private void saveMainClass(PluginProjectInfo info) throws Exception { |
| 41 | + String java = ResourceUtils.readResource("/plugin-template/PLUGIN_NAMEMain.java", CHARSET); |
| 42 | + java = java |
| 43 | + .replace("[PACKAGE]", info.getPackageName()) |
| 44 | + .replace("[PLUGIN_NAME]", info.getPluginName()) |
| 45 | + .replace("[CLASS_NAME]", info.getMainClass()); |
| 46 | + String javaPath = "src/main/java/" + info.getMainFullClass().replace(".", "/") + ".java"; |
| 47 | + writeFile(Paths.get(info.getLocation(), javaPath), java); |
| 48 | + } |
| 49 | + |
| 50 | + private void saveControllerClass(PluginProjectInfo info) throws Exception { |
| 51 | + String java = ResourceUtils.readResource("/plugin-template/PLUGIN_NAMEController.java", CHARSET); |
| 52 | + java = java |
| 53 | + .replace("[PACKAGE]", info.getPackageName()) |
| 54 | + .replace("[CLASS_NAME]", info.getControllerClass()); |
| 55 | + String javaPath = "src/main/java/" + info.getControllerFullClass().replace(".", "/") + ".java"; |
| 56 | + writeFile(Paths.get(info.getLocation(), javaPath), java); |
| 57 | + } |
| 58 | + |
| 59 | + private void saveFxml(PluginProjectInfo info) throws Exception { |
| 60 | + String fxml = ResourceUtils.readResource("/plugin-template/PLUGIN_NAME.fxml", CHARSET); |
| 61 | + fxml = fxml |
| 62 | + .replace("[CONTROLLER]", info.getControllerFullClass()) |
| 63 | + .replace("[PLUGIN_NAME]", info.getPluginName()); |
| 64 | + String resourceBundlePath = "src/main/resources/fxml/" + info.getPluginName() + ".fxml"; |
| 65 | + writeFile(Paths.get(info.getLocation(), resourceBundlePath), fxml); |
| 66 | + } |
| 67 | + |
| 68 | + private void saveResourceBundle(PluginProjectInfo info) throws Exception { |
| 69 | + String resourceBundle = ResourceUtils.readResource("/plugin-template/PLUGIN_NAME.properties", CHARSET); |
| 70 | + resourceBundle = resourceBundle.replace("[TITLE]", toNativeAscii(info.getPluginTitle(), false, true)); |
| 71 | + String resourceBundlePath = "src/main/resources/locale/" + info.getPluginName() + ".properties"; |
| 72 | + writeFile(Paths.get(info.getLocation(), resourceBundlePath), resourceBundle); |
| 73 | + } |
| 74 | + |
| 75 | + private void saveConfiguration(PluginProjectInfo info) throws Exception { |
| 76 | + String config = ResourceUtils.readResource("/plugin-template/toolFxmlLoaderConfiguration.xml", CHARSET); |
| 77 | + config = config.replace("[PLUGIN_NAME]", info.getPluginName()); |
| 78 | + writeFile(Paths.get(info.getLocation(), "src/main/resources/config/toolFxmlLoaderConfiguration.xml"), config); |
| 79 | + } |
| 80 | + |
| 81 | + private void savePom(PluginProjectInfo info) throws Exception { |
| 82 | + String pom = ResourceUtils.readResource("/plugin-template/pom.xml", CHARSET); |
| 83 | + pom = pom |
| 84 | + .replace("[GROUPID]", info.getGroupId()) |
| 85 | + .replace("[ARTIFACTID]", info.getArtifactId()) |
| 86 | + .replace("[VERSION]", info.getVersion()); |
| 87 | + Files.write(Paths.get(info.getLocation(), "pom.xml"), pom.getBytes(CHARSET)); |
| 88 | + } |
12 | 89 |
|
| 90 | + private void writeFile(Path path, String fileContent) throws IOException { |
| 91 | + FileUtils.forceMkdirParent(path.toFile()); |
| 92 | + Files.write(path, fileContent.getBytes(CHARSET)); |
13 | 93 | } |
14 | 94 | } |
0 commit comments