run protoc with the right paths

This commit is contained in:
Jeff Carr 2024-11-30 13:48:50 -06:00
parent d24d836899
commit b1172af227
2 changed files with 59 additions and 30 deletions

11
main.go
View File

@ -66,6 +66,7 @@ func main() {
sortmap = make(map[string]string) sortmap = make(map[string]string)
sortmap["package"] = packageName sortmap["package"] = packageName
sortmap["protofile"] = argv.Proto
sortmap["protobase"] = protobase sortmap["protobase"] = protobase
if argv.LoBase == "" { if argv.LoBase == "" {
// if not set, assumed to be protobase // if not set, assumed to be protobase
@ -89,15 +90,21 @@ func main() {
os.Exit(0) os.Exit(0)
} }
// try to make foo.pb.go with protoc if it's not here
sortmap["protoc"] = protobase + ".pb.go" sortmap["protoc"] = protobase + ".pb.go"
if !shell.Exists(sortmap["protoc"]) { if !shell.Exists(sortmap["protoc"]) {
if err := protocBuild(sortmap); err != nil { if err := protocBuild(sortmap); err != nil {
log.Info("protoc build error:", err) log.Info("protoc build error:", err)
os.Exit(-1) os.Exit(-1)
} }
os.Exit(0)
} }
os.Exit(0)
// if foo.pb.go still doesn't exist, protoc failed
// exit here
if !shell.Exists(sortmap["protoc"]) {
log.Info("protoc build error.", sortmap["protoc"], "failed to be created with protoc and proto-gen-go")
os.Exit(-1)
}
// add mutex // add mutex
if err := addMutex(sortmap); err == nil { if err := addMutex(sortmap); err == nil {

View File

@ -5,6 +5,7 @@ package main
import ( import (
"errors" "errors"
"os" "os"
"path/filepath"
"strings" "strings"
"go.wit.com/lib/gui/shell" "go.wit.com/lib/gui/shell"
@ -23,6 +24,13 @@ import (
// forgeConfig.proto // forgeConfig.proto
func protocBuild(names map[string]string) error { func protocBuild(names map[string]string) error {
// read in the .proto file
data, err := os.ReadFile(names["protofile"])
if err != nil {
// log.Info("open config file :", err)
return err
}
// have to figure out how to run protoc so initialize forge // have to figure out how to run protoc so initialize forge
forge = forgepb.Init() forge = forgepb.Init()
// forge.ConfigPrintTable() // forge.ConfigPrintTable()
@ -31,47 +39,61 @@ func protocBuild(names map[string]string) error {
log.Info("") log.Info("")
if shell.Exists(names["protoc"]) { if shell.Exists(names["protoc"]) {
log.Info("protoc file already created", names["protoc"]) log.Info("protoc file already created", names["protoc"])
return nil // return nil
} }
log.Info("make protoc file:", names["protoc"]) log.Info("make protoc file:", names["protoc"])
log.Info("go src", forge.GetGoSrc()) log.Info("go src", forge.GetGoSrc())
pwd, _ := os.Getwd() pwd, _ := os.Getwd()
log.Info("go.Getwd()", pwd) log.Info("go.Getwd()", pwd)
if ! strings.HasPrefix(pwd, forge.GetGoSrc()) { if !strings.HasPrefix(pwd, forge.GetGoSrc()) {
return errors.New("paths don't match") return errors.New("paths don't match")
} }
gopath := strings.TrimPrefix(pwd, forge.GetGoSrc()) gopath := strings.TrimPrefix(pwd, forge.GetGoSrc())
gopath = strings.Trim(gopath, "/")
log.Info("gopath", gopath) log.Info("gopath", gopath)
return errors.New("make protoc here") cmd := []string{"protoc", "--go_out=."}
} cmd = append(cmd, "--proto_path="+gopath)
cmd = append(cmd, "--go_opt=M"+names["protofile"]+"="+gopath)
/*
data, err := os.ReadFile(fullname)
if err != nil {
// log.Info("open config file :", err)
return err
}
w, _ := os.OpenFile(names["protobase"]+".pb.go", os.O_WRONLY|os.O_CREATE, 0600)
var found bool
// look for included proto files
lines := strings.Split(string(data), "\n") lines := strings.Split(string(data), "\n")
for _, line := range lines { for _, line := range lines {
// log.Info("line:", line) // log.Info("line:", line)
start := "type " + names["Bases"] + " struct {" if strings.HasPrefix(line, "import ") {
if strings.HasSuffix(line, start) { parts := strings.Split(line, "\"")
found = true protofile := parts[1]
log.Info("FOUND line:", line) if shell.Exists(protofile) {
fmt.Fprintln(w, line) log.Info("adding import", protofile)
fmt.Fprintln(w, "\tsync.RWMutex // auto-added by go.wit.com/apps/autogenpb") cmd = append(cmd, "--go_opt=M"+protofile+"="+gopath)
fmt.Fprintln(w, "") } else {
} else { basepath, pname := filepath.Split(protofile)
fmt.Fprintln(w, line) if basepath == "" {
log.Warn("import line:", line, "missing proto file:", pname)
log.Warn("protoc will probably fail here")
} else {
log.Warn("need to check basepath here", basepath, pname)
}
}
} }
/*
start := "type " + names["Bases"] + " struct {"
if strings.HasSuffix(line, start) {
found = true
log.Info("FOUND line:", line)
fmt.Fprintln(w, line)
fmt.Fprintln(w, "\tsync.RWMutex // auto-added by go.wit.com/apps/autogenpb")
fmt.Fprintln(w, "")
} else {
fmt.Fprintln(w, line)
}
*/
} }
// os.Exit(-1)
if found { cmd = append(cmd, names["protofile"])
return nil log.Info("\tpwd", forge.GetGoSrc())
for i, s := range cmd {
log.Info("\t", i, s)
} }
*/ shell.PathRun(forge.GetGoSrc(), cmd)
return nil
}