forgepb/build.go

88 lines
2.1 KiB
Go
Raw Normal View History

2024-12-01 10:44:29 -06:00
package forgepb
// for golang repos, this is an attempt to build the package
// there might be some 'standard' ways to implement a build
// that make sense.
// Additions to 'go build' that are attempted here:
//
// * detect packages that are plugins
// * autogen packages that have .proto protobuf files
// * define some 'standard' ldflags
//
import (
"errors"
"fmt"
"os"
"time"
"go.wit.com/lib/protobuf/gitpb"
"go.wit.com/log"
)
func (f *Forge) Build(repo *gitpb.Repo, userFlags []string) error {
// always assume all sources have been downloaded
// todo: detect when in ~/go/src vs go.work mode
os.Setenv("GO111MODULE", "off")
// 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 {
log.Info("redo go.mod", repo.GetGoPath())
repo.RedoGoMod()
f.Repos.ConfigSave()
}
loop1 := repo.GoDeps.SortByGoPath()
for loop1.Scan() {
t := loop1.Next()
log.Info("Build() dep:", t.GetGoPath(), t.GetVersion())
}
2024-12-01 12:53:08 -06:00
loop2 := repo.Published.SortByGoPath()
for loop2.Scan() {
t := loop2.Next()
log.Info("Build() pub:", t.GetGoPath(), t.GetVersion())
}
2024-12-01 10:44:29 -06:00
log.Info("Build() dep len:", repo.GoDeps.Len())
os.Exit(-1)
if repo.CheckDirty() {
version = version + "-dirty"
}
cmd := []string{"go", "build", "-v"}
// set standard ldflag options
now := time.Now()
datestamp := now.UTC().Format("2006/01/02_1504_UTC")
log.Info("datestamp =", datestamp)
// add some standard golang flags
ldflags := "-X main.VERSION=" + version + " "
ldflags += "-X main.BUILDTIME=" + datestamp + " "
ldflags += "-X main.GUIVERSION=" + version + "" // todo: git this from the filesystem
cmd = append(cmd, "-ldflags", ldflags)
// add any flags from the command line
// this might not actually work
// todo: test this
for _, flag := range userFlags {
cmd = append(cmd, "-ldflags", "-X "+flag)
}
if r := repo.Run(cmd); r.Error == nil {
log.Warn("go build worked")
return nil
} else {
return errors.New("go build failed: " + fmt.Sprint(r.Error))
}
}