summaryrefslogtreecommitdiff
path: root/build.go
diff options
context:
space:
mode:
Diffstat (limited to 'build.go')
-rw-r--r--build.go82
1 files changed, 82 insertions, 0 deletions
diff --git a/build.go b/build.go
new file mode 100644
index 0000000..7e046e5
--- /dev/null
+++ b/build.go
@@ -0,0 +1,82 @@
+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())
+ }
+ 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))
+ }
+}