From e12def998a4fd3efecb1ca4e74b67e37aef2e874 Mon Sep 17 00:00:00 2001 From: Dragan Milic Date: Sun, 5 May 2024 21:45:23 +0200 Subject: [PATCH] watch embedded dirs --- depdirs/dep_dirs.go | 60 +++++++++++++++++++++++++++++++++++++++++++-- go.mod | 2 -- go.sum | 4 --- main.go | 60 ++++++++++++++++++++++++++++----------------- 4 files changed, 95 insertions(+), 31 deletions(-) diff --git a/depdirs/dep_dirs.go b/depdirs/dep_dirs.go index 792a5df..bfe20c9 100644 --- a/depdirs/dep_dirs.go +++ b/depdirs/dep_dirs.go @@ -3,9 +3,11 @@ package depdirs import ( "errors" "fmt" + "io/fs" "path/filepath" "slices" "sort" + "strings" "github.com/samber/lo" "golang.org/x/tools/go/packages" @@ -55,14 +57,25 @@ func DependencyDirs( packageFiles := []string{} packageFiles = append(packageFiles, pkg.GoFiles...) - packageFiles = append(packageFiles, pkg.EmbedFiles...) + packageFiles = append(packageFiles, pkg.OtherFiles...) packageFiles = append(packageFiles, pkg.IgnoredFiles...) dirs := lo.Map(packageFiles, func(n string, _ int) string { - return filepath.Dir(n) + d := filepath.Dir(n) + if !strings.HasSuffix(d, string(filepath.Separator)) { + d = d + string(filepath.Separator) + } + return d }) + recurseEmbeddedDirs, err := recurseEmbeddedDirs(pkg.EmbedFiles) + if err != nil { + return nil, fmt.Errorf("could not recurse embedded dirs: %w", err) + } + + dirs = append(dirs, recurseEmbeddedDirs...) + packageDirs = append(packageDirs, lo.Uniq(dirs)...) } @@ -86,3 +99,46 @@ func visitDeps(pkg *packages.Package, visit map[string]*packages.Package) { } } + +func recurseEmbeddedDirs(embeddedFiles []string) ([]string, error) { + allDirs := []string{} + for _, embeddedFile := range embeddedFiles { + embeddedDir := filepath.Dir(embeddedFile) + allDirs = append(allDirs, embeddedDir) + } + + allDirs = lo.Uniq(allDirs) + + res := []string{} + + for _, embeddedDir := range allDirs { + embeddedSubDirs, err := recurseEmbeddedDir(embeddedDir) + if err != nil { + return nil, fmt.Errorf("could not recurse embedded dir: %w", err) + } + res = append(res, embeddedSubDirs...) + } + + res = lo.Map(res, func(n string, _ int) string { + if !strings.HasSuffix(n, string(filepath.Separator)) { + n = n + string(filepath.Separator) + } + return n + }) + + return lo.Uniq(res), nil +} + +func recurseEmbeddedDir(embeddedDir string) ([]string, error) { + allDirs := []string{} + filepath.WalkDir(embeddedDir, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return fmt.Errorf("could not walk dir: %w", err) + } + if d.IsDir() { + allDirs = append(allDirs, path) + } + return nil + }) + return allDirs, nil +} diff --git a/go.mod b/go.mod index 7dd0b7e..243eb7f 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,6 @@ go 1.21.5 require ( github.com/draganm/gosha v0.0.1 github.com/fsnotify/fsnotify v1.7.0 - github.com/go-logr/logr v1.4.1 - github.com/looplab/fsm v1.0.1 github.com/samber/lo v1.38.1 github.com/urfave/cli/v2 v2.27.1 golang.org/x/sync v0.4.0 diff --git a/go.sum b/go.sum index d78f63f..a78f9ff 100644 --- a/go.sum +++ b/go.sum @@ -4,10 +4,6 @@ github.com/draganm/gosha v0.0.1 h1:lNJI5RyJKs5q7L9mOTorA9G920WVBWR+wA3aacu8YrU= github.com/draganm/gosha v0.0.1/go.mod h1:2xVGz+9XMTKpGs5xBVmtSNh13s5NGuL7CcNkKjIt2YQ= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= -github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= -github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= -github.com/looplab/fsm v1.0.1 h1:OEW0ORrIx095N/6lgoGkFkotqH6s7vaFPsgjLAaF5QU= -github.com/looplab/fsm v1.0.1/go.mod h1:PmD3fFvQEIsjMEfvZdrCDZ6y8VwKTwWNjlpEr6IKPO4= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= diff --git a/main.go b/main.go index 753b27d..24c5354 100644 --- a/main.go +++ b/main.go @@ -62,14 +62,14 @@ func main() { watchedDirs := []string{} - w, err := fsnotify.NewBufferedWatcher(50) + w, err := fsnotify.NewWatcher() if err != nil { return fmt.Errorf("failed to create watcher: %w", err) } defer w.Close() - for ctx.Err() == nil { + updateWatches := func() error { depDirs, err := depdirs.DependencyDirs(moduleDir) if err != nil { return fmt.Errorf("failed to get dependency directories: %w", err) @@ -79,6 +79,16 @@ func main() { toRemove := lo.Without(watchedDirs, depDirs...) for _, d := range toRemove { + + _, err = os.Stat(d) + + switch { + case os.IsNotExist(err): + continue + case err != nil: + return fmt.Errorf("failed to stat %s: %w", d, err) + } + err = w.Remove(d) if err != nil { return fmt.Errorf("failed to remove %s from watcher: %w", d, err) @@ -96,35 +106,39 @@ func main() { fmt.Println("updated watched dirs:", "watching", len(watchedDirs), "added", len(toAdd), "removed", len(toRemove)) - for _, dd := range depDirs { - err = w.Add(dd) - if err != nil { - return fmt.Errorf("failed to add %s to watcher: %w", dd, err) - } - } + return nil - for { - _, err = readLast(ctx, w.Events) - if err != nil { - return err - } + } - sha, err := gosha.CalculatePackageSHA(moduleDir, false, false) - if err != nil { - return fmt.Errorf("failed to calculate package sha: %w", err) - } + err = updateWatches() + if err != nil { + return err + } - if bytes.Equal(sha, lastSha) { - continue - } + for ctx.Err() == nil { + + _, err = readLast(ctx, w.Events) + if err != nil { + return err + } - lastSha = sha - shaChan <- sha + sha, err := gosha.CalculatePackageSHA(moduleDir, false, false) + if err != nil { + return fmt.Errorf("failed to calculate package sha: %w", err) + } - break + err = updateWatches() + if err != nil { + return err + } + if bytes.Equal(sha, lastSha) { + continue } + lastSha = sha + shaChan <- sha + } return ctx.Err()