auto run protoc

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2024-11-30 12:17:38 -06:00
parent 10cf601ea0
commit dc356e9d0f
7 changed files with 94 additions and 84 deletions

View File

@ -1,12 +1,11 @@
VERSION = $(shell git describe --tags)
BUILDTIME = $(shell date +%Y.%m.%d)
run: clean build
make -C testautogen/ all
make -C testSort/
run: build
test:
./autogenpb --dry-run --proto test.proto --lobase gitTag --upbase GitTag --sort "ByPath,Refname"
test: build
@# make -C testautogen/ all
cd testautogen; pwd; ../autogenpb --proto test.proto
vet:
@GO111MODULE=off go vet
@ -22,7 +21,6 @@ install:
goimports:
goimports -w *.go
make -C testSort/ goimports
redomod:
rm -f go.*
@ -35,6 +33,6 @@ reset:
reset
clean:
-rm -f go.*
-rm -f autogenpb
make -C testautogen/ clean
make -C testSort/ clean
@#make -C testautogen/ clean

View File

@ -1,6 +1,8 @@
package main
// eh, screw it, just add the damned mutex to the pb.go file
// will this help things?
// this is a hack for testing for now
// cram a mutex in the pb.go file
import (
"errors"

15
main.go
View File

@ -1,4 +1,6 @@
//go:build go1.20
// +build go1.20
package main
import (
@ -7,6 +9,7 @@ import (
"github.com/alexflint/go-arg"
"go.wit.com/lib/gui/shell"
"go.wit.com/lib/protobuf/forgepb"
"go.wit.com/log"
"golang.org/x/text/cases"
"golang.org/x/text/language"
@ -17,6 +20,7 @@ var VERSION string
var BUILDTIME string
var sortmap map[string]string
var forge *forgepb.Forge // forgepb figures out how to run protoc correctly if it's needed
func main() {
pp := arg.MustParse(&argv)
@ -48,7 +52,6 @@ func main() {
if !shell.Exists("go.sum") {
shell.Run([]string{"go", "mod", "init"})
shell.Run([]string{"go", "mod", "tidy"})
shell.Run([]string{"go", "mod", "edit", "-go=1.20"})
}
cmd := []string{"go", "list", "-f", "'{{.Name}}'"}
@ -86,6 +89,16 @@ func main() {
os.Exit(0)
}
sortmap["protoc"] = protobase + ".pb.go"
if !shell.Exists(sortmap["protoc"]) {
if err := protocBuild(sortmap); err != nil {
log.Info("protoc build error:", err)
os.Exit(-1)
}
os.Exit(0)
}
os.Exit(0)
// add mutex
if err := addMutex(sortmap); err == nil {
log.Info("adding mutex to existing protoc-gen-go file worked")

68
protoc.go Normal file
View File

@ -0,0 +1,68 @@
package main
// auto run protoc with the correct args
import (
"errors"
"os"
"go.wit.com/lib/gui/shell"
"go.wit.com/lib/protobuf/forgepb"
"go.wit.com/log"
)
// 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 protocBuild(names map[string]string) error {
// have to figure out how to run protoc so initialize forge
forge = forgepb.Init()
// forge.ConfigPrintTable()
os.Setenv("REPO_WORK_PATH", forge.GetGoSrc())
log.Info("")
if shell.Exists(names["protoc"]) {
log.Info("protoc file already created", names["protoc"])
return nil
}
log.Info("make protoc file:", names["protoc"])
log.Info("go src", forge.GetGoSrc())
return errors.New("make protoc here")
}
/*
data, err := os.ReadFile(fullname)
if err != nil {
// log.Info("open config file :", err)
return err
}
w, _ := os.OpenFile(names["protobase"]+".pb.go", os.O_WRONLY|os.O_CREATE, 0600)
var found bool
lines := strings.Split(string(data), "\n")
for _, line := range lines {
// log.Info("line:", line)
start := "type " + names["Bases"] + " struct {"
if strings.HasSuffix(line, start) {
found = true
log.Info("FOUND line:", line)
fmt.Fprintln(w, line)
fmt.Fprintln(w, "\tsync.RWMutex // auto-added by go.wit.com/apps/autogenpb")
fmt.Fprintln(w, "")
} else {
fmt.Fprintln(w, line)
}
}
// os.Exit(-1)
if found {
return nil
}
*/

View File

@ -8,7 +8,7 @@ import (
)
func makeSortfile() {
f, _ := os.OpenFile(sortmap["protobase"] + ".sort.pb.go", os.O_WRONLY|os.O_CREATE, 0600)
f, _ := os.OpenFile(sortmap["protobase"]+".sort.pb.go", os.O_WRONLY|os.O_CREATE, 0600)
header(f, sortmap)
syncLock(f, sortmap)
@ -18,8 +18,8 @@ func makeSortfile() {
iterSortAll(f, sortmap)
if argv.Append != "" {
sortmap["append"] = string(argv.Append)
iterAppend(f, sortmap) // Append() enforce unique key argv.Append
sortmap["append"] = string(argv.Append)
iterAppend(f, sortmap) // Append() enforce unique key argv.Append
}
for _, s := range argv.Sort {

View File

@ -1,34 +0,0 @@
VERSION = $(shell git describe --tags)
BUILDTIME = $(shell date +%Y.%m.%d)
run: clean build
./testSort
vet:
@GO111MODULE=off go vet
@echo this go library package builds okay
build:
GO111MODULE=off go build \
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
install:
GO111MODULE=off go install \
-ldflags "-X main.VERSION=${VERSION} -X main.BUILDTIME=${BUILDTIME} -X gui.GUIVERSION=${VERSION}"
goimports:
goimports -w *.go
# // to globally reset paths:
# // gofmt -w -r "go.wit.com/gui -> go.wit.com/gui/gui" .
redomod:
rm -f go.*
GO111MODULE= go mod init
GO111MODULE= go mod tidy
reset:
# clear your terminal
reset
clean:
-rm -f *.pb.go testSort

View File

@ -1,37 +0,0 @@
package main
import (
"log"
// "go.wit.com/apps/autogenpb/testautogen"
)
func main() {
log.Println("empty because go mod tidy failed")
log.Println("why does go mod tidy fail when this is here?")
// go: go.wit.com/apps/autogenpb/testSort imports
// go.wit.com/apps/autogenpb/testautogen: cannot find module providing package go.wit.com/apps/autogenpb/testautogen: unrecognized import path "go.wit.com/apps/autogenpb/testautogen": parse https://go.wit.com/apps/autogenpb/testautogen?go-get=1: no go-import meta tags ()
/*
all := new(testautogen.GitTags)
// newt := new(testautogen.GitTag)
new1 := testautogen.GitTag{
Refname: "master",
Objectname: "blah",
}
all.Append(&new1)
new2 := testautogen.GitTag{
Refname: "devel",
Objectname: "j23jljalse",
}
all.Append(&new2)
loop := all.SortByPath()
for loop.Scan() {
t := loop.Next()
log.Println("hello testSort t.Refname =", t.Refname, t.Objectname)
}
*/
}