auto run autogenpb on Build()
This commit is contained in:
parent
9439051fc6
commit
dc3fe4bd2e
8
Makefile
8
Makefile
|
@ -14,7 +14,6 @@ vet:
|
||||||
# autofixes your import headers in your golang files
|
# autofixes your import headers in your golang files
|
||||||
goimports:
|
goimports:
|
||||||
goimports -w *.go
|
goimports -w *.go
|
||||||
make -C forgeConfig goimports
|
|
||||||
|
|
||||||
redomod:
|
redomod:
|
||||||
rm -f go.*
|
rm -f go.*
|
||||||
|
@ -25,13 +24,6 @@ redomod:
|
||||||
clean:
|
clean:
|
||||||
rm -f *.pb.go
|
rm -f *.pb.go
|
||||||
-rm -f go.*
|
-rm -f go.*
|
||||||
make -C forgeConfig clean
|
|
||||||
|
|
||||||
build:
|
|
||||||
make -C forgeConfig
|
|
||||||
|
|
||||||
install:
|
|
||||||
make -C forgeConfig install
|
|
||||||
|
|
||||||
forgeConfig.pb.go: forgeConfig.proto
|
forgeConfig.pb.go: forgeConfig.proto
|
||||||
autogenpb --proto forgeConfig.proto
|
autogenpb --proto forgeConfig.proto
|
||||||
|
|
80
build.go
80
build.go
|
@ -15,6 +15,8 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.wit.com/lib/protobuf/gitpb"
|
"go.wit.com/lib/protobuf/gitpb"
|
||||||
|
@ -22,23 +24,35 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (f *Forge) Build(repo *gitpb.Repo, userFlags []string) error {
|
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
|
// always assume all sources have been downloaded
|
||||||
// todo: detect when in ~/go/src vs go.work mode
|
// todo: detect when in ~/go/src vs go.work mode
|
||||||
os.Setenv("GO111MODULE", "off")
|
os.Setenv("GO111MODULE", "off")
|
||||||
|
defer os.Unsetenv("GO111MODULE")
|
||||||
|
|
||||||
// get the version
|
// get the version
|
||||||
version := repo.GetCurrentBranchVersion()
|
version := repo.GetCurrentBranchVersion()
|
||||||
|
|
||||||
loop := repo.Tags.SortByRefname()
|
/*
|
||||||
for loop.Scan() {
|
loop := repo.Tags.SortByRefname()
|
||||||
t := loop.Next()
|
for loop.Scan() {
|
||||||
log.Info("Build() tag:", t.Refname)
|
t := loop.Next()
|
||||||
}
|
log.Info("Build() tag:", t.Refname)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
if repo.GoDeps == nil {
|
if repo.GoDeps == nil {
|
||||||
repo.RedoGoMod()
|
repo.RedoGoMod()
|
||||||
}
|
}
|
||||||
if repo.GoDeps.Len() == 0 {
|
if repo.GoDeps.Len() == 0 {
|
||||||
|
// eh, potentially runs it twice. don't care right now
|
||||||
log.Info("redo go.mod", repo.GetGoPath())
|
log.Info("redo go.mod", repo.GetGoPath())
|
||||||
repo.RedoGoMod()
|
repo.RedoGoMod()
|
||||||
f.Repos.ConfigSave()
|
f.Repos.ConfigSave()
|
||||||
|
@ -46,25 +60,23 @@ func (f *Forge) Build(repo *gitpb.Repo, userFlags []string) error {
|
||||||
loop1 := repo.GoDeps.SortByGoPath()
|
loop1 := repo.GoDeps.SortByGoPath()
|
||||||
for loop1.Scan() {
|
for loop1.Scan() {
|
||||||
t := loop1.Next()
|
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() {
|
if repo.CheckDirty() {
|
||||||
version = version + "-dirty"
|
version = version + "-dirty"
|
||||||
}
|
}
|
||||||
cmd := []string{"go", "build", "-v"}
|
cmd := []string{"go", goWhat, "-v"}
|
||||||
|
|
||||||
// set standard ldflag options
|
// set standard ldflag options
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
datestamp := now.UTC().Format("2006/01/02_1504_UTC")
|
datestamp := now.UTC().Format("2006/01/02_1504_UTC")
|
||||||
log.Info("datestamp =", datestamp)
|
// log.Info("datestamp =", datestamp)
|
||||||
// add some standard golang flags
|
// add some standard golang flags
|
||||||
ldflags := "-X main.VERSION=" + version + " "
|
ldflags := "-X main.VERSION=" + version + " "
|
||||||
ldflags += "-X main.BUILDTIME=" + datestamp + " "
|
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)
|
cmd = append(cmd, "-ldflags", "-X "+flag)
|
||||||
}
|
}
|
||||||
|
|
||||||
if r := repo.Run(cmd); r.Error == nil {
|
log.Info("running:", cmd)
|
||||||
log.Warn("go build worked")
|
if r := repo.RunRealtime(cmd); r.Error == nil {
|
||||||
|
// log.Warn("go build worked")
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
|
log.Warn("go build failed", cmd)
|
||||||
return errors.New("go build failed: " + fmt.Sprint(r.Error))
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -15,12 +15,13 @@ import (
|
||||||
|
|
||||||
// look for a go.work file
|
// look for a go.work file
|
||||||
// otherwise use ~/go/src
|
// otherwise use ~/go/src
|
||||||
func FindGoSrc() (string, error) {
|
func (f *Forge) findGoSrc() (string, error) {
|
||||||
pwd, err := os.Getwd()
|
pwd, err := os.Getwd()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// Check for go.work in the current directory and then move up until root
|
// Check for go.work in the current directory and then move up until root
|
||||||
if pwd, err := digup(pwd); err == nil {
|
if pwd, err := digup(pwd); err == nil {
|
||||||
log.Info("using go.work file in directory", pwd)
|
log.Info("using go.work file in directory", pwd)
|
||||||
|
f.goWork = true
|
||||||
// found an existing go.work file
|
// found an existing go.work file
|
||||||
// os.Chdir(pwd)
|
// os.Chdir(pwd)
|
||||||
return pwd, nil
|
return pwd, nil
|
||||||
|
|
28
init.go
28
init.go
|
@ -8,23 +8,22 @@ import (
|
||||||
"go.wit.com/log"
|
"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 {
|
func Init() *Forge {
|
||||||
f := new(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()
|
// cache.go has Do()
|
||||||
// f.initOnce.Do(f.initWork)
|
// f.initOnce.Do(f.initWork)
|
||||||
|
|
||||||
|
@ -46,7 +45,6 @@ func Init() *Forge {
|
||||||
}
|
}
|
||||||
f.Machine.InitWit()
|
f.Machine.InitWit()
|
||||||
|
|
||||||
f.goSrc = os.Getenv("FORGE_GOSRC")
|
|
||||||
f.ScanGoSrc()
|
f.ScanGoSrc()
|
||||||
log.Info("forge.Init() found", f.Repos.Len(), "repos in", f.goSrc)
|
log.Info("forge.Init() found", f.Repos.Len(), "repos in", f.goSrc)
|
||||||
return f
|
return f
|
||||||
|
|
|
@ -14,6 +14,7 @@ type Forge struct {
|
||||||
initErr error // init error, if any
|
initErr error // init error, if any
|
||||||
|
|
||||||
goSrc string // the path to go/src
|
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
|
Config *ForgeConfigs // config repos for readonly, private, etc
|
||||||
Repos *gitpb.Repos
|
Repos *gitpb.Repos
|
||||||
Machine *zoopb.Machine
|
Machine *zoopb.Machine
|
||||||
|
|
Loading…
Reference in New Issue