rearrange code

This commit is contained in:
Jeff Carr 2025-03-05 22:06:44 -06:00
parent f58f7df9ac
commit 9ad1193746
3 changed files with 120 additions and 154 deletions

View File

@ -1,5 +1,14 @@
package gitpb
import (
"errors"
"os"
"path/filepath"
"strings"
"go.wit.com/log"
)
// does this repo build a binary?
func (r *Repo) IsBinary() bool {
if r.GoInfo != nil {
@ -16,3 +25,114 @@ func (r *Repo) IsGoPlugin() bool {
}
return false
}
// 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
}
// make sure every .proto file has a .pb.go file
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")
err = errors.New("compiled file " + pname + ".pb.go missing")
}
}
// assume every .pb.go file is autogenerated
var allc []string
for _, testf := range fullc {
if strings.HasSuffix(testf, ".pb.go") {
basef := filepath.Base(testf)
allc = append(allc, basef) // no, not the 3.5" floppy disks
}
}
return anyfound, allc, err
}
// look for any proto files. do not enter directories
// note: good golang libraries are best done in a single directory
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(WARN, "Error accessing path:", path, err)
return err
}
// ignore the start dir
if srcDir == path {
return nil
}
if strings.HasSuffix(path, ".proto") {
//
protofiles = append(protofiles, path)
}
if strings.HasSuffix(path, ".pb.go") {
compiled = append(compiled, path)
}
// don't go into any directories
if info.IsDir() {
return filepath.SkipDir
}
return nil
})
return protofiles, compiled, err
}
func (repo *Repo) GetProtoFiles() ([]string, error) {
var protofiles []string
srcDir := repo.GetFullPath()
err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Log(WARN, "Error accessing path:", path, err)
return err
}
// ignore the start dir
if srcDir == path {
return nil
}
if strings.HasSuffix(path, ".proto") {
//
protofiles = append(protofiles, path)
}
if info.IsDir() {
return filepath.SkipDir
}
return nil
})
return protofiles, err
}

View File

@ -1,121 +0,0 @@
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
}
// make sure every .proto file has a .pb.go file
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")
err = errors.New("compiled file " + pname + ".pb.go missing")
}
}
// assume every .pb.go file is autogenerated
var allc []string
for _, testf := range fullc {
if strings.HasSuffix(testf, ".pb.go") {
basef := filepath.Base(testf)
allc = append(allc, basef) // no, not the 3.5" floppy disks
}
}
return anyfound, allc, err
}
// look for any proto files. do not enter directories
// note: good golang libraries are best done in a single directory
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(WARN, "Error accessing path:", path, err)
return err
}
// ignore the start dir
if srcDir == path {
return nil
}
if strings.HasSuffix(path, ".proto") {
//
protofiles = append(protofiles, path)
}
if strings.HasSuffix(path, ".pb.go") {
compiled = append(compiled, path)
}
// don't go into any directories
if info.IsDir() {
return filepath.SkipDir
}
return nil
})
return protofiles, compiled, err
}
func (repo *Repo) GetProtoFiles() ([]string, error) {
var protofiles []string
srcDir := repo.GetFullPath()
err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Log(WARN, "Error accessing path:", path, err)
return err
}
// ignore the start dir
if srcDir == path {
return nil
}
if strings.HasSuffix(path, ".proto") {
//
protofiles = append(protofiles, path)
}
if info.IsDir() {
return filepath.SkipDir
}
return nil
})
return protofiles, err
}

View File

@ -1,33 +0,0 @@
// Code generated by go.wit.com/apps/autogenpb DO NOT EDIT.
// This file was autogenerated with autogenpb v0.0.62 2025-03-02_09:17:00_UTC
// go install go.wit.com/apps/autogenpb@latest
//
// define which structs (messages) you want to use in the .proto file
// Then sort.pb.go and marshal.pb.go files are autogenerated
//
// autogenpb uses it and has an example .proto file with instructions
//
package gitpb
/*
func (mt *ReposTable) UpdateTable(pb *Repos) {
// mt.parent.UpdateTable(pb)
}
*/
/*
func (mt *ReposTable) reposCustom(w *guipb.Widget) {
repo := mt.x.Repos[w.Location.Y-1]
mt.CustomFunc(repo)
}
func (mt *ReposTable) Custom(f func(*Repo)) {
mt.pb.RegisterCustom(mt.reposCustom)
mt.CustomFunc = f
}
func (mt *ReposTable) GetUuid() string {
return mt.pb.Uuid
}
*/