autogenpb/protoc.go

107 lines
2.8 KiB
Go
Raw Normal View History

package main
// auto run protoc with the correct args
import (
"errors"
"os"
2024-11-30 13:48:50 -06:00
"path/filepath"
2024-11-30 12:44:04 -06:00
"strings"
"go.wit.com/lib/gui/shell"
"go.wit.com/log"
)
// THIS GENERATES THIS cmd and then runs it:
// autogenpb needs the commands:
// protoc
// gen-proto-go
// these are in the GO tools
// Thsese lines were taken from a working Makefile:
// test.pb.go: test.proto
// cd ~/go/src && protoc --go_out=. --proto_path=go.wit.com/apps/autogenpb/testautogen \
// --go_opt=Mtest.proto=go.wit.com/apps/autogenpb/testautogen \
// test.proto
// forgeConfig.pb.go: forgeConfig.proto
// cd ~/go/src && protoc --go_out=. --proto_path=go.wit.com/apps/autogenpb/testautogen \
// --go_opt=MforgeConfig.proto=go.wit.com/apps/autogenpb/testautogen \
// forgeConfig.proto
func (pb *Files) protocBuild(f *File) error {
2024-11-30 13:48:50 -06:00
// read in the .proto file
data, err := os.ReadFile(f.Filename)
2024-11-30 13:48:50 -06:00
if err != nil {
// log.Info("open config file :", err)
return err
}
if shell.Exists(f.Pbfilename) {
log.Info("protoc file already created", f.Pbfilename)
return nil
}
log.Info("Attempt to generate the protoc file:", f.Pbfilename)
2024-12-17 06:34:27 -06:00
// log.Info("go src", forge.GetGoSrc())
2024-11-30 12:44:04 -06:00
pwd, _ := os.Getwd()
log.Info("go.Getwd()", pwd)
2024-12-17 06:34:27 -06:00
if !strings.HasPrefix(pwd, argv.GoSrc) {
log.Info("are you building using a go.work file? If so,")
log.Info("your paths will not match because the default")
log.Info("is to use ~/go/src so you have to specify")
log.Info("where your path is with --go-src")
log.Info("todo: use forgepb to figure this out")
2024-11-30 12:44:04 -06:00
return errors.New("paths don't match")
}
2024-12-17 06:34:27 -06:00
gopath := strings.TrimPrefix(pwd, argv.GoSrc)
2024-11-30 13:48:50 -06:00
gopath = strings.Trim(gopath, "/")
2024-11-30 12:44:04 -06:00
log.Info("gopath", gopath)
2024-11-30 13:48:50 -06:00
cmd := []string{"protoc", "--go_out=."}
cmd = append(cmd, "--proto_path="+gopath)
cmd = append(cmd, "--go_opt=M"+f.Filename+"="+gopath)
2024-11-30 12:44:04 -06:00
2024-11-30 13:48:50 -06:00
// look for included proto files
2024-11-30 12:44:04 -06:00
lines := strings.Split(string(data), "\n")
for _, line := range lines {
// log.Info("line:", line)
2024-11-30 13:48:50 -06:00
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)
}
}
}
2024-11-30 12:44:04 -06:00
}
2024-11-30 13:48:50 -06:00
cmd = append(cmd, f.Filename)
2024-12-17 06:34:27 -06:00
log.Info("\tpwd", argv.GoSrc)
2024-11-30 13:48:50 -06:00
for i, s := range cmd {
log.Info("\t", i, s)
2024-11-30 12:44:04 -06:00
}
return runprotoc(argv.GoSrc, cmd)
}
func runprotoc(pwd string, mycmd []string) error {
result := shell.PathRun(argv.GoSrc, mycmd)
if result.Error != nil {
return userNotes(result)
}
if result.Exit != 0 {
return userNotes(result)
}
2024-11-30 13:48:50 -06:00
return nil
}