auto run autogenpb on Build()

This commit is contained in:
Jeff Carr 2024-12-01 22:23:02 -06:00
parent 9439051fc6
commit dc3fe4bd2e
5 changed files with 79 additions and 41 deletions

View File

@ -14,7 +14,6 @@ vet:
# autofixes your import headers in your golang files
goimports:
goimports -w *.go
make -C forgeConfig goimports
redomod:
rm -f go.*
@ -25,13 +24,6 @@ redomod:
clean:
rm -f *.pb.go
-rm -f go.*
make -C forgeConfig clean
build:
make -C forgeConfig
install:
make -C forgeConfig install
forgeConfig.pb.go: forgeConfig.proto
autogenpb --proto forgeConfig.proto

View File

@ -15,6 +15,8 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"go.wit.com/lib/protobuf/gitpb"
@ -22,23 +24,35 @@ import (
)
func (f *Forge) Build(repo *gitpb.Repo, userFlags []string) error {
return f.doBuild(repo, userFlags, "build")
}
func (f *Forge) Install(repo *gitpb.Repo, userFlags []string) error {
return f.doBuild(repo, userFlags, "install")
}
func (f *Forge) doBuild(repo *gitpb.Repo, userFlags []string, goWhat string) error {
// always assume all sources have been downloaded
// todo: detect when in ~/go/src vs go.work mode
os.Setenv("GO111MODULE", "off")
defer os.Unsetenv("GO111MODULE")
// get the version
version := repo.GetCurrentBranchVersion()
/*
loop := repo.Tags.SortByRefname()
for loop.Scan() {
t := loop.Next()
log.Info("Build() tag:", t.Refname)
}
*/
if repo.GoDeps == nil {
repo.RedoGoMod()
}
if repo.GoDeps.Len() == 0 {
// eh, potentially runs it twice. don't care right now
log.Info("redo go.mod", repo.GetGoPath())
repo.RedoGoMod()
f.Repos.ConfigSave()
@ -46,25 +60,23 @@ func (f *Forge) Build(repo *gitpb.Repo, userFlags []string) error {
loop1 := repo.GoDeps.SortByGoPath()
for loop1.Scan() {
t := loop1.Next()
log.Info("Build() dep:", t.GetGoPath(), t.GetVersion())
found := f.Repos.FindByGoPath(t.GetGoPath())
if found.RepoType() == "protobuf" {
if err := f.runAutogenpb(found); err != nil {
return err
}
}
loop2 := repo.Published.SortByGoPath()
for loop2.Scan() {
t := loop2.Next()
log.Info("Build() pub:", t.GetGoPath(), t.GetVersion())
}
log.Info("Build() dep len:", repo.GoDeps.Len())
os.Exit(-1)
if repo.CheckDirty() {
version = version + "-dirty"
}
cmd := []string{"go", "build", "-v"}
cmd := []string{"go", goWhat, "-v"}
// set standard ldflag options
now := time.Now()
datestamp := now.UTC().Format("2006/01/02_1504_UTC")
log.Info("datestamp =", datestamp)
// log.Info("datestamp =", datestamp)
// add some standard golang flags
ldflags := "-X main.VERSION=" + version + " "
ldflags += "-X main.BUILDTIME=" + datestamp + " "
@ -78,10 +90,44 @@ func (f *Forge) Build(repo *gitpb.Repo, userFlags []string) error {
cmd = append(cmd, "-ldflags", "-X "+flag)
}
if r := repo.Run(cmd); r.Error == nil {
log.Warn("go build worked")
log.Info("running:", cmd)
if r := repo.RunRealtime(cmd); r.Error == nil {
// log.Warn("go build worked")
return nil
} else {
log.Warn("go build failed", cmd)
return errors.New("go build failed: " + fmt.Sprint(r.Error))
}
}
func (f *Forge) runAutogenpb(repo *gitpb.Repo) error {
// log.Info("run autogenpb here:", repo.GetGoPath())
files, err := repo.GetProtoFiles()
if err != nil {
log.Info("gitpb.GetProtoFiles()", err)
return err
}
for _, s := range files {
_, filename := filepath.Split(s)
pbfile := strings.TrimSuffix(filename, ".proto") + ".pb.go"
cmd := []string{"autogenpb", "--proto", filename}
if repo.Exists(pbfile) {
// log.Info("skip running:", cmd)
continue
}
log.Info("running:", cmd)
r := repo.RunRealtime(cmd)
if r.Error != nil {
log.Warn("")
log.Warn("autogenpb error", r.Error)
log.Warn("")
log.Warn("You might be missing autogenpb?")
log.Warn("")
log.Warn("To install it run: go install go.wit.com/apps/autogenpb@latest")
log.Warn("")
log.Sleep(2)
return r.Error
}
}
return nil
}

View File

@ -15,12 +15,13 @@ import (
// look for a go.work file
// otherwise use ~/go/src
func FindGoSrc() (string, error) {
func (f *Forge) findGoSrc() (string, error) {
pwd, err := os.Getwd()
if err == nil {
// Check for go.work in the current directory and then move up until root
if pwd, err := digup(pwd); err == nil {
log.Info("using go.work file in directory", pwd)
f.goWork = true
// found an existing go.work file
// os.Chdir(pwd)
return pwd, nil

28
init.go
View File

@ -8,23 +8,22 @@ import (
"go.wit.com/log"
)
// set FORGE_GOSRC env if not already set
func init() {
gosrc := os.Getenv("FORGE_GOSRC")
if gosrc != "" {
// already set. ignore init()
}
goSrcDir, err := FindGoSrc()
if err != nil {
log.Warn("forge init() FindGoSrc()", err)
panic("forge init() FindGoSrc()")
}
os.Setenv("FORGE_GOSRC", goSrcDir)
}
func Init() *Forge {
f := new(Forge)
// TODO: rethink this but it works for now
gosrc := os.Getenv("FORGE_GOSRC")
if gosrc == "" {
// already set. ignore init()
goSrcDir, err := f.findGoSrc()
if err != nil {
log.Warn("forge init() findGoSrc()", err)
panic("forge init() findGoSrc()")
}
os.Setenv("FORGE_GOSRC", goSrcDir)
}
f.goSrc = os.Getenv("FORGE_GOSRC")
// cache.go has Do()
// f.initOnce.Do(f.initWork)
@ -46,7 +45,6 @@ func Init() *Forge {
}
f.Machine.InitWit()
f.goSrc = os.Getenv("FORGE_GOSRC")
f.ScanGoSrc()
log.Info("forge.Init() found", f.Repos.Len(), "repos in", f.goSrc)
return f

View File

@ -14,6 +14,7 @@ type Forge struct {
initErr error // init error, if any
goSrc string // the path to go/src
goWork bool // means the user is currently using a go.work file
Config *ForgeConfigs // config repos for readonly, private, etc
Repos *gitpb.Repos
Machine *zoopb.Machine