summaryrefslogtreecommitdiff
path: root/build.go
blob: f958e80c7f309e44369836835ca9840b19688a93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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())
	}
	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"}

	// 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))
	}
}