feat(*): support perComandTimeout & variables in options

Signed-off-by: storyicon <yuanchao@bilibili.com>
This commit is contained in:
storyicon 2021-08-28 14:36:47 +08:00
parent 7b2c0762d4
commit 15047711a9
No known key found for this signature in database
GPG key ID: 245915D985F966CF
15 changed files with 302 additions and 122 deletions

View file

@ -44,12 +44,6 @@ type BasicCompiler struct {
logger.Logger
config configs.ConfigItem
pluginManager pluginmanager.PluginManager
protocPath string
arguments []string
// map[name]local
repositories map[string]string
dir string
}
// NewCompiler is used to create a compiler
@ -69,43 +63,28 @@ func NewBasicCompiler(
pluginManager pluginmanager.PluginManager,
config configs.ConfigItem,
) (*BasicCompiler, error) {
basic := &BasicCompiler{
return &BasicCompiler{
Logger: log.NewLogger("compiler"),
config: config,
pluginManager: pluginManager,
}
basic.repositories = map[string]string{
consts.KeyNamePowerProtocInclude: basic.pluginManager.IncludePath(ctx),
}
if err := basic.calcRepositories(ctx); err != nil {
return nil, err
}
if err := basic.calcProto(ctx); err != nil {
return nil, err
}
if err := basic.calcArguments(ctx); err != nil {
return nil, err
}
if err := basic.calcDir(ctx); err != nil {
return nil, err
}
return basic, nil
}, nil
}
// Compile is used to compile proto file
func (b *BasicCompiler) Compile(ctx context.Context, protoFilePath string) error {
arguments := make([]string, len(b.arguments))
copy(arguments, b.arguments)
// contains source relative
if util.Contains(b.config.Config().ImportPaths,
consts.KeySourceRelative) {
arguments = append(arguments, "--proto_path="+filepath.Dir(protoFilePath))
dir := b.calcDir()
protocPath, err := b.calcProtocPath(ctx)
if err != nil {
return err
}
arguments, err := b.calcArguments(ctx, protoFilePath)
if err != nil {
return err
}
arguments = util.DeduplicateSliceStably(arguments)
arguments = append(arguments, protoFilePath)
_, err := command.Execute(ctx,
b.Logger, b.dir, b.protocPath, arguments, nil)
_, err = command.Execute(ctx,
b.Logger, dir, protocPath, arguments, nil)
if err != nil {
return &ErrCompile{
ErrCommandExec: err.(*command.ErrCommandExec),
@ -119,99 +98,85 @@ func (b *BasicCompiler) GetConfig(ctx context.Context) configs.ConfigItem {
return b.config
}
func (b *BasicCompiler) calcDir(ctx context.Context) error {
func (b *BasicCompiler) calcProtocPath(ctx context.Context) (string, error) {
return b.pluginManager.GetPathForProtoc(ctx, b.config.Config().Protoc)
}
func (b *BasicCompiler) calcDir() string {
if dir := b.config.Config().ProtocWorkDir; dir != "" {
dir = util.RenderPathWithEnv(dir, nil)
if !filepath.IsAbs(dir) {
dir = filepath.Join(b.config.Path(), dir)
}
b.dir = dir
return dir
} else {
b.dir = filepath.Dir(b.config.Path())
return filepath.Dir(b.config.Path())
}
return nil
}
func (b *BasicCompiler) calcProto(ctx context.Context) error {
cfg := b.config
protocVersion := cfg.Config().Protoc
if protocVersion == "latest" {
latestVersion, err := b.pluginManager.GetProtocLatestVersion(ctx)
if err != nil {
return err
}
protocVersion = latestVersion
}
localPath, err := b.pluginManager.InstallProtoc(ctx, protocVersion)
func (b *BasicCompiler) calcArguments(ctx context.Context, protoFilePath string) ([]string, error) {
variables, err := b.calcVariables(ctx, protoFilePath)
if err != nil {
return err
return nil, err
}
b.protocPath = localPath
return nil
}
func (b *BasicCompiler) calcArguments(ctx context.Context) error {
cfg := b.config
arguments := make([]string, len(cfg.Config().Options))
copy(arguments, cfg.Config().Options)
var arguments []string
dir := filepath.Dir(cfg.Path())
// build import paths
Loop:
for _, path := range cfg.Config().ImportPaths {
if path == consts.KeySourceRelative {
continue Loop
// build plugin options
for name, pkg := range cfg.Config().Plugins {
path, version, ok := util.SplitGoPackageVersion(pkg)
if !ok {
return nil, errors.Errorf("failed to parse: %s", pkg)
}
path = util.RenderPathWithEnv(path, b.repositories)
local, err := b.pluginManager.GetPathForPlugin(ctx, path, version)
if err != nil {
return nil, errors.Wrap(err, "failed to get plugin path")
}
arg := fmt.Sprintf("--plugin=%s=%s", name, local)
arguments = append(arguments, arg)
}
// build compile options
for _, option := range cfg.Config().Options {
option = util.RenderWithEnv(option, variables)
arguments = append(arguments, option)
}
// build import paths
dir := filepath.Dir(cfg.Path())
for _, path := range cfg.Config().ImportPaths {
path = util.RenderPathWithEnv(path, variables)
if !filepath.IsAbs(path) {
path = filepath.Join(dir, path)
}
arguments = append(arguments, "--proto_path="+path)
}
// build plugin options
for name, pkg := range cfg.Config().Plugins {
path, version, ok := util.SplitGoPackageVersion(pkg)
if !ok {
return errors.Errorf("failed to parse: %s", pkg)
}
if version == "latest" {
latestVersion, err := b.pluginManager.GetPluginLatestVersion(ctx, path)
if err != nil {
return err
}
version = latestVersion
}
local, err := b.pluginManager.InstallPlugin(ctx, path, version)
if err != nil {
return errors.Wrap(err, "failed to get plugin path")
}
arg := fmt.Sprintf("--plugin=%s=%s", name, local)
arguments = append(arguments, arg)
}
b.arguments = arguments
return nil
arguments = util.DeduplicateSliceStably(arguments)
return arguments, nil
}
func (b *BasicCompiler) calcRepositories(ctx context.Context) error {
func (b *BasicCompiler) calcVariables(ctx context.Context, protoFilePath string) (map[string]string, error) {
cfg := b.config
variables := map[string]string{}
for name, pkg := range cfg.Config().Repositories {
path, version, ok := util.SplitGoPackageVersion(pkg)
_, version, ok := util.SplitGoPackageVersion(pkg)
if !ok {
return errors.Errorf("failed to parse: %s", pkg)
return nil, errors.Errorf("failed to parse: %s", pkg)
}
if version == "latest" {
latestVersion, err := b.pluginManager.GetGitRepoLatestVersion(ctx, path)
if err != nil {
return err
}
version = latestVersion
}
local, err := b.pluginManager.InstallGitRepo(ctx, path, version)
repoPath, err := b.pluginManager.GitRepoPath(ctx, version)
if err != nil {
return errors.Wrap(err, "failed to get plugin path")
return nil, err
}
b.repositories[name] = local
variables[name] = repoPath
}
return nil
includePath, err := b.pluginManager.IncludePath(ctx)
if err != nil {
return nil, err
}
variables[consts.KeyNamePowerProtocInclude] = includePath
variables[consts.KeyNameSourceRelative] = filepath.Dir(protoFilePath)
return variables, nil
}

View file

@ -21,15 +21,12 @@ import (
"os"
"strings"
"sync"
"time"
"github.com/storyicon/powerproto/pkg/consts"
"github.com/storyicon/powerproto/pkg/util"
"github.com/storyicon/powerproto/pkg/util/logger"
)
var defaultExecuteTimeout = time.Second * 60
// PluginManager is used to manage plugins
type PluginManager interface {
// GetPluginLatestVersion is used to get the latest version of plugin
@ -40,6 +37,8 @@ type PluginManager interface {
IsPluginInstalled(ctx context.Context, path string, version string) (bool, string, error)
// InstallPlugin is used to install plugin
InstallPlugin(ctx context.Context, path string, version string) (local string, err error)
// GetPathForPlugin is used to get path for plugin executable file
GetPathForPlugin(ctx context.Context, path string, version string) (local string, err error)
// GetGitRepoLatestVersion is used to get the latest version of google apis
GetGitRepoLatestVersion(ctx context.Context, uri string) (string, error)
@ -47,8 +46,8 @@ type PluginManager interface {
InstallGitRepo(ctx context.Context, uri string, commitId string) (local string, err error)
// IsGitRepoInstalled is used to check whether the protoc is installed
IsGitRepoInstalled(ctx context.Context, uri string, commitId string) (bool, string, error)
// GitRepoPath returns the googleapis path
GitRepoPath(ctx context.Context, commitId string) string
// GitRepoPath returns the git repo path
GitRepoPath(ctx context.Context, commitId string) (string, error)
// GetProtocLatestVersion is used to get the latest version of protoc
GetProtocLatestVersion(ctx context.Context) (string, error)
@ -59,7 +58,9 @@ type PluginManager interface {
// InstallProtoc is used to install protoc of specified version
InstallProtoc(ctx context.Context, version string) (local string, err error)
// IncludePath returns the default include path
IncludePath(ctx context.Context) string
IncludePath(ctx context.Context) (string, error)
// GetPathForProtoc is used to get the path of protoc
GetPathForProtoc(ctx context.Context, version string) (string, error)
}
// Config defines the config of PluginManager
@ -98,6 +99,9 @@ func NewBasicPluginManager(storageDir string, log logger.Logger) (*BasicPluginMa
// GetPluginLatestVersion is used to get the latest version of plugin
func (b *BasicPluginManager) GetPluginLatestVersion(ctx context.Context, path string) (string, error) {
ctx, cancel := consts.GetContextWithPerCommandTimeout(ctx)
defer cancel()
versions, err := b.ListPluginVersions(ctx, path)
if err != nil {
return "", err
@ -110,7 +114,7 @@ func (b *BasicPluginManager) GetPluginLatestVersion(ctx context.Context, path st
// ListPluginVersions is used to list the versions of plugin
func (b *BasicPluginManager) ListPluginVersions(ctx context.Context, path string) ([]string, error) {
ctx, cancel := context.WithTimeout(ctx, defaultExecuteTimeout)
ctx, cancel := consts.GetContextWithPerCommandTimeout(ctx)
defer cancel()
b.versionsLock.RLock()
@ -134,6 +138,11 @@ func (b *BasicPluginManager) IsPluginInstalled(ctx context.Context, path string,
return IsPluginInstalled(ctx, b.storageDir, path, version)
}
// GetPathForPlugin is used to get path for plugin executable file
func (b *BasicPluginManager) GetPathForPlugin(ctx context.Context, path string, version string) (local string, err error) {
return PathForPlugin(b.storageDir, path, version)
}
// InstallPlugin is used to install plugin
func (b *BasicPluginManager) InstallPlugin(ctx context.Context, path string, version string) (local string, err error) {
return InstallPluginUsingGo(ctx, b.Logger, b.storageDir, path, version)
@ -146,8 +155,9 @@ func (b *BasicPluginManager) GetGitRepoLatestVersion(ctx context.Context, url st
// InstallGitRepo is used to install google apis
func (b *BasicPluginManager) InstallGitRepo(ctx context.Context, uri string, commitId string) (string, error) {
ctx, cancel := context.WithTimeout(ctx, defaultExecuteTimeout)
ctx, cancel := consts.GetContextWithPerCommandTimeout(ctx)
defer cancel()
exists, local, err := b.IsGitRepoInstalled(ctx, uri, commitId)
if err != nil {
return "", err
@ -182,8 +192,8 @@ func (b *BasicPluginManager) IsGitRepoInstalled(ctx context.Context, uri string,
}
// GitRepoPath returns the googleapis path
func (b *BasicPluginManager) GitRepoPath(ctx context.Context, commitId string) string {
return PathForGitRepos(b.storageDir, commitId)
func (b *BasicPluginManager) GitRepoPath(ctx context.Context, commitId string) (string, error) {
return PathForGitRepos(b.storageDir, commitId), nil
}
// IsProtocInstalled is used to check whether the protoc is installed
@ -196,6 +206,9 @@ func (b *BasicPluginManager) IsProtocInstalled(ctx context.Context, version stri
// GetProtocLatestVersion is used to geet the latest version of protoc
func (b *BasicPluginManager) GetProtocLatestVersion(ctx context.Context) (string, error) {
ctx, cancel := consts.GetContextWithPerCommandTimeout(ctx)
defer cancel()
versions, err := b.ListProtocVersions(ctx)
if err != nil {
return "", err
@ -208,7 +221,7 @@ func (b *BasicPluginManager) GetProtocLatestVersion(ctx context.Context) (string
// ListProtocVersions is used to list protoc version
func (b *BasicPluginManager) ListProtocVersions(ctx context.Context) ([]string, error) {
ctx, cancel := context.WithTimeout(ctx, defaultExecuteTimeout)
ctx, cancel := consts.GetContextWithPerCommandTimeout(ctx)
defer cancel()
b.versionsLock.RLock()
@ -227,14 +240,16 @@ func (b *BasicPluginManager) ListProtocVersions(ctx context.Context) ([]string,
return versions, nil
}
// GetPathForProtoc is used to get the path for protoc
func (b *BasicPluginManager) GetPathForProtoc(ctx context.Context, version string) (string, error) {
return PathForProtoc(b.storageDir, version), nil
}
// InstallProtoc is used to install protoc of specified version
func (b *BasicPluginManager) InstallProtoc(ctx context.Context, version string) (string, error) {
ctx, cancel := context.WithTimeout(ctx, defaultExecuteTimeout)
ctx, cancel := consts.GetContextWithPerCommandTimeout(ctx)
defer cancel()
if strings.HasPrefix(version, "v") {
version = strings.TrimPrefix(version, "v")
}
local := PathForProtoc(b.storageDir, version)
exists, err := util.IsFileExists(local)
if err != nil {
@ -266,6 +281,6 @@ func (b *BasicPluginManager) InstallProtoc(ctx context.Context, version string)
}
// IncludePath returns the default include path
func (b *BasicPluginManager) IncludePath(ctx context.Context) string {
return PathForInclude(b.storageDir)
func (b *BasicPluginManager) IncludePath(ctx context.Context) (string, error) {
return PathForInclude(b.storageDir), nil
}

View file

@ -18,6 +18,7 @@ import (
"net/url"
"path"
"path/filepath"
"strings"
"golang.org/x/mod/module"
@ -31,6 +32,9 @@ func PathForInclude(storageDir string) string {
// PathForProtoc is used to get the local binary location where the specified version protoc should be stored
func PathForProtoc(storageDir string, version string) string {
if strings.HasPrefix(version, "v") {
version = strings.TrimPrefix(version, "v")
}
return filepath.Join(storageDir, "protoc", version, util.GetBinaryFileName("protoc"))
}
@ -72,6 +76,7 @@ func PathForPluginDir(storageDir string, path string, version string) (string, e
}
// PathForPlugin is used to get the binary path of plugin
// Path: e.g "google.golang.org/protobuf/cmd/protoc-gen-go"
func PathForPlugin(storageDir string, path string, version string) (string, error) {
name := GetGoPkgExecName(path)
dir, err := PathForPluginDir(storageDir, path, version)

View file

@ -53,6 +53,9 @@ func (p *ProtocRelease) Clear() error {
// GetProtocRelease is used to download protoc release
func GetProtocRelease(ctx context.Context, version string) (*ProtocRelease, error) {
if strings.HasPrefix(version, "v") {
version = strings.TrimPrefix(version, "v")
}
workspace, err := os.MkdirTemp("", "")
if err != nil {
return nil, err