Skip to content

Commit 117853c

Browse files
committed
feat: watch mode now runs, all the commands
1 parent eeedbf5 commit 117853c

File tree

7 files changed

+143
-115
lines changed

7 files changed

+143
-115
lines changed

Runfile.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ tasks:
66
go build -o bin/run -ldflags="-s -w" -tags urfave_cli_no_docs ./cmd/run
77
echo "DONE"
88
9+
build:dev:
10+
cmd:
11+
- |+
12+
echo "building ..."
13+
go build -o bin/run-dev -ldflags="-s -w" -tags urfave_cli_no_docs ./cmd/run
14+
echo "DONE"
15+
916
example:
1017
dir: ./examples
1118
cmd:

cmd/run/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func main() {
195195
}
196196

197197
logger := logging.New(logging.Options{
198-
ShowCaller: true,
198+
ShowCaller: false,
199199
SlogKeyAsPrefix: "task",
200200
ShowDebugLogs: debug,
201201
SetAsDefaultLogger: true,

go.mod

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/charmbracelet/lipgloss v1.0.0
88
github.com/joho/godotenv v1.5.1
99
github.com/muesli/termenv v0.15.2
10-
github.com/nxtcoder17/fwatcher v1.0.4-0.20241218102704-76d04c526fb0
10+
github.com/nxtcoder17/fwatcher v1.0.5-0.20250112020608-9c6a66687046
1111
github.com/phuslu/log v1.0.112
1212
github.com/urfave/cli/v3 v3.0.0-beta1
1313
golang.org/x/sync v0.10.0
@@ -16,13 +16,20 @@ require (
1616

1717
require (
1818
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
19+
github.com/charmbracelet/log v0.4.0 // indirect
1920
github.com/charmbracelet/x/ansi v0.6.0 // indirect
21+
github.com/cpuguy83/go-md2man/v2 v2.0.6 // indirect
2022
github.com/dlclark/regexp2 v1.11.4 // indirect
2123
github.com/fsnotify/fsnotify v1.8.0 // indirect
24+
github.com/go-logfmt/logfmt v0.6.0 // indirect
2225
github.com/google/go-cmp v0.6.0 // indirect
2326
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
2427
github.com/mattn/go-isatty v0.0.20 // indirect
2528
github.com/mattn/go-runewidth v0.0.16 // indirect
2629
github.com/rivo/uniseg v0.4.7 // indirect
30+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
31+
github.com/urfave/cli/v2 v2.27.5 // indirect
32+
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
33+
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67 // indirect
2734
golang.org/x/sys v0.28.0 // indirect
2835
)

go.sum

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@ github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiE
88
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
99
github.com/charmbracelet/lipgloss v1.0.0 h1:O7VkGDvqEdGi93X+DeqsQ7PKHDgtQfF8j8/O2qFMQNg=
1010
github.com/charmbracelet/lipgloss v1.0.0/go.mod h1:U5fy9Z+C38obMs+T+tJqst9VGzlOYGj4ri9reL3qUlo=
11+
github.com/charmbracelet/log v0.4.0 h1:G9bQAcx8rWA2T3pWvx7YtPTPwgqpk7D68BX21IRW8ZM=
12+
github.com/charmbracelet/log v0.4.0/go.mod h1:63bXt/djrizTec0l11H20t8FDSvA4CRZJ1KH22MdptM=
1113
github.com/charmbracelet/x/ansi v0.6.0 h1:qOznutrb93gx9oMiGf7caF7bqqubh6YIM0SWKyA08pA=
1214
github.com/charmbracelet/x/ansi v0.6.0/go.mod h1:KBUFw1la39nl0dLl10l5ORDAqGXaeurTQmwyyVKse/Q=
15+
github.com/cpuguy83/go-md2man/v2 v2.0.6 h1:XJtiaUW6dEEqVuZiMTn1ldk455QWwEIsMIJlo5vtkx0=
16+
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
1317
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1418
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1519
github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yAo=
1620
github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
1721
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
1822
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
23+
github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
24+
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
1925
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
2026
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
2127
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
@@ -33,17 +39,33 @@ github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo
3339
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
3440
github.com/nxtcoder17/fwatcher v1.0.4-0.20241218102704-76d04c526fb0 h1:k09TYswdqCsFz0fBnO3AjGT5MHfvjPRyBtHbBWuiJLM=
3541
github.com/nxtcoder17/fwatcher v1.0.4-0.20241218102704-76d04c526fb0/go.mod h1:MNmSwXYOrqp7U1pUxh0GWB5skpjFTWTQXhAA0+sPJcU=
42+
github.com/nxtcoder17/fwatcher v1.0.4-0.20241231074910-70c2485e84d3 h1:QDnTXuLuOEON1lGOYwrakigAAMU37EL4KMID5Uue5TU=
43+
github.com/nxtcoder17/fwatcher v1.0.4-0.20241231074910-70c2485e84d3/go.mod h1:MNmSwXYOrqp7U1pUxh0GWB5skpjFTWTQXhAA0+sPJcU=
44+
github.com/nxtcoder17/fwatcher v1.0.4-0.20250111091953-092ea9c4aee0 h1:hCGAP5LpxYTAb7H9r3bPCxDzwbaaFc5uZdu3wtpeZ28=
45+
github.com/nxtcoder17/fwatcher v1.0.4-0.20250111091953-092ea9c4aee0/go.mod h1:50dOLE7lHu6bwGtBM7lZ5m6b8wKKMUZDPgBKM2pgdEA=
46+
github.com/nxtcoder17/fwatcher v1.0.5-0.20250111155627-8bc1dfea0064 h1:HhEf79xzmViGl/SAvrrD/e5bxSKPZ0+B5zz0mlYKe6o=
47+
github.com/nxtcoder17/fwatcher v1.0.5-0.20250111155627-8bc1dfea0064/go.mod h1:50dOLE7lHu6bwGtBM7lZ5m6b8wKKMUZDPgBKM2pgdEA=
48+
github.com/nxtcoder17/fwatcher v1.0.5-0.20250112020608-9c6a66687046 h1:QL7gonI9kNiYiDbgDraigH9PGkztgahL24+IUp5HWWA=
49+
github.com/nxtcoder17/fwatcher v1.0.5-0.20250112020608-9c6a66687046/go.mod h1:50dOLE7lHu6bwGtBM7lZ5m6b8wKKMUZDPgBKM2pgdEA=
3650
github.com/phuslu/log v1.0.112 h1:vQ0ZFd5O+in/0IQAcjuEl6wRkHiQPw7T0sqwmOjpL0U=
3751
github.com/phuslu/log v1.0.112/go.mod h1:F8osGJADo5qLK/0F88djWwdyoZZ9xDJQL1HYRHFEkS0=
3852
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3953
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4054
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
4155
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
4256
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
57+
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
58+
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
4359
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
4460
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
61+
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
62+
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
4563
github.com/urfave/cli/v3 v3.0.0-beta1 h1:6DTaaUarcM0wX7qj5Hcvs+5Dm3dyUTBbEwIWAjcw9Zg=
4664
github.com/urfave/cli/v3 v3.0.0-beta1/go.mod h1:FnIeEMYu+ko8zP1F9Ypr3xkZMIDqW3DR92yUtY39q1Y=
65+
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
66+
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
67+
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67 h1:1UoZQm6f0P/ZO0w1Ri+f+ifG/gXhegadRdwBIXEFWDo=
68+
golang.org/x/exp v0.0.0-20241217172543-b2144cdd0a67/go.mod h1:qj5a5QZpwLU2NLQudwIN5koi3beDhSAlJwa67PuM98c=
4769
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
4870
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
4971
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

parser/parse-task.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ import (
1212
"github.com/nxtcoder17/runfile/types"
1313
)
1414

15+
func isAbsPath(p string) bool {
16+
j, err := filepath.Abs(p)
17+
if err != nil {
18+
panic(err)
19+
}
20+
return j == p
21+
}
22+
1523
func ParseTask(ctx context.Context, prf *types.ParsedRunfile, task types.Task) (*types.ParsedTask, error) {
1624
workingDir := filepath.Dir(prf.Metadata.RunfilePath)
1725
if task.Metadata.RunfilePath != nil {
@@ -88,12 +96,26 @@ func ParseTask(ctx context.Context, prf *types.ParsedRunfile, task types.Task) (
8896
commands = append(commands, *c2)
8997
}
9098

99+
watch := task.Watch
100+
if watch != nil {
101+
for i := range watch.Dirs {
102+
if !isAbsPath(watch.Dirs[i]) {
103+
watch.Dirs[i] = filepath.Join(*task.Dir, watch.Dirs[i])
104+
}
105+
}
106+
// for i := range watch.ExcludeDirs {
107+
// if !isAbsPath(watch.ExcludeDirs[i]) {
108+
// watch.ExcludeDirs[i] = filepath.Join(*task.Dir, watch.ExcludeDirs[i])
109+
// }
110+
// }
111+
}
112+
91113
return &types.ParsedTask{
92114
Shell: task.Shell,
93115
WorkingDir: *task.Dir,
94116
Interactive: task.Interactive,
95117
Env: taskEnv,
96118
Commands: commands,
97-
Watch: task.Watch,
119+
Watch: watch,
98120
}, nil
99121
}

runner/run-task.go

Lines changed: 75 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ import (
66
"fmt"
77
"os"
88
"os/exec"
9-
"path/filepath"
109
"strings"
1110
"sync"
12-
"time"
1311

1412
"github.com/alecthomas/chroma/v2/quick"
1513
"github.com/charmbracelet/lipgloss"
@@ -162,76 +160,32 @@ func runCommand(ctx Context, prf *types.ParsedRunfile, pt *types.ParsedTask, arg
162160
SlogKeyAsPrefix: "task",
163161
})
164162

165-
ex := executor.NewExecutor(executor.ExecutorArgs{
166-
Logger: logger2,
167-
IsInteractive: pt.Interactive,
168-
Command: func(c context.Context) *exec.Cmd {
169-
return CreateCommand(c, CmdArgs{
170-
Shell: pt.Shell,
171-
Env: fn.ToEnviron(pt.Env),
172-
Cmd: command.Command,
173-
WorkingDir: pt.WorkingDir,
174-
interactive: pt.Interactive,
175-
Stdout: os.Stdout,
176-
Stderr: os.Stderr,
177-
})
163+
ex := executor.NewCmdExecutor(ctx, executor.CmdExecutorArgs{
164+
Logger: logger2,
165+
Interactive: pt.Interactive,
166+
Commands: func(c context.Context) []*exec.Cmd {
167+
return []*exec.Cmd{
168+
CreateCommand(c, CmdArgs{
169+
Shell: pt.Shell,
170+
Env: fn.ToEnviron(pt.Env),
171+
Cmd: command.Command,
172+
WorkingDir: pt.WorkingDir,
173+
interactive: pt.Interactive,
174+
Stdout: os.Stdout,
175+
Stderr: os.Stderr,
176+
}),
177+
}
178178
},
179179
})
180180

181181
wg.Add(1)
182182
go func() {
183183
defer wg.Done()
184184
<-ctx.Done()
185-
ex.Kill()
185+
ex.Stop()
186186
}()
187187

188-
// if task.Watch != nil && (task.Watch.Enable == nil || *task.Watch.Enable) {
189-
// watch, err := watcher.NewWatcher(ctx, watcher.WatcherArgs{
190-
// Logger: logger,
191-
// WatchDirs: append(task.Watch.Dirs, pt.WorkingDir),
192-
// OnlySuffixes: pt.Watch.OnlySuffixes,
193-
// IgnoreSuffixes: pt.Watch.IgnoreSuffixes,
194-
// ExcludeDirs: pt.Watch.ExcludeDirs,
195-
// UseDefaultIgnoreList: true,
196-
// // CooldownDuration: fn.New(1 * time.Second),
197-
// })
198-
// if err != nil {
199-
// return errors.WithErr(err)
200-
// }
201-
//
202-
// go ex.Exec()
203-
//
204-
// go func() {
205-
// <-ctx.Done()
206-
// logger.Debug("fwatcher is closing ...")
207-
// watch.Close()
208-
// <-time.After(200 * time.Millisecond)
209-
// logger.Info("CLOSING..................")
210-
// os.Exit(0)
211-
// }()
212-
//
213-
// watch.WatchEvents(func(event watcher.Event, fp string) error {
214-
// relPath, err := filepath.Rel(fn.Must(os.Getwd()), fp)
215-
// if err != nil {
216-
// return err
217-
// }
218-
// logger.Info(fmt.Sprintf("[RELOADING] due changes in %s", relPath))
219-
// ex.Kill()
220-
// select {
221-
// case <-time.After(100 * time.Millisecond):
222-
// go ex.Exec()
223-
// return nil
224-
// case <-ctx.Done():
225-
// logger.Info("close signal received")
226-
// watch.Close()
227-
// return nil
228-
// }
229-
// })
230-
//
231-
// return nil
232-
// }
233-
234-
if err := ex.Exec(); err != nil {
188+
if err := ex.Start(); err != nil {
235189
return errors.ErrTaskFailed.Wrap(err).KV("task", args.taskName)
236190
}
237191

@@ -278,56 +232,70 @@ func runTask(ctx Context, prf *types.ParsedRunfile, args runTaskArgs) error {
278232

279233
var wg sync.WaitGroup
280234

281-
wg.Add(1)
282-
go func() {
283-
defer wg.Done()
284-
runTaskCommands(NewContext(nctx, ctx.Logger), prf, pt, args)
285-
}()
286-
287-
if pt.Watch != nil && (pt.Watch.Enable == nil || *pt.Watch.Enable) {
288-
watch, err := watcher.NewWatcher(ctx, watcher.WatcherArgs{
289-
Logger: logger,
290-
WatchDirs: append(task.Watch.Dirs, pt.WorkingDir),
291-
OnlySuffixes: pt.Watch.OnlySuffixes,
292-
IgnoreSuffixes: pt.Watch.IgnoreSuffixes,
293-
ExcludeDirs: pt.Watch.ExcludeDirs,
294-
UseDefaultIgnoreList: true,
295-
CooldownDuration: fn.New(1 * time.Second),
296-
})
297-
if err != nil {
298-
return errors.WithErr(err)
235+
switch pt.Watch == nil {
236+
case true:
237+
{
238+
wg.Add(1)
239+
go func() {
240+
defer wg.Done()
241+
runTaskCommands(NewContext(nctx, ctx.Logger), prf, pt, args)
242+
}()
299243
}
244+
case false:
245+
{
246+
if pt.Watch != nil && (pt.Watch.Enable == nil || *pt.Watch.Enable) {
247+
watch, err := watcher.NewWatcher(ctx, watcher.WatcherArgs{
248+
Logger: logger,
249+
WatchDirs: append(task.Watch.Dirs, pt.WorkingDir),
250+
WatchExtensions: pt.Watch.Dirs,
251+
IgnoreList: watcher.DefaultIgnoreList,
252+
Interactive: pt.Interactive,
253+
})
254+
if err != nil {
255+
return errors.WithErr(err)
256+
}
300257

301-
go func() {
302-
<-ctx.Done()
303-
logger.Debug("fwatcher is closing ...")
304-
watch.Close()
305-
}()
306-
307-
watch.WatchEvents(func(event watcher.Event, fp string) error {
308-
relPath, err := filepath.Rel(fn.Must(os.Getwd()), fp)
309-
if err != nil {
310-
return err
311-
}
312-
logger.Info(fmt.Sprintf("[RELOADING] due changes in %s", relPath))
313-
select {
314-
case <-time.After(100 * time.Millisecond):
315-
cf()
316-
317-
nctx, cf = context.WithCancel(ctx)
318-
wg.Add(1)
319258
go func() {
320-
defer wg.Done()
321-
runTaskCommands(NewContext(nctx, ctx.Logger), prf, pt, args)
259+
<-ctx.Done()
260+
logger.Debug("fwatcher is closing ...")
261+
watch.Close()
322262
}()
323263

324-
return nil
325-
case <-ctx.Done():
326-
logger.Info("close signal received")
327-
watch.Close()
328-
return nil
264+
var executors []executor.Executor
265+
266+
if task.Watch.SSE != nil && task.Watch.SSE.Addr != "" {
267+
executors = append(executors, executor.NewSSEExecutor(executor.SSEExecutorArgs{Addr: task.Watch.SSE.Addr}))
268+
}
269+
270+
if len(pt.Commands) > 0 {
271+
executors = append(executors, executor.NewCmdExecutor(ctx, executor.CmdExecutorArgs{
272+
Logger: logger,
273+
Interactive: pt.Interactive,
274+
Commands: func(c context.Context) []*exec.Cmd {
275+
cmds := make([]*exec.Cmd, 0, len(pt.Commands))
276+
277+
for i := range pt.Commands {
278+
cmds = append(cmds, CreateCommand(c, CmdArgs{
279+
Shell: pt.Shell,
280+
Env: fn.ToEnviron(pt.Env),
281+
Cmd: pt.Commands[i].Command,
282+
WorkingDir: pt.WorkingDir,
283+
interactive: pt.Interactive,
284+
Stdout: os.Stdout,
285+
Stderr: os.Stderr,
286+
}))
287+
}
288+
289+
return cmds
290+
},
291+
}))
292+
}
293+
294+
if err := watch.WatchAndExecute(ctx, executors); err != nil {
295+
return err
296+
}
329297
}
330-
})
298+
}
331299

332300
return nil
333301
}

types/types.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ type TaskMetadata struct {
3636
}
3737

3838
type TaskWatch struct {
39-
Enable *bool `json:"enable,omitempty"`
40-
Dirs []string `json:"dirs"`
41-
OnlySuffixes []string `json:"onlySuffixes"`
42-
IgnoreSuffixes []string `json:"ignoreSuffixes"`
43-
ExcludeDirs []string `json:"excludeDirs"`
39+
Enable *bool `json:"enable,omitempty"`
40+
Dirs []string `json:"dirs"`
41+
Extensions []string `json:"extensions"`
42+
SSE *struct {
43+
Addr string `json:"addr"`
44+
} `json:"sse,omitempty"`
45+
// ExcludeDirs []string `json:"excludeDirs"`
4446
}
4547

4648
type Task struct {

0 commit comments

Comments
 (0)