Skip to content

Commit e710f58

Browse files
committed
Merge branch 'main' of github.com:pubgo/protobuild into feat/format
2 parents d6ed94a + 31640cb commit e710f58

4 files changed

Lines changed: 94 additions & 6 deletions

File tree

cmd/protobuild/cmd.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ import (
3030
"google.golang.org/protobuf/proto"
3131
"google.golang.org/protobuf/types/pluginpb"
3232
yaml "gopkg.in/yaml.v3"
33-
34-
_ "github.com/samber/lo"
35-
_ "golang.org/x/mod/module"
3633
)
3734

3835
var (
@@ -334,8 +331,8 @@ func Main() *cli.Command {
334331

335332
var changed bool
336333

337-
// 解析go.mod并获取所有pkg版本
338-
versions := modutil.LoadVersions()
334+
// 通过 go mod graph 获取每个 pkg 最大版本
335+
versions := modutil.LoadVersionGraph()
339336
for i, dep := range globalCfg.Depends {
340337
pathVersion := strings.SplitN(dep.Url, "@", 2)
341338
if len(pathVersion) == 2 {
@@ -357,7 +354,7 @@ func Main() *cli.Command {
357354
v := strutil.FirstFnNotEmpty(func() string {
358355
return versions[url]
359356
}, func() string {
360-
return generic.DePtr(dep.Version)
357+
return generic.FromPtr(dep.Version)
361358
}, func() string {
362359
// go.mod中version不存在, 并且protobuf.yaml也没有指定
363360
// go pkg缓存

docs/docs.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@ package docs
55
// https://github.com/bufbuild/protocompile
66
// https://github.com/mitchellh/protoc-gen-go-json
77
// https://github.com/foxygoat/protog/tree/master/httprule
8+
9+
import (
10+
_ "github.com/samber/lo"
11+
_ "golang.org/x/mod/module"
12+
)

internal/modutil/util.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@ import (
44
"io/ioutil"
55
"os"
66
"path/filepath"
7+
"strings"
78

9+
mapset "github.com/deckarep/golang-set/v2"
10+
ver "github.com/hashicorp/go-version"
811
"github.com/pubgo/funk/assert"
912
"github.com/pubgo/funk/pathutil"
13+
"github.com/pubgo/funk/v2/result"
14+
"github.com/pubgo/protobuild/internal/shutil"
15+
"github.com/samber/lo"
1016
"golang.org/x/mod/modfile"
1117
)
1218

@@ -28,6 +34,35 @@ func GoModPath() string {
2834
return getFileByRecursion("go.mod", pwd)
2935
}
3036

37+
func LoadVersionGraph() map[string]string {
38+
modList := strings.Split(result.Wrap(shutil.GoModGraph()).Must(), "\n")
39+
modSet := mapset.NewSet[string]()
40+
for _, m := range modList {
41+
for _, v := range strings.Split(m, " ") {
42+
modSet.Add(strings.TrimSpace(v))
43+
}
44+
}
45+
46+
var modMap = make(map[string][]*ver.Version)
47+
modSet.Each(func(s string) bool {
48+
ver2 := strings.Split(s, "@")
49+
if len(ver2) != 2 {
50+
return false
51+
}
52+
53+
if !strings.HasPrefix(ver2[1], "v") {
54+
return false
55+
}
56+
57+
modMap[ver2[0]] = append(modMap[ver2[0]], ver.Must(ver.NewSemver(ver2[1])))
58+
return false
59+
})
60+
61+
return lo.MapValues(modMap, func(versions []*ver.Version, path string) string {
62+
return "v" + maxVersion(versions).String()
63+
})
64+
}
65+
3166
func LoadVersions() map[string]string {
3267
path := GoModPath()
3368
assert.Assert(path == "", "go.mod not exists")
@@ -51,3 +86,7 @@ func LoadVersions() map[string]string {
5186

5287
return versions
5388
}
89+
90+
func maxVersion(versions []*ver.Version) *ver.Version {
91+
return lo.MaxBy(versions, func(a *ver.Version, b *ver.Version) bool { return a.GreaterThan(b) })
92+
}

internal/modutil/util_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package modutil
2+
3+
import (
4+
mapset "github.com/deckarep/golang-set/v2"
5+
ver "github.com/hashicorp/go-version"
6+
"github.com/pubgo/funk/pretty"
7+
"github.com/pubgo/funk/v2/result"
8+
"github.com/pubgo/protobuild/internal/shutil"
9+
"github.com/samber/lo"
10+
"strings"
11+
"testing"
12+
)
13+
14+
func TestName(t *testing.T) {
15+
pretty.Println(LoadVersions())
16+
17+
modList := strings.Split(result.Wrap(shutil.GoModGraph()).Must(), "\n")
18+
modSet := mapset.NewSet[string]()
19+
for _, m := range modList {
20+
for _, v := range strings.Split(m, " ") {
21+
modSet.Add(strings.TrimSpace(v))
22+
}
23+
}
24+
25+
var modMap = make(map[string][]*ver.Version)
26+
modSet.Each(func(s string) bool {
27+
ver2 := strings.Split(s, "@")
28+
if len(ver2) != 2 {
29+
return false
30+
}
31+
32+
if !strings.HasPrefix(ver2[1], "v") {
33+
return false
34+
}
35+
36+
modMap[ver2[0]] = append(modMap[ver2[0]], ver.Must(ver.NewSemver(ver2[1])))
37+
return false
38+
})
39+
40+
for k, v := range modMap {
41+
pretty.Println(k, maxVersion(v).String(), minVersion(v).String())
42+
}
43+
}
44+
45+
func minVersion(versions []*ver.Version) *ver.Version {
46+
return lo.MaxBy(versions, func(a *ver.Version, b *ver.Version) bool { return !a.GreaterThan(b) })
47+
}

0 commit comments

Comments
 (0)