go-deb/stateWindow.go

167 lines
3.8 KiB
Go

// 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 {
if c.readControlFile() == nil {
log.Warn("scan worked")
} else {
log.Warn("scan failed")
return false
}
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.Mkdir("files/usr/lib/" + filename) {
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
}
if !shell.Run([]string{"cp", "README.md", "files/usr/lib/" + filename}) {
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
}