2024-11-30 12:17:38 -06:00
|
|
|
//go:build go1.20
|
2024-11-29 21:49:11 -06:00
|
|
|
// +build go1.20
|
2024-11-30 12:17:38 -06:00
|
|
|
|
2025-01-09 06:47:35 -06:00
|
|
|
// protobuf the way I am using them, require GO 1.20. I think. I could be wrong.
|
|
|
|
|
|
|
|
// go:generate go-mod-clean
|
|
|
|
// go:generate autogenpb auto.proto
|
|
|
|
|
2024-11-29 08:30:19 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-11-30 15:06:57 -06:00
|
|
|
"errors"
|
2025-01-09 17:15:53 -06:00
|
|
|
"fmt"
|
2024-11-29 08:30:19 -06:00
|
|
|
"os"
|
2024-12-18 02:54:14 -06:00
|
|
|
"path/filepath"
|
2024-11-29 11:08:11 -06:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/alexflint/go-arg"
|
2024-12-15 20:52:57 -06:00
|
|
|
"github.com/go-cmd/cmd"
|
2024-11-29 11:08:11 -06:00
|
|
|
"go.wit.com/lib/gui/shell"
|
|
|
|
"go.wit.com/log"
|
2024-11-29 08:30:19 -06:00
|
|
|
)
|
|
|
|
|
2024-11-29 11:08:11 -06:00
|
|
|
// sent via -ldflags
|
|
|
|
var VERSION string
|
|
|
|
var BUILDTIME string
|
2024-11-29 10:20:06 -06:00
|
|
|
|
2024-11-29 15:27:56 -06:00
|
|
|
var sortmap map[string]string
|
2024-11-30 15:06:57 -06:00
|
|
|
var marshalKeys []string
|
|
|
|
var uniqueKeys []string
|
2024-11-29 15:27:56 -06:00
|
|
|
|
2024-11-29 08:30:19 -06:00
|
|
|
func main() {
|
2024-11-29 11:08:11 -06:00
|
|
|
pp := arg.MustParse(&argv)
|
|
|
|
|
2025-01-08 19:45:48 -06:00
|
|
|
var pb *Files
|
|
|
|
pb = new(Files)
|
|
|
|
|
2024-11-29 11:15:40 -06:00
|
|
|
// 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)
|
2024-11-29 11:15:40 -06:00
|
|
|
os.Exit(-1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !shell.Exists(argv.Proto) {
|
|
|
|
log.Info("protobuf", argv.Proto, "is missing")
|
2025-01-09 17:15:53 -06:00
|
|
|
os.Exit(-1)
|
2024-11-29 11:15:40 -06:00
|
|
|
}
|
|
|
|
|
2024-11-29 11:21:56 -06:00
|
|
|
if !strings.HasSuffix(argv.Proto, ".proto") {
|
2024-11-29 11:15:40 -06:00
|
|
|
log.Info("protobuf", argv.Proto, "must end in .proto")
|
|
|
|
os.Exit(-1)
|
2024-11-29 11:08:11 -06:00
|
|
|
}
|
|
|
|
|
2025-01-08 19:45:48 -06:00
|
|
|
f := new(File)
|
|
|
|
pb.Files = append(pb.Files, f)
|
|
|
|
f.Filename = argv.Proto
|
2024-11-29 11:21:56 -06:00
|
|
|
|
2024-12-18 02:54:14 -06:00
|
|
|
// todo, look for go.work files
|
2024-12-17 06:34:27 -06:00
|
|
|
if argv.GoSrc == "" {
|
2024-12-18 02:54:14 -06:00
|
|
|
homeDir, _ := os.UserHomeDir()
|
|
|
|
argv.GoSrc = filepath.Join(homeDir, "go/src")
|
2024-12-17 06:34:27 -06:00
|
|
|
}
|
2024-12-04 15:35:27 -06:00
|
|
|
|
2024-12-17 06:34:27 -06:00
|
|
|
if argv.GoPath == "" {
|
|
|
|
pwd, _ := os.Getwd()
|
|
|
|
argv.GoPath = strings.TrimPrefix(pwd, argv.GoSrc)
|
|
|
|
argv.GoPath = strings.Trim(argv.GoPath, "/")
|
2024-12-04 15:35:27 -06:00
|
|
|
}
|
2024-12-17 06:34:27 -06:00
|
|
|
log.Info(argv.GoSrc, argv.GoPath)
|
2024-12-04 15:35:27 -06:00
|
|
|
|
2025-01-08 19:45:48 -06:00
|
|
|
pwd, _ := os.Getwd()
|
|
|
|
log.Info("pwd = ", pwd)
|
|
|
|
|
2024-11-29 15:51:04 -06:00
|
|
|
if !shell.Exists("go.sum") {
|
2025-01-09 04:03:30 -06:00
|
|
|
shell.RunQuiet([]string{"go-mod-clean"})
|
|
|
|
if !shell.Exists("go.sum") {
|
|
|
|
shell.RunQuiet([]string{"go", "mod", "init"})
|
|
|
|
shell.RunQuiet([]string{"go", "mod", "tidy"})
|
|
|
|
shell.RunQuiet([]string{"go", "mod", "edit", "-go=1.18"}) // TODO: pass this as ENV. verify protobuf version needed
|
|
|
|
}
|
2024-11-29 15:51:04 -06:00
|
|
|
}
|
|
|
|
|
2025-01-08 19:45:48 -06:00
|
|
|
var packageName string
|
2024-12-15 20:52:57 -06:00
|
|
|
var result cmd.Status
|
|
|
|
var cmd []string
|
2025-01-08 19:45:48 -06:00
|
|
|
if argv.Package == "" {
|
2025-01-09 04:03:30 -06:00
|
|
|
// TODO: switch to using forgepb (expose the functions/logic from forgepb directly
|
|
|
|
// it could be a bad idea to use forge.Init() here as forge needs this to build
|
2024-12-15 20:52:57 -06:00
|
|
|
// 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}}'"}
|
|
|
|
result = shell.RunQuiet(cmd)
|
|
|
|
os.Unsetenv("GO111MODULE")
|
2024-11-29 11:08:11 -06:00
|
|
|
|
2025-01-08 19:45:48 -06:00
|
|
|
packageName = strings.Join(result.Stdout, "\n")
|
|
|
|
packageName = strings.TrimSpace(packageName)
|
|
|
|
packageName = strings.Trim(packageName, "'")
|
|
|
|
// log.Info("packageName == ", packageName)
|
|
|
|
} else {
|
|
|
|
packageName = argv.Package
|
|
|
|
}
|
|
|
|
f.Package = packageName
|
2024-11-29 11:08:11 -06:00
|
|
|
|
2024-11-29 11:15:40 -06:00
|
|
|
protobase := strings.TrimSuffix(argv.Proto, ".proto")
|
2025-01-08 19:45:48 -06:00
|
|
|
f.Filebase = protobase
|
|
|
|
|
2025-01-09 04:22:11 -06:00
|
|
|
// parse sort & marshal options from the .proto file
|
|
|
|
// this goes through the .proto files and looks
|
|
|
|
// for `autogenpb: ` lines
|
2025-01-09 15:03:05 -06:00
|
|
|
if err := pb.protoParse(f); err != nil {
|
2025-01-09 04:22:11 -06:00
|
|
|
log.Info("autogenpb parse error:", err)
|
2025-01-09 17:15:53 -06:00
|
|
|
badExit(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.Bases == nil {
|
|
|
|
badExit(fmt.Errorf("Base was nil. 'message %s {` did not exist", f.Filebase))
|
|
|
|
}
|
|
|
|
if f.Base == nil {
|
|
|
|
badExit(fmt.Errorf("Base was nil. 'message %s {` did not exist", f.Filebase))
|
2025-01-09 04:22:11 -06:00
|
|
|
}
|
|
|
|
|
2024-11-29 11:32:27 -06:00
|
|
|
if argv.DryRun {
|
2025-01-09 17:15:53 -06:00
|
|
|
okExit("")
|
2024-11-29 11:32:27 -06:00
|
|
|
}
|
|
|
|
|
2024-11-30 13:48:50 -06:00
|
|
|
// try to make foo.pb.go with protoc if it's not here
|
2025-01-08 19:45:48 -06:00
|
|
|
// this is helpful because the protoc-gen-go lines
|
|
|
|
// are also annoying to code by hand
|
2025-01-09 04:35:48 -06:00
|
|
|
|
2025-01-09 05:49:23 -06:00
|
|
|
f.Pbfilename = f.Filebase + ".pb.go"
|
2025-01-09 04:35:48 -06:00
|
|
|
// try to create the foo.pb.go file using protoc if it is not there
|
2025-01-09 05:49:23 -06:00
|
|
|
if !shell.Exists(f.Pbfilename) {
|
|
|
|
if err := pb.protocBuild(f); err != nil {
|
|
|
|
badExit(err)
|
2024-11-30 12:17:38 -06:00
|
|
|
}
|
2025-01-09 04:35:48 -06:00
|
|
|
|
2024-11-30 12:17:38 -06:00
|
|
|
}
|
2024-11-30 13:48:50 -06:00
|
|
|
|
2025-01-09 04:35:48 -06:00
|
|
|
// try to add the Mutex to the pb.go file
|
2025-01-09 05:49:23 -06:00
|
|
|
if err := pb.addMutex(f); err != nil {
|
|
|
|
badExit(err)
|
|
|
|
}
|
2025-01-09 04:35:48 -06:00
|
|
|
|
2024-11-30 13:48:50 -06:00
|
|
|
// if foo.pb.go still doesn't exist, protoc failed
|
2025-01-09 05:49:23 -06:00
|
|
|
if !shell.Exists(f.Pbfilename) {
|
|
|
|
log.Info("protoc build error.", f.Pbfilename)
|
2024-11-30 15:06:57 -06:00
|
|
|
badExit(errors.New("failed to be created with protoc and proto-gen-go"))
|
2024-11-30 13:48:50 -06:00
|
|
|
}
|
2024-11-30 12:17:38 -06:00
|
|
|
|
2025-01-09 17:15:53 -06:00
|
|
|
// make the marshal.pb.go file
|
2025-01-09 05:00:29 -06:00
|
|
|
pb.marshal(f)
|
2024-11-29 13:35:13 -06:00
|
|
|
|
2025-01-09 17:15:53 -06:00
|
|
|
// make the sort.pb.go file
|
2025-01-09 05:20:00 -06:00
|
|
|
pb.makeNewSortfile(f)
|
2024-11-29 13:35:13 -06:00
|
|
|
}
|
2024-11-30 15:06:57 -06:00
|
|
|
|
|
|
|
func okExit(s string) {
|
|
|
|
log.Info("autogenpb ok", s)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func badExit(err error) {
|
|
|
|
log.Info("autogenpb error:", err)
|
|
|
|
os.Exit(-1)
|
|
|
|
}
|