Move isThemeVsHugoVersionMismatch to /commands

To prevent potential package cycles in /helpers.
This commit is contained in:
Bjørn Erik Pedersen 2015-09-14 17:31:39 +02:00
parent e71bef79e5
commit f5308da320
2 changed files with 62 additions and 69 deletions

View file

@ -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
}

View file

@ -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
}