245 lines
7.0 KiB
Go
245 lines
7.0 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"go.wit.com/lib/gui/shell"
|
|
"go.wit.com/lib/protobuf/gitpb"
|
|
"go.wit.com/log"
|
|
)
|
|
|
|
func buildPackage(repo *gitpb.Repo) (bool, error) {
|
|
// TODO: if dirty, set GO111MODULE
|
|
// also, if last tag != version
|
|
/*
|
|
go install -ldflags " \
|
|
-X main.GITCOMMIT=${GITCOMMIT} \
|
|
-X main.GOVERSION='${GOVERSION}' \
|
|
-X main.BUILDTIME='${BUILDTIME}' \
|
|
-X main.VERSION=${VERSION}"
|
|
*/
|
|
// ldflags := "main.GOTAG=" + repo.LastTag()
|
|
|
|
filename := repo.Control["Package"] // c.Package.String()
|
|
if filename == "" {
|
|
return false, errors.New("filename is blank")
|
|
}
|
|
|
|
arch := repo.Control["Architecture"] // c.Architecture.String()
|
|
if arch == "" {
|
|
arch = "amd64" // todo: detect what you are building on
|
|
}
|
|
version := repo.Control["Version"]
|
|
log.Info("version is:", version)
|
|
debname := filename + "_" + version + "_" + arch + ".deb"
|
|
var fulldebname string
|
|
if argv.OutDir == "" {
|
|
fulldebname = debname
|
|
} else {
|
|
fulldebname = filepath.Join(argv.OutDir, debname)
|
|
}
|
|
if shell.Exists(fulldebname) {
|
|
log.Info("debian package already built: " + fulldebname)
|
|
return true, errors.New("debian package already built: " + fulldebname)
|
|
|
|
}
|
|
|
|
var fullfilename string
|
|
_, fullfilename = filepath.Split(filename)
|
|
|
|
if fullfilename == "" {
|
|
log.Info("fullfilename =", fullfilename)
|
|
badExit(log.Errorf("binary name was blank"))
|
|
}
|
|
if fullfilename == "." {
|
|
log.Info("fullfilename =", fullfilename)
|
|
badExit(log.Errorf("binary name was ."))
|
|
}
|
|
if shell.Exists(fullfilename) {
|
|
repo.RunVerbose([]string{"rm", "-f", fullfilename})
|
|
}
|
|
|
|
if shell.Exists(fullfilename) {
|
|
// something wrong
|
|
return false, errors.New("binary existed before build")
|
|
}
|
|
|
|
if argv.Release {
|
|
os.Unsetenv("GO111MODULE")
|
|
cmd := []string{"go"}
|
|
cmd = append(cmd, "install")
|
|
if argv.Verbose {
|
|
cmd = append(cmd, "-v")
|
|
cmd = append(cmd, "-x")
|
|
}
|
|
/*
|
|
cmd = append(cmd, "some path"+"@v"+version)
|
|
if err := shell.PathExecVerbose("", cmd); err != nil {
|
|
badExit(err)
|
|
return false, fmt.Errorf("go build err %v", err)
|
|
}
|
|
*/
|
|
cmd = []string{"go"}
|
|
cmd = append(cmd, "build")
|
|
if argv.Verbose {
|
|
cmd = append(cmd, "-v")
|
|
cmd = append(cmd, "-x")
|
|
}
|
|
cmd = append(cmd, "this should be the path")
|
|
if err := shell.PathExecVerbose("", cmd); err != nil {
|
|
badExit(err)
|
|
return false, fmt.Errorf("go build err %v", err)
|
|
}
|
|
log.Warn("build worked")
|
|
} else {
|
|
// set the GO111 build var to true. pass the versions to the compiler manually
|
|
os.Setenv("GO111MODULE", "off")
|
|
cmd := []string{"go", "build"}
|
|
// 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 argv.Ldflags {
|
|
cmd = append(cmd, "-ldflags", "-X "+flag)
|
|
}
|
|
|
|
err := repo.RunVerbose(cmd)
|
|
if err != nil {
|
|
return false, fmt.Errorf("go build err %v", err)
|
|
}
|
|
log.Warn("go build worked")
|
|
}
|
|
|
|
filebase := filepath.Base(repo.Control["pathL"]) // c.pathL.String())
|
|
if fullfilename != filebase {
|
|
// this exception is for when you want to override a package name
|
|
// sometimes that's the best option. This way you can keep your
|
|
// name, but the .deb package name can be different so you can
|
|
// still apt-get it. For an example, look at the gozookeeper package
|
|
fullfilename = filebase
|
|
}
|
|
|
|
if !shell.Exists(fullfilename) {
|
|
log.Warn("build failed. filename does not exist", fullfilename)
|
|
return false, errors.New("missing " + fullfilename)
|
|
}
|
|
|
|
if shell.Exists("files") {
|
|
repo.RunVerbose([]string{"rm", "-rf", "files"})
|
|
// log.Info("running sync")
|
|
repo.RunVerbose([]string{"sync"})
|
|
if shell.Exists("files") {
|
|
log.Warn("rm failed for some reason")
|
|
return false, errors.New("rm files/")
|
|
}
|
|
}
|
|
repo.RunVerbose([]string{"sync"}) // for some reason the next check fails sometimes?
|
|
if shell.Exists("files") {
|
|
// probably the 'shell' package id being stupid and not waiting for the process to actually exit
|
|
log.Warn("rm failed. files/ still exists. is golang doing these in parallel?")
|
|
return false, errors.New("rm files/")
|
|
}
|
|
if err := os.MkdirAll("files/DEBIAN", os.ModePerm); err != nil {
|
|
return false, errors.New("mkdir files/DEBIAN")
|
|
}
|
|
if err := os.MkdirAll("files/usr/bin", os.ModePerm); err != nil {
|
|
log.Warn("mkdir failed")
|
|
return false, errors.New("mkdir files/usr/bin")
|
|
}
|
|
if os.Getenv("GO_DEB_CUSTOM") == "true" {
|
|
// skip cp & strip on custom 'control' files
|
|
// probably deprecate this
|
|
log.Info("REPO GO_DEB_CUSTOM=true means binary is not copied")
|
|
} else {
|
|
_, fname := filepath.Split(repo.GetFullPath())
|
|
cmd := []string{"cp", fname, "files/usr/bin"}
|
|
log.Info("REPO FILENAME cp", cmd)
|
|
if err := repo.RunVerbose(cmd); err != nil {
|
|
log.Warn("cp failed")
|
|
return false, err
|
|
}
|
|
cmd = []string{"strip", "files/usr/bin/" + fname}
|
|
if err := repo.RunVerbose(cmd); err != nil {
|
|
log.Warn("strip failed")
|
|
return false, err
|
|
}
|
|
}
|
|
|
|
// put the README in there (if missing, generate it?)
|
|
var readme string = ""
|
|
if shell.Exists("README.md") {
|
|
readme = "README.md"
|
|
}
|
|
|
|
if shell.Exists("README") {
|
|
readme = "README"
|
|
}
|
|
|
|
if readme != "" {
|
|
path := filepath.Join("files/usr/lib/" + filename)
|
|
if err := os.MkdirAll(path, os.ModePerm); err != nil {
|
|
return false, errors.New("no files/usr/lib")
|
|
}
|
|
if err := repo.RunVerbose([]string{"cp", readme, path}); err != nil {
|
|
return false, err
|
|
}
|
|
}
|
|
|
|
if !writeDebianControlFile(repo) {
|
|
return false, errors.New("write control file")
|
|
}
|
|
if shell.Exists("postinst") {
|
|
repo.RunVerbose([]string{"cp", "postinst", "files/DEBIAN/"})
|
|
}
|
|
|
|
// experiment for the toolkit package
|
|
// if the git repo has a "./build" script run it before packaging
|
|
// this way the user can put custom files in the .deb package
|
|
if shell.Exists("build") {
|
|
log.Info(repo.FullPath, "FOUND ./build HERE")
|
|
repo.RunVerbose([]string{"./build"})
|
|
} else {
|
|
log.Info(repo.FullPath, "NOT FOUND ./build HERE")
|
|
}
|
|
|
|
cmd := []string{"dpkg-deb", "--build", "files", fulldebname}
|
|
result := repo.RunVerbose(cmd)
|
|
if shell.Exists(fulldebname) {
|
|
} else {
|
|
log.Warn("CMD FAILED", cmd, result)
|
|
log.Warn("build failed: full name was not created:", fulldebname)
|
|
return false, errors.New("dpkg-deb --build failed")
|
|
}
|
|
repo.RunVerbose([]string{"dpkg-deb", "-I", fulldebname})
|
|
repo.RunVerbose([]string{"dpkg-deb", "-c", fulldebname})
|
|
|
|
// cleanup files
|
|
if shell.Exists("files") {
|
|
if argv.KeepFiles {
|
|
log.Info("keeping the build files/")
|
|
} else {
|
|
repo.RunVerbose([]string{"rm", "-rf", "files"})
|
|
// log.Info("running sync")
|
|
repo.RunVerbose([]string{"sync"})
|
|
if shell.Exists("files") {
|
|
log.Warn("rm -rf files/ failed. Run() returned false")
|
|
return false, errors.New("rm files/")
|
|
}
|
|
}
|
|
}
|
|
return true, nil
|
|
}
|