cmd.Flags().BoolP("noChmod", "", false, "don't sync permission mode of files")
cmd.Flags().BoolP("printI18nWarnings", "", false, "print missing translations")
cmd.Flags().BoolP("printPathWarnings", "", false, "print warnings on duplicate target paths etc.")
+ cmd.Flags().BoolP("printUnusedTemplates", "", false, "print warnings on unused templates.")
cmd.Flags().StringVarP(&cc.cpuprofile, "profile-cpu", "", "", "write cpu profile to `file`")
cmd.Flags().StringVarP(&cc.memprofile, "profile-mem", "", "", "write memory profile to `file`")
cmd.Flags().BoolVarP(&cc.printm, "printMemoryUsage", "", false, "print memory usage to screen at intervals")
"--renderToDisk",
"--source=mysource",
"--printPathWarnings",
+ "--printUnusedTemplates",
},
check: func(c *qt.C, sc *serverCmd) {
c.Assert(sc, qt.Not(qt.IsNil))
"time"
"github.com/gohugoio/hugo/hugofs/files"
+ "github.com/gohugoio/hugo/tpl"
"github.com/gohugoio/hugo/common/types"
"force",
"gc",
"printI18nWarnings",
+ "printUnusedTemplates",
"invalidateCDN",
"layoutDir",
"logFile",
return err
}
- // TODO(bep) Feedback?
if !c.h.quiet {
fmt.Println()
c.hugo().PrintProcessingStats(os.Stdout)
c.logger.Warnln("Duplicate target paths:", dupes)
}
}
+
+ unusedTemplates := c.hugo().Tmpl().(tpl.UnusedTemplatesProvider).UnusedTemplates()
+ for _, unusedTemplate := range unusedTemplates {
+ c.logger.Warnf("Template %s is unused, source file %s", unusedTemplate.Name(), unusedTemplate.Filename())
+ }
}
if c.h.buildWatch {
--printI18nWarnings print missing translations
--printMemoryUsage print memory usage to screen at intervals
--printPathWarnings print warnings on duplicate target paths etc.
+ --printUnusedTemplates print warnings on unused templates.
--quiet build in quiet mode
--renderToMemory render to memory (only useful for benchmark testing)
-s, --source string filesystem path to read files relative from
--printI18nWarnings print missing translations
--printMemoryUsage print memory usage to screen at intervals
--printPathWarnings print warnings on duplicate target paths etc.
+ --printUnusedTemplates print warnings on unused templates.
--templateMetrics display metrics about template executions
--templateMetricsHints calculate some improvement hints when combined with --templateMetrics
-t, --theme strings themes to use (located in /themes/THEMENAME/)
--printI18nWarnings print missing translations
--printMemoryUsage print memory usage to screen at intervals
--printPathWarnings print warnings on duplicate target paths etc.
+ --printUnusedTemplates print warnings on unused templates.
--templateMetrics display metrics about template executions
--templateMetricsHints calculate some improvement hints when combined with --templateMetrics
-t, --theme strings themes to use (located in /themes/THEMENAME/)
--printI18nWarnings print missing translations
--printMemoryUsage print memory usage to screen at intervals
--printPathWarnings print warnings on duplicate target paths etc.
+ --printUnusedTemplates print warnings on unused templates.
--renderToDisk render to Destination path (default is render to memory & serve from there)
--templateMetrics display metrics about template executions
--templateMetricsHints calculate some improvement hints when combined with --templateMetrics
TemplateLookupVariant
}
+// UnusedTemplatesProvider lists unused templates if the build is configured to track those.
+type UnusedTemplatesProvider interface {
+ UnusedTemplates() []FileInfo
+}
+
// TemplateHandler finds and executes templates.
type TemplateHandler interface {
TemplateFinder
identity.Provider
}
+type FileInfo interface {
+ Name() string
+ Filename() string
+}
+
type InfoManager interface {
ParseInfo() ParseInfo
return info.Config.Version == 0
}
-// Info holds some info extracted from a parsed template.
-type Info1 struct {
-}
-
type ParseConfig struct {
Version int
}
--- /dev/null
+package tplimpl_test
+
+import (
+ "path/filepath"
+ "testing"
+
+ qt "github.com/frankban/quicktest"
+ "github.com/gohugoio/hugo/hugolib"
+ "github.com/gohugoio/hugo/tpl"
+)
+
+func TestPrintUnusedTemplates(t *testing.T) {
+ t.Parallel()
+
+ files := `
+-- config.toml --
+baseURL = 'http://example.com/'
+printUnusedTemplates=true
+-- content/p1.md --
+---
+title: "P1"
+---
+{{< usedshortcode >}}
+-- layouts/baseof.html --
+{{ block "main" . }}{{ end }}
+-- layouts/baseof.json --
+{{ block "main" . }}{{ end }}
+-- layouts/index.html --
+{{ define "main" }}FOO{{ end }}
+-- layouts/_default/single.json --
+-- layouts/_default/single.html --
+{{ define "main" }}MAIN{{ end }}
+-- layouts/post/single.html --
+{{ define "main" }}MAIN{{ end }}
+-- layouts/partials/usedpartial.html --
+-- layouts/partials/unusedpartial.html --
+-- layouts/shortcodes/usedshortcode.html --
+{{ partial "usedpartial.html" }}
+-- layouts/shortcodes/unusedshortcode.html --
+
+ `
+
+ b := hugolib.NewIntegrationTestBuilder(
+ hugolib.IntegrationTestConfig{
+ T: t,
+ TxtarString: files,
+ NeedsOsFS: true,
+ },
+ )
+ b.Build()
+
+ unused := b.H.Tmpl().(tpl.UnusedTemplatesProvider).UnusedTemplates()
+
+ var names []string
+ for _, tmpl := range unused {
+ names = append(names, tmpl.Name())
+ }
+
+ b.Assert(names, qt.DeepEquals, []string{"_default/single.json", "baseof.json", "partials/unusedpartial.html", "post/single.html", "shortcodes/unusedshortcode.html"})
+ b.Assert(unused[0].Filename(), qt.Equals, filepath.Join(b.Cfg.WorkingDir, "layouts/_default/single.json"))
+}
}
var (
- _ tpl.TemplateManager = (*templateExec)(nil)
- _ tpl.TemplateHandler = (*templateExec)(nil)
- _ tpl.TemplateFuncGetter = (*templateExec)(nil)
- _ tpl.TemplateFinder = (*templateExec)(nil)
+ _ tpl.TemplateManager = (*templateExec)(nil)
+ _ tpl.TemplateHandler = (*templateExec)(nil)
+ _ tpl.TemplateFuncGetter = (*templateExec)(nil)
+ _ tpl.TemplateFinder = (*templateExec)(nil)
+ _ tpl.UnusedTemplatesProvider = (*templateExec)(nil)
_ tpl.Template = (*templateState)(nil)
_ tpl.Info = (*templateState)(nil)
funcMap[k] = v.Interface()
}
+ var templateUsageTracker map[string]templateInfo
+ if d.Cfg.GetBool("printUnusedTemplates") {
+ templateUsageTracker = make(map[string]templateInfo)
+ }
+
h := &templateHandler{
nameBaseTemplateName: make(map[string]string),
transformNotFound: make(map[string]*templateState),
layoutHandler: output.NewLayoutHandler(),
layoutsFs: d.BaseFs.Layouts.Fs,
layoutTemplateCache: make(map[layoutCacheKey]tpl.Template),
+
+ templateUsageTracker: templateUsageTracker,
}
if err := h.loadEmbedded(); err != nil {
defer t.Metrics.MeasureSince(templ.Name(), time.Now())
}
+ if t.templateUsageTracker != nil {
+ if ts, ok := templ.(*templateState); ok {
+ t.templateUsageTrackerMu.Lock()
+ if _, found := t.templateUsageTracker[ts.Name()]; !found {
+ t.templateUsageTracker[ts.Name()] = ts.info
+ }
+
+ if !ts.baseInfo.IsZero() {
+ if _, found := t.templateUsageTracker[ts.baseInfo.name]; !found {
+ t.templateUsageTracker[ts.baseInfo.name] = ts.baseInfo
+ }
+ }
+ t.templateUsageTrackerMu.Unlock()
+ }
+ }
+
execErr := t.executor.Execute(templ, wr, data)
if execErr != nil {
execErr = t.addFileContext(templ, execErr)
}
+
return execErr
}
+// TODO1
+func (t *templateExec) UnusedTemplates() []tpl.FileInfo {
+ if t.templateUsageTracker == nil {
+ return nil
+ }
+ var unused []tpl.FileInfo
+
+ for _, ti := range t.needsBaseof {
+ if _, found := t.templateUsageTracker[ti.name]; !found {
+ unused = append(unused, ti)
+ }
+ }
+
+ for _, ti := range t.baseof {
+ if _, found := t.templateUsageTracker[ti.name]; !found {
+ unused = append(unused, ti)
+ }
+ }
+
+ for _, ts := range t.main.templates {
+ ti := ts.info
+ if strings.HasPrefix(ti.name, "_internal/") {
+ continue
+ }
+ if strings.HasPrefix(ti.name, "partials/inline/pagination") {
+ // TODO(bep) we need to fix this. These are internal partials, but
+ // they may also be defined in the project, which currently could
+ // lead to some false negatives.
+ continue
+ }
+ if _, found := t.templateUsageTracker[ti.name]; !found {
+ unused = append(unused, ti)
+ }
+ }
+
+ sort.Slice(unused, func(i, j int) bool {
+ return unused[i].Name() < unused[j].Name()
+ })
+
+ return unused
+}
+
func (t *templateExec) GetFunc(name string) (reflect.Value, bool) {
v, found := t.funcs[name]
return v, found
// Note that for shortcodes that same information is embedded in the
// shortcodeTemplates type.
templateInfo map[string]tpl.Info
+
+ // May be nil.
+ templateUsageTracker map[string]templateInfo
+ templateUsageTrackerMu sync.Mutex
}
// AddTemplate parses and adds a template to the collection.
realFilename string
}
+func (t templateInfo) Name() string {
+ return t.name
+}
+
+func (t templateInfo) Filename() string {
+ return t.realFilename
+}
+
func (t templateInfo) IsZero() bool {
return t.name == ""
}