Skip to content

Commit

Permalink
Template system in output path
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvillacortac committed Mar 25, 2022
1 parent daf478e commit 09b1b01
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
4 changes: 2 additions & 2 deletions examples/angular/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ generators:
- name: TS Models
ignore: model_ignore
template: templates/model.ts.go.tpl
output: models/[model-].ts
output: models/{{.Model.Name | KebabCase}}.ts
from: ts

- name: TS Service
ignore: service_ignore
template: templates/service.ts.go.tpl
output: services/[model-].service.ts
output: services/{{ .Model.Name | KebabCase }}.service.ts
from: ts
34 changes: 16 additions & 18 deletions pkg/generators/generators.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"strings"
"text/template"

"github.com/iancoleman/strcase"
"github.com/juanvillacortac/ditto/pkg/ast"
)

Expand Down Expand Up @@ -85,38 +82,39 @@ func Generate(root *ast.RootNode, config GenerateConfig, verbose bool) ([]Output
Types: config.Types,
Helpers: config.Helpers,
})
t, err := template.New(config.Name).Funcs(templateHelpers(models, config)).Parse(config.Template)
t, err := createTemplate(config.Name, config.Template, models, config)
if err != nil {
return nil, err
}
ot, err := createTemplate(config.Name, config.Output, models, config)
if err != nil {
return nil, err
}
files := make([]OutputFile, 0)
cnt := 0
for _, m := range models {
context := &TemplateContext{
Root: root,
Model: m,
}
if val, ok := m.ModelOptions[config.Ignore]; ok && val == "true" {
continue
}
if verbose {
fmt.Fprintf(os.Stdout, "-> [%d/%d] Generating \"%s\"\n", cnt+1, len(models), m.Name())
}

writer := &strings.Builder{}
err = t.Execute(writer, &struct {
Root *ast.RootNode
Model *ast.Model
}{
Root: root,
Model: m,
})
body, err := execTemplate(t, context)
if err != nil {
return nil, err
}
output, err := execTemplate(ot, context)
if err != nil {
return nil, err
}
filename := strings.ReplaceAll(config.Output, "[model]", m.ModelName)
filename = strings.ReplaceAll(filename, "[Model]", strcase.ToCamel(m.ModelName))
filename = strings.ReplaceAll(filename, "[model_]", strcase.ToSnake(m.ModelName))
filename = strings.ReplaceAll(filename, "[model-]", strcase.ToKebab(m.ModelName))
files = append(files, OutputFile{
Filename: filename,
Body: writer.String(),
Filename: output,
Body: body,
})
cnt++
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/generators/template.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package generators

import (
"strings"
"text/template"

"github.com/gertd/go-pluralize"
Expand Down Expand Up @@ -34,3 +35,20 @@ func templateHelpers(models ast.ModelMap, options GenerateConfig) template.FuncM
},
}
}

type TemplateContext struct {
Root *ast.RootNode
Model *ast.Model
}

func createTemplate(name string, content string, models ast.ModelMap, options GenerateConfig) (*template.Template, error) {
return template.New(name).Funcs(templateHelpers(models, options)).Parse(content)
}

func execTemplate(t *template.Template, context *TemplateContext) (string, error) {
writer := &strings.Builder{}
if err := t.Execute(writer, context); err != nil {
return "", err
}
return writer.String(), nil
}

0 comments on commit 09b1b01

Please sign in to comment.