summaryrefslogtreecommitdiff
path: root/doBuild.debian.go
blob: e8f497decd4f804cfa21d6ab15da72c50dabf811 (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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0

package main

import (
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"strconv"
	"strings"

	"go.wit.com/lib/ENV"
	"go.wit.com/lib/cobol"
	"go.wit.com/lib/debian"
	"go.wit.com/lib/fhelp"
	"go.wit.com/lib/gui/shell"
	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

// find packages that need to be built
func findBuildDeb() *gitpb.Repos {
	initForge()

	var dumbtable [][]string
	dumbtable = append(dumbtable, []string{"FullPath", "version", "reason"})
	found := gitpb.NewRepos()
	for check := range me.forge.Repos.IterAll() {
		var reason string
		if me.forge.Config.IsReadOnly(check.GetNamespace()) {
			reason = "readonly"
			continue
		}
		if me.forge.Config.IsPrivate(check.GetNamespace()) {
			if !argv.Build.Debian.Priv {
				reason = "private"
				continue
			}
		}

		if !check.IsBinary() {
			reason = "not-binary"
			// can't build packages that aren't GO binaries
			continue
		}
		if argv.Build.Debian.Arch != "" {
			if check.IsGoPlugin() {
				reason = "plugin"
				continue
			}
			// mask some packages for arch builds
			if strings.Contains(check.Namespace, "fyne") {
				reason = "plugin"
				continue
			}
			if strings.Contains(check.Namespace, "toolkit") {
				reason = "broken"
				continue
			}
			if strings.Contains(check.Namespace, "going2git") {
				reason = "broken"
				continue
			}
		}

		if argv.All {
			// don't check if you need to build, just build everything
		} else {
			if shouldBuild(check) == "yes" {
				// need to build
			} else {
				var version string
				bvers, err := getBuildVersion(check)
				if err == nil {
					version = check.DebianCurrentVersion(bvers)
				} else {
					version = fmt.Sprintf("err=(%v)", err)
				}
				// log.Info(check.FullPath, "not building for reasons: todo: get reason")
				dumbtable = append(dumbtable, []string{check.FullPath, version, reason})
				continue
			}
		}

		found.Append(check)
	}
	footer := cobol.SimpleTable(dumbtable)
	log.Info("simple build table footer:", footer)

	found.ActualSort()
	return found
}

var totalBuilt int

func doBuildDeb(all *gitpb.Repos) (string, error) {
	log.Info("STARTING DEBIAN PACKAGE BUILD")
	// clean out old deb files
	globPattern := filepath.Join(me.homedir, "incoming", "*.deb")
	files, err := filepath.Glob(globPattern)
	if len(files) > 0 {
		log.Info(files, err)
		log.Info("You have files in ~/incoming/")
		return "incoming/ not empty", errors.New("already have incoming files")
	}

	footer := printPackagingTable(all)
	log.Info("This is what will and will not be built:", footer)

	me.forge.ConfigRill(16, 16)
	stats := me.forge.RunOnRepos(all, buildDeb)
	for s, stat := range stats {
		if stat.Err != nil {
			return "ERROR WITH buildDeb " + s, stat.Err
		}
	}
	if totalBuilt == 0 {
		if argv.DryRun {
			log.Info("")
			return "--dry-run TRUE (nothing done)", nil
		}
		// nothing built, no need to talk to mirrors
		return "everything is current", nil
	}

	if _, err := shell.RunVerbose([]string{"ls", "-l", "/home/jcarr/incoming"}); err != nil {
		me.argv.BadExit("aptly failed", nil)
	}

	if _, err := fhelp.RunRealtimeError([]string{"do-aptly"}); err != nil {
		me.argv.BadExit("aptly failed", nil)
	}
	return "all .deb built ok", nil
}

// avoids nil panics
func isDebianRelease() bool {
	if argv.Build == nil {
		return false
	}
	if argv.Build.Debian == nil {
		return false
	}
	return argv.Build.Debian.Release
}

func shouldBuild(repo *gitpb.Repo) string {
	// NEVER EVER EVER BUILD DIRTY .deb PACKAGES
	// versioning with debian will prioritize the 'd' before numbers
	// so packages will be randomly upgraded based on the git hash
	// just do a commit. Do not 'fix' this. This is a good idea anyway.
	// This should be a policy everywhere.
	if repo.IsDirty() {
		return "no"
	}
	ver := repo.GetCurrentVersion()
	debname := getDebFilename(repo)
	if !debian.DebFilenameMatchesVersion(debname, ver) {
		return "yes"
	}
	if argv.All {
		return "yes"
	}
	return ""
}

func getBuildVersion(repo *gitpb.Repo) (int, error) {
	_, reponame := filepath.Split(repo.Namespace)
	if !ENV.InitValid() {
		log.Info("ENV.Get() InitValid() false", reponame)
		return 0, ENV.NotInitialized
	}
	if ENV.Get(reponame) == "" {
		// first time for this repo
		ENV.Set(reponame, log.Sprintf("%s %s", repo.DebianCurrentVersion(0), "0"))
		err := ENV.Save()
		log.Info("ENV.Get() was blank", reponame, err)
		return 0, err
	}
	parts := strings.Fields(ENV.Get(reponame))
	// log.Printf("ENV.Get() worked var(%s) parts[%v]\n", reponame, parts)
	if len(parts) != 2 {
		log.Info("ENV.Get() wierd setting. fix manually:", reponame, parts)
		return 0, errors.New("fix manually in ~/.config/mirrors/config.text")
	}
	i, err := strconv.Atoi(parts[1])
	newi := i + 1
	if err != nil {
		log.Info("ENV.Save() strconv.Atoi() err:", reponame, parts, err)
	}

	// log.Info("ENV.Get() worked", parts, err, i, newi)
	ENV.Set(reponame, log.Sprintf("%s %d", repo.DebianCurrentVersion(0), newi))
	errors.Join(err, ENV.Save())
	if err != nil {
		log.Info("ENV.Save() err:", reponame, err)
	}
	return newi, err
}

func buildDeb(repo *gitpb.Repo) error {
	var cmd []string

	outdir := getOutdir(repo)
	os.MkdirAll(outdir, 0755)

	if isDebianRelease() {
		cmd = []string{"go-deb", "--release", "--namespace", repo.Namespace, "--dir", outdir}
	} else {
		cmd = []string{"go-deb", "--namespace", repo.Namespace, "--dir", outdir}
	}

	if me.forge.Config.IsPrivate(repo.GetNamespace()) {
		cmd = []string{"go-deb", "--namespace", repo.Namespace, "--dir", outdir}
		// return nil
	}
	if argv.Build.Debian.Arch != "" {
		cmd = append(cmd, "--arch", argv.Build.Debian.Arch)
	}
	_, reponame := filepath.Split(repo.Namespace)
	if shouldBuild(repo) != "yes" {
		// shouldn't build this one
		return nil
	} else {
		if reponame != "autogenpb" {
			log.Info("WHY BUILD AUTOGEN HERE shouldBuild(repo) said", shouldBuild(repo))
		}
	}

	// try to use lib/config
	bvers, err := getBuildVersion(repo)
	if reponame != "autogenpb" {
		log.Info("WHY BUILD AUTOGEN HERE", reponame, bvers, err)
	}
	if err == nil {
		log.Info("ENV.Get() gave back new buildId", bvers, repo.Namespace)
	} else {
		log.Info("ENV.Get() gave back err new buildId", bvers, err, repo.Namespace)
		panic("ENV.Get() error")
	}
	cmd = append(cmd, "--buildversion", log.Sprintf("%d", bvers))

	if argv.Verbose {
		// log.Info("build cmd:", cmd)
		cmd = append(cmd, "--verbose")
	}

	debname := me.forge.Config.DebName(repo.GetNamespace())
	debname += "." + me.forge.GetPackageVersion(repo) + ".deb"
	if argv.DryRun {
		log.Info("RUN:", repo.FullPath, debname)
		log.Info("RUN:", cmd)
		return nil
	}

	log.Info("Building", debname, cmd)
	if _, err = repo.RunVerboseOnError(cmd); err != nil {
		log.Info(repo.FullPath, cmd)
		return err
	}

	totalBuilt += 1
	log.Info("build worked", cmd, repo.FullPath)
	return nil
}

func getOutdir(repo *gitpb.Repo) string {
	if me.forge.Config.IsPrivate(repo.GetNamespace()) {
		return "/home/jcarr/incoming-private"
	}
	if argv.Force {
		return "/home/jcarr/incoming"
	}
	if repo.GetLastTag() != repo.GetMasterVersion() {
		return "/home/jcarr/incoming-devel"
	}

	if repo.GetCurrentBranchVersion() != repo.GetMasterVersion() {
		return "/home/jcarr/incoming-devel"
	}

	if repo.CheckDirty() {
		return "/home/jcarr/incoming-dirty-junk"
	}

	return "/home/jcarr/incoming"
}