From f5308da32013279664379111d84fb8fa50d7e5f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 14 Sep 2015 17:31:39 +0200 Subject: [PATCH] Move isThemeVsHugoVersionMismatch to /commands To prevent potential package cycles in /helpers. --- commands/hugo.go | 58 +++++++++++++++++++++++++++++++++++++- helpers/hugo.go | 73 ++++-------------------------------------------- 2 files changed, 62 insertions(+), 69 deletions(-) diff --git a/commands/hugo.go b/commands/hugo.go index 725591008..804817478 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -17,6 +17,8 @@ package commands import ( "fmt" + "github.com/spf13/hugo/parser" + "io/ioutil" "net/http" "os" "path/filepath" @@ -292,7 +294,7 @@ func InitializeConfig() { } } - themeVersionMismatch, minVersion := helpers.IsThemeVsHugoVersionMismatch() + themeVersionMismatch, minVersion := isThemeVsHugoVersionMismatch() if themeVersionMismatch { jww.ERROR.Printf("Current theme does not support Hugo version %s. Minimum version required is %s\n", @@ -535,3 +537,57 @@ func NewWatcher(port int) error { wg.Wait() return nil } + +// isThemeVsHugoVersionMismatch returns whether the current Hugo version is < theme's min_version +func isThemeVsHugoVersionMismatch() (mismatch bool, requiredMinVersion string) { + if !helpers.ThemeSet() { + return + } + + themeDir := helpers.GetThemeDir() + + fs := hugofs.SourceFs + path := filepath.Join(themeDir, "theme.toml") + + exists, err := helpers.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 helpers.HugoVersionNumber < minVersion.(float32), fmt.Sprint(minVersion) + case float64: + return helpers.HugoVersionNumber < minVersion.(float64), fmt.Sprint(minVersion) + default: + return + } + + } + + return +} diff --git a/helpers/hugo.go b/helpers/hugo.go index 35466a7dc..2e48ff5c9 100644 --- a/helpers/hugo.go +++ b/helpers/hugo.go @@ -15,31 +15,26 @@ package helpers import ( "fmt" - "io/ioutil" - "path/filepath" - - "github.com/spf13/hugo/hugofs" - "github.com/spf13/hugo/parser" ) // this should be the only one -const hugoVersionMain = 0.15 -const hugoVersionSuffix = "-DEV" // blank this when doing a release +const HugoVersionNumber = 0.15 +const HugoVersionSuffix = "-DEV" // blank this when doing a release // HugoVersion returns the current Hugo version. It will include // a suffix, typically '-DEV', if it's development version. func HugoVersion() string { - return hugoVersion(hugoVersionMain, hugoVersionSuffix) + return hugoVersion(HugoVersionNumber, HugoVersionSuffix) } // HugoReleaseVersion is same as HugoVersion, but no suffix. func HugoReleaseVersion() string { - return hugoVersionNoSuffix(hugoVersionMain) + return hugoVersionNoSuffix(HugoVersionNumber) } // NextHugoReleaseVersion returns the next Hugo release version. func NextHugoReleaseVersion() string { - return hugoVersionNoSuffix(hugoVersionMain + 0.01) + return hugoVersionNoSuffix(HugoVersionNumber + 0.01) } func hugoVersion(version float32, suffix string) string { @@ -49,61 +44,3 @@ 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 -}