添加 protobuf

This commit is contained in:
tiger_zhou 2024-01-31 11:13:17 +08:00
parent 006367f3de
commit 92bfa114be
1 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,101 @@
package club.joylink.rtss;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import org.assertj.core.util.Lists;
public class GenerateProtoBufUtil {
private static final String EXE_BIN_FILE =
String.join(
File.separator,
System.getProperty("user.dir"),
"rtss-message",
"protoc-23.1",
"bin",
"win64",
"protoc");
private File protoFilePath;
private String outPath;
private boolean pullMessage;
// private String gitPullPath;
public GenerateProtoBufUtil(String rootPath, boolean pullMessage) {
this.pullMessage = pullMessage;
String sourcePath = String.join(File.separator, rootPath, "rtss-message", "protos");
this.outPath = String.join(File.separator, rootPath, "src", "main", "java");
this.protoFilePath = new File(sourcePath);
if (!protoFilePath.exists() || !protoFilePath.isDirectory()) {
throw new RuntimeException("proto不是目录或目录不存在");
}
}
public List<File> findFiles() {
return this.findFiles(this.protoFilePath);
}
public void execCommand() throws IOException {
if (this.pullMessage) {
this.PullTmmsMessage();
}
for (File file : this.findFiles()) {
String command =
String.format(
"%s --proto_path=%s -I=%s --java_out=%s %s",
EXE_BIN_FILE,
this.protoFilePath.getPath(),
file.getParentFile().getAbsolutePath(),
this.outPath,
file.getName());
this.genertateFile(command);
}
}
private void genertateFile(String command) throws IOException {
System.out.println(String.format("执行指令: %s", command));
InputStream errorStream = Runtime.getRuntime().exec(command).getErrorStream();
byte[] ebs = new byte[512];
StringBuilder sb = new StringBuilder();
int size;
while ((size = errorStream.read(ebs)) > 0) {
String err = new String(Arrays.copyOf(ebs, size), Charset.forName("UTF-8"));
sb.append(err);
}
System.out.println(sb);
}
private List<File> findFiles(File file) {
List<File> findFiles = Lists.newArrayList();
for (File files : file.listFiles()) {
if (files.isDirectory()) {
findFiles.addAll(this.findFiles(files));
} else if (files.getName().endsWith(".proto")) {
findFiles.add(files);
}
}
return findFiles;
}
private void PullTmmsMessage() throws IOException {
InputStream inputStream =
Runtime.getRuntime().exec("git pull", null, protoFilePath).getInputStream();
byte[] ebs = new byte[512];
StringBuilder sb = new StringBuilder();
int size;
while ((size = inputStream.read(ebs)) > 0) {
String err = new String(Arrays.copyOf(ebs, size), Charset.forName("UTF-8"));
sb.append(err);
}
System.out.println(sb);
}
public static void main(String[] args) throws IOException {
GenerateProtoBufUtil gu = new GenerateProtoBufUtil(System.getProperty("user.dir"), true);
gu.execCommand();
}
}