From dc3fe4bd2e4fff95bd938eada5d71314e124b2c1 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 1 Dec 2024 22:23:02 -0600 Subject: [PATCH] auto run autogenpb on Build() --- Makefile | 8 ------ build.go | 80 +++++++++++++++++++++++++++++++++++++++++----------- goSrcFind.go | 3 +- init.go | 28 +++++++++--------- structs.go | 1 + 5 files changed, 79 insertions(+), 41 deletions(-) diff --git a/Makefile b/Makefile index e20aa05..ee2b4ed 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/build.go b/build.go index f958e80..697f558 100644 --- a/build.go +++ b/build.go @@ -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) - } + /* + 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 +} diff --git a/goSrcFind.go b/goSrcFind.go index a035205..7393a35 100644 --- a/goSrcFind.go +++ b/goSrcFind.go @@ -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 diff --git a/init.go b/init.go index b8b8498..1a8384b 100644 --- a/init.go +++ b/init.go @@ -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 diff --git a/structs.go b/structs.go index 34550b5..0bb770d 100644 --- a/structs.go +++ b/structs.go @@ -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