shithub: hugo

Download patch

ref: be29c0bfbdbb38d0f4e170d235221db09aa20174
parent: a8f91ace11c51e1a4c72b41d22bc80b23b9ea9b5
author: bep <[email protected]>
date: Tue Apr 28 16:39:11 EDT 2015

Print ERROR on theme vs Hugo version mismatch

Fixes #1070

--- a/commands/hugo.go
+++ b/commands/hugo.go
@@ -251,6 +251,12 @@
 	}
 
 	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) {
--- a/helpers/hugo.go
+++ b/helpers/hugo.go
@@ -15,6 +15,10 @@
 
 import (
 	"fmt"
+	"github.com/spf13/hugo/hugofs"
+	"github.com/spf13/hugo/parser"
+	"io/ioutil"
+	"path/filepath"
 )
 
 // this should be the only one
@@ -43,4 +47,62 @@
 
 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
 }