diff --git a/main.go b/main.go index cbff6a6..18cff07 100644 --- a/main.go +++ b/main.go @@ -66,6 +66,7 @@ func main() { sortmap = make(map[string]string) sortmap["package"] = packageName + sortmap["protofile"] = argv.Proto sortmap["protobase"] = protobase if argv.LoBase == "" { // if not set, assumed to be protobase @@ -89,15 +90,21 @@ func main() { os.Exit(0) } + // try to make foo.pb.go with protoc if it's not here sortmap["protoc"] = protobase + ".pb.go" if !shell.Exists(sortmap["protoc"]) { if err := protocBuild(sortmap); err != nil { log.Info("protoc build error:", err) 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 if err := addMutex(sortmap); err == nil { diff --git a/protoc.go b/protoc.go index b67effa..acd6ad7 100644 --- a/protoc.go +++ b/protoc.go @@ -5,6 +5,7 @@ package main import ( "errors" "os" + "path/filepath" "strings" "go.wit.com/lib/gui/shell" @@ -23,6 +24,13 @@ import ( // forgeConfig.proto 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 forge = forgepb.Init() // forge.ConfigPrintTable() @@ -31,47 +39,61 @@ func protocBuild(names map[string]string) error { log.Info("") if shell.Exists(names["protoc"]) { log.Info("protoc file already created", names["protoc"]) - return nil + // return nil } log.Info("make protoc file:", names["protoc"]) log.Info("go src", forge.GetGoSrc()) pwd, _ := os.Getwd() log.Info("go.Getwd()", pwd) - if ! strings.HasPrefix(pwd, forge.GetGoSrc()) { + if !strings.HasPrefix(pwd, forge.GetGoSrc()) { return errors.New("paths don't match") } gopath := strings.TrimPrefix(pwd, forge.GetGoSrc()) + gopath = strings.Trim(gopath, "/") log.Info("gopath", gopath) - return errors.New("make protoc here") -} - -/* - 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 + cmd := []string{"protoc", "--go_out=."} + cmd = append(cmd, "--proto_path="+gopath) + cmd = append(cmd, "--go_opt=M"+names["protofile"]+"="+gopath) + // look for included proto files lines := strings.Split(string(data), "\n") for _, line := range lines { // log.Info("line:", line) - 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) + if strings.HasPrefix(line, "import ") { + parts := strings.Split(line, "\"") + protofile := parts[1] + if shell.Exists(protofile) { + log.Info("adding import", protofile) + cmd = append(cmd, "--go_opt=M"+protofile+"="+gopath) + } else { + basepath, pname := filepath.Split(protofile) + 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 { - return nil + + cmd = append(cmd, names["protofile"]) + log.Info("\tpwd", forge.GetGoSrc()) + for i, s := range cmd { + log.Info("\t", i, s) } -*/ + shell.PathRun(forge.GetGoSrc(), cmd) + return nil +}