proto2swift 需要使用第三方插件 GitHub - apple/swift-protobuf: Plugin and runtime library for using protobuf with Swift
需要在不同系统环境下生成 protoc-gen-swift 编译插件,在 linux 中编译需要安装 swift。
安装 Swift
下面是 centos 下安装 swift 的步骤 首先到 swift 官网下载 release 版本编译好的二进制包
解压后将路径配置到环境变量
PATH=$PATH:/usr/local/swift/swift-5.3.3-RELEASE-centos7/usr/bin
export PATH
查看 swift 是否成功配置
swift -version
# 输出内容以下类似内容则为成功
# Swift version 5.3.3 (swift-5.3.3-RELEASE)
# Target: x86_64-unknown-linux-gnu
构建 protoc-gen-swift 插件
拉取 swift-protobuf 项目,执行构建
git clone https://github.com/apple/swift-protobuf
swift build -c release
执行成功后将在 .build/release 目录下生成 protoc-gen-swift 文件,然后将 protoc-gen-swift 配置到环境变量。
转换
执行转换,输出 .swift 文件
protoc --swift_out=. my.proto