Skip to content

refactor: rework generator command#1211

Merged
a-h merged 18 commits intomainfrom
generate_refactor_watch
Jul 7, 2025
Merged

refactor: rework generator command#1211
a-h merged 18 commits intomainfrom
generate_refactor_watch

Conversation

@a-h
Copy link
Copy Markdown
Owner

@a-h a-h commented Jul 2, 2025

When reviewing @philip-peterson's PR, I was struck by the general messiness of the generator code, caused by adding on extra features over time.

My plan is to get the generator into a better testing position. At the moment, the generator is tested well by generating templ files, and comparing outputs, and the watch mode has a basic integration test to validate that it works, but it didn't catch a regression in watch mode behaviour in the current release, so I want to restructure the code to support adding some behavioural unit tests to cover more of the feature set.

So, this PR is a bit of a grab bag of general improvements: moving the concurrent maps into their own types, simplifying waitgroups with errgroup, consolidating the argument processing for the generate command into a single area, and improving test coverage.

@a-h a-h requested review from Copilot and joerdav July 2, 2025 16:26
@a-h
Copy link
Copy Markdown
Owner Author

a-h commented Jul 2, 2025

When I get to your suggestions @philip-peterson, I'll be sure to give you credit!

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR refactors the generator command to improve testability and maintainability by:

  • Extracting concurrent maps/sets into their own generic types
  • Replacing manual sync.WaitGroup and error slices with errgroup for clearer error handling
  • Centralizing CLI argument parsing, error handling, and version bump

Reviewed Changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
generator/test-css-middleware/render_test.go Switched from sync.WaitGroup and error slice to errgroup
generator/htmldiff/diff.go Replaced manual goroutines and sync.WaitGroup with errgroup
cmd/templ/sloghandler/handler.go Added NewLogger factory to configure slog.Logger
cmd/templ/main_test.go Updated tests to match new logger and CLI behavior
cmd/templ/main.go Wired in sloghandler.NewLogger and removed inline logger setup
cmd/templ/generatecmd/testwatch/generate_test.go Updated watch-mode tests to use new Run signature and errgroup
cmd/templ/generatecmd/syncset/set_test.go Added unit tests for new generic syncset
cmd/templ/generatecmd/syncset/set.go Introduced Set[T] with mutex and basic methods
cmd/templ/generatecmd/syncmap/map_test.go Added unit tests for new generic syncmap
cmd/templ/generatecmd/syncmap/map.go Introduced Map[K,V] with mutex and CAS
cmd/templ/generatecmd/symlink/symlink_test.go Updated symlink tests to new Run signature
cmd/templ/generatecmd/main_test.go Adapted generate CLI tests to new argument parsing
cmd/templ/generatecmd/main.go Centralized NewArguments, Run signature, and usage handling
cmd/templ/generatecmd/eventhandler.go Replaced manual maps/mutexes with syncmap/syncset and errgroup
cmd/templ/generatecmd/cmd.go Simplified NewGenerate; removed inline defaults and regex logic
.version Bumped version from 0.3.906 to 0.3.908
Comments suppressed due to low confidence (2)

cmd/templ/generatecmd/eventhandler.go:258

  • This fmt.Println appears to be a stray debug statement; consider removing it or replacing with structured logging (h.Log.Debug or h.Log.Info) to keep output consistent.
// Takes an error from the formatter and attempts to convert the positions reported in the target file to their positions

cmd/templ/sloghandler/handler.go:13

  • This function signature references io.Writer and slog.Logger but neither are imported; add import "io" and import "log/slog" (or alias) to fix the compile error.
func NewLogger(logLevel string, verbose bool, stderr io.Writer) *slog.Logger {

a-h and others added 5 commits July 2, 2025 17:31
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@philip-peterson
Copy link
Copy Markdown
Contributor

@a-h Thanks for doing this! If merged, looks like this will be way better than my refactors anyway.

@joerdav
Copy link
Copy Markdown
Collaborator

joerdav commented Jul 3, 2025

Nice, some real improvements here!

@a-h a-h merged commit 73832bd into main Jul 7, 2025
9 checks passed
@a-h a-h deleted the generate_refactor_watch branch July 7, 2025 07:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants