Skip to content

Commit

Permalink
Add ability to specify templates from arbitrary file.
Browse files Browse the repository at this point in the history
  • Loading branch information
LandonTClipp committed Jan 1, 2025
1 parent ca21e9a commit fe4ac46
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
File renamed without changes.
File renamed without changes.
22 changes: 2 additions & 20 deletions pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/huandu/xstrings"
"github.com/vektra/mockery/v3/pkg/registry"
"github.com/vektra/mockery/v3/pkg/stackerr"
)

// Template is the Moq template. It is capable of generating the Moq
Expand All @@ -21,26 +20,9 @@ type Template struct {
tmpl *template.Template
}

var (
//go:embed moq.templ
templateMoq string
//go:embed mockery.templ
templateMockery string
)

var styleTemplates = map[string]string{
"moq": templateMoq,
"mockery": templateMockery,
}

// New returns a new instance of Template.
func New(style string) (Template, error) {
templateString, styleExists := styleTemplates[style]
if !styleExists {
return Template{}, stackerr.NewStackErrf(nil, "style %s does not exist", style)
}

tmpl, err := template.New(style).Funcs(templateFuncs).Parse(templateString)
func New(templateString string, name string) (Template, error) {
tmpl, err := template.New(name).Funcs(templateFuncs).Parse(templateString)
if err != nil {
return Template{}, err
}
Expand Down
31 changes: 30 additions & 1 deletion pkg/template_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ const (
FORMAT_NOOP Formatter = "noop"
)

var (
//go:embed moq.templ
templateMoq string
//go:embed mockery.templ
templateMockery string
)

var styleTemplates = map[string]string{
"moq": templateMoq,
"mockery": templateMockery,
}

// findPkgPath returns the fully-qualified go import path of a given dir. The
// dir must be relative to a go.mod file. In the case it isn't, an error is returned.
func findPkgPath(dirPath *pathlib.Path) (string, error) {
Expand Down Expand Up @@ -297,7 +309,24 @@ func (g *TemplateGenerator) Generate(
}
data.Imports = g.registry.Imports()

templ, err := template.New(g.templateName)
var templateString string
if strings.HasPrefix(g.templateName, "file://") {
templatePath := pathlib.NewPath(strings.SplitAfterN(g.templateName, "file://", 2)[1])
templateBytes, err := templatePath.ReadFile()
if err != nil {
log.Err(err).Str("template-path", g.templateName).Msg("Failed to read template")
return nil, err
}
templateString = string(templateBytes)
} else {
var styleExists bool
templateString, styleExists = styleTemplates[g.templateName]
if !styleExists {
return nil, stackerr.NewStackErrf(nil, "style %s does not exist", g.templateName)
}
}

templ, err := template.New(templateString, g.templateName)
if err != nil {
return []byte{}, fmt.Errorf("creating new template: %w", err)
}
Expand Down

0 comments on commit fe4ac46

Please sign in to comment.