summaryrefslogtreecommitdiff
path: root/main.go
blob: 311f2e3803132897b596e1e4ff022c86de933d5f (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
package main

import (
	"embed"
	"os"
	"path/filepath"
	"strings"

	"go.wit.com/gui"
	"go.wit.com/lib/gui/shell"
	"go.wit.com/log"
)

// TODO: autocompute these in the gui
var releaseReasonS string
var releaseVersion string
var widgetVersion string

//go:embed resources/*
var resToolkit embed.FS

func main() {
	me = new(autoType)

	// TODO: autocompute these in the gui
	releaseReasonS = os.Getenv("GUIRELEASE_REASON")
	releaseVersion = os.Getenv("GUIRELEASE_VERSION")
	widgetVersion = os.Getenv("GUIRELEASE_WIDGET")

	if releaseVersion == "" {
		log.Info("GUIRELEASE_VERSION not set")
		os.Exit(0)
	}

	// unset the go development ENV var to generate release files
	// this is required for go mod init & tidy. Also, if the
	// user drops to a shell or xterm, then they shouldn't be set there either
	os.Unsetenv("GO111MODULE")

	me.myGui = gui.New()
	me.myGui.InitEmbed(resToolkit)
	me.myGui.Default()

	// our main window
	me.mainWindow = me.myGui.NewWindow("GUI release manager")
	me.mainBox = me.mainWindow.NewBox("bw hbox", true)

	// sanity check of things that might be around that mess
	// up things later
	// if you have a go.work file, you must delete it
	// TODO: check for go.work files anywhere
	homeDir, _ := os.UserHomeDir()
	gowork := filepath.Join(homeDir, "go/src/go.work")
	if shell.Exists(gowork) {
		log.Info("go.work must be deleted")
		os.Exit(0)
	}

	// sanity check of things that might be around that mess
	// up things later
	// check to make sure we have a go.sum here
	gosum := filepath.Join(homeDir, "go/src/go.wit.com/apps/guireleaser/go.sum")
	if !shell.Exists(gosum) {
		log.Info("go.sum must exist here")
		os.Exit(0)
	}

	// save the ENV var here
	me.releaseReasonS = releaseReasonS

	log.Info("Creating the Release Window")

	// initialize the repo list window
	// which should be all the git repositories in ~/go/src & the .config file
	me.repos = makeRepoView()

	// the left side of the window options
	globalDisplayOptions(me.mainBox)

	// create the right side of the main window
	createReleaseBox(me.mainBox)

	// disable the open repo button. this isn't really important
	// but does indicates the app (and toolkit) is working
	// this can be removed later, but in these early days, I'm using this
	// tool to release the code for this app, the gui and the gui toolkits
	// and sometimes they lie, don't display stuff, don't even disable things
	// so I can't trust even what I see. It's complicated right now still.
	me.release.openrepo.Disable()

	me.Disable()

	// parse config file and scan for .git repos
	me.repos.initRepoList()
	setTargetVersion()

	// register a Show/Hide function for the repo list table
	me.repos.View.RegisterHideFunction(showHideRepos)

	// scan in the State of all the repos
	// TODO: should not really be necessary directly after init()
	me.repos.View.ScanRepositories()

	// find myself. the guireleaser directory is used as a working scratchpad
	// for running go commands that can mess up the go.* files
	for _, repo := range me.repos.View.AllRepos() {
		if repo.GoPath() == "go.wit.com/apps/guireleaser" {
			if me.release.guireleaser == nil {
				me.release.guireleaser = repo
			}
		}
	}

	if me.release.guireleaser == nil {
		log.Info("Can not release if guireleaser was not found")
		os.Exit(0)
	}
	me.Enable()

	// intermittently scans the status indefinitly
	me.repos.View.Watchdog(func() {
		log.Info("In main()")
		// processing is done. update the repo summary box
		// me.summary.Update()
	})
}

// start the initail scan and make sure each repo is set
// to the master branch
func setAllBranchesToMaster() bool {
	var worked bool = true
	for _, repo := range me.repos.View.AllRepos() {
		if repo.ReadOnly() {
			continue
		}
		if repo.IsDirty() {
			continue
		}
		if whitelist(repo.GoPath()) {
			continue
		}
		if repo.Status.CheckoutMaster() {
			log.Warn("git checkout master branch worked", repo.Name())
		} else {
			log.Warn("git checkout master branch failed", repo.Name())
			worked = false
		}
		// repo.NewScan()
	}
	return worked
}

func setTargetVersion() {
	// go through and set the target versions for each repo
	// todo: add sanity checking in repolist to verify these and/or autocompute them
	// for now, just don't be stupid when you set your ENV vars
	// widget I versioned early before I knew what the hell this would mean and can
	// not be down versioned because that's not how GO versioning works. Once you
	// set the version for a path, it's set in stone forever. (smart system!)
	// we could rename go.wit.com/widget to go.wit.com/newwidget and restart the versioning
	// system, but that's rediculous and this servers to always remind me to never make this mistake again
	var count int
	for i, repo := range me.repos.View.AllRepos() {
		if repo == nil {
			log.Info("initial scan repo = nil", i)
			continue
		}

		if repo.Status == nil {
			log.Info("repo.status == nil", repo.Name())
			continue
		}

		if repo.GoPath() == "go.wit.com/widget" {
			repo.Status.SetTargetVersion("v" + widgetVersion)
		} else {
			repo.Status.SetTargetVersion("v" + releaseVersion)
		}
		if strings.HasPrefix(repo.GoPath(), "go.wit.com/dev/") {
			lasttag := repo.Status.GetLastTagVersion()
			repo.Status.SetTargetVersion(lasttag)
		}
		// repo.NewScan()
		count += 1
	}
	log.Info("target versions set for", count, "repos")
}