Skip to content

Commit

Permalink
feat(templates): Adding default templates to the binary (#43)
Browse files Browse the repository at this point in the history
* using default templates

* Updating example
  • Loading branch information
Jacobbrewer1 authored Nov 3, 2024
1 parent f364a36 commit 59af4f8
Show file tree
Hide file tree
Showing 16 changed files with 48 additions and 905 deletions.
24 changes: 20 additions & 4 deletions cmd_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"embed"
"flag"
"log/slog"
"path/filepath"
Expand All @@ -10,6 +11,9 @@ import (
"github.com/jacobbrewer1/goschema/pkg/generation"
)

//go:embed templates/*.tmpl
var defaultTemplates embed.FS

type generateCmd struct {
// templatesLocation is the location of the templates to use.
templatesLocation string
Expand All @@ -22,6 +26,9 @@ type generateCmd struct {

// fileExtensionPrefix is the prefix to add to the generated file extension.
fileExtensionPrefix string

// defaultTemplates is whether to use the binary templates.
defaultTemplates bool
}

func (g *generateCmd) Name() string {
Expand All @@ -43,6 +50,7 @@ func (g *generateCmd) SetFlags(f *flag.FlagSet) {
f.StringVar(&g.outputLocation, "out", ".", "The location to write the generated files to.")
f.StringVar(&g.sqlLocation, "sql", "./schemas/*.sql", "The location of the SQL files to use.")
f.StringVar(&g.fileExtensionPrefix, "extension", "xo", "The prefix to add to the generated file extension.")
f.BoolVar(&g.defaultTemplates, "default", true, "Whether to use the default templates.")
}

func (g *generateCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
Expand All @@ -69,10 +77,18 @@ func (g *generateCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface
return subcommands.ExitFailure
}

err = generation.RenderTemplates(tables, g.templatesLocation, g.outputLocation, g.fileExtensionPrefix)
if err != nil {
slog.Error("Error rendering templates", slog.String("templatesLocation", g.templatesLocation), slog.String("outputLocation", g.outputLocation), slog.String("error", err.Error()))
return subcommands.ExitFailure
if g.defaultTemplates {
err = generation.RenderWithTemplates(defaultTemplates, tables, g.outputLocation, g.fileExtensionPrefix)
if err != nil {
slog.Error("Error rendering default templates", slog.String("outputLocation", g.outputLocation), slog.String("error", err.Error()))
return subcommands.ExitFailure
}
} else {
err = generation.RenderTemplates(tables, g.templatesLocation, g.outputLocation, g.fileExtensionPrefix)
if err != nil {
slog.Error("Error rendering templates", slog.String("templatesLocation", g.templatesLocation), slog.String("outputLocation", g.outputLocation), slog.String("error", err.Error()))
return subcommands.ExitFailure
}
}

return subcommands.ExitSuccess
Expand Down
2 changes: 1 addition & 1 deletion example/models/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ if [ "$forced" = false ]; then
fi

for model in $togen; do
gum spin --spinner dot --title "Generating model $model" -- goschema generate --templates=./templates/*tmpl --out=./ --sql=./schemas/"$model".sql --extension=xo
gum spin --spinner dot --title "Generating model $model" -- goschema generate --out=./ --sql=./schemas/"$model".sql --extension=xo
go fmt ./"$model".xo.go
goimports -w ./"$model".xo.go
done
24 changes: 0 additions & 24 deletions example/models/templates/_delete.tmpl

This file was deleted.

73 changes: 0 additions & 73 deletions example/models/templates/_insert.tmpl

This file was deleted.

37 changes: 0 additions & 37 deletions example/models/templates/_insert_update.tmpl

This file was deleted.

13 changes: 0 additions & 13 deletions example/models/templates/_tags.tmpl

This file was deleted.

169 changes: 0 additions & 169 deletions example/models/templates/_type.tmpl

This file was deleted.

Loading

0 comments on commit 59af4f8

Please sign in to comment.