feat: add version_schema config option to allow controlling how the version string is parsed. (#324)

closes #283
This commit is contained in:
Dj Gilcrease
2021-04-22 17:36:16 -07:00
committed by GitHub
parent 39f5bf392d
commit fe1413c7af
3 changed files with 45 additions and 1 deletions
+26
View File
@@ -192,6 +192,7 @@ type Info struct {
Platform string `yaml:"platform,omitempty"`
Epoch string `yaml:"epoch,omitempty"`
Version string `yaml:"version,omitempty"`
VersionSchema string `yaml:"version_schema,omitempty"`
Release string `yaml:"release,omitempty"`
Prerelease string `yaml:"prerelease,omitempty"`
VersionMetadata string `yaml:"version_metadata,omitempty"`
@@ -230,6 +231,21 @@ func (i *Info) GetChangeLog() (log *chglog.PackageChangeLog, err error) {
}, nil
}
func (i *Info) parseSemver() {
// parse the version as a semver so we can properly split the parts
// and support proper ordering for both rpm and deb
if v, err := semver.NewVersion(i.Version); err == nil {
i.Version = fmt.Sprintf("%d.%d.%d", v.Major(), v.Minor(), v.Patch())
if i.Prerelease == "" {
i.Prerelease = v.Prerelease()
}
if i.VersionMetadata == "" {
i.VersionMetadata = v.Metadata()
}
}
}
// Overridables contain the field which are overridable in a package.
type Overridables struct {
Replaces []string `yaml:"replaces,omitempty"`
@@ -387,6 +403,16 @@ func WithDefaults(info *Info) *Info {
}
}
switch info.VersionSchema {
case "none":
// No change to the version or prerelease info set in the YAML file
break
case "semver":
fallthrough
default:
info.parseSemver()
}
return info
}
+13 -1
View File
@@ -38,7 +38,8 @@ func TestGet(t *testing.T) {
func TestDefaultsVersion(t *testing.T) {
info := &nfpm.Info{
Version: "v1.0.0",
Version: "v1.0.0",
VersionSchema: "semver",
}
info = nfpm.WithDefaults(info)
require.NotEmpty(t, info.Platform)
@@ -81,6 +82,17 @@ func TestDefaultsVersion(t *testing.T) {
require.Equal(t, "1.0.0", info.Version)
require.Equal(t, "2", info.Release)
require.Equal(t, "beta1", info.Prerelease)
info = &nfpm.Info{
Version: "this.is.my.version",
VersionSchema: "none",
Release: "2",
Prerelease: "beta1",
}
info = nfpm.WithDefaults(info)
require.Equal(t, "this.is.my.version", info.Version)
require.Equal(t, "2", info.Release)
require.Equal(t, "beta1", info.Prerelease)
}
func TestDefaults(t *testing.T) {
+6
View File
@@ -19,6 +19,12 @@ platform: linux
# This will expand any env var you set in the field, eg version: v${SEMVER}
version: v1.2.3
# Version Schema allows you to specify how to parse the version String.
# Default is `semver`
# `semver` parse the version string as a valid semver version
# `none` skip trying to parse the version string and just use what is passed in
version_schema: semver
# Version Epoch.
# Default is extracted from `version` if it is semver compatible.
epoch: 2