// Copyright 2017-2025 WIT.COM Inc. All rights reserved. // Use of this source code is governed by the GPL 3.0 package main import ( "fmt" "os" "path/filepath" "strings" "go.wit.com/lib/cobol" "go.wit.com/lib/env" "go.wit.com/lib/protobuf/argvpb" "go.wit.com/lib/protobuf/gitpb" "go.wit.com/log" ) func doBuild() (string, error) { initForge() if argv.Build.Install != nil { if err := doInstall(me.forge.Repos); err != nil { // log.Info("doInstall() failed", err) argvpb.BadExit("doInstall() failed", err) } if env.True("--dry-run") { argvpb.GoodExit("Nothing built --dry-run") } argvpb.GoodExit("EVERYTHING BUILT!") } if argv.Build.MacBuild != nil { return "todo: add mac builds", nil } if argv.Build.Debian != nil { // figure out what to build // found := findBuildDeb() data, err := resources.ReadFile("resources/go-deb") if err != nil { return "missing resources/go-deb list", err } tobuild := gitpb.NewRepos() var simpletable [][]string simpletable = append(simpletable, []string{"debname", "version", " age", "smartver", "curb", "curver", "newb", "final", "build", "arch", "FullPath"}) for i, line := range strings.Split(string(data), "\n") { var path string var debname string var arches string line = strings.TrimSpace(line) if line == "" { continue } if strings.HasPrefix(line, "#") { continue } parts := strings.Split(line, ",") if len(parts) > 0 { path = parts[0] } if len(parts) > 1 { debname = parts[1] } if len(parts) > 2 { arches = parts[2] } repo := me.forge.Repos.FindByNamespace(path) if repo == nil { log.Info("CAN NOT FIND REPO FOR:", i, path, debname, arches) continue } if debname == "" { _, debname = filepath.Split(path) } p := me.machine.Wit.FindByPackage(debname) if p == nil { log.Info("CAN NOT FIND PACKAGE FOR:", debname) continue } // var version string // version = actualp.GetVersion() // log.Info("MAKE DEB FOR:", i, path, debname, arches) bcount := fmt.Sprintf("%d", p.GetBuildCount()) var newcount string var final string smartver := p.GetSmartVersion() curver := repo.GetSmartVersion() if smartver == curver { newcount = fmt.Sprintf("%d", p.GetBuildCount()+1) final = repo.DebianCurrentVersion(p.GetBuildCount() + 1) } else { newcount = fmt.Sprintf("%d", 0) final = repo.DebianCurrentVersion(0) } var age string if t := p.GetAge(); t == nil { age = "notsure" } else { age = cobol.Duration(t) } cmd := makeGoDebCmd(repo, newcount, "") cmdstr := fmt.Sprintf("%v", cmd) build := "no" if (smartver != curver) || repo.CheckDirty() || argv.All { build = "yes" repo.DirtyList = cmd tobuild.Clone(repo) } simpletable = append(simpletable, []string{debname, p.Version, age, smartver, bcount, curver, newcount, final, build, arches, cmdstr}) } footer := cobol.SimpleTable(simpletable) log.Info("simple build table footer:", footer) if env.True("--dry-run") { return "dryrun", nil } // for r := range tobuild.IterAll() { // log.Info(r.FullPath, r.DirtyList) // } return doBuildDeb(tobuild) } return "todo: doBuild()", nil } func doInstall(all *gitpb.Repos) error { // doInstallScan() found := gitpb.NewRepos() for check := range all.IterAll() { if me.forge.Config.IsReadOnly(check.Namespace) { continue } if !check.IsBinary() { continue } if check.IsGoPlugin() { continue } found.Append(check) } footer := found.PrintForgedTable() log.Info("Starting 'go install' on these repos:", footer) if env.True("--dry-run") { return nil } me.forge.ConfigRill(16, 16) os.Setenv("GO111MODULE", "off") stats := me.forge.RunOnRepos(found, doInstallRepo) os.Unsetenv("GO111MODULE") failed := gitpb.NewRepos() for s, stat := range stats { if stat.Err == nil { continue } // log.Info("CRAP. INSTALL FAILED", config.FormatDuration(dur), s, stat.Err) msg := "go install FAILED " + s log.Info(msg) found := me.forge.Repos.FindByFullPath(s) if found == nil { log.Info("found", found, "'"+s+"'") panic("wrong namespace logic. couldn't find repo from stats") } failed.Append(found) } os.Remove(filepath.Join(env.Get("homedir"), "go/bin/forged")) if failed.Len() > 0 { for r := range failed.IterAll() { log.Info("r.Namespace", r.Namespace) } failed.SortNamespace() footer = failed.PrintForgedTable() log.Info("TABLE WITH NULLS", footer) argvpb.BadExit("go install Failed for: "+footer, nil) } return nil } func doInstallRepo(check *gitpb.Repo) error { repotype := check.GetRepoType() if repotype == "binary" || repotype == "plugin" { // we only want to process things that can be compiled with 'go build' } else { // log.Info("skipping repo", check.Namespace, repotype) return nil } if me.forge.Config.IsReadOnly(check.Namespace) { // ignore read only stuff return nil } if argv.Verbose { verbose := []string{"-v", "-x"} if err := me.forge.Install(check, verbose); err != nil { log.Warn("INSTALL FAILED", check.Namespace, err) return err } } else { if err := me.forge.Install(check, nil); err != nil { log.Warn("INSTALL FAILED", check.Namespace, err) return err } } return nil } func doInstallScan() { all := me.forge.Repos.SortByFullPath() for all.Scan() { check := all.Next() repotype := check.GetRepoType() if repotype == "binary" || repotype == "plugin" { // we only want to process things that can be compiled with 'go build' } else { // log.Info("skipping repo", check.Namespace, repotype) continue } if me.forge.Config.IsReadOnly(check.Namespace) { // ignore read only stuff continue } if check.GetState() == "" { check.State = "need to build" } // var cmd []string var start string var end string manufactured := check.GetCurrentVersion() ver := trimNonNumericFromStart(manufactured) name := me.forge.Config.DebName(check.GetNamespace()) var realver string if installedPackage := me.machine.FindInstalledByName(name); installedPackage != nil { realver = installedPackage.Version } if actualp := me.machine.FindByVersion(name, ver); actualp != nil { end += " (version match) " + actualp.Version + " " + ver + " " check.State = "on mirrors" } else { if realver != "" { end += fmt.Sprintf(" (version miss) %s vs %s ", realver, ver) } // end += "" + ver + " " } if me.machine.IsInstalled(name) { if actualp := me.machine.FindInstalledByName(name); actualp != nil { if ver != actualp.Version { end += "(installed " + actualp.Version + ") " } else { end += "(installed ok) " } } else { end += "(installed) " } } state := check.GetState() // todo: get all this shit into the protobuf start = fmt.Sprintf("%-18.18s %-24s", state, ver) if strings.HasPrefix(check.GetState(), "unknown bran") { state = "need to build" } if state == "need to build" { end = "(will build)" + end } end = strings.TrimSpace(end) if end == "" { end = check.GetState() } log.Info(start, check.GetRepoType(), end) if name == "" { // err := fmt.Sprintf("name is blank error %+v", repo) log.Warn("name is blank error", check.GetNamespace()) } } }