Skip to content

Commit 311e43d

Browse files
author
Atterpac
committed
go1.24 + engine improvements
1 parent d6556d9 commit 311e43d

17 files changed

Lines changed: 323 additions & 88 deletions

File tree

engine/config.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ import (
1515
)
1616

1717
type Config struct {
18-
RootPath string `toml:"root_path" yaml:"root_path"`
18+
RootPath string `toml:"root_path" yaml:"root_path"`
1919
BackgroundStruct process.Execute `toml:"background" yaml:"background"`
20-
BackgroundCallback func() bool `toml:"-" yaml:"-"`
21-
Ignore Ignore `toml:"ignore" yaml:"ignore"`
22-
ExecStruct []process.Execute `toml:"executes" yaml:"executes"`
23-
ExecList []string `toml:"exec_list" yaml:"exec_list"`
24-
LogLevel string `toml:"log_level" yaml:"log_level"`
25-
Debounce int `toml:"debounce" yaml:"debounce"`
20+
BackgroundCallback func() bool `toml:"-" yaml:"-"`
21+
Ignore Ignore `toml:"ignore" yaml:"ignore"`
22+
ExecStruct []process.Execute `toml:"executes" yaml:"executes"`
23+
ExecList []string `toml:"exec_list" yaml:"exec_list"`
24+
LogLevel string `toml:"log_level" yaml:"log_level"`
25+
Debounce int `toml:"debounce" yaml:"debounce"`
2626
Callback func(*EventCallback) EventHandle
2727
Slog *slog.Logger
2828
ignoreMap ignoreMap
@@ -165,6 +165,6 @@ func changeWorkingDirectory(path string) {
165165

166166
func (e *Engine) generateProcess() {
167167
for _, ex := range e.Config.ExecStruct {
168-
e.ProcessManager.AddProcess(ex.Cmd, ex.Type, ex.ChangeDir)
168+
e.ProcessManager.AddProcess(ex.Cmd, string(ex.Type), ex.ChangeDir)
169169
}
170170
}

engine/ignore.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package engine
22

33
import (
4+
"log/slog"
45
"path/filepath"
56
"strings"
67
)
78

89
type Ignore struct {
9-
Dir []string `toml:"dir" yaml:"dir"`
10-
File []string `toml:"file" yaml:"file"`
10+
Dir []string `toml:"dir" yaml:"dir"`
11+
File []string `toml:"file" yaml:"file"`
1112
WatchedExten []string `toml:"watched_extension" yaml:"watched_extension"`
12-
IgnoreGit bool `toml:"git" yaml:"git"`
13+
IgnoreGit bool `toml:"git" yaml:"git"`
1314
}
1415

1516
type ignoreMap struct {
@@ -37,22 +38,32 @@ type ignoreMap struct {
3738
// }
3839

3940
func (i *Ignore) shouldIgnore(path string) bool {
40-
if isIgnoreDir(path, i.Dir) {
41-
return true
42-
}
43-
if patternMatch(path, i.Dir) {
44-
return true
45-
}
46-
if patternMatch(path, i.File) {
47-
return true
48-
}
4941
if i.isWatchedExtension(path) {
42+
slog.Debug("Checking Watched Extension", "path", path)
43+
if isIgnoreDir(path, i.Dir) ||
44+
patternMatch(path, i.Dir) ||
45+
patternMatch(path, i.File) {
46+
return true
47+
}
5048
return false
5149
}
5250
return true
5351
}
5452

5553
func (i *Ignore) isWatchedExtension(path string) bool {
54+
ext := filepath.Ext(path)
55+
if ext == "" {
56+
return false
57+
}
58+
59+
// First check for direct extension matches (e.g., ".go")
60+
for _, watchedExt := range i.WatchedExten {
61+
if watchedExt == ext || watchedExt == "*"+ext {
62+
return true
63+
}
64+
}
65+
66+
// Then try pattern matching for more complex patterns
5667
return patternMatch(path, i.WatchedExten)
5768
}
5869

@@ -82,6 +93,7 @@ func isIgnoreDir(path string, rules []string) bool {
8293
for _, dir := range dirs {
8394
for _, rule := range rules {
8495
if dir == rule {
96+
slog.Debug("Ignore Dir", "dir", dir)
8597
return true
8698
}
8799
}

engine/ignore_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,61 @@ func Test_patternCompare(t *testing.T) {
3232
}
3333
}
3434
}
35+
36+
func Test_isWatchedExtension(t *testing.T) {
37+
tests := []struct {
38+
name string
39+
path string
40+
watchedExten []string
41+
wantIsWatched bool
42+
}{
43+
{
44+
name: "go file with full path should match *.go",
45+
path: "/Users/atterpac/projects/atterpac/refresh/example/test/monitored/ignore.go",
46+
watchedExten: []string{"*.go"},
47+
wantIsWatched: true,
48+
},
49+
{
50+
name: "go file with just extension match",
51+
path: "/some/path/file.go",
52+
watchedExten: []string{".go"},
53+
wantIsWatched: true,
54+
},
55+
{
56+
name: "go file with *extension match",
57+
path: "/some/path/file.go",
58+
watchedExten: []string{"*.go"},
59+
wantIsWatched: true,
60+
},
61+
{
62+
name: "txt file should not match go patterns",
63+
path: "/some/path/file.txt",
64+
watchedExten: []string{"*.go", ".go"},
65+
wantIsWatched: false,
66+
},
67+
{
68+
name: "multiple extensions",
69+
path: "/some/path/file.js",
70+
watchedExten: []string{"*.go", "*.js", "*.html"},
71+
wantIsWatched: true,
72+
},
73+
{
74+
name: "no extension in path",
75+
path: "/some/path/noextension",
76+
watchedExten: []string{"*.go", "*.js"},
77+
wantIsWatched: false,
78+
},
79+
}
80+
81+
for _, tt := range tests {
82+
t.Run(tt.name, func(t *testing.T) {
83+
i := &Ignore{
84+
WatchedExten: tt.watchedExten,
85+
}
86+
got := i.isWatchedExtension(tt.path)
87+
if got != tt.wantIsWatched {
88+
t.Errorf("isWatchedExtension() = %v, want %v", got, tt.wantIsWatched)
89+
}
90+
})
91+
}
92+
}

engine/patternMatch.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package engine
22

33
import (
4+
"fmt"
45
"log/slog"
56
"path/filepath"
67
"unicode/utf8"
@@ -9,14 +10,11 @@ import (
910
func patternMatch(path string, PatternMap []string) bool {
1011
path = filepath.ToSlash(path)
1112
for _, pattern := range PatternMap {
13+
slog.Debug("Checking Pattern", "pattern", pattern)
1214
pattern = filepath.ToSlash(pattern)
13-
matched, err := filepath.Match(pattern, path)
14-
if err != nil {
15-
slog.Error("Error matching filepath", "err", err)
16-
return false
17-
}
15+
matched := patternCompare(pattern, path)
1816
if matched {
19-
// slog.Debug(fmt.Sprintf("Ignore Pattern Match: %s with %s", path, pattern))
17+
slog.Debug(fmt.Sprintf("Pattern Match: %s with %s", path, pattern))
2018
return true
2119
}
2220
}

engine/watch.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package engine
55

66
import (
77
"context"
8+
"log/slog"
9+
810
// "log/slog"
911
"os"
1012
"path/filepath"
@@ -37,7 +39,7 @@ func NewEventManager(engine *Engine, debounce int) *EventManager {
3739
func (em *EventManager) HandleEvent(ei notify.EventInfo) {
3840
eventInfo, ok := EventMap[ei.Event()]
3941
if !ok {
40-
// slog.Error("Unknown event", "event", ei.Event())
42+
slog.Error("Unknown event", "event", ei.Event())
4143
return
4244
}
4345

@@ -68,11 +70,11 @@ func (em *EventManager) HandleEvent(ei notify.EventInfo) {
6870
if em.engine.Config.Ignore.shouldIgnore(ei.Path()) {
6971
return
7072
}
71-
// slog.Debug("Event", "event", ei.Event(), "path", ei.Path(), "time", time.Now())
73+
slog.Debug("Event", "event", ei.Event(), "path", ei.Path(), "time", time.Now())
7274
currentTime := time.Now()
7375
if currentTime.Sub(em.lastEventTime) >= em.debounceThreshold {
74-
// slog.Debug("Setting debounce timer", "event", ei.Event(), "path", ei.Path(), "time", time.Now())
75-
// slog.Info("File modified...Refreshing", "file", getPath(ei.Path()))
76+
slog.Debug("Setting debounce timer", "event", ei.Event(), "path", ei.Path(), "time", time.Now())
77+
slog.Info("File modified...Refreshing", "file", getPath(ei.Path()))
7678

7779
// Find the specific process associated with the file change event
7880
for _, p := range em.engine.ProcessManager.Processes {
@@ -111,12 +113,17 @@ func (em *EventManager) HandleEvent(ei notify.EventInfo) {
111113
}
112114

113115
func (engine *Engine) watch(eventManager *EventManager) {
114-
// slog.Info("Watching", "path", engine.Config.RootPath)
115-
engine.Chan = make(chan notify.EventInfo, 1)
116+
engine.Chan = make(chan notify.EventInfo, 5)
116117
defer notify.Stop(engine.Chan)
117118

118-
if err := notify.Watch(engine.Config.RootPath+"/...", engine.Chan, notify.All); err != nil {
119-
// slog.Error("Watch Error", "err", err.Error())
119+
wd, err := os.Getwd()
120+
if err != nil {
121+
slog.Error("Getting working directory")
122+
return
123+
}
124+
125+
if err := notify.Watch(wd+"/...", engine.Chan, notify.All); err != nil {
126+
slog.Error("Watch Error", "err", err.Error())
120127
return
121128
}
122129

example/example.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
config:
22
label: My Project
3-
root_path: ./test
4-
log_level: info
3+
root_path: test
4+
log_level: debug
55
debounce: 1000
66
ignore:
77
dir:

example/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module example
22

3-
go 1.21.1
3+
go 1.24.0
44

55
require github.com/atterpac/refresh v0.1.0
66

example/main.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,26 @@ import (
44
"time"
55

66
refresh "github.com/atterpac/refresh/engine"
7+
"github.com/atterpac/refresh/process"
78
)
89

910
func main() {
10-
background := refresh.Execute{
11+
background := process.Execute{
1112
Cmd: "pwd",
1213
}
13-
tidy := refresh.Execute{
14-
Cmd: "go mod tidy",
15-
IsBlocking: true,
16-
IsPrimary: false,
14+
tidy := process.Execute{
15+
Cmd: "go mod tidy",
16+
Type: process.Background,
1717
}
18-
build := refresh.Execute{
19-
Cmd: "go build -o ./bin/myapp",
20-
IsBlocking: true,
21-
IsPrimary: false,
18+
build := process.Execute{
19+
Cmd: "go build -o ./bin/myapp",
20+
Type: process.Blocking,
2221
}
23-
kill := refresh.KILL_STALE
24-
run := refresh.Execute{
25-
Cmd: "./myapp",
26-
ChangeDir: "./binn",
27-
IsBlocking: false,
28-
IsPrimary: true,
22+
kill := process.KILL_STALE
23+
run := process.Execute{
24+
Cmd: "./myapp",
25+
ChangeDir: "./binn",
26+
Type: process.Primary,
2927
}
3028
ignore := refresh.Ignore{
3129
File: []string{"ignore.go"},
@@ -40,18 +38,21 @@ func main() {
4038
Ignore: ignore,
4139
Debounce: 1000,
4240
LogLevel: "debug",
43-
ExecStruct: []refresh.Execute{tidy, build, kill, run},
41+
ExecStruct: []process.Execute{tidy, build, kill, run},
4442
Slog: nil,
4543
}
4644

4745
// watch := refresh.NewEngineFromConfig(config)
48-
watch := refresh.NewEngineFromTOML("./example.toml")
46+
watch, err := refresh.NewEngineFromYAML("./example.yaml")
47+
if err != nil {
48+
panic(err)
49+
}
4950

5051
watch.AttachBackgroundCallback(func() bool {
5152
time.Sleep(5000 * time.Millisecond)
5253
return true
5354
})
54-
err := watch.Start()
55+
err = watch.Start()
5556
if err != nil {
5657
panic(err)
5758
}

example/test/bin/app

2.24 MB
Binary file not shown.

example/test/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77

88
func main() {
99
for i := 0; i < 30; i++ {
10-
fmt.Println("change me", i)
10+
time.Sleep(1 * time.Second)
11+
fmt.Println("changed me", i)
1112
time.Sleep(1 * time.Second)
1213
}
1314
}

0 commit comments

Comments
 (0)