Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make version optional, and allow timestamp in generated output - fixes #123 #257

Merged
merged 4 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ goreleaser build --snapshot --clean
Run templ generate using local version.

```sh
go run ./cmd/templ generate
go run ./cmd/templ generate -include-version=false
```

### test
Expand All @@ -71,7 +71,7 @@ mkdir -p coverage/unit
# Build the test binary.
go build -cover -o ./coverage/templ-cover ./cmd/templ
# Run the covered generate command.
GOCOVERDIR=coverage/generate ./coverage/templ-cover generate
GOCOVERDIR=coverage/generate ./coverage/templ-cover generate -include-version=false
# Run the unit tests.
go test -cover ./... -args -test.gocoverdir="$PWD/coverage/unit"
# Display the combined percentage.
Expand All @@ -87,7 +87,7 @@ go tool cover -func coverage.out | grep total
Run benchmarks.

```sh
go run ./cmd/templ generate && go test ./... -bench=. -benchmem
go run ./cmd/templ generate -include-version=false && go test ./... -bench=. -benchmem
```

### lint
Expand Down
10 changes: 5 additions & 5 deletions benchmarks/templ/template_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions cmd/templ/generatecmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

_ "net/http/pprof"

"github.com/a-h/templ"
"github.com/a-h/templ/cmd/templ/generatecmd/proxy"
"github.com/a-h/templ/cmd/templ/generatecmd/run"
"github.com/a-h/templ/cmd/templ/visualize"
Expand All @@ -39,6 +40,8 @@ type Arguments struct {
Proxy string
WorkerCount int
GenerateSourceMapVisualisations bool
IncludeVersion bool
IncludeTimestamp bool
// PPROFPort is the port to run the pprof server on.
PPROFPort int
}
Expand Down Expand Up @@ -84,8 +87,15 @@ func runCmd(ctx context.Context, args Arguments) (err error) {
if args.Watch && args.FileName != "" {
return fmt.Errorf("cannot watch a single file, remove the -f or -watch flag")
}
var opts []generator.GenerateOpt
if args.IncludeVersion {
opts = append(opts, generator.WithVersion(templ.Version))
}
if args.IncludeTimestamp {
opts = append(opts, generator.WithTimestamp(time.Now()))
}
if args.FileName != "" {
return processSingleFile(ctx, args.FileName, args.GenerateSourceMapVisualisations)
return processSingleFile(ctx, args.FileName, args.GenerateSourceMapVisualisations, opts...)
}
var target *url.URL
if args.Proxy != "" {
Expand Down Expand Up @@ -251,17 +261,17 @@ func openURL(url string) error {
return browser.OpenURL(url)
}

func processSingleFile(ctx context.Context, fileName string, generateSourceMapVisualisations bool) error {
func processSingleFile(ctx context.Context, fileName string, generateSourceMapVisualisations bool, opts ...generator.GenerateOpt) error {
start := time.Now()
err := compile(ctx, fileName, generateSourceMapVisualisations)
err := compile(ctx, fileName, generateSourceMapVisualisations, opts...)
if err != nil {
return err
}
fmt.Printf("Generated code for %q in %s\n", fileName, time.Since(start))
return err
}

func compile(ctx context.Context, fileName string, generateSourceMapVisualisations bool) (err error) {
func compile(ctx context.Context, fileName string, generateSourceMapVisualisations bool, opts ...generator.GenerateOpt) (err error) {
if err = ctx.Err(); err != nil {
return
}
Expand All @@ -273,7 +283,7 @@ func compile(ctx context.Context, fileName string, generateSourceMapVisualisatio
targetFileName := strings.TrimSuffix(fileName, ".templ") + "_templ.go"

var b bytes.Buffer
sourceMap, err := generator.Generate(t, &b)
sourceMap, err := generator.Generate(t, &b, opts...)
if err != nil {
return fmt.Errorf("%s generation error: %w", fileName, err)
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/templ/lspcmd/httpdebug/list_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions cmd/templ/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func generateCmd(args []string) {
fileNameFlag := cmd.String("f", "", "Optionally generates code for a single file, e.g. -f header.templ")
pathFlag := cmd.String("path", ".", "Generates code for all files in path.")
sourceMapVisualisations := cmd.Bool("sourceMapVisualisations", false, "Set to true to generate HTML files to visualise the templ code and its corresponding Go code.")
includeVersionFlag := cmd.Bool("include-version", true, "Set to false to skip inclusion of the templ version in the generated code.")
includeTimestampFlag := cmd.Bool("include-timestamp", false, "Set to true to include the current time in the generated code.")
watchFlag := cmd.Bool("watch", false, "Set to true to watch the path for changes and regenerate code.")
cmdFlag := cmd.String("cmd", "", "Set the command to run after generating code.")
proxyFlag := cmd.String("proxy", "", "Set the URL to proxy after generating code and executing the command.")
Expand All @@ -98,6 +100,8 @@ func generateCmd(args []string) {
ProxyPort: *proxyPortFlag,
WorkerCount: *workerCountFlag,
GenerateSourceMapVisualisations: *sourceMapVisualisations,
IncludeVersion: *includeVersionFlag,
IncludeTimestamp: *includeTimestampFlag,
PPROFPort: *pprofPortFlag,
})
if err != nil {
Expand Down
32 changes: 16 additions & 16 deletions cmd/templ/visualize/sourcemapvisualisation_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading