summaryrefslogtreecommitdiff
path: root/build.go
blob: 0b8b58acf64b5e49f927c3c63a5b66012e97ea89 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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"
	"path/filepath"
	"strings"
	"time"

	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

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

	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()
	}
	loop1 := repo.GoDeps.SortByGoPath()
	for loop1.Scan() {
		t := loop1.Next()
		found := f.Repos.FindByGoPath(t.GetGoPath())
		if found.RepoType() == "protobuf" {
			if err := f.runAutogenpb(found); err != nil {
				return err
			}
		}
	}

	if repo.CheckDirty() {
		version = version + "-dirty"
	}
	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)
	// 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)
	}

	log.Info("running:", cmd)
	result := repo.RunRealtime(cmd)
	if result.Exit == 0 {
		log.Info(strings.Join(result.Stdout, "\n"))
		return nil
	} else {
		log.DaemonMode(true)
		log.Info(strings.Join(result.Stdout, "\n"))
		log.Info(strings.Join(result.Stderr, "\n"))
		log.DaemonMode(false)
		log.Warn("go build failed", cmd)
		return errors.New("go build failed: " + fmt.Sprint(result.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
}