Skip to content

Commit

Permalink
Fix 240505 (#2)
Browse files Browse the repository at this point in the history
* fix: 2024-05-05 14:31:36

* fix: 2024-05-05 16:33:53

* fix: 2024-05-07 10:16:27

* fix: 2024-05-07 11:41:51

* fix: 2024-05-07 11:52:52
  • Loading branch information
kooksee authored May 7, 2024
1 parent 0fe47a4 commit 5f4b3f7
Show file tree
Hide file tree
Showing 45 changed files with 412 additions and 430 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ build:

vet:
@go vet ./...
gofumpt -l -w -extra .

generate:
@go generate ./...
Expand Down
95 changes: 95 additions & 0 deletions cmd/format/fmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Refer: https://github.com/emicklei/proto-contrib/tree/master/cmd/protofmt
// https://github.com/bufbuild/buf/blob/main/private/buf/bufformat/bufformat.go

package format

import (
"bytes"
"io"
"io/ioutil"
"os"

"github.com/emicklei/proto"
"github.com/emicklei/proto-contrib/pkg/protofmt"
"github.com/pubgo/funk/assert"
)

var overwrite = false

// func fmtCmd() *cli.Command {
// return &cli.Command{
// Name: "fmt",
// Usage: "格式化protobuf文件",
// Flags: typex.Flags{&cli.BoolFlag{
// Name: "overwrite",
// Usage: "write result to (source) file instead of stdout",
// Aliases: typex.Strs{"w"},
// Value: overwrite,
// Destination: &overwrite,
// }},
// Before: func(context *cli.Context) error {
// return parseConfig()
// },
// Action: func(ctx *cli.Context) error {
// protoList := make(map[string]bool)

// for i := range cfg.Root {
// if pathutil.IsNotExist(cfg.Root[i]) {
// logger.Info().Msgf("file %s not found", cfg.Root[i])
// continue
// }

// assert.Must(filepath.Walk(cfg.Root[i], func(path string, info fs.FileInfo, err error) error {
// if err != nil {
// return err
// }

// if info.IsDir() {
// return nil
// }

// if strings.HasSuffix(info.Name(), ".proto") {
// protoList[path] = true
// return nil
// }

// return nil
// }))
// }

// for name := range protoList {
// //_ = shutil.MustRun("clang-format", "-i", fmt.Sprintf("-style=google %s", name))
// //readFormatWrite(name)
// format.Format(name)
// }

// return nil
// },
// }
// }

func readFormatWrite(filename string) {
// open for read
file := assert.Must1(os.Open(filename))

// buffer before write
buf := new(bytes.Buffer)
format1(filename, file, buf)

if overwrite {
// write back to input
assert.Must(ioutil.WriteFile(filename, buf.Bytes(), os.ModePerm))
} else {
// write to stdout
buf.WriteString("\n================================================================================================\n")
assert.Must1(io.Copy(os.Stdout, bytes.NewReader(buf.Bytes())))

}
}

func format1(filename string, input io.Reader, output io.Writer) {
parser := proto.NewParser(input)
parser.Filename(filename)
def := assert.Must1(parser.Parse())
protofmt.NewFormatter(output, " ").Format(def) // 2 spaces
}
10 changes: 5 additions & 5 deletions cmd/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ package format

import (
"bytes"
"os"

"github.com/bufbuild/protocompile/parser"
"github.com/bufbuild/protocompile/reporter"
"github.com/pubgo/funk/assert"
"os"
)

// Format formats and writes the target module files into a read bucket.
func Format(path string) {
var data = assert.Must1(os.ReadFile(path))
fileNode, err := parser.Parse(path, bytes.NewBuffer(data), reporter.NewHandler(nil))
assert.Must(err)
data := assert.Must1(os.ReadFile(path))
fileNode := assert.Must1(parser.Parse(path, bytes.NewBuffer(data), reporter.NewHandler(nil)))

var buf bytes.Buffer
assert.Must(newFormatter(&buf, fileNode).Run())
assert.Must(os.WriteFile(path, buf.Bytes(), 0644))
assert.Must(os.WriteFile(path, buf.Bytes(), 0o644))
}
2 changes: 1 addition & 1 deletion cmd/protobuild/lint.go → cmd/format/lint.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package protobuild
package format

// https://github.com/yoheimuta/protolint
// clang-format -i -style=google role.proto
Expand Down
63 changes: 33 additions & 30 deletions cmd/protobuild/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var (
modPath = filepath.Join(os.Getenv("GOPATH"), "pkg", "mod")
pwd = assert.Exit1(os.Getwd())
logger = log.GetLogger("proto-build")
//binPath = []string{os.ExpandEnv("$HOME/bin"), os.ExpandEnv("$HOME/.local/bin"), os.ExpandEnv("./bin")}
// binPath = []string{os.ExpandEnv("$HOME/bin"), os.ExpandEnv("$HOME/.local/bin"), os.ExpandEnv("./bin")}
)

func parseConfig() error {
Expand Down Expand Up @@ -80,7 +80,7 @@ func parseConfig() error {

func Main() *cli.App {
var force bool
var app = &cli.App{
app := &cli.App{
Name: "protobuf build",
Usage: "protobuf generation, configuration and management",
Version: version.Version,
Expand All @@ -94,6 +94,10 @@ func Main() *cli.App {
},
},
Action: func(context *cli.Context) error {
if shutil.IsHelp() {
return nil
}

in := assert.Must1(io.ReadAll(os.Stdin))
req := &pluginpb.CodeGeneratorRequest{}
assert.Must(proto.Unmarshal(in, req))
Expand Down Expand Up @@ -148,18 +152,17 @@ func Main() *cli.App {
return nil
},
Commands: cli.Commands{
fmtCmd(),
&cli.Command{
Name: "gen",
Usage: "编译protobuf文件",
Usage: "编译 protobuf 文件",
Before: func(context *cli.Context) error {
return parseConfig()
},
Action: func(ctx *cli.Context) error {
defer recovery.Exit()

var protoList sync.Map
var basePlugin = cfg.BasePlugin
basePlugin := cfg.BasePlugin
for i := range cfg.Root {
if pathutil.IsNotExist(cfg.Root[i]) {
log.Printf("file %s not found", cfg.Root[i])
Expand Down Expand Up @@ -190,21 +193,21 @@ func Main() *cli.App {
}

protoList.Range(func(key, _ interface{}) bool {
var in = key.(string)
in := key.(string)

var data = ""
var base = fmt.Sprintf("protoc -I %s -I %s", cfg.Vendor, pwd)
data := ""
base := fmt.Sprintf("protoc -I %s -I %s", cfg.Vendor, pwd)
logger.Info().Msgf("includes=%q", cfg.Includes)
for i := range cfg.Includes {
base += fmt.Sprintf(" -I %s", cfg.Includes[i])
}

var retagOut = ""
var retagOpt = ""
retagOut := ""
retagOpt := ""
for i := range cfg.Plugins {
var plg = cfg.Plugins[i]
plg := cfg.Plugins[i]

var name = plg.Name
name := plg.Name

// 指定plugin path
if plg.Path != "" {
Expand All @@ -213,11 +216,11 @@ func Main() *cli.App {
data += fmt.Sprintf(" --plugin=protoc-gen-%s=%s", name, plg.Path)
}

var out = func() string {
out := func() string {
// https://github.com/pseudomuto/protoc-gen-doc
// 目录特殊处理
if name == "doc" {
var out = filepath.Join(plg.Out, in)
out := filepath.Join(plg.Out, in)
assert.Must(pathutil.IsNotExistMkDir(out))
return out
}
Expand All @@ -235,8 +238,8 @@ func Main() *cli.App {

_ = pathutil.IsNotExistMkDir(out)

var opts = plg.Opt
var hasPath = func() bool {
opts := plg.Opt
hasPath := func() bool {
for _, opt := range opts {
if strings.HasPrefix(opt, "paths=") {
return true
Expand All @@ -245,7 +248,7 @@ func Main() *cli.App {
return false
}

var hasModule = func() bool {
hasModule := func() bool {
for _, opt := range opts {
if strings.HasPrefix(opt, "module=") {
return true
Expand Down Expand Up @@ -300,7 +303,7 @@ func Main() *cli.App {
},
&cli.Command{
Name: "vendor",
Usage: "同步项目protobuf依赖到.proto中",
Usage: "同步项目 protobuf 依赖到 .proto 目录中",
Before: func(context *cli.Context) error {
return parseConfig()
},
Expand All @@ -319,32 +322,32 @@ func Main() *cli.App {
var changed bool

// 解析go.mod并获取所有pkg版本
var versions = modutil.LoadVersions()
versions := modutil.LoadVersions()
for i, dep := range cfg.Depends {
pathVersion := strings.SplitN(dep.Url, "@", 2)
if len(pathVersion) == 2 {
dep.Version = generic.Ptr(pathVersion[1])
dep.Path = pathVersion[0]
}

var url = os.ExpandEnv(dep.Url)
url := os.ExpandEnv(dep.Url)

// url是本地目录, 不做检查
if pathutil.IsDir(url) {
continue
}

var v = strutil.FirstFnNotEmpty(func() string {
v := strutil.FirstFnNotEmpty(func() string {
return versions[url]
}, func() string {
return generic.DePtr(dep.Version)
}, func() string {
// go.mod中version不存在, 并且protobuf.yaml也没有指定
// go pkg缓存
var localPkg, err = os.ReadDir(filepath.Dir(filepath.Join(modPath, url)))
localPkg, err := os.ReadDir(filepath.Dir(filepath.Join(modPath, url)))
assert.Must(err)

var _, name = filepath.Split(url)
_, name := filepath.Split(url)
for j := range localPkg {
if !localPkg[j].IsDir() {
continue
Expand Down Expand Up @@ -378,11 +381,11 @@ func Main() *cli.App {
}

var buf bytes.Buffer
var enc = yaml.NewEncoder(&buf)
enc := yaml.NewEncoder(&buf)
enc.SetIndent(2)
defer enc.Close()
assert.Must(enc.Encode(cfg))
assert.Must(os.WriteFile(protoCfg, buf.Bytes(), 0666))
assert.Must(os.WriteFile(protoCfg, buf.Bytes(), 0o666))

if !changed && !cfg.changed && !force {
fmt.Println("No changes")
Expand All @@ -398,8 +401,8 @@ func Main() *cli.App {
continue
}

var url = os.ExpandEnv(dep.Url)
var v = generic.DePtr(dep.Version)
url := os.ExpandEnv(dep.Url)
v := generic.DePtr(dep.Version)

// 加载版本
if v != "" {
Expand All @@ -416,7 +419,7 @@ func Main() *cli.App {
fmt.Println(url)

url = assert.Must1(filepath.Abs(url))
var newUrl = filepath.Join(cfg.Vendor, dep.Name)
newUrl := filepath.Join(cfg.Vendor, dep.Name)
assert.Must(filepath.Walk(url, func(path string, info fs.FileInfo, err error) (gErr error) {
if err != nil {
return err
Expand All @@ -437,7 +440,7 @@ func Main() *cli.App {
return nil
}

var newPath = filepath.Join(newUrl, strings.TrimPrefix(path, url))
newPath := filepath.Join(newUrl, strings.TrimPrefix(path, url))
assert.Must(pathutil.IsNotExistMkDir(filepath.Dir(newPath)))
assert.Must1(copyFile(newPath, path))

Expand All @@ -452,7 +455,7 @@ func Main() *cli.App {
return app
}

func copyFile(dstFilePath string, srcFilePath string) (written int64, err error) {
func copyFile(dstFilePath, srcFilePath string) (written int64, err error) {
srcFile, err := os.Open(srcFilePath)
defer srcFile.Close()
assert.Must(err, "打开源文件错误", srcFilePath)
Expand Down
35 changes: 0 additions & 35 deletions cmd/protobuild/config.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,5 @@
package protobuild

import (
"fmt"

"github.com/pubgo/funk/errors"
"gopkg.in/yaml.v3"
)

var _ yaml.Unmarshaler = (*pluginOpts)(nil)

type pluginOpts []string

func (p *pluginOpts) UnmarshalYAML(value *yaml.Node) error {
if value.IsZero() {
return nil
}

switch value.Kind {
case yaml.ScalarNode:
if value.Value != "" {
*p = []string{value.Value}
return nil
}
return nil
case yaml.SequenceNode:
var data []string
if err := value.Decode(&data); err != nil {
return err
}
*p = data
return nil
default:
return errors.New(fmt.Sprintf("yaml kind type error, data=%s", value.Value))
}
}

type Cfg struct {
Checksum string `yaml:"checksum,omitempty" hash:"-"`
Vendor string `yaml:"vendor,omitempty"`
Expand Down
Loading

0 comments on commit 5f4b3f7

Please sign in to comment.