mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-29 22:29:56 +02:00
releaser: Replace the magic version handling
Take the new version as a string value.
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
package helpers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
@@ -37,6 +38,29 @@ func (v HugoVersion) String() string {
|
||||
return hugoVersion(v.Number, v.PatchLevel, v.Suffix)
|
||||
}
|
||||
|
||||
func ParseHugoVersion(s string) (HugoVersion, error) {
|
||||
var vv HugoVersion
|
||||
|
||||
if strings.Contains(s, "DEV") {
|
||||
return vv, errors.New("DEV versions not supported by parse")
|
||||
}
|
||||
|
||||
v, p := parseVersion(s)
|
||||
|
||||
vv.Number = v
|
||||
vv.PatchLevel = p
|
||||
|
||||
return vv, nil
|
||||
}
|
||||
|
||||
func MustParseHugoVersion(s string) HugoVersion {
|
||||
vv, err := ParseHugoVersion(s)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return vv
|
||||
}
|
||||
|
||||
// ReleaseVersion represents the release version.
|
||||
func (v HugoVersion) ReleaseVersion() HugoVersion {
|
||||
v.Suffix = ""
|
||||
@@ -100,18 +124,7 @@ func compareVersions(inVersion float32, inPatchVersion int, in interface{}) int
|
||||
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))
|
||||
v, p := parseVersion(s)
|
||||
|
||||
if v == inVersion && p == inPatchVersion {
|
||||
return 0
|
||||
@@ -125,6 +138,23 @@ func compareVersions(inVersion float32, inPatchVersion int, in interface{}) int
|
||||
}
|
||||
}
|
||||
|
||||
func parseVersion(s string) (float32, int) {
|
||||
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))
|
||||
|
||||
return v, p
|
||||
}
|
||||
|
||||
func compareFloatVersions(version float32, v float32) int {
|
||||
if v == version {
|
||||
return 0
|
||||
|
@@ -49,3 +49,11 @@ func TestCompareVersions(t *testing.T) {
|
||||
require.Equal(t, 1, compareVersions(0.20, 1, "0.20.2"))
|
||||
require.Equal(t, 1, compareVersions(0.21, 1, "0.22.1"))
|
||||
}
|
||||
|
||||
func TestParseHugoVersion(t *testing.T) {
|
||||
require.Equal(t, "0.25", MustParseHugoVersion("0.25").String())
|
||||
require.Equal(t, "0.25.2", MustParseHugoVersion("0.25.2").String())
|
||||
|
||||
_, err := ParseHugoVersion("0.25-DEV")
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user