From 56f42e7a9352b8aeb4d7ffe17a82228f24e516aa Mon Sep 17 00:00:00 2001 From: Krystian Panek Date: Thu, 26 Jan 2023 15:01:15 +0100 Subject: [PATCH] App build fix --- cmd/aem/app.go | 12 ++------ cmd/aem/file.go | 2 +- cmd/aem/package.go | 4 +-- pkg/app.go | 58 ++++++++++++++++++++--------------- pkg/common/pathx/pathx.go | 32 ++++++++++++++++--- pkg/local_instance.go | 2 +- pkg/local_instance_manager.go | 4 +-- 7 files changed, 70 insertions(+), 44 deletions(-) diff --git a/cmd/aem/app.go b/cmd/aem/app.go index 0f7b4bb4..da4967eb 100644 --- a/cmd/aem/app.go +++ b/cmd/aem/app.go @@ -3,7 +3,6 @@ package main import ( "github.com/samber/lo" "github.com/spf13/cobra" - "github.com/wttech/aemc/pkg/common/pathx" ) func (c *CLI) appCmd() *cobra.Command { @@ -22,26 +21,21 @@ func (c *CLI) appBuildCmd() *cobra.Command { Short: "Build application only when needed", Run: func(cmd *cobra.Command, args []string) { command, _ := cmd.Flags().GetString("command") - file, _ := cmd.Flags().GetString("file") - fileGlobbed, err := pathx.GlobOne(file) - if err != nil { - c.Error(err) - return - } + filePattern, _ := cmd.Flags().GetString("file") appManager := c.aem.AppManager() sourcePaths, _ := cmd.Flags().GetStringSlice("sources") sourcesIgnoredExtra, _ := cmd.Flags().GetStringSlice("sources-ignored") appManager.SourcesIgnored = lo.Uniq(append(appManager.SourcesIgnored, sourcesIgnoredExtra...)) - changed, err := appManager.BuildWithChanged(command, fileGlobbed, sourcePaths) + file, changed, err := appManager.BuildWithChanged(command, filePattern, sourcePaths) if err != nil { c.Error(err) return } c.SetOutput("command", command) - c.SetOutput("file", fileGlobbed) + c.SetOutput("file", file) c.SetOutput("sources", sourcePaths) if changed { diff --git a/cmd/aem/file.go b/cmd/aem/file.go index 5f0f5697..9c96b1a8 100644 --- a/cmd/aem/file.go +++ b/cmd/aem/file.go @@ -28,7 +28,7 @@ func (c *CLI) fileFindCmd() *cobra.Command { Short: "Find file by pattern", Run: func(cmd *cobra.Command, args []string) { file, _ := cmd.Flags().GetString("file") - fileGlobbed, err := pathx.GlobOne(file) + fileGlobbed, err := pathx.GlobSome(file) if err != nil { c.Error(err) return diff --git a/cmd/aem/package.go b/cmd/aem/package.go index 580d4088..bb38287a 100644 --- a/cmd/aem/package.go +++ b/cmd/aem/package.go @@ -410,7 +410,7 @@ func pkgByFlags(cmd *cobra.Command, instance pkg.Instance) (*pkg.Package, error) } file, _ := cmd.Flags().GetString("file") if len(file) > 0 { - fileGlobbed, err := pathx.GlobOne(file) + fileGlobbed, err := pathx.GlobSome(file) if err != nil { return nil, err } @@ -447,7 +447,7 @@ func (c *CLI) pkgPathByFlags(cmd *cobra.Command) (string, error) { } file, _ := cmd.Flags().GetString("file") if len(file) > 0 { - fileGlobbed, err := pathx.GlobOne(file) + fileGlobbed, err := pathx.GlobSome(file) if err != nil { return "", err } diff --git a/pkg/app.go b/pkg/app.go index 1a63fecc..f694dba0 100644 --- a/pkg/app.go +++ b/pkg/app.go @@ -14,6 +14,10 @@ type AppManager struct { SourcesIgnored []string } +const ( + AppChecksumFileSuffix = ".build.md5" +) + func NewAppManager(aem *Aem) *AppManager { result := new(AppManager) result.aem = aem @@ -61,39 +65,45 @@ func (am *AppManager) Build(command string) error { return nil } -func (am *AppManager) BuildWithChanged(command string, file string, sourcePaths []string) (bool, error) { - fileExists, err := pathx.ExistsStrict(file) - if err != nil { - return false, err - } - checksumFile := file + ".build.md5" - checksumExists, err := pathx.ExistsStrict(checksumFile) - if err != nil { - return false, err - } - checksumPrevious := "" - if checksumExists { - checksumPrevious, err = filex.ReadString(checksumFile) +func (am *AppManager) BuildWithChanged(command string, filePattern string, sourcePaths []string) (string, bool, error) { + file, err := pathx.GlobOne(filePattern) + if file != "" { + checksumFile := file + AppChecksumFileSuffix + checksumExists, err := pathx.ExistsStrict(checksumFile) if err != nil { - return false, err + return file, false, err + } + checksumPrevious := "" + if checksumExists { + checksumPrevious, err = filex.ReadString(checksumFile) + if err != nil { + return file, false, err + } + } + checksumCurrent, err := filex.ChecksumPaths(sourcePaths, am.SourcesIgnored) + if err != nil { + return file, false, err + } + if checksumPrevious == checksumCurrent { + return file, false, nil } } - checksumCurrent, err := filex.ChecksumPaths(sourcePaths, am.SourcesIgnored) + err = am.Build(command) if err != nil { - return false, err + return "", false, err } - upToDate := fileExists && checksumPrevious == checksumCurrent - if upToDate { - return false, nil + file, err = pathx.GlobSome(filePattern) + if err != nil { + return "", false, err } - err = am.Build(command) + checksumCurrent, err := filex.ChecksumPaths(sourcePaths, am.SourcesIgnored) if err != nil { - return false, err + return file, false, err } - if err = filex.WriteString(checksumFile, checksumCurrent); err != nil { - return false, err + if err = filex.WriteString(file+AppChecksumFileSuffix, checksumCurrent); err != nil { + return file, false, err } - return true, nil + return file, true, nil } func (am *AppManager) Configure(config *cfg.Config) { diff --git a/pkg/common/pathx/pathx.go b/pkg/common/pathx/pathx.go index 30960d24..05bcea69 100644 --- a/pkg/common/pathx/pathx.go +++ b/pkg/common/pathx/pathx.go @@ -98,20 +98,42 @@ func NameWithoutExt(path string) string { return name[:len(name)-len(ext)] } +func GlobSome(pathPattern string) (string, error) { + path, err := GlobOne(pathPattern) + if err != nil { + return "", err + } + if path == "" { + return "", fmt.Errorf("cannot find any file matching pattern '%s'", pathPattern) + } + return path, nil +} + func GlobOne(pathPattern string) (string, error) { - dir := stringsx.BeforeLast(pathPattern, "/") - pattern := stringsx.AfterLast(pathPattern, "/") - paths, err := GlobDir(dir, pattern) + paths, err := GlobAll(pathPattern) if err != nil { return "", err } - sort.Strings(paths) if len(paths) == 0 { - return "", fmt.Errorf("cannot find any file matching pattern '%s'", pathPattern) + return "", nil } + sort.Strings(paths) return paths[len(paths)-1], nil } +func GlobAll(pathPattern string) ([]string, error) { + dir := stringsx.BeforeLast(pathPattern, "/") + if !Exists(dir) { + return nil, nil + } + pattern := stringsx.AfterLast(pathPattern, "/") + paths, err := GlobDir(dir, pattern) + if err != nil { + return nil, err + } + return paths, nil +} + // GlobDir is a modified version of 'go/1.19.2/libexec/src/path/filepath/match.go' func GlobDir(dir string, pattern string) ([]string, error) { m := []string{} diff --git a/pkg/local_instance.go b/pkg/local_instance.go index 67894174..45a2a10d 100644 --- a/pkg/local_instance.go +++ b/pkg/local_instance.go @@ -630,7 +630,7 @@ func (li LocalInstance) ProposeBackupFileToUse() (string, error) { } else { pathPattern = fmt.Sprintf("%s/%s-*.%s", li.Opts().BackupDir, li.Name(), LocalInstanceBackupExtension) } - file, err := pathx.GlobOne(pathPattern) + file, err := pathx.GlobSome(pathPattern) if err != nil { return "", fmt.Errorf("no backup file found to use for instance '%s': %w", li.instance.ID(), err) } diff --git a/pkg/local_instance_manager.go b/pkg/local_instance_manager.go index 302610dd..d90d083c 100644 --- a/pkg/local_instance_manager.go +++ b/pkg/local_instance_manager.go @@ -122,11 +122,11 @@ func (o *Quickstart) Validate() error { } func (o *Quickstart) FindDistFile() (string, error) { - return pathx.GlobOne(o.DistFile) + return pathx.GlobSome(o.DistFile) } func (o *Quickstart) FindLicenseFile() (string, error) { - return pathx.GlobOne(o.LicenseFile) + return pathx.GlobSome(o.LicenseFile) } // LocalValidate checks prerequisites needed to manage local instances