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

@ -45,10 +45,11 @@ type BasicCompiler struct {
config configs.ConfigItem
pluginManager pluginmanager.PluginManager
protocPath string
arguments []string
googleapisPath string
dir string
protocPath string
arguments []string
// map[name]local
repositories map[string]string
dir string
}
// NewCompiler is used to create a compiler
@ -73,7 +74,10 @@ func NewBasicCompiler(
config: config,
pluginManager: pluginManager,
}
if err := basic.calcGoogleAPIs(ctx); err != nil {
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 {
@ -98,7 +102,7 @@ func (b *BasicCompiler) Compile(ctx context.Context, protoFilePath string) error
consts.KeySourceRelative) {
arguments = append(arguments, "--proto_path="+filepath.Dir(protoFilePath))
}
arguments = util.DeduplicateSliceStably(arguments)
arguments = append(arguments, protoFilePath)
_, err := command.Execute(ctx,
b.Logger, b.dir, b.protocPath, arguments, nil)
@ -117,7 +121,7 @@ func (b *BasicCompiler) GetConfig(ctx context.Context) configs.ConfigItem {
func (b *BasicCompiler) calcDir(ctx context.Context) error {
if dir := b.config.Config().ProtocWorkDir; dir != "" {
dir = util.RenderPathWithEnv(dir)
dir = util.RenderPathWithEnv(dir, nil)
if !filepath.IsAbs(dir) {
dir = filepath.Join(b.config.Path(), dir)
}
@ -128,30 +132,6 @@ func (b *BasicCompiler) calcDir(ctx context.Context) error {
return nil
}
func (b *BasicCompiler) calcGoogleAPIs(ctx context.Context) error {
cfg := b.config.Config()
commitId := cfg.GoogleAPIs
if commitId == "" {
return nil
}
if !util.Contains(cfg.ImportPaths, consts.KeyPowerProtoGoogleAPIs) {
return nil
}
if commitId == "latest" {
latestVersion, err := b.pluginManager.GetGoogleAPIsLatestVersion(ctx)
if err != nil {
return err
}
commitId = latestVersion
}
local, err := b.pluginManager.InstallGoogleAPIs(ctx, commitId)
if err != nil {
return err
}
b.googleapisPath = local
return nil
}
func (b *BasicCompiler) calcProto(ctx context.Context) error {
cfg := b.config
protocVersion := cfg.Config().Protoc
@ -179,17 +159,10 @@ func (b *BasicCompiler) calcArguments(ctx context.Context) error {
// build import paths
Loop:
for _, path := range cfg.Config().ImportPaths {
switch path {
case consts.KeyPowerProtoInclude:
path = b.pluginManager.IncludePath(ctx)
case consts.KeyPowerProtoGoogleAPIs:
if b.googleapisPath != "" {
path = b.googleapisPath
}
case consts.KeySourceRelative:
if path == consts.KeySourceRelative {
continue Loop
}
path = util.RenderPathWithEnv(path)
path = util.RenderPathWithEnv(path, b.repositories)
if !filepath.IsAbs(path) {
path = filepath.Join(dir, path)
}
@ -219,3 +192,26 @@ Loop:
b.arguments = arguments
return nil
}
func (b *BasicCompiler) calcRepositories(ctx context.Context) error {
cfg := b.config
for name, pkg := range cfg.Config().Repositories {
path, version, ok := util.SplitGoPackageVersion(pkg)
if !ok {
return 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)
if err != nil {
return errors.Wrap(err, "failed to get plugin path")
}
b.repositories[name] = local
}
return nil
}