Skip to content

Commit

Permalink
feat(*): support repositories
Browse files Browse the repository at this point in the history
Signed-off-by: storyicon <yuanchao@bilibili.com>
  • Loading branch information
storyicon committed Jul 23, 2021
1 parent 1c73b92 commit 9a99c53
Show file tree
Hide file tree
Showing 18 changed files with 539 additions and 252 deletions.
36 changes: 26 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ PowerProto is used to solve the following three main problems:
5. support batch and recursive compilation of proto files to improve efficiency.
6. cross-platform support PostAction, you can perform some routine operations (such as replacing "omitempty" in all generated files) after the compilation.
7. support PostShell, execute specific shell scripts after the compilation.
8. one-click installation and version control of google apis。
8. one-click installation and version control of google apis and gogo protobuf etc

## Installation and Dependencies

Expand Down Expand Up @@ -179,6 +179,8 @@ protocWorkDir: ""
plugins:
protoc-gen-go: google.golang.org/protobuf/cmd/protoc-gen-go@latest
protoc-gen-go-grpc: google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
repositories:
GOOGLE_APIS: https://github.com/googleapis/googleapis@75e9812478607db997376ccea247dd6928f70f45
options:
- --go_out=.
- --go_opt=paths=source_relative
Expand All @@ -188,6 +190,7 @@ importPaths:
- .
- $GOPATH
- $POWERPROTO_INCLUDE
- $GOOGLE_APIS/github.com/googleapis/googleapis
postActions: []
postShell: ""
```
Expand Down Expand Up @@ -222,6 +225,7 @@ $POWERPROTO_HOME/protoc/3.17.3/protoc --go_out=. \
--proto_path=/mnt/data/hello \
--proto_path=$GOPATH \
--proto_path=$POWERPROTO_HOME/include \
--proto_path=$POWERPROTO_HOME/gits/75e9812478607db997376ccea247dd6928f70f45/github.com/googleapis/googleapis \
--plugin=protoc-gen-go=$POWERPROTO_HOME/plugins/google.golang.org/protobuf/cmd/protoc-gen-go@v1.27.1/protoc-gen-go \
--plugin=protoc-gen-go-grpc=$POWERPROTO_HOME/plugins/google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1.0/protoc-gen-go-grpc \
/mnt/data/hello/apis/hello.proto
Expand Down Expand Up @@ -249,9 +253,17 @@ protoc: 3.17.3
# the default is the directory where the config file is located.
# support mixed environment variables in path, such as $GOPATH
protocWorkDir: ""
# optional. If you need to use googleapis, you should fill in the commit id of googleapis here.
# You can fill in the latest, it will be automatically converted to the latest version.
googleapis: 75e9812478607db997376ccea247dd6928f70f45
# optional. define dependent Git repositories
# Generally used for dependency control of public protobuf libraries
repositories:
# Definition depends on the 27156597fdf4fb77004434d4409154a230dc9a32 version of https://github.com/googleapis/googleapis
# and defines its name as GOOGLE_APIS
# It can be referenced in importPaths by $GOOGLE_APIS
GOOGLE_APIS: https://github.com/googleapis/googleapis@27156597fdf4fb77004434d4409154a230dc9a32
# Definition depends on the 226206f39bd7276e88ec684ea0028c18ec2c91ae version of https://github.com/gogo/protobuf
# and defines its name as GOGO_PROTOBUF
# It can be referenced in the importPaths by $GOGO_PROTOBUF
GOGO_PROTOBUF: https://github.com/gogo/protobuf@226206f39bd7276e88ec684ea0028c18ec2c91ae
# required. it is used to describe which plug-ins are required for compilation
plugins:
# the name, path, and version number of the plugin.
Expand Down Expand Up @@ -280,8 +292,10 @@ importPaths:
# Special variables. Reference to the directory where the proto file to be compiled is located
# For example, if /a/b/data.proto is to be compiled, then the /a/b directory will be automatically referenced
- $SOURCE_RELATIVE
# Special variables. Will be replaced with the local path to the version of google apis specified by the googleapis field
- $POWERPROTO_GOOGLEAPIS
# References GOOGLE_APIS as defined in repositories
- $GOOGLE_APIS/github.com/googleapis/googleapis
# References GOGO_PROTOBUF as defined in repositories
- $GOGO_PROTOBUF
# optional. The operation is executed after compilation.
# its working directory is the directory where the config file is located.
# postActions is cross-platform compatible.
Expand Down Expand Up @@ -312,10 +326,11 @@ scopes:
- ./apis1
protoc: v3.17.3
protocWorkDir: ""
googleapis: 75e9812478607db997376ccea247dd6928f70f45
plugins:
protoc-gen-go: google.golang.org/protobuf/cmd/protoc-gen-go@v1.25.0
protoc-gen-go-grpc: google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1.0
repositories:
GOOGLE_APIS: https://github.com/googleapis/googleapis@75e9812478607db997376ccea247dd6928f70f45
options:
- --go_out=.
- --go_opt=paths=source_relative
Expand All @@ -325,6 +340,7 @@ importPaths:
- .
- $GOPATH
- $POWERPROTO_INCLUDE
- $GOOGLE_APIS/github.com/googleapis/googleapis
postActions: []
postShell: ""

Expand All @@ -334,10 +350,11 @@ scopes:
- ./apis2
protoc: v3.17.3
protocWorkDir: ""
googleapis: 75e9812478607db997376ccea247dd6928f70f45
plugins:
protoc-gen-go: google.golang.org/protobuf/cmd/protoc-gen-go@v1.27.0
protoc-gen-go-grpc: google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1.0
repositories:
GOOGLE_APIS: https://github.com/googleapis/googleapis@75e9812478607db997376ccea247dd6928f70f45
options:
- --go_out=.
- --go_opt=paths=source_relative
Expand All @@ -347,12 +364,11 @@ importPaths:
- .
- $GOPATH
- $POWERPROTO_INCLUDE
- $GOOGLE_APIS/github.com/googleapis/googleapis
postActions: []
postShell: ""
```
### PostAction
PostAction allows to perform specific actions after all proto files have been compiled. In contrast to `PostShell`, it is cross-platform supported.
Expand Down
34 changes: 26 additions & 8 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ PowerProto主要用于解决下面三个问题:
5. 支持批量、递归编译proto文件,提高效率。
6. 跨平台支持PostAction,可以在编译完成之后执行一些常规操作(比如替换掉所有生成文件中的"omitempty")。
7. 支持PostShell,在编译完成之后执行特定的shell脚本。
8. 支持 google api 的一键安装与版本控制
8. 支持 google api, gogo protobuf 等的一键安装与版本控制

## 安装与依赖

Expand Down Expand Up @@ -171,6 +171,8 @@ protocWorkDir: ""
plugins:
protoc-gen-go: google.golang.org/protobuf/cmd/protoc-gen-go@latest
protoc-gen-go-grpc: google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
repositories:
GOOGLE_APIS: https://github.com/googleapis/googleapis@75e9812478607db997376ccea247dd6928f70f45
options:
- --go_out=.
- --go_opt=paths=source_relative
Expand All @@ -180,6 +182,7 @@ importPaths:
- .
- $GOPATH
- $POWERPROTO_INCLUDE
- $GOOGLE_APIS/github.com/googleapis/googleapis
postActions: []
postShell: ""
```
Expand Down Expand Up @@ -214,6 +217,7 @@ $POWERPROTO_HOME/protoc/3.17.3/protoc --go_out=. \
--proto_path=/mnt/data/hello \
--proto_path=$GOPATH \
--proto_path=$POWERPROTO_HOME/include \
--proto_path=$POWERPROTO_HOME/gits/75e9812478607db997376ccea247dd6928f70f45/github.com/googleapis/googleapis \
--plugin=protoc-gen-go=$POWERPROTO_HOME/plugins/google.golang.org/protobuf/cmd/protoc-gen-go@v1.27.1/protoc-gen-go \
--plugin=protoc-gen-go-grpc=$POWERPROTO_HOME/plugins/google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1.0/protoc-gen-go-grpc
/mnt/data/hello/apis/hello.proto
Expand All @@ -239,9 +243,17 @@ protoc: 3.17.3
# 选填,执行protoc命令的工作目录,默认是配置文件所在目录
# 支持路径中混用环境变量,比如$GOPATH
protocWorkDir: ""
# 选填,如果需要使用 googleapis,你应该在这里填写googleapis的commit id
# 可以填 latest,会自动转换成最新的版本
googleapis: 75e9812478607db997376ccea247dd6928f70f45
# 选填,定义依赖的Git存储库
# 一般用于公共的protobuf库的依赖控制
repositories:
# 定义依赖 27156597fdf4fb77004434d4409154a230dc9a32 版本的 https://github.com/googleapis/googleapis
# 并且定义其名字为 GOOGLE_APIS
# 在 importPaths 中可以通过 $GOOGLE_APIS 来引用它
GOOGLE_APIS: https://github.com/googleapis/googleapis@27156597fdf4fb77004434d4409154a230dc9a32
# 定义依赖 226206f39bd7276e88ec684ea0028c18ec2c91ae 版本的 https://github.com/gogo/protobuf
# 并且定义其名字为 GOGO_PROTOBUF
# 在 importPaths 中可以通过 $GOGO_PROTOBUF 来引用它
GOGO_PROTOBUF: https://github.com/gogo/protobuf@226206f39bd7276e88ec684ea0028c18ec2c91ae
# 必填,代表scope匹配的目录中的proto文件,在编译时需要用到哪些插件
plugins:
# 插件的名字、路径以及版本号。
Expand All @@ -268,8 +280,10 @@ importPaths:
# 特殊变量。引用待编译的proto文件所在的目录
# 比如将要编译 /a/b/data.proto,那么 /a/b 目录将会被自动引用
- $SOURCE_RELATIVE
# 特殊变量。引用googleapis字段所指定的版本的google apis
- $POWERPROTO_GOOGLEAPIS
# 引用 repositories 中的 GOOGLE_APIS
- $GOOGLE_APIS/github.com/googleapis/googleapis
# 引用 repositories 中的 GOGO_PROTOBUF
- $GOGO_PROTOBUF
# 选填,构建完成之后执行的操作,工作目录是配置文件所在目录
# postActions是跨平台兼容的
# 注意,必须在 powerproto build 时附加 -p 参数,才会执行配置文件中的postActions
Expand Down Expand Up @@ -299,10 +313,11 @@ scopes:
- ./apis1
protoc: v3.17.3
protocWorkDir: ""
googleapis: 75e9812478607db997376ccea247dd6928f70f45
plugins:
protoc-gen-go: google.golang.org/protobuf/cmd/protoc-gen-go@v1.25.0
protoc-gen-go-grpc: google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1.0
repositories:
GOOGLE_APIS: https://github.com/googleapis/googleapis@75e9812478607db997376ccea247dd6928f70f45
options:
- --go_out=.
- --go_opt=paths=source_relative
Expand All @@ -312,6 +327,7 @@ importPaths:
- .
- $GOPATH
- $POWERPROTO_INCLUDE
- $GOOGLE_APIS/github.com/googleapis/googleapis
postActions: []
postShell: ""

Expand All @@ -321,10 +337,11 @@ scopes:
- ./apis2
protoc: v3.17.3
protocWorkDir: ""
googleapis: 75e9812478607db997376ccea247dd6928f70f45
plugins:
protoc-gen-go: google.golang.org/protobuf/cmd/protoc-gen-go@v1.27.0
protoc-gen-go-grpc: google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.1.0
repositories:
GOOGLE_APIS: https://github.com/googleapis/googleapis@75e9812478607db997376ccea247dd6928f70f45
options:
- --go_out=.
- --go_opt=paths=source_relative
Expand All @@ -334,6 +351,7 @@ importPaths:
- .
- $GOPATH
- $POWERPROTO_INCLUDE
- $GOOGLE_APIS/github.com/googleapis/googleapis
postActions: []
postShell: ""
```
Expand Down
62 changes: 37 additions & 25 deletions cmd/powerproto/subcommands/init/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package build

import (
"fmt"

"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"

Expand All @@ -26,7 +28,8 @@ import (

// UserPreference defines the model of user preference
type UserPreference struct {
Plugins []string `survey:"plugins"`
Plugins []string `survey:"plugins"`
Repositories []string `survey:"repositories"`
}

// GetUserPreference is used to get user preference
Expand All @@ -40,7 +43,25 @@ func GetUserPreference() (*UserPreference, error) {
Options: GetWellKnownPluginsOptionValues(),
},
},
{
Name: "repositories",
Prompt: &survey.MultiSelect{
Message: "select repositories to use. Later, you can also manually add in the configuration file",
Options: GetWellKnownRepositoriesOptionValues(),
},
},
}, &preference)
if len(preference.Plugins) == 0 {
preference.Plugins = []string{
GetPluginProtocGenGo().GetOptionsValue(),
GetPluginProtocGenGoGRPC().GetOptionsValue(),
}
}
if len(preference.Repositories) == 0 {
preference.Repositories = []string{
GetRepositoryGoogleAPIs().GetOptionsValue(),
}
}
return &preference, err
}

Expand All @@ -50,23 +71,14 @@ func GetDefaultConfig() *configs.Config {
Scopes: []string{
"./",
},
Protoc: "latest",
GoogleAPIs: "75e9812478607db997376ccea247dd6928f70f45",
Plugins: map[string]string{
"protoc-gen-go": "google.golang.org/protobuf/cmd/protoc-gen-go@latest",
"protoc-gen-go-grpc": "google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest",
},
Options: []string{
"--go_out=.",
"--go_opt=paths=source_relative",
"--go-grpc_out=.",
"--go-grpc_opt=paths=source_relative",
},
Protoc: "latest",
Plugins: map[string]string{},
Repositories: map[string]string{},
Options: []string{},
ImportPaths: []string{
".",
"$GOPATH",
consts.KeyPowerProtoInclude,
consts.KeyPowerProtoGoogleAPIs,
consts.KeySourceRelative,
},
}
Expand Down Expand Up @@ -95,18 +107,18 @@ func CommandInit(log logger.Logger) *cobra.Command {
return
}
config := GetDefaultConfig()
if len(preference.Plugins) != 0 {
var compileOptions []string
plugins := map[string]string{}
for _, val := range preference.Plugins {
plugin, ok := GetPluginFromOptionsValue(val)
if ok {
plugins[plugin.Name] = plugin.Pkg
compileOptions = append(compileOptions, plugin.Options...)
}
for _, val := range preference.Plugins {
if plugin, ok := GetPluginFromOptionsValue(val); ok {
config.Plugins[plugin.Name] = plugin.Pkg
config.Options = append(config.Options, plugin.Options...)
}
}
fmt.Println(">>>>>>>>>>>>>>", preference.Repositories)
for _, val := range preference.Repositories {
if repo, ok := GetRepositoryFromOptionsValue(val); ok {
config.Repositories[repo.Name] = repo.Pkg
config.ImportPaths = append(config.ImportPaths, repo.ImportPaths...)
}
config.Plugins = plugins
config.Options = compileOptions
}
if err := configs.SaveConfigs(consts.ConfigFileName, config); err != nil {
log.LogFatal(nil, "failed to save config: %s", err)
Expand Down
Loading

0 comments on commit 9a99c53

Please sign in to comment.