feat(*): support repositories

Signed-off-by: storyicon <yuanchao@bilibili.com>
This commit is contained in:
storyicon 2021-07-23 14:56:00 +08:00
parent 1c73b92f0f
commit 9a99c53c5b
No known key found for this signature in database
GPG key ID: 245915D985F966CF
18 changed files with 542 additions and 255 deletions

View file

@ -15,6 +15,8 @@
package build
import (
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
@ -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
@ -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
}
@ -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,
},
}
@ -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)

View file

@ -20,35 +20,50 @@ import (
// Plugin defines the plugin options
type Plugin struct {
OptionsValue string
Name string
Pkg string
Options []string
}
// String implements the standard string interface
func (options *Plugin) String() string {
return fmt.Sprintf("%s: %s", options.Name, options.Pkg)
// GetOptionsValue is used to get options value of plugin
func (plugin *Plugin) GetOptionsValue() string {
if plugin.OptionsValue != "" {
return plugin.OptionsValue
}
return fmt.Sprintf("%s: %s", plugin.Name, plugin.Pkg)
}
// GetPluginProtocGenGo is used to get protoc-gen-go plugin
func GetPluginProtocGenGo() *Plugin {
return &Plugin{
Name: "protoc-gen-go",
Pkg: "google.golang.org/protobuf/cmd/protoc-gen-go@latest",
Options: []string{
"--go_out=.",
"--go_opt=paths=source_relative",
},
}
}
// GetPluginProtocGenGoGRPC is used to get protoc-gen-go-grpc plugin
func GetPluginProtocGenGoGRPC() *Plugin {
return &Plugin{
Name: "protoc-gen-go-grpc",
Pkg: "google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest",
Options: []string{
"--go-grpc_out=.",
"--go-grpc_opt=paths=source_relative",
},
}
}
// GetWellKnownPlugins is used to get well known plugins
func GetWellKnownPlugins() []*Plugin {
return []*Plugin{
{
Name: "protoc-gen-go",
Pkg: "google.golang.org/protobuf/cmd/protoc-gen-go@latest",
Options: []string{
"--go_out=.",
"--go_opt=paths=source_relative",
},
},
{
Name: "protoc-gen-go-grpc",
Pkg: "google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest",
Options: []string{
"--go-grpc_out=.",
"--go-grpc_opt=paths=source_relative",
},
},
GetPluginProtocGenGo(),
GetPluginProtocGenGoGRPC(),
{
Name: "protoc-gen-grpc-gateway",
Pkg: "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest",
@ -56,6 +71,15 @@ func GetWellKnownPlugins() []*Plugin {
"--grpc-gateway_out=.",
},
},
{
OptionsValue: "protoc-gen-go-grpc (SupportPackageIsVersion6)",
Name: "protoc-gen-go-grpc",
Pkg: "google.golang.org/grpc/cmd/protoc-gen-go-grpc@ad51f572fd270f2323e3aa2c1d2775cab9087af2",
Options: []string{
"--go-grpc_out=.",
"--go-grpc_opt=paths=source_relative",
},
},
{
Name: "protoc-gen-openapiv2",
Pkg: "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest",
@ -98,7 +122,7 @@ func GetWellKnownPlugins() []*Plugin {
func GetPluginFromOptionsValue(val string) (*Plugin, bool) {
plugins := GetWellKnownPlugins()
for _, plugin := range plugins {
if plugin.String() == val {
if plugin.GetOptionsValue() == val {
return plugin, true
}
}
@ -110,7 +134,7 @@ func GetWellKnownPluginsOptionValues() []string {
plugins := GetWellKnownPlugins()
packages := make([]string, 0, len(plugins))
for _, plugin := range plugins {
packages = append(packages, plugin.String())
packages = append(packages, plugin.GetOptionsValue())
}
return packages
}

View file

@ -0,0 +1,88 @@
// Copyright 2021 storyicon@foxmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package build
import (
"fmt"
"strings"
)
// Repository defines the plugin options
type Repository struct {
OptionsValue string
Name string
Pkg string
ImportPaths []string
}
// OptionsValue is used to return the options value
func (repo *Repository) GetOptionsValue() string {
if repo.OptionsValue != "" {
return repo.OptionsValue
}
return fmt.Sprintf("%s: %s", strings.ToLower(repo.Name), repo.Pkg)
}
// GetWellKnownRepositories is used to get well known plugins
func GetWellKnownRepositories() []*Repository {
return []*Repository{
GetRepositoryGoogleAPIs(),
GetRepositoryGoGoProtobuf(),
}
}
// GetRepositoryGoGoProtobuf is used to get gogo protobuf repository
func GetRepositoryGoGoProtobuf() *Repository {
return &Repository{
Name: "GOGO_PROTOBUF",
Pkg: "https://github.com/gogo/protobuf@226206f39bd7276e88ec684ea0028c18ec2c91ae",
ImportPaths: []string{
"$GOGO_PROTOBUF",
},
}
}
// GetRepositoryGoogleAPIs is used to get google apis repository
func GetRepositoryGoogleAPIs() *Repository {
return &Repository{
Name: "GOOGLE_APIS",
Pkg: "https://github.com/googleapis/googleapis@75e9812478607db997376ccea247dd6928f70f45",
ImportPaths: []string{
"$GOOGLE_APIS/github.com/googleapis/googleapis",
},
}
}
// GetRepositoryFromOptionsValue is used to get plugin by option value
func GetRepositoryFromOptionsValue(val string) (*Repository, bool) {
repositories := GetWellKnownRepositories()
for _, repo := range repositories {
if repo.GetOptionsValue() == val {
return repo, true
}
}
return nil, false
}
// GetWellKnownRepositoriesOptionValues is used to get option values of well known plugins
func GetWellKnownRepositoriesOptionValues() []string {
repos := GetWellKnownRepositories()
packages := make([]string, 0, len(repos))
for _, repo := range repos {
packages = append(packages, repo.GetOptionsValue())
}
return packages
}

View file

@ -48,7 +48,7 @@ func tidy(ctx context.Context,
if err := bootstraps.StepInstallProtoc(ctx, pluginManager, configItems); err != nil {
return err
}
if err := bootstraps.StepInstallGoogleAPIs(ctx, pluginManager, configItems); err != nil {
if err := bootstraps.StepInstallRepositories(ctx, pluginManager, configItems); err != nil {
return err
}
if err := bootstraps.StepInstallPlugins(ctx, pluginManager, configItems); err != nil {