summaryrefslogtreecommitdiff
path: root/common.go
blob: cbf798c0fb5c2d7c5126a707bbcd8c0f75dcd5f0 (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
package repostatus

import (
	"strings"

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

// reports externally if something has changed
// since the last time it was asked about it
func (rs *RepoStatus) Changed() (string, bool) {
	if !rs.Ready() {
		return "", false
	}

	return rs.getChanges(), rs.changed
}

// keeps a human readable list of things that have
// changed. todo: timestampe these?
func (rs *RepoStatus) getChanges() string {
	return rs.changes
}

func (rs *RepoStatus) NoteChange(s string) {
	rs.changed = true
	rs.changes += s + "\n"
}

// deprecate this. returns the gopath right now
func (rs *RepoStatus) String() string {
	// log.Warn("RepoStatus.String() is to be deprecated")
	return rs.path.String()
}

// returns the go path for the repo. "go.wit.com/apps/autotypist"
func (rs *RepoStatus) GoName() string {
	return rs.GoPath()
}

// not sure which name is easier to remember. probably this one
func (rs *RepoStatus) GoPath() string {
	return rs.goPath.String()
}

func (rs *RepoStatus) IsPrimitive() bool {
	if rs.primitive.String() == "true" {
		return true
	}
	return false
}

// returns the filesystem path to the repo
func (rs *RepoStatus) Path() string {
	return rs.realPath.String()
}

func (rs *RepoStatus) Show() {
	if !rs.Ready() {
		return
	}
	log.Log(CHANGE, "Show() window ready =", rs.ready)
	rs.window.Show()
}

func (rs *RepoStatus) Hide() {
	if !rs.Ready() {
		return
	}
	log.Log(CHANGE, "Hide() window ready =", rs.ready)
	rs.window.Hide()
}

func (rs *RepoStatus) Toggle() {
	if !rs.Ready() {
		return
	}
	log.Log(CHANGE, "Toggle() window ready =", rs.ready)
	if rs.window.Hidden() {
		rs.Show()
	} else {
		rs.Hide()
	}
}

func (rs *RepoStatus) Ready() bool {
	if rs == nil {
		return false
	}
	if rs.window == nil {
		return false
	}
	return rs.ready
}

func (rs *RepoStatus) IsGoLang() bool {
	if !rs.Ready() {
		return false
	}
	if rs.isGoLang.String() == "true" {
		return true
	}
	return false
}

func (rs *RepoStatus) RepoType() string {
	if !rs.IsGoLang() {
		return ""
	}
	err, output := rs.RunCmd([]string{"go", "list", "-f", "'{{if eq .Name \"main\"}}binary{{else}}library{{end}}'"})
	if err == nil {
		output = strings.Trim(output, "'")
		// log.Info("go package is:", output)
		return output
	}
	// log.Info("package is: unknown", err)
	return ""
}

func (rs *RepoStatus) BinaryName() string {
	// get the package name from the repo name
	path := rs.String()
	parts := strings.Split(path, "/")
	name := parts[len(parts)-1]
	return name
}

func (rs *RepoStatus) Build() bool {
	if !rs.IsGoLang() {
		return false
	}
	name := rs.BinaryName()
	// removes the binary if it already exists
	rs.RunCmd([]string{"rm", "-f", name})
	if rs.Exists(name) {
		log.Warn("file could not be removed filename =", name)
		return false
	}
	log.Info("need to build here", rs.String())
	// rs.RunCmd([]string{"go", "build", "-v", "-x"})
	rs.XtermBash([]string{"go", "build", "-v", "-x"})
	if rs.Exists(name) {
		log.Warn("build worked", name)
		return true
	}
	log.Warn("build failed", name)
	return false
}

func (rs *RepoStatus) GetTargetVersion() string {
	return rs.targetReleaseVersion.String()
}

func (rs *RepoStatus) GetCurrentVersion() string {
	return rs.currentVersion.String()
}

func (rs *RepoStatus) LastTag() string {
	return rs.lasttag.String()
}

func (rs *RepoStatus) IncrementVersion() bool {
	rs.incrementRevision()
	rs.EnableSelectTag()
	rs.setTag()
	newtag := "v" + rs.newversion.String()
	log.Log(REPOWARN, rs.GoPath(), "old version:", rs.LastTag(), "new version:", newtag)
	rs.targetReleaseVersion.SetText(newtag)
	return true
}

// TODO: run this through the sanity check!
func (rs *RepoStatus) SetTargetVersion(s string) {
	// todo: redo setTag to do increment logic
	// func (rs *RepoStatus) setTag() bool {
	rs.targetReleaseVersion.SetText(s)
}

// returns a widget of the last tag that acts as a mirror
func (rs *RepoStatus) MirrorLastTag() *gui.Node {
	return rs.lasttag.MirrorValue()
}

func (rs *RepoStatus) MirrorTargetVersion() *gui.Node {
	return rs.targetReleaseVersion.MirrorValue()
}

func (rs *RepoStatus) MirrorCurrentVersion() *gui.Node {
	return rs.currentVersion.MirrorValue()
}

func (rs *RepoStatus) MirrorCurrentName() *gui.Node {
	return rs.currentBranch.MirrorValue()
}

func (rs *RepoStatus) MirrorGitState() *gui.Node {
	return rs.gitState.MirrorValue()
}

func (rs *RepoStatus) MirrorMasterVersion() *gui.Node {
	return rs.mainBranchVersion.MirrorValue()
}

func (rs *RepoStatus) MirrorDevelVersion() *gui.Node {
	return rs.develBranchVersion.MirrorValue()
}

func (rs *RepoStatus) MirrorUserVersion() *gui.Node {
	return rs.userBranchVersion.MirrorValue()
}