gitpb/repo.protofiles.go

75 lines
1.7 KiB
Go
Raw Normal View History

2024-11-29 23:19:09 -06:00
package gitpb
import (
"errors"
"os"
"path/filepath"
"strings"
"go.wit.com/log"
)
// This returns the list of autogenerated protobuf files
// it assumes any file *.pb.go is autogenerated
//
// this are made from protoc / proto-gen-go
// these packages also use go.wit.com/apps/autogenpb
//
// errors() if a .proto file does not have an autogenerated .pb.go file
func (repo *Repo) IsProtobuf() (bool, []string, error) {
fullp, fullc, err := scanForProtobuf(repo.FullPath)
protos := make(map[string]string)
protoc := make(map[string]string)
var anyfound bool = false
var allc []string
for _, s := range fullp {
filebase := filepath.Base(s)
name := strings.TrimSuffix(filebase, ".proto")
anyfound = true
protos[name] = s
}
for pname, _ := range protos {
var found bool = false
for _, s := range fullc {
cfilebase := filepath.Base(s)
cname := strings.TrimSuffix(cfilebase, ".pb.go")
protoc[cname] = s
if cname == pname {
found = true
allc = append(allc, cfilebase)
}
}
if found {
// log.Info("found ok")
} else {
log.Info("missing compiled proto file:", pname+"pb.go")
err = errors.New("compiled file " + pname + ".pb.go missing")
}
}
return anyfound, allc, err
}
func scanForProtobuf(srcDir string) ([]string, []string, error) {
var protofiles []string
var compiled []string
err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Log(GITPBWARN, "Error accessing path:", path, err)
return err
}
if strings.HasSuffix(path, ".proto") {
//
protofiles = append(protofiles, path)
}
if strings.HasSuffix(path, ".pb.go") {
compiled = append(compiled, path)
}
return nil
})
return protofiles, compiled, err
}