Skip to content

Commit

Permalink
feat: add flag to allow generating code for a specific file
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Mar 20, 2022
1 parent d103bea commit 1c6c2c9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
25 changes: 22 additions & 3 deletions cmd/templ/generatecmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,29 @@ import (

const workerCount = 4

func Run(args []string) (err error) {
type Arguments struct {
FileName string
Path string
}

func Run(args Arguments) (err error) {
if args.FileName != "" {
return processSingleFile(args.FileName)
}
return processPath(args.Path)
}

func processSingleFile(fileName string) error {
start := time.Now()
err := compile(fileName)
fmt.Printf("Generated code for %q in %s\n", fileName, time.Since(start))
return err
}

func processPath(path string) (err error) {
start := time.Now()
results := make(chan processor.Result)
go processor.Process(".", compile, workerCount, results)
go processor.Process(path, compile, workerCount, results)
var successCount, errorCount int
for r := range results {
if r.Error != nil {
Expand All @@ -30,7 +49,7 @@ func Run(args []string) (err error) {
fmt.Printf("%s complete in %v\n", r.FileName, r.Duration)
}
fmt.Printf("Generated code for %d templates with %d errors in %s\n", successCount+errorCount, errorCount, time.Since(start))
return
return err
}

func compile(fileName string) (err error) {
Expand Down
7 changes: 6 additions & 1 deletion cmd/templ/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,18 @@ examples:

func generateCmd(args []string) {
cmd := flag.NewFlagSet("generate", flag.ExitOnError)
fileName := cmd.String("f", "", "Optionally generates code for a single file, e.g. -f header.templ")
path := cmd.String("path", ".", "Generates code for all files in path.")
helpFlag := cmd.Bool("help", false, "Print help and exit.")
err := cmd.Parse(args)
if err != nil || *helpFlag {
cmd.PrintDefaults()
return
}
err = generatecmd.Run(args)
err = generatecmd.Run(generatecmd.Arguments{
FileName: *fileName,
Path: *path,
})
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
Expand Down

0 comments on commit 1c6c2c9

Please sign in to comment.