From 15454aeb4e300b6809dde427ebfd94f3498a59b2 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 23 Feb 2024 13:15:51 -0600 Subject: [PATCH] move build code to seperate file --- buildPackage.go | 225 ++++++++++++++++++++++++++++++++++++++++++++++++ stateWindow.go | 206 -------------------------------------------- 2 files changed, 225 insertions(+), 206 deletions(-) create mode 100644 buildPackage.go diff --git a/buildPackage.go b/buildPackage.go new file mode 100644 index 0000000..814aa30 --- /dev/null +++ b/buildPackage.go @@ -0,0 +1,225 @@ +// 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" + "strconv" + "strings" + "time" + + "go.wit.com/lib/gui/shell" + "go.wit.com/log" +) + +func (c *controlBox) buildPackage() bool { + // TODO: if dirty, set GO111MODULE + // also, if last tag != version + /* + go install -ldflags " \ + -X main.GITCOMMIT=${GITCOMMIT} \ + -X main.GOVERSION='${GOVERSION}' \ + -X main.BUILDTIME='${BUILDTIME}' \ + -X main.VERSION=${VERSION}" + */ + // ldflags := "main.GOTAG=" + repo.LastTag() + + if args.Release { + os.Unsetenv("GO111MODULE") + path := c.pathL.String() + "@latest" + cmd := []string{"go", "install", "-v", "-x", path} + if shell.Run(cmd) { + log.Warn("build worked") + } else { + log.Warn("build failed") + return false + } + } else { + + 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.Exists("files") { + if !shell.Run([]string{"rm", "-rf", "files"}) { + log.Warn("rm failed") + return false + } + } + if shell.Exists("files") { + log.Warn("rm failed") + return false + } + if !shell.Mkdir("files/DEBIAN") { + log.Warn("mkdir failed") + return false + } + if !shell.Mkdir("files/usr/bin") { + 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 + } + + // experiment for the toolkit package + if c.status.Exists("build") { + if args.Release { + os.Unsetenv("GO111MODULE") + } else { + os.Setenv("GO111MODULE", "off") + } + shell.Run([]string{"./build"}) + } + + 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()) + stamp := time.Now().UTC().Format("2006/01/02 15:04:05 UTC") + // update to now now despite what the GUI is showing + fmt.Fprintln(cf, "Package-Build-Date:", stamp) + fmt.Fprintln(cf, "Git-Tag-Date:", c.tagDate.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 ") + } + // TODO: get this from gitea (or gitlab or github, etc) + // or from the README.md ? + if c.Description.String() == "" { + path := c.pathL.String() + c.Description.SetText("GO binary of " + path) + } + return true +} + +// stamp := time.Now().UTC().Format("2006/01/02 15:04:05 UTC") + +func (c *controlBox) getDateStamp(tag string) string { + _, out := c.status.RunCmd([]string{"git", "log", "-1", "--format=%at", tag}) + out = strings.TrimSpace(out) + + // Convert the string to an integer + gitTagTimestampInt, err := strconv.ParseInt(out, 10, 64) + if err != nil { + fmt.Println("Error converting timestamp:", err) + return "git tag " + tag + " unknown" + } + + // Parse the Unix timestamp into a time.Time object + gitTagDate := time.Unix(gitTagTimestampInt, 0) + return gitTagDate.UTC().Format("2006/01/02 15:04:05 UTC") +} diff --git a/stateWindow.go b/stateWindow.go index 0e0c61b..0831d43 100644 --- a/stateWindow.go +++ b/stateWindow.go @@ -3,12 +3,7 @@ package main import ( - "fmt" "os" - "path/filepath" - "strconv" - "strings" - "time" "go.wit.com/lib/gadgets" "go.wit.com/lib/gui/shell" @@ -56,204 +51,3 @@ func makebasicWindow() *gadgets.BasicWindow { return basicWindow } - -func (c *controlBox) buildPackage() bool { - // TODO: if dirty, set GO111MODULE - // also, if last tag != version - if args.Release { - os.Unsetenv("GO111MODULE") - path := c.pathL.String() + "@latest" - cmd := []string{"go", "install", "-v", "-x", path} - if shell.Run(cmd) { - log.Warn("build worked") - } else { - log.Warn("build failed") - return false - } - } else { - - 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.Exists("files") { - if !shell.Run([]string{"rm", "-rf", "files"}) { - log.Warn("rm failed") - return false - } - } - if shell.Exists("files") { - log.Warn("rm failed") - return false - } - if !shell.Mkdir("files/DEBIAN") { - log.Warn("mkdir failed") - return false - } - if !shell.Mkdir("files/usr/bin") { - 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 - } - - // experiment for the toolkit package - if c.status.Exists("build") { - if args.Release { - os.Unsetenv("GO111MODULE") - } else { - os.Setenv("GO111MODULE", "off") - } - shell.Run([]string{"./build"}) - } - - 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()) - stamp := time.Now().UTC().Format("2006/01/02 15:04:05 UTC") - // update to now now despite what the GUI is showing - fmt.Fprintln(cf, "Package-Build-Date:", stamp) - fmt.Fprintln(cf, "Git-Tag-Date:", c.tagDate.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 ") - } - // TODO: get this from gitea (or gitlab or github, etc) - // or from the README.md ? - if c.Description.String() == "" { - path := c.pathL.String() - c.Description.SetText("GO binary of " + path) - } - return true -} - -// stamp := time.Now().UTC().Format("2006/01/02 15:04:05 UTC") - -func (c *controlBox) getDateStamp(tag string) string { - _, out := c.status.RunCmd([]string{"git", "log", "-1", "--format=%at", tag}) - out = strings.TrimSpace(out) - - // Convert the string to an integer - gitTagTimestampInt, err := strconv.ParseInt(out, 10, 64) - if err != nil { - fmt.Println("Error converting timestamp:", err) - return "git tag " + tag + " unknown" - } - - // Parse the Unix timestamp into a time.Time object - gitTagDate := time.Unix(gitTagTimestampInt, 0) - return gitTagDate.UTC().Format("2006/01/02 15:04:05 UTC") -}