From 3df20c01a7663c9fbb99e51e29b006bfe5c3cbde Mon Sep 17 00:00:00 2001 From: mikestefanello Date: Sun, 10 Dec 2023 09:33:34 -0500 Subject: [PATCH] Replace template dir file path hack with embed directive. --- README.md | 2 +- config/config.go | 3 --- go.mod | 2 +- pkg/controller/controller_test.go | 5 +++-- pkg/services/template_renderer.go | 30 ++++++-------------------- pkg/services/template_renderer_test.go | 4 ++-- templates/templates.go | 8 +++++++ 7 files changed, 21 insertions(+), 33 deletions(-) create mode 100644 templates/templates.go diff --git a/README.md b/README.md index bc75b0e..aba29bb 100644 --- a/README.md +++ b/README.md @@ -904,7 +904,7 @@ If the current [environment](#environments) is set to `config.EnvLocal`, which i ### File configuration -To make things easier and less repetitive, parameters given to the _template renderer_ must not include the `templates` directory or the template file extensions. These are stored as constants within the `config` package. If your project has a need to change either of these, simply adjust the `TemplateDir` and `TemplateExt` constants. +To make things easier and less repetitive, parameters given to the _template renderer_ must not include the `templates` directory or the template file extensions. The file extension is stored as a constant (`TemplateExt`) within the `config` package. ## Funcmap diff --git a/config/config.go b/config/config.go index 6427e5e..b17ef24 100644 --- a/config/config.go +++ b/config/config.go @@ -9,9 +9,6 @@ import ( ) const ( - // TemplateDir stores the name of the directory that contains templates - TemplateDir = "../templates" - // TemplateExt stores the extension used for the template files TemplateExt = ".gohtml" diff --git a/go.mod b/go.mod index 7312f07..9681dbd 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/mikestefanello/pagoda -go 1.19 +go 1.21 require ( entgo.io/ent v0.12.5 diff --git a/pkg/controller/controller_test.go b/pkg/controller/controller_test.go index 0bd3177..60c3063 100644 --- a/pkg/controller/controller_test.go +++ b/pkg/controller/controller_test.go @@ -12,6 +12,7 @@ import ( "github.com/mikestefanello/pagoda/pkg/middleware" "github.com/mikestefanello/pagoda/pkg/services" "github.com/mikestefanello/pagoda/pkg/tests" + "github.com/mikestefanello/pagoda/templates" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -98,7 +99,7 @@ func TestController_RenderPage(t *testing.T) { expectedTemplates := make(map[string]bool) expectedTemplates[p.Name+config.TemplateExt] = true expectedTemplates[p.Layout+config.TemplateExt] = true - components, err := os.ReadDir(c.TemplateRenderer.GetTemplatesPath() + "/components") + components, err := templates.Templates.ReadDir("components") require.NoError(t, err) for _, f := range components { expectedTemplates[f.Name()] = true @@ -131,7 +132,7 @@ func TestController_RenderPage(t *testing.T) { expectedTemplates := make(map[string]bool) expectedTemplates[p.Name+config.TemplateExt] = true expectedTemplates["htmx"+config.TemplateExt] = true - components, err := os.ReadDir(c.TemplateRenderer.GetTemplatesPath() + "/components") + components, err := templates.Templates.ReadDir("components") require.NoError(t, err) for _, f := range components { expectedTemplates[f.Name()] = true diff --git a/pkg/services/template_renderer.go b/pkg/services/template_renderer.go index 5cdff50..de60a0e 100644 --- a/pkg/services/template_renderer.go +++ b/pkg/services/template_renderer.go @@ -5,13 +5,11 @@ import ( "errors" "fmt" "html/template" - "path" - "path/filepath" - "runtime" "sync" "github.com/mikestefanello/pagoda/config" "github.com/mikestefanello/pagoda/pkg/funcmap" + "github.com/mikestefanello/pagoda/templates" ) type ( @@ -24,9 +22,6 @@ type ( // funcMap stores the template function map funcMap template.FuncMap - // templatePath stores the complete path to the templates directory - templatesPath string - // config stores application configuration config *config.Config } @@ -58,19 +53,11 @@ type ( // NewTemplateRenderer creates a new TemplateRenderer func NewTemplateRenderer(cfg *config.Config) *TemplateRenderer { - t := &TemplateRenderer{ + return &TemplateRenderer{ templateCache: sync.Map{}, funcMap: funcmap.GetFuncMap(), config: cfg, } - - // Gets the complete templates directory path - // This is needed in case this is called from a package outside of main, such as within tests - _, b, _, _ := runtime.Caller(0) - d := path.Join(path.Dir(b)) - t.templatesPath = filepath.Join(filepath.Dir(d), config.TemplateDir) - - return t } // Parse creates a template build operation @@ -81,11 +68,6 @@ func (t *TemplateRenderer) Parse() *templateBuilder { } } -// GetTemplatesPath gets the complete path to the templates directory -func (t *TemplateRenderer) GetTemplatesPath() string { - return t.templatesPath -} - // getCacheKey gets a cache key for a given group and ID func (t *TemplateRenderer) getCacheKey(group, key string) string { if group != "" { @@ -124,10 +106,10 @@ func (t *TemplateRenderer) parse(build *templateBuild) (*TemplateParsed, error) // Parse all files provided if len(build.files) > 0 { for k, v := range build.files { - build.files[k] = fmt.Sprintf("%s/%s%s", t.templatesPath, v, config.TemplateExt) + build.files[k] = fmt.Sprintf("%s%s", v, config.TemplateExt) } - parsed, err = parsed.ParseFiles(build.files...) + parsed, err = parsed.ParseFS(templates.Templates, build.files...) if err != nil { return nil, err } @@ -135,8 +117,8 @@ func (t *TemplateRenderer) parse(build *templateBuild) (*TemplateParsed, error) // Parse all templates within the provided directories for _, dir := range build.directories { - dir = fmt.Sprintf("%s/%s/*%s", t.templatesPath, dir, config.TemplateExt) - parsed, err = parsed.ParseGlob(dir) + dir = fmt.Sprintf("%s/*%s", dir, config.TemplateExt) + parsed, err = parsed.ParseFS(templates.Templates, dir) if err != nil { return nil, err } diff --git a/pkg/services/template_renderer_test.go b/pkg/services/template_renderer_test.go index ab589b1..6e6805c 100644 --- a/pkg/services/template_renderer_test.go +++ b/pkg/services/template_renderer_test.go @@ -1,10 +1,10 @@ package services import ( - "os" "testing" "github.com/mikestefanello/pagoda/config" + "github.com/mikestefanello/pagoda/templates" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -37,7 +37,7 @@ func TestTemplateRenderer(t *testing.T) { expectedTemplates := make(map[string]bool) expectedTemplates["htmx"+config.TemplateExt] = true expectedTemplates["error"+config.TemplateExt] = true - components, err := os.ReadDir(c.TemplateRenderer.GetTemplatesPath() + "/components") + components, err := templates.Templates.ReadDir("components") require.NoError(t, err) for _, f := range components { expectedTemplates[f.Name()] = true diff --git a/templates/templates.go b/templates/templates.go new file mode 100644 index 0000000..83a8320 --- /dev/null +++ b/templates/templates.go @@ -0,0 +1,8 @@ +package templates + +import ( + "embed" +) + +//go:embed * +var Templates embed.FS