autogenpb/main.go

163 lines
4.0 KiB
Go
Raw Normal View History

//go:build go1.20
2024-11-29 21:49:11 -06:00
// +build go1.20
2024-11-29 08:30:19 -06:00
package main
import (
"errors"
2024-11-29 08:30:19 -06:00
"os"
"strings"
"github.com/alexflint/go-arg"
"go.wit.com/lib/gui/shell"
"go.wit.com/lib/protobuf/forgepb"
"go.wit.com/log"
"golang.org/x/text/cases"
"golang.org/x/text/language"
2024-11-29 08:30:19 -06:00
)
// sent via -ldflags
var VERSION string
var BUILDTIME string
var sortmap map[string]string
var forge *forgepb.Forge // forgepb figures out how to run protoc correctly if it's needed
var marshalKeys []string
var uniqueKeys []string
2024-11-29 08:30:19 -06:00
func main() {
pp := arg.MustParse(&argv)
// you need a proto file
if argv.Proto == "" {
log.Info("you must provide --proto <filename>")
2024-12-01 22:21:09 -06:00
pp.WriteHelp(os.Stdout)
os.Exit(-1)
}
if !shell.Exists(argv.Proto) {
log.Info("protobuf", argv.Proto, "is missing")
if !argv.DryRun {
os.Exit(-1)
}
}
if !strings.HasSuffix(argv.Proto, ".proto") {
log.Info("protobuf", argv.Proto, "must end in .proto")
os.Exit(-1)
}
2024-12-01 22:21:09 -06:00
if err := findGlobalAutogenpb(argv.Proto); err != nil {
log.Info("autogenpb parse error:", err)
os.Exit(-1)
}
if !shell.Exists("go.sum") {
2024-12-01 22:21:09 -06:00
shell.RunQuiet([]string{"go", "mod", "init"})
shell.RunQuiet([]string{"go", "mod", "tidy"})
shell.RunQuiet([]string{"go", "mod", "edit", "-go=1.18"}) // TODO: make this a global
}
2024-12-01 22:21:09 -06:00
// TODO: switch to using forgepb
// switch to forgepb
os.Setenv("GO111MODULE", "off") // keeps go list working if go version is back versioned for compatability
cmd := []string{"go", "list", "-f", "'{{.Name}}'"}
2024-12-01 22:21:09 -06:00
result := shell.RunQuiet(cmd)
os.Unsetenv("GO111MODULE")
packageName := strings.Join(result.Stdout, "\n")
packageName = strings.TrimSpace(packageName)
packageName = strings.Trim(packageName, "'")
2024-12-01 22:21:09 -06:00
// log.Info("packageName == ", packageName)
protobase := strings.TrimSuffix(argv.Proto, ".proto")
sortmap = make(map[string]string)
sortmap["package"] = packageName
2024-11-30 13:48:50 -06:00
sortmap["protofile"] = argv.Proto
sortmap["protobase"] = protobase
if argv.LoBase == "" {
// if not set, assumed to be protobase
sortmap["base"] = protobase
} else {
sortmap["base"] = argv.LoBase
}
sortmap["lock"] = sortmap["base"] + "sMu" // is nonglobal and plural
if argv.UpBase == "" {
sortmap["Base"] = cases.Title(language.English, cases.NoLower).String(protobase)
sortmap["Bases"] = sortmap["Base"] + "s"
} else {
sortmap["Base"] = argv.UpBase
sortmap["Bases"] = sortmap["Base"] + "s"
}
if argv.DryRun {
for k, v := range sortmap {
log.Info(k, "=", v)
}
os.Exit(0)
}
// parse sort & marshal options from the .proto file
if err := findAutogenpb(sortmap); err != nil {
log.Info("autogenpb parse error:", err)
os.Exit(-1)
}
2024-11-30 13:48:50 -06:00
// 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)
}
// experiment to add a mutex to the structs.
// this might fix my other not so great lock implementation on sort (?)
2024-12-01 10:42:12 -06:00
// seems to work, but proto.Marshal() breaks with nil reference
if argv.Mutex {
if err := addMutex(sortmap); err == nil {
2024-12-01 22:21:09 -06:00
// log.Info("adding mutex to existing protoc-gen-go file worked")
2024-12-01 10:42:12 -06:00
sortmap["mutex"] = "true"
sortmap["lock"] = "all"
} else {
2024-12-01 22:21:09 -06:00
log.Info("adding mutex to existing protoc-gen-go file did not work", err)
2024-12-01 10:42:12 -06:00
sortmap["mutex"] = "false"
}
}
}
2024-11-30 13:48:50 -06:00
// 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")
badExit(errors.New("failed to be created with protoc and proto-gen-go"))
2024-11-30 13:48:50 -06:00
}
if argv.NoSort {
2024-12-01 22:21:09 -06:00
// log.Info("not making sort.pb.go file (--no-sort == true)")
} else {
if len(uniqueKeys) != 0 {
}
makeSortfile()
}
if argv.NoMarshal {
2024-12-01 22:21:09 -06:00
// log.Info("not making marshal.pb.go file (--no-marshal == true)")
} else {
// make the foo.marshal.pb.go file
marshal(sortmap)
}
}
func okExit(s string) {
log.Info("autogenpb ok", s)
os.Exit(0)
}
func badExit(err error) {
log.Info("autogenpb error:", err)
os.Exit(-1)
}