protoc

Protocol Buffers可以使用Google提供的protoc来生成不同语言的版本.

protoc -I=$SRC_DIR --type_out=$DST_DIR $SRC_DIR/file.proto
  • $SRC_DIR: proto源文件目录
  • type_out:支持的输出语言
  • $DST_DIR: 输出目录
  • file.proto: proto文件名

type_out支持的输出类型有:

类型语言输出文件
cpp_outC++file.pb.cc,file.pb.h
csharp_outC#file.cs
go_outGofile.pb.go
java_outJavafile..java

js/ts

对于js/ts的生成,可以使用protobufjs插件来完成

npm install -g protobufjs
# 生成js
pbjs -t static-module -w commonjs -o  file.js file.proto

# 生成ts
pbts -o file.ts file.js

脚本实现多proto生成

自己用python 3.7写了一个proto生成的脚本.https://github.com/zngw/protocol

将所有要生成proto源文件放在proto_raw目录中,可以是多个文件

//登录请求
message GameLoginReq {
	string account = 1;		// 登录帐号
	string password = 2;	// 登录密码
}

//登录返回
message GameLoginResp {
	int32 result = 1;	// 返回结果.
}

然后使用不同的to_xx.py生成不同的语言.生成的文件在out目录中