summaryrefslogtreecommitdiff
path: root/repo.protofiles.go
blob: 81a4b6ff04612b528db32ebfcf74d9c0a6c80a95 (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
package gitpb

import (
	"errors"
	"os"
	"path/filepath"
	"strings"

	"go.wit.com/log"
)

// This returns the list of autogenerated protobuf files
// it assumes any file *.pb.go is autogenerated
//
// this are made from protoc / proto-gen-go
// these packages also use go.wit.com/apps/autogenpb
//
// errors() if a .proto file does not have an autogenerated .pb.go file
func (repo *Repo) IsProtobuf() (bool, []string, error) {
	fullp, fullc, err := scanForProtobuf(repo.FullPath)
	protos := make(map[string]string)
	protoc := make(map[string]string)
	var anyfound bool = false
	var allc []string
	for _, s := range fullp {
		filebase := filepath.Base(s)
		name := strings.TrimSuffix(filebase, ".proto")
		anyfound = true
		protos[name] = s
	}
	for pname, _ := range protos {
		var found bool = false
		for _, s := range fullc {
			cfilebase := filepath.Base(s)
			cname := strings.TrimSuffix(cfilebase, ".pb.go")
			protoc[cname] = s
			if cname == pname {
				found = true
				allc = append(allc, cfilebase)
			}
		}
		if found {
			// log.Info("found ok")
		} else {
			log.Info("missing compiled proto file:", pname+"pb.go")
			err = errors.New("compiled file " + pname + ".pb.go missing")
		}
	}
	return anyfound, allc, err
}

func scanForProtobuf(srcDir string) ([]string, []string, error) {
	var protofiles []string
	var compiled []string
	err := filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
		if err != nil {
			log.Log(GITPBWARN, "Error accessing path:", path, err)
			return err
		}

		if strings.HasSuffix(path, ".proto") {
			//
			protofiles = append(protofiles, path)
		}

		if strings.HasSuffix(path, ".pb.go") {
			compiled = append(compiled, path)
		}

		return nil
	})

	return protofiles, compiled, err
}