summaryrefslogtreecommitdiff
path: root/doMtime.go
blob: 12f1e29406fd70d32705c1a98f61ea3b28105619 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// 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
}