gitpb/repo.protofiles.go

122 lines
2.8 KiB
Go
Raw Permalink 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
for _, s := range fullp {
filebase := filepath.Base(s)
name := strings.TrimSuffix(filebase, ".proto")
anyfound = true
protos[name] = s
}
2024-11-29 23:27:40 -06:00
// make sure every .proto file has a .pb.go file
2024-11-29 23:19:09 -06:00
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
}
}
if found {
// log.Info("found ok")
} else {
// log.Info("gitpb: IsProtobuf() missing compiled proto file:", pname+".pb.go")
2024-11-29 23:19:09 -06:00
err = errors.New("compiled file " + pname + ".pb.go missing")
}
}
2024-11-29 23:27:40 -06:00
// assume every .pb.go file is autogenerated
var allc []string
for _, testf := range fullc {
if strings.HasSuffix(testf, ".pb.go") {
2024-11-29 23:32:19 -06:00
basef := filepath.Base(testf)
allc = append(allc, basef) // no, not the 3.5" floppy disks
2024-11-29 23:27:40 -06:00
}
}
2024-11-29 23:19:09 -06:00
return anyfound, allc, err
}
2024-12-02 05:15:39 -06:00
// look for any proto files. do not enter directories
// note: good golang libraries are best done in a single directory
2024-11-29 23:19:09 -06:00
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
}
2024-12-02 05:15:39 -06:00
// ignore the start dir
if srcDir == path {
return nil
}
2024-11-29 23:19:09 -06:00
if strings.HasSuffix(path, ".proto") {
//
protofiles = append(protofiles, path)
}
if strings.HasSuffix(path, ".pb.go") {
compiled = append(compiled, path)
}
2024-12-02 05:15:39 -06:00
// don't go into any directories
if info.IsDir() {
return filepath.SkipDir
}
2024-11-29 23:19:09 -06:00
return nil
})
return protofiles, compiled, err
}
2024-12-01 22:23:38 -06:00
func (repo *Repo) GetProtoFiles() ([]string, error) {
var protofiles []string
2024-12-02 05:15:39 -06:00
srcDir := repo.GetFullPath()
err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
2024-12-01 22:23:38 -06:00
if err != nil {
log.Log(GITPBWARN, "Error accessing path:", path, err)
return err
}
2024-12-02 05:15:39 -06:00
// ignore the start dir
if srcDir == path {
return nil
}
2024-12-01 22:23:38 -06:00
if strings.HasSuffix(path, ".proto") {
//
protofiles = append(protofiles, path)
}
2024-12-02 05:15:39 -06:00
if info.IsDir() {
return filepath.SkipDir
}
2024-12-01 22:23:38 -06:00
return nil
})
return protofiles, err
}