autotypist/repolist.go

389 lines
9.3 KiB
Go

package main
import (
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strings"
"go.wit.com/gui"
"go.wit.com/lib/gadgets"
"go.wit.com/lib/gui/repostatus"
"go.wit.com/log"
)
func (r *repo) String() string {
return r.status.String()
}
func (r *repo) getPath() string {
return r.path
}
func RemoveFirstElement(slice []string) (string, []string) {
if len(slice) == 0 {
return "", slice // Return the original slice if it's empty
}
return slice[0], slice[1:] // Return the slice without the first element
}
// returns path, master branch name, devel branch name, user branch name
func splitLine(line string) (string, string, string, string) {
var path, master, devel, user string
parts := strings.Split(line, " ")
path, parts = RemoveFirstElement(parts)
master, parts = RemoveFirstElement(parts)
devel, parts = RemoveFirstElement(parts)
user, parts = RemoveFirstElement(parts)
// path, master, devel, user := strings.Split(line, " ")
return path, master, devel, user
}
func myrepolist() []string {
homeDir, _ := os.UserHomeDir()
cfgfile := filepath.Join(homeDir, ".config/myrepolist")
content, _ := ioutil.ReadFile(cfgfile)
out := string(content)
out = strings.TrimSpace(out)
lines := strings.Split(out, "\n")
return lines
}
func (r *repo) Hide() {
r.pLabel.Hide()
r.lastTag.Hide()
r.vLabel.Hide()
r.masterVersion.Hide()
r.develVersion.Hide()
r.userVersion.Hide()
r.dirtyLabel.Hide()
r.endBox.Hide()
// r.statusButton.Hide()
// r.diffButton.Hide()
r.hidden = true
}
func (r *repo) Hidden() bool {
return r.hidden
}
func (r *repo) Show() {
r.pLabel.Show()
r.lastTag.Show()
r.vLabel.Show()
r.masterVersion.Show()
r.develVersion.Show()
r.userVersion.Show()
r.dirtyLabel.Show()
r.endBox.Show()
// r.statusButton.Show()
// r.diffButton.Show()
r.hidden = false
}
func addRepo(grid *gui.Node, path string, master string, devel string, user string) {
_, ok := me.allrepos[path]
if ok {
log.Info("addRepo() already had path", path)
return
}
newRepo := new(repo)
path = strings.Trim(path, "/") // trim any extranous '/' chars put in the config file by the user
if path == "" {
log.Warn("addRepo() got empty path", path, master, devel, user)
return
}
if repostatus.VerifyLocalGoRepo(path) {
log.Verbose("newRepo actually exists", newRepo.getPath())
} else {
log.Warn("repostatus.VerifyLocalGoRepo() failed for for", path, master, devel, user)
return
}
newRepo.path = path
newRepo.pLabel = grid.NewLabel(path).SetProgName("path")
newRepo.lastTag = grid.NewLabel("").SetProgName("lastTag")
newRepo.masterVersion = grid.NewLabel("").SetProgName("masterVersion")
newRepo.develVersion = grid.NewLabel("").SetProgName("develVersion")
newRepo.userVersion = grid.NewLabel("").SetProgName("userVersion")
newRepo.dirtyLabel = grid.NewLabel("")
newRepo.vLabel = grid.NewLabel("").SetProgName("current")
newRepo.endBox = grid.NewHorizontalBox("HBOX")
newRepo.endBox.NewButton("Configure", func() {
if newRepo.status == nil {
log.Warn("status window wasn't created")
return
}
newRepo.status.Toggle()
})
newRepo.endBox.NewButton("show diff", func() {
me.reposwin.Disable()
newRepo.status.XtermNohup([]string{"git diff"})
me.reposwin.Enable()
})
newRepo.endBox.NewButton("commit all", func() {
me.reposwin.Disable()
// restore anything staged so everything can be reviewed
newRepo.status.Xterm([]string{"git restore --staged ."})
// newRepo.status.Xterm([]string{"git diff"})
newRepo.status.Xterm([]string{"git add --all"})
newRepo.status.XtermNohup([]string{"git diff --cached"})
newRepo.status.Xterm([]string{"git commit -a"})
if newRepo.status.CheckDirty() {
// commit was not done, restore diff
newRepo.status.Xterm([]string{"git restore --staged ."})
} else {
newRepo.status.Update()
newRepo.newScan()
}
me.reposwin.Enable()
})
newRepo.status = repostatus.NewRepoStatusWindow(newRepo.path)
newRepo.hidden = false
newRepo.status.SetMainWorkingName(master)
newRepo.status.SetDevelWorkingName(devel)
newRepo.status.SetUserWorkingName(user)
var showBuildB bool = false
switch newRepo.status.RepoType() {
case "binary":
log.Info("compile here. Show()")
showBuildB = true
case "library":
log.Info("library here. Hide()")
default:
log.Info("unknown. Show()")
}
if showBuildB {
newRepo.endBox.NewButton("build", func() {
newRepo.status.Build()
})
}
me.allrepos[path] = newRepo
}
// This creates a window
func repolistWindow() {
me.reposwin = gadgets.NewBasicWindow(me.myGui, "All git repositories in ~/go/src/")
me.reposwin.Make()
me.reposbox = me.reposwin.Box().NewBox("bw vbox", false)
// me.reposwin.Draw()
me.reposwin.Custom = func() {
log.Warn("GOT HERE: main() gadgets.NewBasicWindow() close")
log.Warn("Should I do something special here?")
}
repoAllButtons(me.reposbox)
me.reposgroup = me.reposbox.NewGroup("go repositories (read from ~/.config/myrepolist)")
me.reposgrid = me.reposgroup.NewGrid("test", 0, 0)
me.reposgrid.NewLabel("") // path goes here
me.reposgrid.NewLabel("last tag").SetProgName("last tag")
me.reposgrid.NewLabel("master version")
me.reposgrid.NewLabel("devel version")
me.reposgrid.NewLabel("user version")
me.reposgrid.NewLabel("Status")
me.reposgrid.NewLabel("Current Version").SetProgName("Current Version")
me.reposgrid.NextRow()
usr, _ := user.Current()
repos := myrepolist()
for _, line := range repos {
log.Verbose("repo =", line)
path, mbranch, dbranch, ubranch := splitLine(line)
if mbranch == "" {
mbranch = "master"
}
if dbranch == "" {
dbranch = "devel"
}
if ubranch == "" {
ubranch = usr.Username
}
addRepo(me.reposgrid, path, mbranch, dbranch, ubranch)
me.reposgrid.NextRow()
}
// TODO: figure out why this borks everything
/*
for i, path := range repostatus.ListGitDirectories() {
// log.Info("addRepo()", i, path)
tmp := strings.TrimPrefix(path, me.goSrcPwd.String())
log.Info("addRepo()", i, tmp)
addRepo(reposgrid, tmp, "guimaster", "guidevel", usr.Username)
}
*/
// reposwin.Toggle()
}
func showApps() {
for _, repo := range me.allrepos {
switch repo.status.RepoType() {
case "binary":
log.Info("compile here. Show()")
repo.Show()
case "library":
log.Info("library here. Hide()")
repo.Hide()
default:
log.Info("unknown. Show()")
repo.Hide()
}
}
}
func repoAllButtons(box *gui.Node) {
// reposbox.SetExpand(false)
group1 := box.NewGroup("Run on all repos:")
hbox := group1.Box()
// hbox.Horizontal()
hbox.Vertical()
box1 := hbox.Box().Vertical()
box1.NewButton("status.Update() all", func() {
for _, repo := range me.allrepos {
repo.status.Update()
repo.newScan()
}
})
box2 := hbox.Box().Vertical()
box2.NewButton("merge all user to devel", func() {
me.reposwin.Disable()
if !mergeAllUserToDevel() {
return
}
me.reposwin.Enable()
})
box2.NewButton("merge all devel to main", func() {
me.reposwin.Disable()
if !mergeAllDevelToMain() {
return
}
me.reposwin.Enable()
})
box2.NewButton("merge it all", func() {
me.reposwin.Disable()
if !mergeAllUserToDevel() {
return
}
if !mergeAllDevelToMain() {
return
}
me.reposwin.Enable()
})
box2.NewButton("test all builds", func() {
me.reposwin.Disable()
defer me.reposwin.Enable()
showApps()
for _, repo := range me.allrepos {
if repo.Hidden() {
// log.Info("skip hidden", repo.String())
} else {
log.Info("try to build", repo.String())
if repo.status.Build() {
log.Info("build worked", repo.String())
} else {
log.Info("build failed", repo.String())
go repo.status.Xterm([]string{"bash"})
return
}
}
}
log.Info("")
log.Info("every build worked !!!")
log.Info("")
})
}
func mergeAllDevelToMain() bool {
log.Info("merge all here")
for _, repo := range me.allrepos {
if repo.status.ReadOnly() {
log.Info("skipping readonly", repo.String(), repo.dirtyLabel.String())
continue
}
if repo.dirtyLabel.String() != "merge to main" {
log.Info("skipping. not merge to main", repo.String(), repo.dirtyLabel.String())
continue
}
if repo.status.CheckDirty() {
log.Info("skipping dirty", repo.String(), repo.dirtyLabel.String())
continue
}
log.Info("found", repo.String(), repo.dirtyLabel.String())
// repo.status.Update()
if repo.status.RunDevelMergeB() {
log.Warn("THINGS SEEM OK fullAutomation() returned true.")
} else {
log.Warn("THINGS FAILED fullAutomation() returned false")
return false
}
repo.status.Update()
repo.newScan()
}
log.Warn("EVERYTHING WORKED")
return true
}
func mergeAllUserToDevel() bool {
log.Info("merge all here")
for _, repo := range me.allrepos {
if repo.status.ReadOnly() {
log.Info("skipping readonly", repo.String(), repo.dirtyLabel.String())
continue
}
if repo.dirtyLabel.String() != "merge to devel" {
log.Info("skipping. not merge to devel", repo.String(), repo.dirtyLabel.String())
continue
}
if repo.status.CheckDirty() {
log.Info("skipping dirty", repo.String(), repo.dirtyLabel.String())
continue
}
log.Info("found", repo.String(), repo.dirtyLabel.String())
// repo.status.Update()
if repo.status.RunDevelMergeB() {
log.Warn("THINGS SEEM OK fullAutomation() returned true.")
} else {
log.Warn("THINGS FAILED fullAutomation() returned false")
return false
}
repo.status.Update()
repo.newScan()
}
log.Warn("EVERYTHING WORKED")
return true
}