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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
package forgepb
// for golang repos, this is an attempt to build the package
//
// Additions to 'go build' that are attempted here:
//
// * detect packages that are plugins
// * run autogenpb packages that have .proto protobuf files
// * use some 'standard' ldflags (VERISON, BUILDTIME)
//
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"go.wit.com/lib/gui/shell"
"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")
}
// userflags are intended for "-v" and "-x" right now
func (f *Forge) doBuild(repo *gitpb.Repo, userFlags []string, goWhat string) error {
if repo == nil {
log.Warn("forge.doBuild repo == nil")
return errors.New("forge.doBuild repo == nil")
}
if f.IsGoWork() {
// there must be a valid go.mod file if compiling with go.work
if err := repo.ValidGoSum(); err != nil {
log.Warn("forge.doBuild() failed. run go-mod-clean here?")
return err
}
}
if repo.Exists(".forge") {
log.Info("custom build instructions")
data, _ := repo.ReadFile(".forge")
log.Info(".forge =", string(data))
log.Info("todo: do the custom build instructions")
basedir, filename := filepath.Split(repo.GetGoPath())
log.Info("touching filename", basedir, filename)
repo.RunVerbose([]string{"touch", filename})
return nil
}
// if not GoPrimitive, autogen each dependent git repo
if repo.GoDepsLen() != 0 {
// build the protobuf files in all protobuf repos
all := repo.GoDeps.SortByGoPath()
for all.Scan() {
t := all.Next()
found := f.Repos.FindByNamespace(t.GetGoPath())
if found.GetRepoType() == "protobuf" {
if err := f.runAutogenpb(found); err != nil {
return err
}
}
}
}
// get the version
version := repo.GetCurrentBranchVersion()
if version == "" {
version = "forgeErr"
}
if repo.CheckDirty() {
version = version + "-dirty"
}
cmd := []string{"go"}
if repo.GetRepoType() == "plugin" {
if goWhat == "install" {
log.Info("Can not go install plugins yet. just building to ~/go/lib/")
}
cmd = append(cmd, "build")
} else {
cmd = append(cmd, goWhat)
}
_, fname := filepath.Split(repo.FullPath)
homeDir, _ := os.UserHomeDir()
soname := fname + "." + version + ".so"
linkname := fname + ".so"
sopath := filepath.Join(homeDir, "go/lib/go-gui")
// if this is a plugin, use buildmode=plugin
if repo.GetRepoType() == "plugin" {
if goWhat == "install" {
fullname := filepath.Join(sopath, soname)
cmd = append(cmd, "-buildmode=plugin", "-o", fullname)
} else {
cmd = append(cmd, "-buildmode=plugin", "-o", soname)
}
}
for _, flag := range userFlags {
cmd = append(cmd, flag)
}
// set standard ldflag options
now := time.Now()
datestamp := log.Sprintf("%d", now.Unix()) // print seconds, parse in go-args --version
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)
var buildnotes string
if os.Getenv("GO111MODULE") == "off" {
buildnotes = "GO111MODULE=off"
} else {
buildnotes = "GO111MODULE"
}
if f.IsGoWork() {
buildnotes += " go.work=true"
}
log.Infof("Run: %s %s %v\n", buildnotes, repo.FullPath, cmd)
result := repo.RunRealtime(cmd)
if result.Exit != 0 {
// build failed
log.DaemonMode(true)
log.Info(strings.Join(result.Stdout, "\n"))
log.Info(strings.Join(result.Stderr, "\n"))
log.Info("result.Error =", result.Error)
log.Info("result.Exit =", result.Exit)
log.DaemonMode(false)
log.Warn("go build failed", cmd)
return errors.New("go " + goWhat + " failed: " + fmt.Sprint(result.Error))
}
// make symlinks
if repo.GetRepoType() == "plugin" {
cmd := []string{"ln", "-sf", soname, linkname}
if goWhat == "install" {
shell.PathRun(sopath, cmd)
} else {
repo.Run(cmd)
}
}
// log.Info(strings.Join(result.Stdout, "\n"))
return nil
}
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
}
// Never do this. this is stupid. fix your repo
// Never try to version & release broken repos
// leave this code here as a reminder to never attempt this
func (f *Forge) forgeIgnoreGoMod(repo *gitpb.Repo) bool {
if !repo.Exists(".forge") {
return false
}
log.Info("custom build instructions")
data, _ := repo.ReadFile(".forge")
log.Info(".forge =", string(data))
for _, line := range strings.Split(string(data), "\n") {
if strings.Contains(line, "forge:ignore:gomod") { // this is stupid
return true
}
}
return false
}
|