Skip to content

Commit

Permalink
refactor: use packages.Package in Templates parser
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Oct 22, 2024
1 parent f39e75a commit 3416a35
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 249 deletions.
2 changes: 1 addition & 1 deletion cmd/muxt/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func generateCommand(args []string, workingDirectory string, getEnv func(string)
g.Package = list[0]
g.goPackage = list[0].ID
}
ts, err := source.Templates(workingDirectory, g.templatesVariable, g.Package.Fset, g.Package.Syntax, g.Package.EmbedFiles)
ts, err := source.Templates(workingDirectory, g.templatesVariable, g.Package)
if err != nil {
return err
}
Expand Down
10 changes: 6 additions & 4 deletions internal/source/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ import (
"slices"
"strings"
"unicode"

"golang.org/x/tools/go/packages"
)

func Templates(workingDirectory, templatesVariable string, fileSet *token.FileSet, files []*ast.File, embeddedAbsolutePath []string) (*template.Template, error) {
for _, tv := range IterateValueSpecs(files) {
func Templates(workingDirectory, templatesVariable string, pkg *packages.Package) (*template.Template, error) {
for _, tv := range IterateValueSpecs(pkg.Syntax) {
i := slices.IndexFunc(tv.Names, func(e *ast.Ident) bool {
return e.Name == templatesVariable
})
if i < 0 || i >= len(tv.Values) {
continue
}
embeddedPaths, err := relFilepaths(workingDirectory, embeddedAbsolutePath...)
embeddedPaths, err := relFilepaths(workingDirectory, pkg.EmbedFiles...)
if err != nil {
return nil, fmt.Errorf("failed to calculate relative path for embedded files: %w", err)
}
const templatePackageIdent = "template"
ts, err := evaluateTemplateSelector(nil, tv.Values[i], workingDirectory, templatesVariable, templatePackageIdent, "", "", fileSet, files, embeddedPaths)
ts, err := evaluateTemplateSelector(nil, tv.Values[i], workingDirectory, templatesVariable, templatePackageIdent, "", "", pkg.Fset, pkg.Syntax, embeddedPaths)
if err != nil {
return nil, fmt.Errorf("run template %s failed at %w", templatesVariable, err)
}
Expand Down
Loading

0 comments on commit 3416a35

Please sign in to comment.