From be29c0bfbdbb38d0f4e170d235221db09aa20174 Mon Sep 17 00:00:00 2001 From: bep Date: Tue, 28 Apr 2015 20:39:11 +0200 Subject: [PATCH] Print ERROR on theme vs Hugo version mismatch Fixes #1070 --- commands/hugo.go | 6 +++++ helpers/hugo.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/commands/hugo.go b/commands/hugo.go index b9afb3ec0..6d4750165 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -251,6 +251,12 @@ func InitializeConfig() { } jww.INFO.Println("Using config file:", viper.ConfigFileUsed()) + + themeVersionMismatch, minVersion := helpers.IsThemeVsHugoVersionMismatch() + if themeVersionMismatch { + jww.ERROR.Printf("Current theme does not support Hugo version %s. Minimum version required is %s\n", + helpers.HugoReleaseVersion(), minVersion) + } } func build(watches ...bool) { diff --git a/helpers/hugo.go b/helpers/hugo.go index ab840a2d7..d20a0e34e 100644 --- a/helpers/hugo.go +++ b/helpers/hugo.go @@ -15,6 +15,10 @@ package helpers import ( "fmt" + "github.com/spf13/hugo/hugofs" + "github.com/spf13/hugo/parser" + "io/ioutil" + "path/filepath" ) // this should be the only one @@ -44,3 +48,61 @@ func hugoVersion(version float32, suffix string) string { func hugoVersionNoSuffix(version float32) string { return fmt.Sprintf("%.2g", version) } + +// IsThemeVsHugoVersionMismatch returns whether the current Hugo version is < theme's min_version +func IsThemeVsHugoVersionMismatch() (mismatch bool, requiredMinVersion string) { + if !ThemeSet() { + return + } + + themeDir, err := getThemeDirPath("") + + if err != nil { + return + } + + fs := hugofs.SourceFs + path := filepath.Join(themeDir, "theme.toml") + + exists, err := Exists(path, fs) + + if err != nil || !exists { + return + } + + f, err := fs.Open(path) + + if err != nil { + return + } + + defer f.Close() + + b, err := ioutil.ReadAll(f) + + if err != nil { + return + } + + c, err := parser.HandleTOMLMetaData(b) + + if err != nil { + return + } + + config := c.(map[string]interface{}) + + if minVersion, ok := config["min_version"]; ok { + switch minVersion.(type) { + case float32: + return hugoVersionMain < minVersion.(float32), fmt.Sprint(minVersion) + case float64: + return hugoVersionMain < minVersion.(float64), fmt.Sprint(minVersion) + default: + return + } + + } + + return +}