mirror of
https://github.com/gohugoio/hugo.git
synced 2025-03-20 05:24:17 +00:00
parent
a0e3ff1645
commit
3669015f56
4 changed files with 85 additions and 10 deletions
|
@ -1001,15 +1001,7 @@ func (c *commandeer) isThemeVsHugoVersionMismatch() (mismatch bool, requiredMinV
|
||||||
config := tomlMeta.(map[string]interface{})
|
config := tomlMeta.(map[string]interface{})
|
||||||
|
|
||||||
if minVersion, ok := config["min_version"]; ok {
|
if minVersion, ok := config["min_version"]; ok {
|
||||||
switch minVersion.(type) {
|
return helpers.CompareVersion(minVersion) > 0, fmt.Sprint(minVersion)
|
||||||
case float32:
|
|
||||||
return helpers.HugoVersionNumber < minVersion.(float32), fmt.Sprint(minVersion)
|
|
||||||
case float64:
|
|
||||||
return helpers.HugoVersionNumber < minVersion.(float64), fmt.Sprint(minVersion)
|
|
||||||
default:
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
|
@ -314,7 +314,7 @@ description = ""
|
||||||
homepage = "http://siteforthistheme.com/"
|
homepage = "http://siteforthistheme.com/"
|
||||||
tags = []
|
tags = []
|
||||||
features = []
|
features = []
|
||||||
min_version = 0.19
|
min_version = "0.19"
|
||||||
|
|
||||||
[author]
|
[author]
|
||||||
name = ""
|
name = ""
|
||||||
|
|
|
@ -15,6 +15,9 @@ package helpers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/spf13/cast"
|
||||||
)
|
)
|
||||||
|
|
||||||
// HugoVersionNumber represents the current build version.
|
// HugoVersionNumber represents the current build version.
|
||||||
|
@ -61,3 +64,64 @@ func hugoVersionNoSuffix(version float32, patchVersion int) string {
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%.2f", version)
|
return fmt.Sprintf("%.2f", version)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CompareVersion compares the given version string or number against the
|
||||||
|
// running Hugo version.
|
||||||
|
// It returns -1 if the given version is less than, 0 if equal and 1 if greater than
|
||||||
|
// the running version.
|
||||||
|
func CompareVersion(version interface{}) int {
|
||||||
|
return compareVersions(HugoVersionNumber, HugoPatchVersion, version)
|
||||||
|
}
|
||||||
|
|
||||||
|
func compareVersions(inVersion float32, inPatchVersion int, in interface{}) int {
|
||||||
|
switch d := in.(type) {
|
||||||
|
case float64:
|
||||||
|
return compareFloatVersions(inVersion, inPatchVersion, float32(d))
|
||||||
|
case float32:
|
||||||
|
return compareFloatVersions(inVersion, inPatchVersion, d)
|
||||||
|
case int:
|
||||||
|
return compareFloatVersions(inVersion, inPatchVersion, float32(d))
|
||||||
|
case int32:
|
||||||
|
return compareFloatVersions(inVersion, inPatchVersion, float32(d))
|
||||||
|
case int64:
|
||||||
|
return compareFloatVersions(inVersion, inPatchVersion, float32(d))
|
||||||
|
default:
|
||||||
|
s, err := cast.ToStringE(in)
|
||||||
|
if err != nil {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
v float32
|
||||||
|
p int
|
||||||
|
)
|
||||||
|
|
||||||
|
if strings.Count(s, ".") == 2 {
|
||||||
|
li := strings.LastIndex(s, ".")
|
||||||
|
p = cast.ToInt(s[li+1:])
|
||||||
|
s = s[:li]
|
||||||
|
}
|
||||||
|
|
||||||
|
v = float32(cast.ToFloat64(s))
|
||||||
|
|
||||||
|
if v == inVersion && p == inPatchVersion {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
if v < inVersion || (v == inVersion && p < inPatchVersion) {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func compareFloatVersions(version float32, patchVersion int, v float32) int {
|
||||||
|
if v == version {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
if v < version {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestHugoVersion(t *testing.T) {
|
func TestHugoVersion(t *testing.T) {
|
||||||
|
@ -26,3 +27,21 @@ func TestHugoVersion(t *testing.T) {
|
||||||
assert.Equal(t, "0.15.2-DEV", hugoVersion(0.15, 2, "-DEV"))
|
assert.Equal(t, "0.15.2-DEV", hugoVersion(0.15, 2, "-DEV"))
|
||||||
assert.Equal(t, "0.17.3", hugoVersionNoSuffix(0.16+0.01, 3))
|
assert.Equal(t, "0.17.3", hugoVersionNoSuffix(0.16+0.01, 3))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCompareVersions(t *testing.T) {
|
||||||
|
require.Equal(t, 0, compareVersions(0.20, 0, 0.20))
|
||||||
|
require.Equal(t, 0, compareVersions(0.20, 0, float32(0.20)))
|
||||||
|
require.Equal(t, 0, compareVersions(0.20, 0, float64(0.20)))
|
||||||
|
require.Equal(t, 1, compareVersions(0.19, 1, 0.20))
|
||||||
|
require.Equal(t, 1, compareVersions(0.19, 3, "0.20.2"))
|
||||||
|
require.Equal(t, -1, compareVersions(0.19, 1, 0.01))
|
||||||
|
require.Equal(t, 1, compareVersions(0, 1, 3))
|
||||||
|
require.Equal(t, 1, compareVersions(0, 1, int32(3)))
|
||||||
|
require.Equal(t, 1, compareVersions(0, 1, int64(3)))
|
||||||
|
require.Equal(t, 0, compareVersions(0.20, 0, "0.20"))
|
||||||
|
require.Equal(t, 0, compareVersions(0.20, 1, "0.20.1"))
|
||||||
|
require.Equal(t, -1, compareVersions(0.20, 1, "0.20"))
|
||||||
|
require.Equal(t, 1, compareVersions(0.20, 0, "0.20.1"))
|
||||||
|
require.Equal(t, 1, compareVersions(0.20, 1, "0.20.2"))
|
||||||
|
require.Equal(t, 1, compareVersions(0.21, 1, "0.22.1"))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue