diff options
| author | Jeff Carr <[email protected]> | 2025-10-14 17:23:28 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-14 17:23:28 -0500 |
| commit | b7c2f78414f1b7113d4f760b81b51d7b6758ea35 (patch) | |
| tree | 40fe3cc827ee7d88e19a19667292df419f52c4b0 | |
| parent | c182558c57fcc02314dbb109b8e515c37a8e00a9 (diff) | |
run only on os.Stat(). hopefully helps go generatev0.5.24
| -rw-r--r-- | argv.go | 10 | ||||
| -rw-r--r-- | doClean.go | 77 | ||||
| -rw-r--r-- | main.go | 36 |
3 files changed, 111 insertions, 12 deletions
@@ -25,11 +25,13 @@ type args struct { Regret bool `arg:"--regret" help:"ignore needed UUID. You will eventually regret this."` Delete bool `arg:"--delete" help:"use delete with copy experiment"` DryRun bool `arg:"--dry-run" help:"check the .proto syntax, but don't do anything"` - Format bool `arg:"--format" help:"format the .proto file and exit"` + ReFormat bool `arg:"--reformat" help:"reformat the .proto file and exit"` Debug bool `arg:"--debug" help:"enable debugging information"` Comments bool `arg:"--format-comments" help:"enforce parseable comments in a .proto file"` NoFormat bool `arg:"--no-format" help:"do not auto-reformat the .proto file"` Renumber bool `arg:"--renumber" help:"renumber everything. obviously breaks backwards compatiblity"` + Clean bool `arg:"--clean" help:"clean out any *pb.go files; bypassing ctime sanity checks"` + Ctime bool `arg:"--ctime" help:"do os.Stat() createtime sanity checks"` GoSrc string `arg:"--go-src" help:"default is ~/go/src. could be set to your go.work path"` GoPath string `arg:"--gopath" help:"the gopath of this repo"` Identify string `arg:"--identify" help:"identify file"` @@ -64,13 +66,17 @@ func (args) Appname() string { return ARGNAME } +func (args) MatchClean() string { + return "foo.proto bar.proto" +} + func (args) Buildtime() (string, string) { return BUILDTIME, VERSION } func (a args) DoAutoComplete(pb *prep.Auto) { if pb.Cmd == "" { - pb.Autocomplete3([]string{"--bash", "--proto", "--regret", "--debug", "--renumber", "--delete", "--dry-run", "--version"}) + pb.Autocomplete3([]string{"--bash", "--proto", "--regret", "--debug", "--renumber", "--reformat", "--delete", "--dry-run"}) } else { pb.SubCommand(pb.Goargs...) } diff --git a/doClean.go b/doClean.go new file mode 100644 index 0000000..d1af455 --- /dev/null +++ b/doClean.go @@ -0,0 +1,77 @@ +//go:build go1.20 +// +build go1.20 + +// Copyright 2017-2025 WIT.COM Inc. All rights reserved. +// Use of this source code is governed by the GPL 3.0 + +// protobuf the way I am using them, require GO 1.20. I think. I could be wrong. +// The Go Protocol Buffers library embeds a sync.Mutex within the MessageState struct to prevent unintended shallow copies of message structs +// this optionally (but it is the default) inserts a mutex into the struct generated by protoc + +// go:generate autogenpb --proto file.proto + +package main + +import ( + "errors" + "os" + "path/filepath" + + "go.wit.com/log" +) + +func doClean(filebase string) error { + globPattern := filebase + ".*.pb.go" + files, err := filepath.Glob(globPattern) + if err != nil { + log.Info("glob error", err, files) + } + for _, filename := range files { + // err += os.Remove(filename) + err = errors.Join(err, os.Remove(filename)) + } + log.Info("Removed:", files) + return err +} + +// is true if no errors and nothing is new +func doCtime(filebase string) bool { + var allerr error + statf, err := os.Stat(filebase + ".proto") + allerr = errors.Join(allerr, err) + basetime := statf.ModTime() + globPattern := filebase + ".*.pb.go" + files, err := filepath.Glob(globPattern) + if err != nil { + log.Info("glob error", err, files) + allerr = errors.Join(allerr, err) + } + if len(files) == 0 { + log.Info("no pb.go files found. need to re-run") + return false + } + for _, filename := range files { + fstats, err := os.Stat(filename) + allerr = errors.Join(allerr, err) + newtime := fstats.ModTime() + if basetime.Before(newtime) { + // everything is okay + // log.Info(filebase+".proto was older than", filename) + } else { + log.Info(filebase+".proto was newer than", filename) + // need to rerun + err := doClean(filebase) + allerr = errors.Join(allerr, err) + if allerr != nil { + log.Info("autogenpb doCtime() had errors:", allerr) + } + return false + } + } + log.Info(filebase + ".proto was older than all pb.go files. No need to re-run autogenpb.") + if allerr == nil { + return true + } + log.Info("autogenpb doCtime() had errors:", allerr) + return false +} @@ -32,11 +32,9 @@ var BUILDTIME string var ARGNAME string = "autogenpb" -// var fsort *os.File // the sort.pb.go output file - func main() { me = new(mainType) - me.sh = prep.Bash(&argv) // add support for bash autocomplete with go-arg + me.sh = prep.Autocomplete(&argv) // adds shell auto complete to go-args me.pb = new(Files) if argv.Identify != "" { @@ -67,13 +65,31 @@ func main() { } func doProto(argvProto string) error { - if argv.Format { + pf := new(File) + me.pb.Files = append(me.pb.Files, pf) + pf.Filename = argvProto + pf.IterMap = make(map[string]string) + + pf.Filebase = strings.TrimSuffix(argvProto, ".proto") + + if argv.Clean { + doClean(pf.Filebase) + me.sh.GoodExit("doClean() ran") + } + + if argv.Ctime { + doCtime(pf.Filebase) + me.sh.GoodExit("doClean() ran") + } + + if argv.ReFormat { protoReformatComments(argvProto) // time.Sleep(5 * time.Second) protoReformat(argvProto) log.Info("format done") okExit("") } + if argv.Comments { protoReformatComments(argvProto) okExit("") @@ -84,12 +100,12 @@ func doProto(argvProto string) error { os.Setenv("PROTOBUF_REGRET", "true") } - pf := new(File) - me.pb.Files = append(me.pb.Files, pf) - pf.Filename = argvProto - pf.IterMap = make(map[string]string) - - pf.Filebase = strings.TrimSuffix(argvProto, ".proto") + if doCtime(pf.Filebase) { + log.Info("nothing changed. exit here") + me.sh.GoodExit("doCtime() ran") + } else { + log.Info("ctime check: need to re-run autogenpb") + } // parse sort & marshal options from the .proto file // this goes through the .proto files and looks |
