summaryrefslogtreecommitdiff
path: root/protoc.go
blob: e0c128ef3a7062f23cba80091aba9632082be071 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main

// auto run protoc with the correct args

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

	"go.wit.com/lib/gui/shell"
	"go.wit.com/log"
)

// THIS GENERATES THIS cmd and then runs it:

// autogenpb needs the commands:

// protoc
// gen-proto-go

// these are in the GO tools

// Thsese lines were taken from a working Makefile:

// test.pb.go: test.proto
// 	cd ~/go/src && protoc --go_out=. --proto_path=go.wit.com/apps/autogenpb/testautogen \
// 		--go_opt=Mtest.proto=go.wit.com/apps/autogenpb/testautogen \
// 		test.proto

// forgeConfig.pb.go: forgeConfig.proto
// 	cd ~/go/src && protoc --go_out=. --proto_path=go.wit.com/apps/autogenpb/testautogen \
// 		--go_opt=MforgeConfig.proto=go.wit.com/apps/autogenpb/testautogen \
// 		forgeConfig.proto

func (pb *Files) protocBuild(pf *File) error {
	// read in the .proto file
	data, err := os.ReadFile(pf.Filename)
	if err != nil {
		// log.Info("open config file :", err)
		return err
	}

	if shell.Exists(pf.Pbfilename) {
		log.Info("protoc file already created", pf.Pbfilename)
		return nil
	}
	log.Info("Attempt to generate the protoc file:", pf.Pbfilename)
	// log.Info("go src", forge.GetGoSrc())
	pwd, _ := os.Getwd()
	log.Info("go.Getwd()", pwd)
	if !strings.HasPrefix(pwd, argv.GoSrc) {
		log.Info("are you building using a go.work file? If so,")
		log.Info("your paths will not match because the default")
		log.Info("is to use ~/go/src so you have to specify")
		log.Info("where your path is with --go-src")
		log.Info("todo: use forgepb to figure this out")
		return errors.New("paths don't match")
	}
	gopath := strings.TrimPrefix(pwd, argv.GoSrc)
	gopath = strings.Trim(gopath, "/")
	log.Info("gopath", gopath)
	pf.GoPath = gopath
	cmd := []string{"protoc", "--go_out=."}
	cmd = append(cmd, "--proto_path="+gopath)
	cmd = append(cmd, "--go_opt=M"+pf.Filename+"="+gopath)
	// cmd = append(cmd, "--print_free_field_numbers")

	// look for included proto files
	lines := strings.Split(string(data), "\n")
	for _, line := range lines {
		// log.Info("line:", line)
		if strings.HasPrefix(line, "import ") {
			parts := strings.Split(line, "\"")
			protofile := parts[1]
			if shell.Exists(protofile) {
				log.Info("adding import", protofile)
				cmd = append(cmd, "--go_opt=M"+protofile+"="+gopath)
			} else {
				basepath, pname := filepath.Split(protofile)
				if basepath == "" {
					log.Warn("import line:", line, "missing proto file:", pname)
					log.Warn("protoc will probably fail here")
				} else {
					log.Warn("need to check basepath here", basepath, pname)
				}
			}
		}
	}

	cmd = append(cmd, pf.Filename)
	log.Info("\tpwd", argv.GoSrc)
	for i, s := range cmd {
		log.Info("\t", i, s)
	}
	return runprotoc(argv.GoSrc, cmd)
}

func runprotoc(pwd string, mycmd []string) error {
	result := shell.PathRun(argv.GoSrc, mycmd)
	if result.Error != nil {
		return userNotes(result)
	}
	if result.Exit != 0 {
		return userNotes(result)
	}
	return nil
}