summaryrefslogtreecommitdiff
path: root/doMtime.go
diff options
context:
space:
mode:
Diffstat (limited to 'doMtime.go')
-rw-r--r--doMtime.go80
1 files changed, 80 insertions, 0 deletions
diff --git a/doMtime.go b/doMtime.go
new file mode 100644
index 0000000..4f69642
--- /dev/null
+++ b/doMtime.go
@@ -0,0 +1,80 @@
+//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/lib/config"
+ "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 doMtime(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 doMtime() had errors:", allerr)
+ }
+ return false
+ }
+ }
+ if config.Verbose() {
+ 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 doMtime() had errors:", allerr)
+ return false
+}