Skip to content

Commit

Permalink
Merge pull request #45 from wttech/app-build-fix
Browse files Browse the repository at this point in the history
App build fix
  • Loading branch information
krystian-panek-vmltech authored Jan 26, 2023
2 parents d0ba7d9 + 56f42e7 commit c814d6d
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 44 deletions.
12 changes: 3 additions & 9 deletions cmd/aem/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/aem/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/aem/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
58 changes: 34 additions & 24 deletions pkg/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type AppManager struct {
SourcesIgnored []string
}

const (
AppChecksumFileSuffix = ".build.md5"
)

func NewAppManager(aem *Aem) *AppManager {
result := new(AppManager)
result.aem = aem
Expand Down Expand Up @@ -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) {
Expand Down
32 changes: 27 additions & 5 deletions pkg/common/pathx/pathx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
2 changes: 1 addition & 1 deletion pkg/local_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/local_instance_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c814d6d

Please sign in to comment.