Print ERROR on theme vs Hugo version mismatch

Fixes #1070
This commit is contained in:
bep 2015-04-28 20:39:11 +02:00
parent a8f91ace11
commit be29c0bfbd
2 changed files with 68 additions and 0 deletions

View file

@ -251,6 +251,12 @@ func InitializeConfig() {
} }
jww.INFO.Println("Using config file:", viper.ConfigFileUsed()) 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) { func build(watches ...bool) {

View file

@ -15,6 +15,10 @@ package helpers
import ( import (
"fmt" "fmt"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/parser"
"io/ioutil"
"path/filepath"
) )
// this should be the only one // this should be the only one
@ -44,3 +48,61 @@ func hugoVersion(version float32, suffix string) string {
func hugoVersionNoSuffix(version float32) string { func hugoVersionNoSuffix(version float32) string {
return fmt.Sprintf("%.2g", version) 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
}