Skip to content

Commit

Permalink
watch embedded dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
draganm committed May 5, 2024
1 parent de31a7c commit e12def9
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 31 deletions.
60 changes: 58 additions & 2 deletions depdirs/dep_dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)...)

}
Expand All @@ -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
}
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
60 changes: 37 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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()

Expand Down

0 comments on commit e12def9

Please sign in to comment.