Skip to content

Commit 73832bd

Browse files
a-hCopilot
andauthored
refactor: rework generator command (#1211)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 732862b commit 73832bd

File tree

15 files changed

+665
-422
lines changed

15 files changed

+665
-422
lines changed

cmd/templ/generatecmd/cmd.go

Lines changed: 11 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"os"
1111
"path"
1212
"path/filepath"
13-
"regexp"
14-
"runtime"
1513
"strings"
1614
"sync"
1715
"sync/atomic"
@@ -30,30 +28,17 @@ import (
3028
"github.com/fsnotify/fsnotify"
3129
)
3230

33-
const defaultWatchPattern = `(.+\.go$)|(.+\.templ$)`
34-
3531
func NewGenerate(log *slog.Logger, args Arguments) (g *Generate, err error) {
3632
g = &Generate{
3733
Log: log,
38-
Args: &args,
39-
}
40-
if g.Args.WorkerCount == 0 {
41-
g.Args.WorkerCount = runtime.NumCPU()
42-
}
43-
if g.Args.WatchPattern == "" {
44-
g.Args.WatchPattern = defaultWatchPattern
45-
}
46-
g.WatchPattern, err = regexp.Compile(g.Args.WatchPattern)
47-
if err != nil {
48-
return nil, fmt.Errorf("failed to compile watch pattern %q: %w", g.Args.WatchPattern, err)
34+
Args: args,
4935
}
5036
return g, nil
5137
}
5238

5339
type Generate struct {
54-
Log *slog.Logger
55-
Args *Arguments
56-
WatchPattern *regexp.Regexp
40+
Log *slog.Logger
41+
Args Arguments
5742
}
5843

5944
type GenerationEvent struct {
@@ -67,17 +52,6 @@ func (cmd Generate) Run(ctx context.Context) (err error) {
6752
if cmd.Args.NotifyProxy {
6853
return proxy.NotifyProxy(cmd.Args.ProxyBind, cmd.Args.ProxyPort)
6954
}
70-
if cmd.Args.Watch && cmd.Args.FileName != "" {
71-
return fmt.Errorf("cannot watch a single file, remove the -f or -watch flag")
72-
}
73-
writingToWriter := cmd.Args.FileWriter != nil
74-
if cmd.Args.FileName == "" && writingToWriter {
75-
return fmt.Errorf("only a single file can be output to stdout, add the -f flag to specify the file to generate code for")
76-
}
77-
// Default to writing to files.
78-
if cmd.Args.FileWriter == nil {
79-
cmd.Args.FileWriter = FileWriter
80-
}
8155
if cmd.Args.PPROFPort > 0 {
8256
go func() {
8357
_ = http.ListenAndServe(fmt.Sprintf("localhost:%d", cmd.Args.PPROFPort), nil)
@@ -160,7 +134,7 @@ func (cmd Generate) Run(ctx context.Context) (err error) {
160134
slog.String("path", cmd.Args.Path),
161135
slog.Bool("devMode", cmd.Args.Watch),
162136
)
163-
if err := watcher.WalkFiles(ctx, cmd.Args.Path, cmd.WatchPattern, events); err != nil {
137+
if err := watcher.WalkFiles(ctx, cmd.Args.Path, cmd.Args.WatchPattern, events); err != nil {
164138
cmd.Log.Error("WalkFiles failed, exiting", slog.Any("error", err))
165139
errs <- FatalError{Err: fmt.Errorf("failed to walk files: %w", err)}
166140
return
@@ -170,7 +144,7 @@ func (cmd Generate) Run(ctx context.Context) (err error) {
170144
return
171145
}
172146
cmd.Log.Info("Watching files")
173-
rw, err := watcher.Recursive(ctx, cmd.Args.Path, cmd.WatchPattern, events, errs)
147+
rw, err := watcher.Recursive(ctx, cmd.Args.Path, cmd.Args.WatchPattern, events, errs)
174148
if err != nil {
175149
cmd.Log.Error("Recursive watcher setup failed, exiting", slog.Any("error", err))
176150
errs <- FatalError{Err: fmt.Errorf("failed to setup recursive watcher: %w", err)}
@@ -194,12 +168,12 @@ func (cmd Generate) Run(ctx context.Context) (err error) {
194168
)
195169
fileEvents := make(chan fsnotify.Event)
196170
go func() {
197-
if err := watcher.WalkFiles(ctx, cmd.Args.Path, cmd.WatchPattern, fileEvents); err != nil {
171+
defer close(fileEvents)
172+
if err := watcher.WalkFiles(ctx, cmd.Args.Path, cmd.Args.WatchPattern, fileEvents); err != nil {
198173
cmd.Log.Error("Post dev mode WalkFiles failed", slog.Any("error", err))
199174
errs <- FatalError{Err: fmt.Errorf("failed to walk files: %w", err)}
200175
return
201176
}
202-
close(fileEvents)
203177
}()
204178
for event := range fileEvents {
205179
if strings.HasSuffix(event.Name, "_templ.go") || strings.HasSuffix(event.Name, ".templ") {
@@ -229,13 +203,13 @@ func (cmd Generate) Run(ctx context.Context) (err error) {
229203
if err != nil {
230204
errs <- err
231205
}
232-
if !r.GoUpdated && !r.TextUpdated && !r.WatchfileUpdated {
206+
if !r.GoUpdated && !r.TextUpdated && !r.WatchedfileUpdated {
233207
cmd.Log.Debug("File not updated", slog.String("file", event.Name))
234208
return
235209
}
236210
e := &GenerationEvent{
237211
Event: event,
238-
WatchedFileUpdated: r.WatchfileUpdated,
212+
WatchedFileUpdated: r.WatchedfileUpdated,
239213
GoUpdated: r.GoUpdated,
240214
TextUpdated: r.TextUpdated,
241215
}
@@ -281,8 +255,9 @@ func (cmd Generate) Run(ctx context.Context) (err error) {
281255
break
282256
}
283257
postGenerationEventsWG.Add(1)
258+
fmt.Println("Post-generation event received, processing...", cmd.Args.Command, goUpdated, watchedFileUpdated)
284259
if cmd.Args.Command != "" && (goUpdated || watchedFileUpdated) {
285-
cmd.Log.Debug("Executing command", slog.String("command", cmd.Args.Command))
260+
cmd.Log.Info("Executing command", slog.String("command", cmd.Args.Command))
286261
if cmd.Args.Watch {
287262
if err := os.Setenv("TEMPL_DEV_MODE", "true"); err != nil {
288263
cmd.Log.Error("Error setting TEMPL_DEV_MODE environment variable", slog.Any("error", err))
@@ -365,12 +340,6 @@ func (cmd *Generate) StartProxy(ctx context.Context) (p *proxy.Handler, err erro
365340
if err != nil {
366341
return nil, FatalError{Err: fmt.Errorf("failed to parse proxy URL: %w", err)}
367342
}
368-
if cmd.Args.ProxyPort == 0 {
369-
cmd.Args.ProxyPort = 7331
370-
}
371-
if cmd.Args.ProxyBind == "" {
372-
cmd.Args.ProxyBind = "127.0.0.1"
373-
}
374343
p = proxy.New(cmd.Log, cmd.Args.ProxyBind, cmd.Args.ProxyPort, target)
375344
go func() {
376345
cmd.Log.Info("Proxying", slog.String("from", p.URL), slog.String("to", p.Target.String()))

0 commit comments

Comments
 (0)