summaryrefslogtreecommitdiff
path: root/stateWindow.go
blob: d42de224dbe1f19e8d3c558b7806ec44f752a279 (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
// This window, when it's hidden, still exists to the application
// so it can be treated as if it really exists
package main

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

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

// This initializes the first window, a group and a button
func makebasicWindow() *gadgets.BasicWindow {
	log.Warn("init basicWindow state")
	basicWindow = gadgets.NewBasicWindow(myGui, "Create .deb files for GO applications")
	basicWindow.Make()
	basicWindow.Custom = func() {
		log.Info("got to close")
		os.Exit(0)
	}

	box1 := basicWindow.Box()
	cBox = newControl(box1)

	vbox := box1.Box().Horizontal()
	group1 := vbox.NewGroup("controls").Horizontal() // Vertical()

	group1.NewButton("go build", func() {
		shell.Run([]string{"go", "build", "-v", "-x"})
	})

	group1.NewButton("read control file", func() {
		cBox.readControlFile()
	})

	group1.NewButton("Make .deb", func() {
		basicWindow.Disable()
		if cBox.buildPackage() {
			log.Info("build worked")
		} else {
			log.Warn("build failed")
		}
		basicWindow.Enable()
	})

	group1.NewButton("open repo", func() {
		cBox.status.Update()
		cBox.status.Toggle()
	})

	return basicWindow
}

func (c *controlBox) buildPackage() bool {
	// TODO: if dirty, set GO111MODULE
	// also, if last tag != version
	if c.status.CheckDirty() {
		os.Setenv("GO111MODULE", "off")
	}
	if shell.Run([]string{"go", "build", "-v", "-x"}) {
		log.Warn("build worked")
	} else {
		log.Warn("build failed")
		return false
	}

	filename := c.Package.String()
	if filename == "" {
		log.Warn("build failed")
		return false
	}
	if !shell.Exists(filename) {
		log.Warn("build failed")
		return false
	}

	arch := c.Architecture.String()
	version := c.Version.String()
	if version == "" {
		version = "0.0.0"
	}

	debname := filename + "_" + version + "_" + arch + ".deb"

	if !shell.Mkdir("files/usr/bin") {
		log.Warn("mkdir failed")
		return false
	}
	if shell.Exists("files/DEBIAN") {
		if !shell.Run([]string{"rm", "-rf", "files/DEBIAN"}) {
			log.Warn("rm failed")
			return false
		}
	}
	if shell.Exists("files/DEBIAN") {
		log.Warn("rm failed")
		return false
	}
	if !shell.Mkdir("files/DEBIAN") {
		log.Warn("mkdir failed")
		return false
	}
	if !shell.Run([]string{"cp", filename, "files/usr/bin"}) {
		log.Warn("cp failed")
		return false
	}
	if !shell.Run([]string{"strip", "files/usr/bin/" + filename}) {
		log.Warn("strip failed")
		return false
	}

	// 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 !shell.Mkdir(path) {
			log.Warn("mkdir failed")
			return false
		}
		if !shell.Run([]string{"cp", readme, path}) {
			log.Warn("cp failed")
			return false
		}
	}

	if !c.writeFiles() {
		log.Warn("write control file failed")
		return false
	}

	homeDir, err := os.UserHomeDir()
	if err != nil {
		log.Warn("os.UserHomeDir() failed")
		return false
	}

	fulldebname := filepath.Join(homeDir, "incoming", debname)
	shell.Run([]string{"dpkg-deb", "--build", "files", fulldebname})
	if shell.Exists(fulldebname) {
		log.Warn("build worked")
	} else {
		log.Warn("build failed")
		return false
	}
	shell.Run([]string{"dpkg-deb", "-I", fulldebname})
	shell.Run([]string{"dpkg-deb", "-c", fulldebname})
	return true
}

func (c *controlBox) writeFiles() bool {
	cf, err := os.OpenFile("files/DEBIAN/control", os.O_RDWR|os.O_CREATE, 0644)
	if err != nil {
		log.Info("open control file failed", err)
		return false
	}
	fmt.Fprintln(cf, "Package:", c.Package.String())
	fmt.Fprintln(cf, "Source:", c.Source.String())
	fmt.Fprintln(cf, "Version:", c.Version.String())
	fmt.Fprintln(cf, "Architecture:", c.Architecture.String())
	fmt.Fprintln(cf, "Depends:", c.Depends.String())
	fmt.Fprintln(cf, "Build-Depends:", c.BuildDepends.String())
	fmt.Fprintln(cf, "Maintainer:", c.Maintainer.String())
	desc := c.Description.String()
	parts := strings.Split(desc, "\n")
	fmt.Fprintln(cf, "Description:", strings.Join(parts, "\n "))

	return true
}

// try to guess or figure out the config file values
// if there is not a control file
func (c *controlBox) computeControlValues() bool {
	if c.Package.String() == "" {
		// get the package name from the repo name
		path := c.pathL.String()
		parts := strings.Split(path, "/")
		name := parts[len(parts) - 1]
		c.Package.SetText(name)
	}
	if c.Source.String() == "" {
		c.Source.SetText(c.Package.String())
	}
	if c.BuildDepends.String() == "" {
		c.BuildDepends.SetText("golang")
	}
	if c.Recommends.String() == "" {
		c.Recommends.SetText("go-gui-toolkits")
	}
	// TODO: get this from the git log
	if c.Maintainer.String() == "" {
		c.Maintainer.SetText("Jeff Carr <[email protected]>")
	}
	// TODO: get this from gitea (or gitlab or github, etc)
	// or from the README.md ?
	if c.Description.String() == "" {
		c.Description.SetText("no control file")
	}
	return true
}