From 1111d29fd06c6ddd30d269ddd1fe421dcdc01c05 Mon Sep 17 00:00:00 2001 From: thesai <1021828630@qq.com> Date: Mon, 5 Feb 2024 15:45:16 +0800 Subject: [PATCH] =?UTF-8?q?proto=E6=96=87=E4=BB=B6=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB=E5=92=8C=E7=BC=96=E8=AF=91=E5=90=8E?= =?UTF-8?q?=E7=9A=84=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- {src => proto}/common/modify_info.proto | 0 {src => proto}/common/page_query.proto | 0 {src => proto}/race/race_module.proto | 0 {src => proto}/race/race_paper.proto | 0 {src => proto}/race/race_scene.proto | 0 {src => proto}/race/race_scoring_rule.proto | 0 {src => proto}/race/race_season.proto | 0 {src => proto}/race/race_task.proto | 0 src/Compiler.java | 80 +++++++++++++++++++++ 9 files changed, 80 insertions(+) rename {src => proto}/common/modify_info.proto (100%) rename {src => proto}/common/page_query.proto (100%) rename {src => proto}/race/race_module.proto (100%) rename {src => proto}/race/race_paper.proto (100%) rename {src => proto}/race/race_scene.proto (100%) rename {src => proto}/race/race_scoring_rule.proto (100%) rename {src => proto}/race/race_season.proto (100%) rename {src => proto}/race/race_task.proto (100%) create mode 100644 src/Compiler.java diff --git a/src/common/modify_info.proto b/proto/common/modify_info.proto similarity index 100% rename from src/common/modify_info.proto rename to proto/common/modify_info.proto diff --git a/src/common/page_query.proto b/proto/common/page_query.proto similarity index 100% rename from src/common/page_query.proto rename to proto/common/page_query.proto diff --git a/src/race/race_module.proto b/proto/race/race_module.proto similarity index 100% rename from src/race/race_module.proto rename to proto/race/race_module.proto diff --git a/src/race/race_paper.proto b/proto/race/race_paper.proto similarity index 100% rename from src/race/race_paper.proto rename to proto/race/race_paper.proto diff --git a/src/race/race_scene.proto b/proto/race/race_scene.proto similarity index 100% rename from src/race/race_scene.proto rename to proto/race/race_scene.proto diff --git a/src/race/race_scoring_rule.proto b/proto/race/race_scoring_rule.proto similarity index 100% rename from src/race/race_scoring_rule.proto rename to proto/race/race_scoring_rule.proto diff --git a/src/race/race_season.proto b/proto/race/race_season.proto similarity index 100% rename from src/race/race_season.proto rename to proto/race/race_season.proto diff --git a/src/race/race_task.proto b/proto/race/race_task.proto similarity index 100% rename from src/race/race_task.proto rename to proto/race/race_task.proto diff --git a/src/Compiler.java b/src/Compiler.java new file mode 100644 index 0000000..2f9e1bf --- /dev/null +++ b/src/Compiler.java @@ -0,0 +1,80 @@ +import java.io.BufferedReader; +import java.io.File; +import java.io.IOException; +import java.io.InputStreamReader; + +public class Compiler { + + static String I; + + public static void main(String[] args) { + String basePath = System.getProperty("user.dir"); + String protocPath = String.join(File.separator, basePath, "rtss-message", "protoc-23.1", "bin", "win64", "protoc.exe"); + String protoFolderPath = String.join(File.separator, basePath, "rtss-message", "proto"); + String outputDirectory = String.join(File.separator, basePath, "src", "main", "java"); + StringBuilder sb = new StringBuilder(); + File dir = new File(protoFolderPath); + buildI(dir, sb); + I = sb.toString(); + recursiveCompile(protocPath, dir, outputDirectory); + } + + private static void buildI(File file, StringBuilder sb) { + if (file.isDirectory()) { + sb.append(" -I="); + sb.append(file.getAbsolutePath()); + File[] files = file.listFiles(); + if (files == null) { + return; + } + for (File f : files) { + buildI(f, sb); + } + } + } + + public static void recursiveCompile(String protocPath, File file, String outputDirectory) { + if (file.isDirectory()) { + File[] files = file.listFiles(); + if (files == null) { + return; + } + for (File f : files) { + recursiveCompile(protocPath, f, outputDirectory); + } + } else { + compiler(protocPath, outputDirectory, file.getAbsolutePath()); + + } + } + + private static void compiler(String protocPath, String outputDirectory, String protoPath) { + try { + // 构建 protoc 命令 + String command = protocPath + I + " --java_out=" + outputDirectory + " " + protoPath; + System.out.println(command); + + // 在命令行运行 protoc 命令 + Process process = Runtime.getRuntime().exec(command); + + // 读取并打印输出 + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line; + while ((line = reader.readLine()) != null) { + System.out.println(line); + } + + // 等待进程完成 + int exitCode = process.waitFor(); + + if (exitCode == 0) { + System.out.println("Proto file compilation successful."); + } else { + System.err.println("Error during proto file compilation."); + } + + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + } +}