Replace template dir file path hack with embed directive.

This commit is contained in:
mikestefanello 2023-12-10 09:33:34 -05:00
parent 0879aaf21d
commit 3df20c01a7
7 changed files with 21 additions and 33 deletions

View File

@ -904,7 +904,7 @@ If the current [environment](#environments) is set to `config.EnvLocal`, which i
### File configuration ### 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 ## Funcmap

View File

@ -9,9 +9,6 @@ import (
) )
const ( const (
// TemplateDir stores the name of the directory that contains templates
TemplateDir = "../templates"
// TemplateExt stores the extension used for the template files // TemplateExt stores the extension used for the template files
TemplateExt = ".gohtml" TemplateExt = ".gohtml"

2
go.mod
View File

@ -1,6 +1,6 @@
module github.com/mikestefanello/pagoda module github.com/mikestefanello/pagoda
go 1.19 go 1.21
require ( require (
entgo.io/ent v0.12.5 entgo.io/ent v0.12.5

View File

@ -12,6 +12,7 @@ import (
"github.com/mikestefanello/pagoda/pkg/middleware" "github.com/mikestefanello/pagoda/pkg/middleware"
"github.com/mikestefanello/pagoda/pkg/services" "github.com/mikestefanello/pagoda/pkg/services"
"github.com/mikestefanello/pagoda/pkg/tests" "github.com/mikestefanello/pagoda/pkg/tests"
"github.com/mikestefanello/pagoda/templates"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -98,7 +99,7 @@ func TestController_RenderPage(t *testing.T) {
expectedTemplates := make(map[string]bool) expectedTemplates := make(map[string]bool)
expectedTemplates[p.Name+config.TemplateExt] = true expectedTemplates[p.Name+config.TemplateExt] = true
expectedTemplates[p.Layout+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) require.NoError(t, err)
for _, f := range components { for _, f := range components {
expectedTemplates[f.Name()] = true expectedTemplates[f.Name()] = true
@ -131,7 +132,7 @@ func TestController_RenderPage(t *testing.T) {
expectedTemplates := make(map[string]bool) expectedTemplates := make(map[string]bool)
expectedTemplates[p.Name+config.TemplateExt] = true expectedTemplates[p.Name+config.TemplateExt] = true
expectedTemplates["htmx"+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) require.NoError(t, err)
for _, f := range components { for _, f := range components {
expectedTemplates[f.Name()] = true expectedTemplates[f.Name()] = true

View File

@ -5,13 +5,11 @@ import (
"errors" "errors"
"fmt" "fmt"
"html/template" "html/template"
"path"
"path/filepath"
"runtime"
"sync" "sync"
"github.com/mikestefanello/pagoda/config" "github.com/mikestefanello/pagoda/config"
"github.com/mikestefanello/pagoda/pkg/funcmap" "github.com/mikestefanello/pagoda/pkg/funcmap"
"github.com/mikestefanello/pagoda/templates"
) )
type ( type (
@ -24,9 +22,6 @@ type (
// funcMap stores the template function map // funcMap stores the template function map
funcMap template.FuncMap funcMap template.FuncMap
// templatePath stores the complete path to the templates directory
templatesPath string
// config stores application configuration // config stores application configuration
config *config.Config config *config.Config
} }
@ -58,19 +53,11 @@ type (
// NewTemplateRenderer creates a new TemplateRenderer // NewTemplateRenderer creates a new TemplateRenderer
func NewTemplateRenderer(cfg *config.Config) *TemplateRenderer { func NewTemplateRenderer(cfg *config.Config) *TemplateRenderer {
t := &TemplateRenderer{ return &TemplateRenderer{
templateCache: sync.Map{}, templateCache: sync.Map{},
funcMap: funcmap.GetFuncMap(), funcMap: funcmap.GetFuncMap(),
config: cfg, 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 // 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 // getCacheKey gets a cache key for a given group and ID
func (t *TemplateRenderer) getCacheKey(group, key string) string { func (t *TemplateRenderer) getCacheKey(group, key string) string {
if group != "" { if group != "" {
@ -124,10 +106,10 @@ func (t *TemplateRenderer) parse(build *templateBuild) (*TemplateParsed, error)
// Parse all files provided // Parse all files provided
if len(build.files) > 0 { if len(build.files) > 0 {
for k, v := range build.files { 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 { if err != nil {
return nil, err return nil, err
} }
@ -135,8 +117,8 @@ func (t *TemplateRenderer) parse(build *templateBuild) (*TemplateParsed, error)
// Parse all templates within the provided directories // Parse all templates within the provided directories
for _, dir := range build.directories { for _, dir := range build.directories {
dir = fmt.Sprintf("%s/%s/*%s", t.templatesPath, dir, config.TemplateExt) dir = fmt.Sprintf("%s/*%s", dir, config.TemplateExt)
parsed, err = parsed.ParseGlob(dir) parsed, err = parsed.ParseFS(templates.Templates, dir)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -1,10 +1,10 @@
package services package services
import ( import (
"os"
"testing" "testing"
"github.com/mikestefanello/pagoda/config" "github.com/mikestefanello/pagoda/config"
"github.com/mikestefanello/pagoda/templates"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -37,7 +37,7 @@ func TestTemplateRenderer(t *testing.T) {
expectedTemplates := make(map[string]bool) expectedTemplates := make(map[string]bool)
expectedTemplates["htmx"+config.TemplateExt] = true expectedTemplates["htmx"+config.TemplateExt] = true
expectedTemplates["error"+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) require.NoError(t, err)
for _, f := range components { for _, f := range components {
expectedTemplates[f.Name()] = true expectedTemplates[f.Name()] = true

8
templates/templates.go Normal file
View File

@ -0,0 +1,8 @@
package templates
import (
"embed"
)
//go:embed *
var Templates embed.FS